Documentation
¶
Overview ¶
Package jwtauth provides JWT authentication for Connect RPC services.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingToken is returned when no Authorization header is present. ErrMissingToken = errors.New("missing authorization token") // ErrInvalidTokenFormat is returned when the token format is invalid. ErrInvalidTokenFormat = errors.New("invalid token format") // ErrTokenExpired is returned when the token has expired. ErrTokenExpired = errors.New("token has expired") // ErrTokenNotYetValid is returned when the token is not yet valid (nbf claim). ErrTokenNotYetValid = errors.New("token not yet valid") // ErrInvalidIssuer is returned when the issuer claim does not match. ErrInvalidIssuer = errors.New("invalid token issuer") // ErrInvalidAudience is returned when the audience claim does not match. ErrInvalidAudience = errors.New("invalid token audience") // ErrSignatureVerification is returned when signature verification fails. ErrSignatureVerification = errors.New("signature verification failed") // ErrJWKSFetch is returned when JWKS cannot be fetched. ErrJWKSFetch = errors.New("failed to fetch JWKS") )
Sentinel errors for JWT authentication.
Functions ¶
func NewInterceptor ¶
func NewInterceptor(auth *Authenticator) connect.Interceptor
NewInterceptor creates a Connect RPC interceptor that validates JWT tokens. It extracts the token from the Authorization header, validates it, and injects claims into the request context using ctxutil.WithClaims.
Types ¶
type Authenticator ¶
type Authenticator struct {
// contains filtered or unexported fields
}
Authenticator validates JWT tokens and extracts claims.
func NewAuthenticator ¶
func NewAuthenticator(ctx context.Context, cfg Config) (*Authenticator, error)
NewAuthenticator creates a new JWT authenticator with the given configuration. The ctx controls the lifecycle of the background JWKS refresh goroutine. Returns error if required config fields are empty or if initial JWKS fetch fails.
func (*Authenticator) Authenticate ¶
Authenticate validates the JWT token and returns extracted claims. Token should be the raw JWT string (without "Bearer " prefix).
type ClaimsMapping ¶
type ClaimsMapping struct {
// UserID is the JWT claim path for user ID (e.g., "sub", "user_id").
UserID string `koanf:"user_id"`
// TenantID is the JWT claim path for tenant ID (e.g., "tenant_id", "org.id").
TenantID string `koanf:"tenant_id"`
// Roles is the JWT claim path for roles (e.g., "roles", "realm_access.roles").
Roles string `koanf:"roles"`
// Permissions is the JWT claim path for permissions (e.g., "permissions", "scope").
Permissions string `koanf:"permissions"`
}
ClaimsMapping defines how JWT claims map to ctxutil.Claims fields. Use dot notation for nested claims (e.g., "realm_access.roles" for Keycloak).
type Config ¶
type Config struct {
// JWKSURL is the URL to fetch JSON Web Key Set
// (e.g., "https://idp.example.com/.well-known/jwks.json").
// Required.
JWKSURL string `koanf:"jwks_url"`
// Issuer is the expected "iss" claim value.
// Required.
Issuer string `koanf:"issuer"`
// Audience is the expected "aud" claim value.
// Required.
Audience string `koanf:"audience"`
// ClaimsMapping defines how JWT claims map to application claims.
ClaimsMapping *ClaimsMapping `koanf:"claims_mapping"`
// HTTPTimeout is the timeout for JWKS fetch requests.
HTTPTimeout time.Duration `koanf:"http_timeout"`
// Leeway allows clock skew tolerance for exp/nbf/iat validation.
Leeway time.Duration `koanf:"leeway"`
}
Config holds configuration for the JWT authentication interceptor.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible default values. JWKSURL, Issuer, and Audience are required and must be set by the caller.