Documentation
¶
Index ¶
- Variables
- func ClearCache()
- func CreateToken(secretKey string, claims Claims) (string, error)
- func RevokeToken(secretKey, tokenString string) error
- type BlacklistConfig
- type CacheStats
- type Claims
- type Config
- type NumericDate
- type Processor
- func (p *Processor) Close() error
- func (p *Processor) CreateRefreshToken(claims Claims) (string, error)
- func (p *Processor) CreateToken(claims Claims) (string, error)
- func (p *Processor) IsClosed() bool
- func (p *Processor) IsTokenRevoked(tokenString string) (bool, error)
- func (p *Processor) RefreshToken(refreshTokenString string) (string, error)
- func (p *Processor) RevokeToken(tokenString string) error
- func (p *Processor) ValidateToken(tokenString string) (Claims, bool, error)
- type RateLimiter
- type RegisteredClaims
- type SigningMethod
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidConfig = errors.New("invalid configuration") ErrInvalidSecretKey = errors.New("invalid secret key") ErrInvalidSigningMethod = errors.New("invalid signing method") ErrInvalidToken = errors.New("invalid token") ErrEmptyToken = errors.New("empty token") ErrTokenRevoked = errors.New("token revoked") ErrTokenMissingID = errors.New("token missing ID") ErrInvalidClaims = errors.New("invalid claims") ErrRateLimitExceeded = errors.New("rate limit exceeded") ErrProcessorClosed = errors.New("processor closed") )
Functions ¶
func ClearCache ¶ added in v1.0.1
func ClearCache()
func RevokeToken ¶
Types ¶
type BlacklistConfig ¶
func DefaultBlacklistConfig ¶
func DefaultBlacklistConfig() BlacklistConfig
type CacheStats ¶ added in v1.0.2
func GetCacheStats ¶ added in v1.0.2
func GetCacheStats() CacheStats
type Claims ¶
type Claims struct {
UserID string `json:"user_id,omitempty"`
Username string `json:"username,omitempty"`
Role string `json:"role,omitempty"`
Permissions []string `json:"permissions,omitempty"`
Scopes []string `json:"scopes,omitempty"`
Extra map[string]any `json:"extra,omitempty"`
SessionID string `json:"session_id,omitempty"`
ClientID string `json:"client_id,omitempty"`
RegisteredClaims
}
Claims represents JWT claims with custom application-specific fields.
type Config ¶
type Config struct {
SecretKey string `yaml:"secret_key" json:"secret_key"`
AccessTokenTTL time.Duration `yaml:"access_token_ttl" json:"access_token_ttl"`
RefreshTokenTTL time.Duration `yaml:"refresh_token_ttl" json:"refresh_token_ttl"`
Issuer string `yaml:"issuer" json:"issuer"`
SigningMethod SigningMethod `yaml:"signing_method" json:"signing_method"`
EnableRateLimit bool `yaml:"enable_rate_limit" json:"enable_rate_limit"`
RateLimitRate int `yaml:"rate_limit_rate" json:"rate_limit_rate"`
RateLimitWindow time.Duration `yaml:"rate_limit_window" json:"rate_limit_window"`
RateLimiter *RateLimiter `yaml:"-" json:"-"`
}
func DefaultConfig ¶
func DefaultConfig() Config
type NumericDate ¶
func NewNumericDate ¶
func NewNumericDate(t time.Time) NumericDate
func (*NumericDate) MarshalJSON ¶
func (date *NumericDate) MarshalJSON() ([]byte, error)
func (*NumericDate) UnmarshalJSON ¶
func (date *NumericDate) UnmarshalJSON(b []byte) error
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
func New ¶
New creates a new JWT Processor with secretKey and optional configuration. The processor is thread-safe and can be used concurrently by multiple goroutines. Always call Close() when done to release resources and securely clear the secret key.
func NewWithBlacklist ¶
func NewWithBlacklist(secretKey string, blacklistConfig BlacklistConfig, config ...Config) (*Processor, error)
NewWithBlacklist creates a new JWT Processor with custom blacklist configuration. Use this when you need fine-grained control over token revocation behavior. The processor is thread-safe and can be used concurrently by multiple goroutines. Always call Close() when done to release resources and securely clear the secret key.
func (*Processor) CreateRefreshToken ¶
func (*Processor) IsTokenRevoked ¶
func (*Processor) RefreshToken ¶
func (*Processor) RevokeToken ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter provides rate limiting for JWT operations using token bucket algorithm.
func NewRateLimiter ¶
func NewRateLimiter(maxRate int, window time.Duration) *RateLimiter
NewRateLimiter creates a new rate limiter with the specified rate and window.
func (*RateLimiter) Allow ¶
func (rl *RateLimiter) Allow(key string) bool
Allow checks if a single request is allowed for the given key.
func (*RateLimiter) AllowN ¶
func (rl *RateLimiter) AllowN(key string, n int) bool
AllowN checks if n requests are allowed for the given key.
func (*RateLimiter) Close ¶
func (rl *RateLimiter) Close()
Close closes the rate limiter and releases all resources.
func (*RateLimiter) Reset ¶
func (rl *RateLimiter) Reset(key string)
Reset removes the rate limit bucket for the given key.
type RegisteredClaims ¶
type RegisteredClaims struct {
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Audience []string `json:"aud,omitempty"`
ExpiresAt NumericDate `json:"exp"`
NotBefore NumericDate `json:"nbf"`
IssuedAt NumericDate `json:"iat"`
ID string `json:"jti,omitempty"`
}
type SigningMethod ¶
type SigningMethod string
const ( SigningMethodHS256 SigningMethod = "HS256" SigningMethodHS384 SigningMethod = "HS384" SigningMethodHS512 SigningMethod = "HS512" )
type ValidationError ¶
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error