Documentation
¶
Overview ¶
Package jwtblacklist provides a Caddy middleware for integrated JWT authentication and blacklist validation using Redis. This module combines JWT token validation with Redis-based blacklist checking in a single middleware.
Index ¶
- Variables
- type Claims
- type Config
- type JWTBlacklist
- func (JWTBlacklist) CaddyModule() caddy.ModuleInfo
- func (jb *JWTBlacklist) Cleanup() error
- func (jb *JWTBlacklist) Provision(ctx caddy.Context) error
- func (jb *JWTBlacklist) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error
- func (jb *JWTBlacklist) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
- func (jb *JWTBlacklist) Validate() error
- type JWTConfig
- type RedisClient
- type TLSConfig
- type Token
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingKeys = errors.New("missing sign_key and jwk_url") ErrInvalidPublicKey = errors.New("invalid PEM-formatted public key") ErrInvalidSignAlgorithm = errors.New("invalid sign_alg") ErrInvalidIssuer = errors.New("invalid issuer") ErrInvalidAudience = errors.New("invalid audience") ErrEmptyUserClaim = errors.New("user claim is empty") )
JWT error constants
Functions ¶
This section is empty.
Types ¶
type Claims ¶
type Claims struct {
UserID string `json:"sub"`
APIKeyID string `json:"jti"` // This is the API key ID we check against blacklist
Tier string `json:"tier"`
Scope string `json:"scope"`
}
Claims represents the JWT claims we're interested in
type Config ¶
type Config struct {
// Redis connection settings
RedisAddr string `json:"redis_addr,omitempty"`
RedisPassword string `json:"redis_password,omitempty"`
RedisDB int `json:"redis_db,omitempty"`
RedisTLS *TLSConfig `json:"redis_tls,omitempty"`
// JWT settings (for backward compatibility)
JWTSecret string `json:"jwt_secret,omitempty"`
// Advanced JWT configuration
JWT *JWTConfig `json:"jwt,omitempty"`
// Blacklist settings
BlacklistPrefix string `json:"blacklist_prefix,omitempty"`
// Behavior settings
FailOpen bool `json:"fail_open,omitempty"`
Timeout caddy.Duration `json:"timeout,omitempty"`
LogBlocked bool `json:"log_blocked,omitempty"`
}
Config holds the configuration for the JWT blacklist plugin
type JWTBlacklist ¶
type JWTBlacklist struct {
Config *Config `json:"config,omitempty"`
// contains filtered or unexported fields
}
JWTBlacklist is the main middleware struct
func (JWTBlacklist) CaddyModule ¶
func (JWTBlacklist) CaddyModule() caddy.ModuleInfo
CaddyModule returns the Caddy module information
func (*JWTBlacklist) Cleanup ¶
func (jb *JWTBlacklist) Cleanup() error
Cleanup closes the Redis connection
func (*JWTBlacklist) Provision ¶
func (jb *JWTBlacklist) Provision(ctx caddy.Context) error
Provision sets up the module
func (*JWTBlacklist) ServeHTTP ¶
func (jb *JWTBlacklist) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error
ServeHTTP implements the integrated JWT authentication and blacklist validation CRITICAL: Blacklist check happens BEFORE full JWT authentication for performance and security
func (*JWTBlacklist) UnmarshalCaddyfile ¶
func (jb *JWTBlacklist) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
UnmarshalCaddyfile implements caddyfile.Unmarshaler
func (*JWTBlacklist) Validate ¶
func (jb *JWTBlacklist) Validate() error
Validate ensures the configuration is valid
type JWTConfig ¶ added in v1.0.6
type JWTConfig struct {
// SignKey is the key used by the signing algorithm to verify the signature
SignKey string `json:"sign_key"`
// JWKURL is the URL where a provider publishes their JWKs
JWKURL string `json:"jwk_url"`
// SignAlgorithm is the signing algorithm used
SignAlgorithm string `json:"sign_alg"`
// SkipVerification disables the verification of the JWT token signature
SkipVerification bool `json:"skip_verification"`
// FromQuery defines a list of names to get tokens from query parameters
FromQuery []string `json:"from_query"`
// FromHeader defines a list of names to get tokens from HTTP headers
FromHeader []string `json:"from_header"`
// FromCookies defines a list of names to get tokens from HTTP cookies
FromCookies []string `json:"from_cookies"`
// IssuerWhitelist defines a list of allowed issuers
IssuerWhitelist []string `json:"issuer_whitelist"`
// AudienceWhitelist defines a list of allowed audiences
AudienceWhitelist []string `json:"audience_whitelist"`
// UserClaims defines a list of names to find the ID of the authenticated user
UserClaims []string `json:"user_claims"`
// MetaClaims defines a map to populate user metadata placeholders
MetaClaims map[string]string `json:"meta_claims"`
// contains filtered or unexported fields
}
JWTConfig holds the JWT authentication configuration
type RedisClient ¶
type RedisClient struct {
// contains filtered or unexported fields
}
RedisClient wraps the Redis client with blacklist-specific functionality
func NewRedisClient ¶
func NewRedisClient(addr, password string, db int, tlsConfig *TLSConfig, logger *zap.Logger) (*RedisClient, error)
NewRedisClient creates a new Redis client with optional TLS support
func (*RedisClient) GetBlacklistInfo ¶
func (rc *RedisClient) GetBlacklistInfo(ctx context.Context, apiKeyID string, prefix string) (string, time.Duration, error)
GetBlacklistInfo retrieves additional information about a blacklisted key
func (*RedisClient) IsBlacklisted ¶
func (rc *RedisClient) IsBlacklisted(ctx context.Context, apiKeyID string, prefix string) (bool, error)
IsBlacklisted checks if an API key is blacklisted
type TLSConfig ¶ added in v1.0.1
type TLSConfig struct {
Enabled bool `json:"enabled,omitempty"`
InsecureSkipVerify bool `json:"insecure_skip_verify,omitempty"`
ServerName string `json:"server_name,omitempty"`
MinVersion string `json:"min_version,omitempty"`
CertFile string `json:"cert_file,omitempty"`
KeyFile string `json:"key_file,omitempty"`
CAFile string `json:"ca_file,omitempty"`
}
TLSConfig holds TLS configuration options