jwtauth

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInsufficientScope = errors.New("jwtauth: insufficient_scope")

ErrInsufficientScope indicates the token was valid but did not satisfy the required scopes policy; callers should respond with HTTP 403 where relevant.

View Source
var ErrUnauthorized = errors.New("jwtauth: unauthorized")

ErrUnauthorized indicates that the access token failed validation (e.g., signature, issuer, audience, exp/nbf) and the request should be treated as unauthenticated.

Functions

func NewFromDiscovery

func NewFromDiscovery(ctx context.Context, cfg *Config) (*discoveryAuthenticator, error)

NewFromDiscovery performs OIDC discovery to obtain jwks_uri and issuer, and constructs an Authenticator that validates RFC 9068 access tokens using the configured policies in Config. JWKS keys are auto-refreshed.

func NewStatic added in v0.7.0

func NewStatic(ctx context.Context, cfg *StaticConfig, jwksURI string) (*staticAuthenticator, error)

NewStatic constructs an authenticator that validates RFC 9068 JWT access tokens against a statically configured issuer, audiences and JWKS URI (no discovery).

Types

type Authenticator

type Authenticator interface {
	CheckAuthentication(ctx context.Context, tok string) (UserInfo, error)
}

Authenticator validates access tokens and returns a minimal UserInfo that exposes the subject and access to raw claims. Implementations MUST perform signature, issuer, audience and time validations.

type Config

type Config struct {
	Issuer string
	// ExpectedAudiences contains the primary audience (index 0) followed by any
	// additional accepted audiences. The first entry SHOULD be the production
	// audience registered with the authorization server; subsequent entries are
	// primarily intended for local / testing scenarios where the served MCP
	// endpoint base URL differs from the production one. Avoid growing this set
	// in production unless deliberately operating a multi-audience design.
	ExpectedAudiences []string
	RequiredScopes    []string
	ScopeModeAny      bool // if true, any of RequiredScopes is sufficient; else all are required
	AllowedAlgs       []string
	Leeway            time.Duration
	// HintScopes carries an optional set of scopes that transports may echo
	// in WWW-Authenticate "scope" parameters when constructing Bearer
	// challenges. They are advisory only and do not affect token validation.
	HintScopes []string
	// AdvertisedScopesTransform is an optional function that receives the scopes
	// discovered from the authorization server's OIDC metadata and returns the
	// scopes that should be advertised in the protected resource metadata. If nil,
	// the discovered scopes are used as-is.
	AdvertisedScopesTransform func(discovered []string) []string
}

Config controls validation behavior for access tokens. It is used by discovery-based authenticators to enforce issuer, audience, scope, algorithm, and clock-skew policies.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with safe defaults for algorithm and leeway.

type DiscoveryMetadata added in v0.7.0

type DiscoveryMetadata interface {
	AuthorizationEndpoint() string
	TokenEndpoint() string
}

DiscoveryMetadata exposes optional advertisement-only endpoints learned via OIDC discovery. Implementations may return empty strings if not applicable.

type StaticConfig added in v0.7.0

type StaticConfig struct {
	Issuer            string
	ExpectedAudiences []string
	AllowedAlgs       []string
	Leeway            time.Duration
}

StaticConfig controls validation for manual (non-discovery) JWT access tokens. Caller supplies issuer, one or more expected audiences, and JWKS URI.

func DefaultStaticConfig added in v0.7.0

func DefaultStaticConfig() *StaticConfig

DefaultStaticConfig returns a StaticConfig with safe algorithm + leeway defaults.

type UserInfo

type UserInfo interface {
	UserID() string
	Claims(ref any) error
}

UserInfo is the internal user claims carrier for validated tokens. It mirrors the minimal contract needed by the public auth package.

Jump to

Keyboard shortcuts

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