jwt

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package jwt provides a JWT-based implementation of the security interfaces.

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

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

Index

Constants

View Source
const (
	// DefaultIssuer is the default issuer for JWT tokens.
	DefaultIssuer = "origadmin"
	// DefaultAccessTokenTTL is the default time-to-live for access tokens.
	DefaultAccessTokenTTL = 2 * time.Hour
	// DefaultRefreshTokenTTL is the default time-to-live for refresh tokens.
	DefaultRefreshTokenTTL = 7 * 24 * time.Hour
)

Variables

This section is empty.

Functions

func NewAuthenticator

func NewAuthenticator(cfg *authnv1.Authenticator, opts ...Option) (authn.Authenticator, error)

NewAuthenticator creates a new JWT Provider from the given configuration and options.

Types

type Authenticator

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

Authenticator implements the security interfaces for JWT.

func New added in v1.2.0

func New(opts *Options, logger log.Logger) (*Authenticator, error)

New creates a new Authenticator instance from a pre-built Options object and a logger.

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(ctx context.Context, cred security.Credential) (security.Principal, error)

Authenticate validates the provided credential and returns a Principal if successful.

func (*Authenticator) CreateCredential

CreateCredential issues a new credential for the given principal.

func (*Authenticator) RefreshCredential added in v1.3.0

func (a *Authenticator) RefreshCredential(ctx context.Context, refreshToken string) (security.CredentialResponse, error)

RefreshCredential issues a new credential based on a valid refresh token.

func (*Authenticator) Revoke

func (a *Authenticator) Revoke(ctx context.Context, cred security.Credential) error

Revoke invalidates the given credential.

func (*Authenticator) Supports

func (a *Authenticator) Supports(cred security.Credential) bool

Supports returns true if this authenticator can handle the given credential.

type Claims

type Claims struct {
	jwtv5.RegisteredClaims
	Roles       []string        `json:"roles,omitempty"`
	Permissions []string        `json:"permissions,omitempty"`
	Scopes      map[string]bool `json:"scopes,omitempty"`
}

Claims represents the JWT claims, including standard claims and custom ones.

func (*Claims) Export

func (c *Claims) Export() map[string]*structpb.Value

func (*Claims) Get

func (c *Claims) Get(key string) (interface{}, bool)

func (*Claims) GetBool

func (c *Claims) GetBool(key string) (bool, bool)

func (*Claims) GetFloat64

func (c *Claims) GetFloat64(key string) (float64, bool)

func (*Claims) GetInt64

func (c *Claims) GetInt64(key string) (int64, bool)

func (*Claims) GetMap

func (c *Claims) GetMap(key string) (map[string]any, bool)

func (*Claims) GetString

func (c *Claims) GetString(key string) (string, bool)

func (*Claims) GetStringSlice

func (c *Claims) GetStringSlice(key string) ([]string, bool)

func (*Claims) UnmarshalValue

func (c *Claims) UnmarshalValue(key string, target any) error

type Option

type Option = options.Option

Option is a functional option type for configuring the JWT authenticator.

func WithAccessTokenLifetime

func WithAccessTokenLifetime(d time.Duration) Option

WithAccessTokenLifetime returns an options.Option that sets access token expiration.

func WithAudience

func WithAudience(audience []string) Option

WithAudience returns an options.Option that sets JWT audience.

func WithCache

func WithCache(cache securitycache.Cache) Option

WithCache returns an options.Option that sets token cache.

func WithClock

func WithClock(c func() time.Time) Option

WithClock provides a function to return the current time, useful for testing.

func WithExtraClaims

func WithExtraClaims(extras map[string]string) Option

WithExtraClaims returns an options.Option that sets extra claims.

func WithGenerateID

func WithGenerateID(g func() string) Option

WithGenerateID provides a function to generate unique IDs (e.g., for 'jti' claims).

func WithIssuer

func WithIssuer(issuer string) Option

WithIssuer returns an options.Option that sets JWT issuer.

func WithKeyFunc

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

WithKeyFunc returns an options.Option that sets key function.

func WithLogger added in v1.3.0

func WithLogger(logger log.Logger) Option

WithLogger sets the logger for the authenticator.

func WithRefreshTokenLifetime

func WithRefreshTokenLifetime(d time.Duration) Option

WithRefreshTokenLifetime returns an options.Option that sets refresh token expiration.

func WithSigningKey

func WithSigningKey(algorithm, keyData string) Option

WithSigningKey sets the JWT signing method and key function from algorithm and key data strings. This is a convenience option for common use cases.

func WithSigningMethod

func WithSigningMethod(signingMethod jwtv5.SigningMethod) Option

WithSigningMethod returns an options.Option that sets JWT signing method.

type Options

type Options struct {
	Logger log.Logger
	// contains filtered or unexported fields
}

Options holds the configuration options for the JWT authenticator.

func FromOptions

func FromOptions(opts ...Option) *Options

FromOptions creates a new Options struct from a slice of option functions.

func NewOptions added in v1.2.0

func NewOptions(cfg *authnv1.Authenticator, opts ...Option) (*Options, error)

NewOptions creates a new Options object from the given configuration and functional options.

Jump to

Keyboard shortcuts

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