Skip to content

Processor ​

Processor is the core JWT operation type, implementing the TokenManager interface. All methods are concurrency-safe.

Create an instance via jwt.New(cfg).

Create ​

go
func (p *Processor) Create(claims CustomClaims) (string, error)

Creates a new JWT access token. Accepts any type implementing the CustomClaims interface.

Parameters ​

ParameterTypeDescription
claimsCustomClaimsToken claims

Returns ​

ReturnTypeDescription
tokenstringSigned JWT string
errerrorError on validation or signing failure

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrInvalidClaimsClaims validation failed
ErrRateLimitExceededRate limit threshold exceeded

Example ​

go
// Built-in Claims
claims := &jwt.Claims{UserID: "user123", Username: "alice"}
token, err := processor.Create(claims)

// Custom Claims
myClaims := &MyClaims{UserID: "123"}
token, err := processor.Create(myClaims)

Parse v1.2.3+ ​

go
func (p *Processor) Parse(tokenString string) (Claims, error)

Validates a JWT access token and returns the parsed Claims (value copy). A nil err means the token is valid — there is no separate boolean to check. Replaces the deprecated Validate.

Parameters ​

ParameterTypeDescription
tokenStringstringJWT string

Returns ​

ReturnTypeDescription
claimsClaimsParsed claims (value copy)
errerrorError on validation failure; nil means valid

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrInvalidTokenInvalid signature
ErrAlgorithmMismatchToken algorithm does not match config
ErrExpirationRequiredRequireExpiration is enabled but the token lacks an exp claim
ErrTokenExpiredToken expired
ErrTokenNotValidYetToken not yet valid
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenRevokedToken revoked
ErrInvalidClaimsClaims validation failed
ErrTokenTypeMismatchToken has type refresh and RejectRefreshAsAccess is enabled

Example ​

go
claims, err := processor.Parse(tokenString)
if err != nil {
    // Handle error
    return
}
fmt.Println(claims.UserID)

Validate ​

go
func (p *Processor) Validate(tokenString string) (Claims, bool, error)

Deprecated

Validate is deprecated since v1.2.3: the boolean result valid is always equivalent to err == nil and carries no extra information. Use Parse, which returns (Claims, error).

Validates a JWT access token and returns the parsed Claims. Internally delegates directly to Parse.

Migrating from Validate to Parse ​

valid is always equivalent to err == nil — migrating is just dropping the boolean check:

go
// Old style (deprecated)
parsed, valid, err := processor.Validate(token)
if valid {
    // ...
}

// New style: nil err means valid
parsed, err := processor.Parse(token)
if err != nil {
    // Handle error
}

Parameters ​

ParameterTypeDescription
tokenStringstringJWT string

Returns ​

ReturnTypeDescription
claimsClaimsParsed claims (value copy)
validboolWhether valid (always equal to err == nil)
errerrorError on validation failure

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrInvalidTokenInvalid signature
ErrAlgorithmMismatchToken algorithm does not match config
ErrExpirationRequiredRequireExpiration is enabled but the token lacks an exp claim
ErrTokenExpiredToken expired
ErrTokenNotValidYetToken not yet valid
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenRevokedToken revoked
ErrInvalidClaimsClaims validation failed
ErrTokenTypeMismatchToken has type refresh and RejectRefreshAsAccess is enabled

Example ​

go
claims, valid, err := processor.Validate(tokenString)
if err != nil {
    // Handle error
    return
}
if valid {
    fmt.Println(claims.UserID)
}

CreateRefresh ​

go
func (p *Processor) CreateRefresh(claims CustomClaims) (string, error)

Creates a refresh token using RefreshTokenTTL instead of AccessTokenTTL.

Parameters ​

ParameterTypeDescription
claimsCustomClaimsToken claims

Returns ​

ReturnTypeDescription
tokenstringSigned refresh token
errerrorError on validation or signing failure

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrInvalidClaimsClaims validation failed
ErrRateLimitExceededRate limit threshold exceeded

Refresh ​

go
func (p *Processor) Refresh(refreshTokenString string) (string, error)

Refreshes an existing refresh token and returns a new access token. The refresh token is fully validated (signature, expiration, blacklist) before a new access token is issued; the original token's IssuedAt, ExpiresAt, and ID are reset and regenerated.

