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") // ErrMissingToken is returned when no JWT token is found in the request. ErrMissingToken = errors.New("missing or malformed JWT") )
Functions ¶
func FromContext ¶
FromContext returns the token from the context. It accepts fiber.CustomCtx, fiber.Ctx, *fasthttp.RequestCtx, and context.Context. If there is no token, nil is returned.
Types ¶
type Config ¶
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next 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
// Claims are extendable claims data defining token content.
// Optional. Default value jwt.MapClaims
Claims jwt.Claims
// Extractor defines a function to extract the token from the request.
// Optional. Default: FromAuthHeader("Bearer").
Extractor extractors.Extractor
// TokenProcessorFunc processes the token extracted using the Extractor.
// Optional. Default: nil
TokenProcessorFunc func(token string) (string, error)
// 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.