jwt

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 14 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.ErrorAuthErrorReasonBearerTokenMissing("invalid bearer token")
	ErrTokenNotFound            = securityv1.ErrorAuthErrorReasonTokenNotFound("token not found")
	ErrTokenMalformed           = securityv1.ErrorAuthErrorReasonBearerTokenMissing("token malformed")
	ErrTokenSignatureInvalid    = securityv1.ErrorAuthErrorReasonSignTokenFailed("token signature invalid")
	ErrTokenExpired             = securityv1.ErrorAuthErrorReasonTokenExpired("token expired")
	ErrTokenNotValidYet         = securityv1.ErrorAuthErrorReasonTokenExpired("token not valid yet")
	ErrUnsupportedSigningMethod = securityv1.ErrorAuthErrorReasonUnsupportedSigningMethod("unsupported signing method")
	ErrInvalidClaims            = securityv1.ErrorAuthErrorReasonInvalidClaims("invalid Claims")
	ErrBearerTokenMissing       = securityv1.ErrorAuthErrorReasonBearerTokenMissing("bearer token missing")
	ErrSignTokenFailed          = securityv1.ErrorAuthErrorReasonSignTokenFailed("sign token failed")
	ErrMissingKeyFunc           = securityv1.ErrorAuthErrorReasonMissingKeyFunc("missing key function")
	ErrGetKeyFailed             = securityv1.ErrorAuthErrorReasonGetKeyFailed("get key failed")
	ErrInvalidSubject           = securityv1.ErrorAuthErrorReasonInvalidSubject("invalid subject")
	ErrInvalidIssuer            = securityv1.ErrorAuthErrorReasonInvalidIssuer("invalid issuer")
	ErrInvalidAudience          = securityv1.ErrorAuthErrorReasonInvalidAudience("invalid audience")
	ErrInvalidExpiration        = securityv1.ErrorAuthErrorReasonInvalidExpiration("invalid expiration")
)

Functions

func ClaimsToJwtClaims

func ClaimsToJwtClaims(raw security.Claims) jwtv5.Claims

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 GetSigningMethodFromAlg

func GetSigningMethodFromAlg(algorithm string) jwtv5.SigningMethod

GetSigningMethodFromAlg returns the signing method for a given algorithm.

func MapToClaims

func MapToClaims(rawClaims jwtv5.MapClaims, extraKeys ...string) (security.Claims, error)

func NewAuthenticator

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

NewAuthenticator creates a new Authenticator.

func RegisteredToClaims

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

func ToClaims

func ToClaims(rawClaims jwtv5.Claims, extraKeys ...string) (security.Claims, error)

Types

type Authenticator

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

Authenticator is a struct that implements the Authenticator interface.

func (*Authenticator) ApplyDefaults

func (obj *Authenticator) ApplyDefaults() error

func (*Authenticator) Authenticate

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

Authenticate authenticates the token string.

func (*Authenticator) AuthenticateContext

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

AuthenticateContext authenticates the token string from the context.

func (*Authenticator) AuthenticateToken

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

AuthenticateToken authenticates the token string.

func (*Authenticator) AuthenticateTokenContext

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

AuthenticateTokenContext authenticates the token string from the context.

func (*Authenticator) Close

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

Close closes the token cache service.

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) WithConfig

func (obj *Authenticator) 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(*Authenticator)

Setting is a function type for setting the Authenticator.

func WithCache

func WithCache(cache security.TokenCacheService) Setting

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

func WithExtraKeys

func WithExtraKeys(extraKeys ...string) Setting

WithExtraKeys returns a Setting function that sets the extra keys 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 WithScoped

func WithScoped(scoped bool) Setting

WithScoped 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