jwt

package module
v0.0.31 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package jwt implements the functions, types, and interfaces for the module.

Package jwt implements the functions, types, and interfaces for the module.

Package jwt implements the functions, types, and interfaces for the module.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken             = securityv1.ErrorSecurityErrorReasonBearerTokenMissing("invalid bearer token")
	ErrTokenNotFound            = securityv1.ErrorSecurityErrorReasonTokenNotFound("token not found")
	ErrTokenMalformed           = securityv1.ErrorSecurityErrorReasonBearerTokenMissing("token malformed")
	ErrTokenSignatureInvalid    = securityv1.ErrorSecurityErrorReasonSignTokenFailed("token signature invalid")
	ErrTokenExpired             = securityv1.ErrorSecurityErrorReasonTokenExpired("token expired")
	ErrTokenNotValidYet         = securityv1.ErrorSecurityErrorReasonTokenExpired("token not valid yet")
	ErrUnsupportedSigningMethod = securityv1.ErrorSecurityErrorReasonUnsupportedSigningMethod("unsupported signing method")
	ErrInvalidClaims            = securityv1.ErrorSecurityErrorReasonInvalidClaims("invalid Claims")
	ErrBearerTokenMissing       = securityv1.ErrorSecurityErrorReasonBearerTokenMissing("bearer token missing")
	ErrSignTokenFailed          = securityv1.ErrorSecurityErrorReasonSignTokenFailed("sign token failed")
	ErrMissingKeyFunc           = securityv1.ErrorSecurityErrorReasonMissingKeyFunc("missing key function")
	ErrGetKeyFailed             = securityv1.ErrorSecurityErrorReasonGetKeyFailed("get key failed")
	ErrInvalidSubject           = securityv1.ErrorSecurityErrorReasonInvalidSubject("invalid subject")
	ErrInvalidIssuer            = securityv1.ErrorSecurityErrorReasonInvalidIssuer("invalid issuer")
	ErrInvalidAudience          = securityv1.ErrorSecurityErrorReasonInvalidAudience("invalid audience")
	ErrInvalidExpiration        = securityv1.ErrorSecurityErrorReasonInvalidExpiration("invalid expiration")
)

Functions

func ClaimsToJwtClaims

func ClaimsToJwtClaims(raw security.Claims) jwtv5.Claims

func GetAlgorithmSigningMethod added in v0.0.3

func GetAlgorithmSigningMethod(algorithm string) jwtv5.SigningMethod

GetAlgorithmSigningMethod returns the signing method for a given algorithm.

func GetKeyFunc

func GetKeyFunc(key string) func(token *jwtv5.Token) (any, error)

GetKeyFunc returns a function that retrieves the key for a given token. The returned function takes a jwtv5.Token as an argument and returns the key as a string.

func GetKeyFuncWithAlg

func GetKeyFuncWithAlg(alg, key string) func(token *jwtv5.Token) (any, error)

GetKeyFuncWithAlg returns a function that retrieves the key for a given token with a specific algorithm. The returned function takes a jwtv5.Token as an argument and returns the key as a byte slice.

func MapToClaims

func MapToClaims(rawClaims jwtv5.MapClaims, extras map[string]string) (security.Claims, error)

func NewAuthenticator

func NewAuthenticator(cfg *configv1.Security, ss ...Setting) (security.Authenticator, error)

NewAuthenticator creates a new Authenticator.

func ProtoClaimsToClaims

func ProtoClaimsToClaims(rawClaims *securityv1.Claims) security.Claims

func RegisteredToClaims

func RegisteredToClaims(rawClaims *jwtv5.RegisteredClaims) (security.Claims, error)

func ToClaims

func ToClaims(rawClaims jwtv5.Claims, extras map[string]string) (security.Claims, error)

Types

type Authenticator

type Authenticator struct {
	*Option
	// contains filtered or unexported fields
}

Authenticator is a struct that implements the Authenticator interface.

func (*Authenticator) Authenticate

func (obj *Authenticator) Authenticate(ctx context.Context, tokenStr string) (security.Claims, error)

func (*Authenticator) AuthenticateContext

func (obj *Authenticator) AuthenticateContext(ctx context.Context, tokenType security.TokenType) (security.Claims, error)

func (*Authenticator) Close

func (obj *Authenticator) Close(ctx context.Context) error

Close closes the token cache service.

func (*Authenticator) CreateIdentityClaims

func (obj *Authenticator) CreateIdentityClaims(_ context.Context, id string, refresh bool) (security.Claims, error)

func (*Authenticator) CreateIdentityClaimsContext

