solauth

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

solauth

Tests

Issuing access token based on signed message by Solana wallet.

Usage

1. Start server
$ ./bin/server
2. Request authorization
$ curl -X POST -H "Content-Type: application/json" -d '{"public_key": "[base58 encoded wallet address]"}' http://localhost:8080/auth/request
3. Sign message

Sign the message with your wallet.

4. Get access token
$ curl -X POST -H "Content-Type: application/json" -d '{"public_key": "[base58 encoded wallet address]", "signature": "[base64 encoded signature]", "message": "[same message from first request]"}' http://localhost:8080/auth/verify
5. Refresh access token
$ curl -X POST -H "Content-Type: application/json" -d '{"refresh_token": "[refresh token from prev req]"}' http://localhost:8080/auth/refresh

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized = errors.New("Missing or invalid access token")
)

Predefined errors

View Source
var TokenClaimsContextKey = &contextKey{name: "token-claims"}

TokenClaimsContextKey is the key for the token claims in the request context.

Functions

func GoKitMiddleware

func GoKitMiddleware(v verifier) endpoint.Middleware

GoKitMiddleware is a middleware for SolAuth. It will check the context for a valid token and add the claims to the context.

func Middleware

func Middleware(v verifier) func(http.Handler) http.Handler

Middleware is a middleware for SolAuth. It will check the request for a valid token and add the claims to the request context.

func RefreshToken

func RefreshToken(jwt interface {
	RefreshToken(tokenString string) (TokenResponse, error)
},
) http.HandlerFunc

RefreshToken is the handler for the refresh token. It refreshes the access token.

func RequestAuth

func RequestAuth(w http.ResponseWriter, r *http.Request)

RequestAuth is the handler for the request authentication. It gets the wallet address and returns message to sign. The message must be signed by the wallet and sent back to the server. The server will verify the signature and return the result.

func VerifySignature

func VerifySignature(message, signature, publicKey string) error

VerifySignature verifies the signature of the request. This function verifies the signature of the message using the public key of the sender. It returns error if the signature is NOT valid, otherwise nil.

func VerifySignedMessage

func VerifySignedMessage(jwt interface {
	IssueTokens(walletAddr string) (TokenResponse, error)
},
) http.HandlerFunc

VerifySignedMessage is the handler for the signed message verification. It verifies the signature of the message using the public key of the sender. It returns access token if the signature is valid, otherwise error.

Types

type Claims

type Claims struct {
	Wallet string `json:"wallet"`
	jwt.RegisteredClaims
}

Claims is the claims for the token.

func GetClaimsFromContext

func GetClaimsFromContext(ctx context.Context) *Claims

Get claims from context

func GetClaimsFromRequest

func GetClaimsFromRequest(r *http.Request) *Claims

Get claims from request context

type JWT

type JWT struct {
	// contains filtered or unexported fields
}

JWT is the interactor for JWT.

func NewJWT

func NewJWT(signingKey []byte) *JWT

NewJWT creates a new JWT interactor.

func (*JWT) IssueTokens

func (j *JWT) IssueTokens(walletAddr string) (TokenResponse, error)

IssueToken issues a token for the user. This function generates a token for the user and returns it.

func (*JWT) RefreshToken

func (j *JWT) RefreshToken(tokenString string) (TokenResponse, error)

RefreshToken refreshes the token. This function refreshes the token and returns the new token.

func (*JWT) VerifyToken

func (j *JWT) VerifyToken(tokenString string) (*Claims, error)

VerifyToken verifies the token. This function verifies the token and returns the claims.

type RefreshTokenPayload

type RefreshTokenPayload struct {
	// RefreshToken is the refresh token.
	RefreshToken string `json:"refresh_token"`
}

RefreshTokenPayload is the payload for the refresh token.

type RequestAuthHandlePayload

type RequestAuthHandlePayload struct {
	// PublicKey is the public key of the sender.
	PublicKey string `json:"public_key"`
}

RequestAuthHandlePayload is the payload for the request authentication.

type TokenResponse

type TokenResponse struct {
	Access    string `json:"access_token"`
	Refresh   string `json:"refresh_token"`
	ExpiresIn int64  `json:"expires_in"`
}

TokenResponse is the response for the token request.

type VerifySignedMessagePayload

type VerifySignedMessagePayload struct {
	// Message is the message that was signed.
	Message string `json:"message"`
	// Signature is the signature of the message.
	Signature string `json:"signature"`
	// PublicKey is the public key of the sender.
	PublicKey string `json:"public_key"`
}

VerifySignedMessagePayload is the payload for the signed message verification.

func (*VerifySignedMessagePayload) Validate

func (p *VerifySignedMessagePayload) Validate() error

Validate validates the payload.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL