Skip to content

Config ​

Config ​

go
type Config struct {
    SecretKey       string
    SigningKey      any
    VerificationKey any
    SigningMethod   SigningMethod

    AccessTokenTTL        time.Duration
    RefreshTokenTTL       time.Duration
    Issuer                string
    ExpectedAudience      string
    RequireExpiration     bool
    RejectRefreshAsAccess bool
    RotateRefreshTokens   bool
    ClockSkew             time.Duration

    Blacklist BlacklistConfig

    EnableRateLimit bool
    RateLimitRate   int
    RateLimitWindow time.Duration
    RateLimiter     RateLimitProvider

    Clock ClockProvider
}

Unified configuration for the JWT Processor. Zero-value fields are filled with defaults in New() (via normalizeConfig).

Auto-fill Rules

  • RateLimitRate and RateLimitWindow are only populated when EnableRateLimit = true
  • EnableAutoCleanup only takes effect with a custom Store; the built-in store always has auto-cleanup enabled
  • SecretKey, SigningKey, and VerificationKey are not auto-populated; they must be set manually
  • An explicitly provided RateLimiter always takes effect regardless of EnableRateLimit — supplying one is treated as explicit intent
struct

Fields ​

FieldTypeDefaultDescription
SecretKeystring—HMAC key (at least 32 bytes)
SigningKeyany—Asymmetric algorithm private key (*rsa.PrivateKey or *ecdsa.PrivateKey)
VerificationKeyany—Asymmetric algorithm public key (optional, defaults to SigningKey)
SigningMethodSigningMethodHS256Signing algorithm
AccessTokenTTLtime.Duration15mAccess token lifetime; derived only when the claims' ExpiresAt is zero
RefreshTokenTTLtime.Duration168hRefresh token lifetime; derived only when the claims' ExpiresAt is zero
Issuerstring"jwt-service"Issuer; written only when the claims' Issuer is zero (an explicit caller value wins)
ExpectedAudiencestring—Expected audience (optional)
RequireExpirationboolfalseWhen true, rejects tokens lacking an exp claim during validation (returns ErrExpirationRequired)
RejectRefreshAsAccessboolfalse(New in v1.2.3) When true, Parse / ParseInto reject token_type=refresh tokens being used as access credentials (returns ErrTokenTypeMismatch); Refresh / RefreshInto are never affected
RotateRefreshTokensboolfalse(New in v1.2.3) When true, Refresh / RefreshInto revoke the supplied refresh token (by jti) before minting the new token, giving one-time-use semantics. The path is fail-closed: a store error returns ErrRefreshRotationFailed, and a missing jti returns ErrTokenMissingID. See Token Refresh & Rotation
ClockSkewtime.Duration0Clock skew leeway applied to exp/nbf during validation (tolerates clock drift between issuer and validator); negative values are rejected by Validate()
BlacklistBlacklistConfig—Blacklist configuration
EnableRateLimitboolfalseEnable rate limiting
RateLimitRateint100Max requests per window
RateLimitWindowtime.Duration1mRate limit window
RateLimiterRateLimitProvider—Custom rate limiter (optional)
ClockClockProviderSystemClock{}Clock provider

Registered Claim Override Rules

The library only fills zero values: an explicit iss/exp/iat/jti set by the caller is used as-is, overriding the configuration and the TTLs.

When the claims set Issuer explicitly, Parse still checks the token against Config.Issuer. A differing value makes self-signed tokens fail verification with ErrTokenInvalidIssuer. The same applies to ExpiresAt: an explicit expiration bypasses the AccessTokenTTL / RefreshTokenTTL policy. Leave ExpiresAt unset when the TTL policy must apply, and keep Issuer equal to Config.Issuer when self-signed tokens must verify.

Ownership and Cascade Close

Processor.Close closes the limiter injected via Config.RateLimiter and also closes the custom blacklist store supplied through BlacklistConfig.Store.

Do not share one instance across processors that may outlive each other. After one processor closes a shared limiter, its Allow always returns false and the other processors' token creation fails with ErrRateLimitExceeded.

Methods ​

MethodSignatureDescription
Validatefunc (c *Config) Validate() errorValidates configuration

New() fills zero-value defaults (normalizeConfig) and then validates. A direct Validate() call leaves zero-value fields untouched, so the same configuration can fail Validate() while passing New(). To match New() behavior, start from DefaultConfig().

Validate() checks:

CheckDescription
Signing keysHMAC requires SecretKey ≥32 bytes and non-weak; RSA/ECDSA requires correct SigningKey type; ECDSA requires curve match; VerificationKey must match algorithm public key type
TTL validityAccessTokenTTL and RefreshTokenTTL must be positive
TTL orderingAccessTokenTTL must be less than RefreshTokenTTL
ClockSkewClockSkew must not be negative
Signing algorithmMust be one of the 12 built-in algorithms
Rate limit parametersWhen EnableRateLimit is true, a negative RateLimitRate or RateLimitWindow returns ErrInvalidConfig
Rate limit ceilingWhen EnableRateLimit is true, RateLimitRate > 1000000 returns ErrInvalidConfig
Rate limit window ceilingWhen EnableRateLimit is true, RateLimitWindow > 30 days returns ErrInvalidConfig
BlacklistBuilt-in store requires positive MaxSize and CleanupInterval

BlacklistConfig ​

go
type BlacklistConfig struct {
    CleanupInterval   time.Duration
    MaxSize           int
    EnableAutoCleanup bool
    Store             BlacklistStore
}

Blacklist configuration. BlacklistConfig.Store supplies a custom storage backend (e.g. Redis).

When set, CleanupInterval and MaxSize are ignored, and the backend owns cleanup and capacity. Processor.Close cascades a close to that store (see the ownership note above). EnableAutoCleanup only takes effect with a custom Store; the built-in store always has auto-cleanup enabled.

Built-in Store Capacity Behavior

When the built-in store is full, expired entries are removed first, roughly the earliest-expiring 10% is then evicted, and the new entry is written directly — Add never fails because the store is full.

The cost is fail-open: revocation records dropped by eviction silently stop taking effect, and those tokens become usable again. Raising MaxSize lowers the fail-open probability.

struct

Fields ​

FieldTypeDefaultDescription
CleanupIntervaltime.Duration5mExpired entry cleanup interval (built-in store only)
MaxSizeint100000Max entries in the memory store (built-in store only; when full, the entries closest to expiry are evicted — see the capacity behavior above)
EnableAutoCleanupbooltrueAutomatic cleanup of expired entries (only takes effect with a custom Store; the built-in store always enables it)
StoreBlacklistStore—Custom storage backend (when set, other fields are ignored)