func (obj *Authenticator) CreateIdentityClaimsContext(ctx context.Context, tokenType security.TokenType, id string) (context.Context, error)

func (*Authenticator) CreateToken

func (obj *Authenticator) CreateToken(ctx context.Context, claims security.Claims) (string, error)

CreateToken creates a token string from the claims.

func (*Authenticator) CreateTokenContext

func (obj *Authenticator) CreateTokenContext(ctx context.Context, tokenType security.TokenType, claims security.Claims) (context.Context, error)

CreateTokenContext creates a token string from the claims and adds it to the context.

func (*Authenticator) DestroyToken

func (obj *Authenticator) DestroyToken(ctx context.Context, tokenStr string) error

DestroyToken destroys the token string.

func (*Authenticator) DestroyTokenContext

func (obj *Authenticator) DestroyTokenContext(ctx context.Context, token security.TokenType) error

DestroyTokenContext destroys the token string from the context.

func (*Authenticator) Verify

func (obj *Authenticator) Verify(ctx context.Context, tokenStr string) (bool, error)

func (*Authenticator) VerifyContext

func (obj *Authenticator) VerifyContext(ctx context.Context, tokenType security.TokenType) (bool, error)

type Option added in v0.0.3

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

func (*Option) ApplyDefaults added in v0.0.3

func (option *Option) ApplyDefaults() error

func (*Option) WithConfig added in v0.0.3

func (option *Option) WithConfig(config *configv1.AuthNConfig_JWTConfig) error

type SecurityClaims

type SecurityClaims struct {
	*securityv1.Claims
	Extra map[string]string
}

func (*SecurityClaims) GetAudience

func (s *SecurityClaims) GetAudience() []string

func (*SecurityClaims) GetExpiration

func (s *SecurityClaims) GetExpiration() time.Time

func (*SecurityClaims) GetExtra

func (s *SecurityClaims) GetExtra() map[string]string

func (*SecurityClaims) GetIssuedAt

func (s *SecurityClaims) GetIssuedAt() time.Time

func (*SecurityClaims) GetIssuer

func (s *SecurityClaims) GetIssuer() string

func (*SecurityClaims) GetJWTID

func (s *SecurityClaims) GetJWTID() string

func (*SecurityClaims) GetNotBefore

func (s *SecurityClaims) GetNotBefore() time.Time

func (*SecurityClaims) GetScopes

func (s *SecurityClaims) GetScopes() map[string]bool

func (*SecurityClaims) GetSubject

func (s *SecurityClaims) GetSubject() string

type Setting

type Setting = func(*Option)

Setting is a function type for setting the Authenticator.

func WithAudience added in v0.0.3

func WithAudience(audience []string) Setting

WithAudience returns a Setting function that sets the audience for an Authenticator.

func WithCache

func WithCache(cache security.TokenCacheService) Setting

WithCache returns a Setting function that sets the token cache service for an Authenticator.

func WithExpireAccess added in v0.0.3

func WithExpireAccess(expiresAt time.Duration) Setting

WithExpireAccess returns a Setting function that sets the expiration time for an Authenticator.

func WithExpireRefresh added in v0.0.3

func WithExpireRefresh(expiresAt time.Duration) Setting

WithExpireRefresh returns a Setting function that sets the expiration time for an Authenticator.

func WithExtraClaims added in v0.0.3

func WithExtraClaims(extras map[string]string) Setting

WithExtraClaims returns a Setting function that sets the extra keys for an Authenticator.

func WithIssuer added in v0.0.3

func WithIssuer(issuer string) Setting

WithIssuer returns a Setting function that sets the issuer for an Authenticator.

func WithJTI added in v0.0.3

func WithJTI(fn func() string) Setting

WithJTI returns a Setting function that sets the JTI generator function for an Authenticator.

func WithKeyFunc

func WithKeyFunc(keyFunc func(token *jwtv5.Token) (any, error)) Setting

WithKeyFunc returns a Setting function that sets the key function for an Authenticator. The key function is used to retrieve the key for a given token.

func WithScheme

func WithScheme(scheme security.Scheme) Setting

WithScheme returns a Setting function that sets the scheme for an Authenticator.

func WithScopes added in v0.0.3

func WithScopes(scopes map[string]bool) Setting

WithScopes returns a Setting function that sets the scoped flag for an Authenticator. The scoped flag determines whether the Authenticator should use scoped tokens.

func WithSigningMethod

func WithSigningMethod(signingMethod jwtv5.SigningMethod) Setting

WithSigningMethod returns a Setting function that sets the signing method for an Authenticator. The signing method is used to sign and verify tokens.

Jump to

Keyboard shortcuts

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