Token Type & Rotation

  • Token type check: Tokens with token_type=access are rejected (returns ErrTokenTypeMismatch) to prevent access tokens from being used to obtain new tokens; older tokens without a token_type are still accepted for backward compatibility.
  • Does not auto-revoke the original token by default: Refresh does not revoke the supplied refresh token by default. The original token remains valid until it expires or is explicitly Revoke()d. For one-time-use semantics, enable Config.RotateRefreshTokens (since v1.2.3; revocation happens before the new token is minted — see Token Refresh & Rotation); manually calling Revoke(refreshTokenString) after a successful Refresh is an equivalent alternative.

Security Note

Refresh only validates standard JWT fields (exp, nbf, iss, aud, blacklist) and basic structural validity (UserID or Username must exist). Deep field constraints (length limits, injection patterns) are not re-checked since they were validated at creation time.

Parameters ​

ParameterTypeDescription
refreshTokenStringstringRefresh token

Returns ​

ReturnTypeDescription
tokenstringNew access token
errerrorError on validation failure

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrInvalidTokenInvalid signature
ErrAlgorithmMismatchToken algorithm does not match config
ErrExpirationRequiredRequireExpiration is enabled but the token lacks an exp claim
ErrTokenExpiredToken expired
ErrTokenNotValidYetToken not yet valid
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenRevokedToken revoked
ErrInvalidClaimsClaims validation failed
ErrTokenTypeMismatchRefreshing with an access token (token_type=access)
ErrTokenMissingIDRotateRefreshTokens is enabled but the refresh token lacks a jti claim
ErrBlacklistNotConfiguredRotateRefreshTokens is enabled but the blacklist is not configured
ErrRefreshRotationFailedRevoking the old refresh token failed while RotateRefreshTokens is enabled (wraps the underlying store error)
ErrRateLimitExceededRate limit threshold exceeded

ParseInto v1.2.3+ ​

go
func (p *Processor) ParseInto(tokenString string, claims CustomClaims) (CustomClaims, error)

Verifies a token and populates the provided custom Claims struct in place; on success it returns the same pointer that was passed in. A nil err means the token is valid. Unlike Parse, which returns a value copy, this fills the caller-supplied struct directly.

Claims state on error

On validation failure the supplied claims may be partially populated and must not be used.

Parameters ​

ParameterTypeDescription
tokenStringstringJWT string
claimsCustomClaimsTarget Claims pointer

Returns ​

ReturnTypeDescription
claimsCustomClaimsPopulated Claims (same pointer as the input)
errerrorError on validation failure; nil means valid

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrInvalidTokenInvalid signature
ErrAlgorithmMismatchToken algorithm does not match config
ErrExpirationRequiredRequireExpiration is enabled but the token lacks an exp claim
ErrTokenExpiredToken expired
ErrTokenNotValidYetToken not yet valid
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenRevokedToken revoked
ErrInvalidClaimsClaims validation failed
ErrTokenTypeMismatchToken has type refresh and RejectRefreshAsAccess is enabled

Example ​

go
myClaims := &MyClaims{}
result, err := processor.ParseInto(tokenString, myClaims)
if err != nil {
    // Handle error
    return
}
fmt.Println(result.(*MyClaims).UserID)

ValidateInto ​

go
func (p *Processor) ValidateInto(tokenString string, claims CustomClaims) (CustomClaims, bool, error)

Deprecated

ValidateInto is deprecated since v1.2.3: the boolean result valid is always equivalent to err == nil. Use ParseInto, which returns (CustomClaims, error); the migration is identical to Validate → Parse.

Validates a token and populates a custom Claims struct. Returns the same pointer as the input claims.

Parameters ​

ParameterTypeDescription
tokenStringstringJWT string
claimsCustomClaimsTarget Claims pointer

Returns ​

ReturnTypeDescription
claimsCustomClaimsPopulated Claims
validboolWhether valid (always equal to err == nil)
errerrorError on validation failure

Example ​

go
myClaims := &MyClaims{}
result, valid, err := processor.ValidateInto(tokenString, myClaims)
if valid {
    fmt.Println(result.(*MyClaims).UserID)
}

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrInvalidTokenInvalid signature
ErrAlgorithmMismatchToken algorithm does not match config
ErrExpirationRequiredRequireExpiration is enabled but the token lacks an exp claim
ErrTokenExpiredToken expired
ErrTokenNotValidYetToken not yet valid
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenRevokedToken revoked
ErrInvalidClaimsClaims validation failed

