Documentation
¶
Index ¶
Constants ¶
View Source
const ( // HS256 represents a public cryptography key generated by a 256 bit HMAC algorithm. HS256 = "HS256" // HS384 represents a public cryptography key generated by a 384 bit HMAC algorithm. HS384 = "HS384" // HS512 represents a public cryptography key generated by a 512 bit HMAC algorithm. HS512 = "HS512" // ES256 represents a public cryptography key generated by a 256 bit ECDSA algorithm. ES256 = "ES256" // ES384 represents a public cryptography key generated by a 384 bit ECDSA algorithm. ES384 = "ES384" // ES512 represents a public cryptography key generated by a 512 bit ECDSA algorithm. ES512 = "ES512" // P256 represents a cryptographic elliptical curve type. P256 = "P-256" // P384 represents a cryptographic elliptical curve type. P384 = "P-384" // P521 represents a cryptographic elliptical curve type. P521 = "P-521" // RS256 represents a public cryptography key generated by a 256 bit RSA algorithm. RS256 = "RS256" // RS384 represents a public cryptography key generated by a 384 bit RSA algorithm. RS384 = "RS384" // RS512 represents a public cryptography key generated by a 512 bit RSA algorithm. RS512 = "RS512" // PS256 represents a public cryptography key generated by a 256 bit RSA algorithm. PS256 = "PS256" // PS384 represents a public cryptography key generated by a 384 bit RSA algorithm. PS384 = "PS384" // PS512 represents a public cryptography key generated by a 512 bit RSA algorithm. PS512 = "PS512" )
Variables ¶
View Source
var ( // ErrJWTAlg is returned when the JWT header did not contain the expected algorithm. ErrJWTAlg = errors.New("the JWT header did not contain the expected algorithm") )
View Source
var ( // ErrJWTMissingOrMalformed is returned when the JWT is missing or malformed. ErrJWTMissingOrMalformed = errors.New("missing or malformed JWT") )
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Filter is a function to skip middleware execution for specific requests.
// Optional. Default: nil
Filter func(*fiber.Ctx) bool
// SuccessHandler is executed when a token is successfully validated.
// Optional. Default: nil
SuccessHandler fiber.Handler
// ErrorHandler is executed when token validation fails.
// It allows customization of JWT error responses.
// Optional. Default: 401 Invalid or expired JWT
ErrorHandler fiber.ErrorHandler
// SigningKey is the primary key used to validate tokens.
// Used as a fallback if SigningKeys is empty.
// At least one of the following is required: KeyFunc, JWKSetURLs, SigningKeys, or SigningKey.
SigningKey SigningKey
// SigningKeys is a map of keys used to validate tokens with the "kid" field.
// At least one of the following is required: KeyFunc, JWKSetURLs, SigningKeys, or SigningKey.
SigningKeys map[string]SigningKey
// ContextKey specifies the key used to store user information in the context.
// Optional. Default: "user".
ContextKey string
// Claims defines the structure of token claims.
// Optional. Default: jwt.MapClaims
Claims jwt.Claims
// TokenLookup specifies how to extract the token from the request.
// Format: "<source>:<name>"
// Optional. Default: "header:Authorization".
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "param:<name>"
// - "cookie:<name>"
TokenLookup string
// TokenProcessorFunc processes the token extracted using TokenLookup.
// Optional. Default: nil
TokenProcessorFunc func(token string) (string, error)
// AuthScheme specifies the scheme used in the Authorization header.
// Optional. Default: "Bearer".
AuthScheme string
// KeyFunc provides the public key for JWT verification.
// It handles algorithm verification and key selection.
// By default, the github.com/MicahParks/keyfunc/v2 package is used.
// At least one of the following is required: KeyFunc, JWKSetURLs, SigningKeys, or SigningKey.
KeyFunc jwt.Keyfunc
// JWKSetURLs is a list of URLs containing JSON Web Key Sets (JWKS) for signature verification.
// HTTPS is recommended. The "kid" field in the JWT header and JWKs is mandatory.
// Default behavior:
// - Refresh every hour.
// - Auto-refresh on new "kid" in JWT.
// - Rate limit refreshes to once every 5 minutes.
// - Timeout refreshes after 10 seconds.
// At least one of the following is required: KeyFunc, JWKSetURLs, SigningKeys, or SigningKey.
JWKSetURLs []string
}
Config defines the config for JWT middleware
type SigningKey ¶
type SigningKey struct {
// JWTAlg is the algorithm used to sign JWTs. If this value is a non-empty string, this will be checked against the
// "alg" value in the JWT header.
//
// https://www.rfc-editor.org/rfc/rfc7518#section-3.1
JWTAlg string
// Key is the cryptographic key used to sign JWTs. For supported types, please see
// https://github.com/golang-jwt/jwt.
Key interface{}
}
SigningKey holds information about the recognized cryptographic keys used to sign JWTs by this program.
Click to show internal directories.
Click to hide internal directories.