jwtware

package
v0.45.7 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrJWTMissingOrMalformed = errors.New("missing or malformed JWT")
)

Functions

func ExtractRawTokenFromContext

func ExtractRawTokenFromContext(ctx router.Context, extractors []JWTExtractor) (string, error)

func New

func New(config ...Config) router.MiddlewareFunc

Types

type AuthClaims added in v0.7.0

type AuthClaims interface {
	Subject() string
	UserID() string
	Role() string
	CanRead(resource string) bool
	CanEdit(resource string) bool
	CanCreate(resource string) bool
	CanDelete(resource string) bool
	HasRole(role string) bool
	IsAtLeast(minRole string) bool
}

AuthClaims interface for structured claims without import cycles This mirrors the AuthClaims interface from the auth package

type Config

type Config struct {
	Filter func(router.Context) bool
	// SuccessHandler is invoked after successful auth; it must decide whether
	// to call the wrapped handler (next) or short-circuit.
	SuccessHandler      func(ctx router.Context, next router.HandlerFunc) error
	ErrorHandler        router.ErrorHandler
	SigningKey          SigningKey
	SigningKeys         map[string]SigningKey
	ContextKey          string
	TokenLookup         string
	AuthScheme          string
	KeyFunc             jwt.Keyfunc
	JWKSetURLs          []string
	LocalTokenSerilizer func(*jwt.Token) any
	// TokenValidator is required for token validation
	TokenValidator TokenValidator

	// Optional RBAC fields for enhanced middleware functionality
	// RoleChecker is an optional function to validate roles against custom logic
	RoleChecker func(AuthClaims, string) bool
	// RequiredRole specifies an exact role that must be present
	RequiredRole string
	// MinimumRole specifies the minimum role level required (uses role hierarchy)
	MinimumRole string

	// ContextEnricher is an optional function to propagate claims to the standard
	// Go context. If provided, it will be called after successful token validation.
	ContextEnricher func(c context.Context, claims AuthClaims) context.Context

	// ValidationListeners are invoked after token validation succeeds. Use them to
	// emit events, update schema caches, or perform bookkeeping before the request proceeds.
	ValidationListeners []ValidationListener

	// Template integration fields for automatic user context registration
	// TemplateUserKey specifies the key for storing user data for templates in router context.
	// If set, the middleware will automatically store user data under this key for template usage.
	TemplateUserKey string
	// UserProvider is an optional function to convert AuthClaims to a User object for templates.
	// If not provided, the AuthClaims will be stored directly under TemplateUserKey.
	UserProvider func(AuthClaims) (any, error)
}

func GetDefaultConfig

func GetDefaultConfig(config ...Config) (cfg Config)

type JWTExtractor

type JWTExtractor func(c router.Context) (string, error)

func GetExtractors

func GetExtractors(tokenLookup string, authSchemes ...string) []JWTExtractor

type SigningKey

type SigningKey struct {
	JWTAlg string
	Key    any
}

type TokenValidator added in v0.7.0

type TokenValidator interface {
	Validate(tokenString string) (AuthClaims, error)
}

TokenValidator interface for validating tokens without import cycles This mirrors the TokenService.Validate method from the auth package

type ValidationListener added in v0.15.0

type ValidationListener func(ctx router.Context, claims AuthClaims) error

ValidationListener is invoked after a token has been validated but before authorization checks.

Jump to

Keyboard shortcuts

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