RefreshInto ​

go
func (p *Processor) RefreshInto(refreshTokenString string, claims CustomClaims) (string, error)

Refreshes a token using custom Claims. Timing fields (IssuedAt, ExpiresAt, ID) are automatically restored after the operation, even if an error or panic occurs.

Token Type Check

Tokens with token_type=access are rejected (returns ErrTokenTypeMismatch) to prevent access tokens from being used to obtain new tokens; older tokens without a token_type are still accepted for backward compatibility.

Security Note

Refresh only validates standard JWT fields and basic structural validity. Deep field constraints are not re-checked since they were validated at creation time.

Parameters ​

ParameterTypeDescription
refreshTokenStringstringRefresh token
claimsCustomClaimsTarget Claims pointer

Returns ​

ReturnTypeDescription
tokenstringNew access token
errerrorError on validation failure

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrInvalidTokenInvalid signature
ErrAlgorithmMismatchToken algorithm does not match config
ErrExpirationRequiredRequireExpiration is enabled but the token lacks an exp claim
ErrTokenExpiredToken expired
ErrTokenNotValidYetToken not yet valid
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenRevokedToken revoked
ErrInvalidClaimsClaims validation failed
ErrTokenTypeMismatchRefreshing with an access token (token_type=access)
ErrTokenMissingIDRotateRefreshTokens is enabled but the refresh token lacks a jti claim
ErrBlacklistNotConfiguredRotateRefreshTokens is enabled but the blacklist is not configured
ErrRefreshRotationFailedRevoking the old refresh token failed while RotateRefreshTokens is enabled (wraps the underlying store error)
ErrRateLimitExceededRate limit threshold exceeded

Revoke ​

go
func (p *Processor) Revoke(tokenString string) error

Adds the token to the blacklist by verifying the signature and extracting the token ID (jti). Only tokens with a valid signature can be revoked, preventing malicious callers from adding arbitrary token IDs to the blacklist.

TTL Behavior

  • The token's exp determines the TTL of the blacklist entry
  • Tokens without an exp default to a 7-day TTL
  • TTL is capped at 30 days to prevent forged excessive exp values from locking memory (DoS protection)
  • Expired tokens can still be revoked; the entry is automatically cleaned up by the blacklist

Parameters ​

ParameterTypeDescription
tokenStringstringToken to revoke

Returns ​

ReturnTypeDescription
errerrorError on revocation failure

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrBlacklistNotConfiguredBlacklist not configured
ErrInvalidTokenInvalid signature or malformed token
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenMissingIDToken missing jti field

IsRevoked ​

go
func (p *Processor) IsRevoked(tokenString string) (bool, error)

Checks whether a token has been revoked. After verifying the signature, it looks up the token's jti status in the blacklist. When the blacklist is not configured, returns false and a nil error.

Parameters ​

ParameterTypeDescription
tokenStringstringJWT string

Returns ​

ReturnTypeDescription
revokedboolWhether revoked
errerrorError on query failure

Errors ​

ErrorTrigger Condition
ErrProcessorClosedProcessor is closed
ErrEmptyTokenToken is empty
ErrInvalidTokenInvalid signature or malformed token
ErrTokenInvalidIssuerIssuer mismatch
ErrTokenInvalidAudienceAudience mismatch
ErrTokenMissingIDToken missing jti field

ParseUnverified ​

go
func (p *Processor) ParseUnverified(tokenString string, claims any) error

Parses a token without verifying the signature. Useful for extracting Claims information without needing to trust the token.

DANGER

The returned Claims are not verified and must not be trusted. Only use for debugging or logging.

Parameters ​

ParameterTypeDescription
tokenStringstringJWT string
claimsanyTarget Claims pointer

Returns ​

ReturnTypeDescription
errerrorError on parse failure

Errors ​

ErrorCondition
ErrProcessorClosedProcessor has been closed
ErrEmptyTokenToken is empty
Wrapped errorReturns a wrapped parse error on malformed tokens (not a sentinel error; cannot be matched with errors.Is)

Close ​

go
func (p *Processor) Close() error

Releases resources and securely clears keys. Can be called multiple times; subsequent calls return ErrProcessorClosed.

Returns ​

ReturnTypeDescription
errerrorError on close failure

IsClosed ​

go
func (p *Processor) IsClosed() bool

Checks if the Processor is closed.

Returns ​

ReturnTypeDescription
closedboolWhether closed