jwt

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenInvalid           = errors.Unauthorized(reason, "Token is invalid")
	ErrTokenExpired           = errors.Unauthorized(reason, "Token has expired")
	ErrTokenParseFail         = errors.Unauthorized(reason, "Fail to parse token")
	ErrUnSupportSigningMethod = errors.Unauthorized(reason, "Wrong signing method")
	ErrSignTokenFailed        = errors.Unauthorized(reason, "Failed to sign token")
)
View Source
var (
	MessageTokenInvalid           = &goi18n.Message{ID: "jwt.token.invalid", Other: ErrTokenInvalid.Error()}
	MessageTokenExpired           = &goi18n.Message{ID: "jwt.token.expired", Other: ErrTokenExpired.Error()}
	MessageTokenParseFail         = &goi18n.Message{ID: "jwt.token.parse.failed", Other: ErrTokenParseFail.Error()}
	MessageUnSupportSigningMethod = &goi18n.Message{ID: "jwt.wrong.signing.method", Other: ErrUnSupportSigningMethod.Error()}
	MessageSignTokenFailed        = &goi18n.Message{ID: "jwt.token.sign.failed", Other: ErrSignTokenFailed.Error()}
)

Define i18n messages.

Functions

This section is empty.

Types

type JWTAuth

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

JWTAuth implement the authn.Authenticator interface.

func New

func New(store Storer, opts ...Option) *JWTAuth

New create a authentication instance.

func (*JWTAuth) Destroy

func (a *JWTAuth) Destroy(ctx context.Context, refreshToken string) error

Destroy is used to destroy a token.

func (*JWTAuth) ParseClaims

func (a *JWTAuth) ParseClaims(ctx context.Context, refreshToken string) (*jwt.RegisteredClaims, error)

ParseClaims parse the token and return the claims.

func (*JWTAuth) Release

func (a *JWTAuth) Release() error

Release used to release the requested resources.

func (*JWTAuth) Sign

func (a *JWTAuth) Sign(ctx context.Context, userID string) (authn.IToken, error)

Sign is used to generate a token.

type Option

type Option func(*options)

Option is jwt option.

func WithExpired

func WithExpired(expired time.Duration) Option

WithExpired set the token expiration time (in seconds, default 2h).

func WithIssuer

func WithIssuer(issuer string) Option

WithIssuer set token issuer which is identifies the principal that issued the JWT.

func WithKeyfunc

func WithKeyfunc(keyFunc jwt.Keyfunc) Option

WithKeyfunc set the callback function for verifying the key.

func WithSigningKey

func WithSigningKey(key any) Option

WithSigningKey set the signature key.

func WithSigningMethod

func WithSigningMethod(method jwt.SigningMethod) Option

WithSigningMethod set signature method.

func WithTokenHeader

func WithTokenHeader(header map[string]any) Option

WithTokenHeader set the customer tokenHeader for client side.

type Storer

type Storer interface {
	// Store token data and specify expiration time.
	Set(ctx context.Context, accessToken string, expiration time.Duration) error

	// Delete token data from storage.
	Delete(ctx context.Context, accessToken string) (bool, error)

	// Check if token exists.
	Check(ctx context.Context, accessToken string) (bool, error)

	// Close the storage.
	Close() error
}

Storer token storage interface.

Directories

Path Synopsis
store

Jump to

Keyboard shortcuts

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