middleware/keyauth
import "github.com/gofiber/fiber/v3/middleware/keyauth"
Constants
Source: middleware/keyauth/config.go:13
const (
ErrorInvalidRequest = "invalid_request"
ErrorInvalidToken = "invalid_token"
ErrorInsufficientScope = "insufficient_scope"
)Variables
ConfigDefault is the default config
Source: middleware/keyauth/config.go:93
var ConfigDefault = Config{
SuccessHandler: func(c fiber.Ctx) error {
return c.Next()
},
ErrorHandler: func(c fiber.Ctx, _ error) error {
return c.Status(fiber.StatusUnauthorized).SendString(ErrMissingOrMalformedAPIKey.Error())
},
Realm: "Restricted",
Extractor: extractors.FromAuthHeader("Bearer"),
}ErrMissingOrMalformedAPIKey is returned when the API key is missing or invalid.
Source: middleware/keyauth/keyauth.go:26
var ErrMissingOrMalformedAPIKey = errors.New("missing or invalid API Key")Functions
func New(config ...Config) fiber.Handler
Source: middleware/keyauth/keyauth.go:31
New creates a new middleware handler
func TokenFromContext(ctx any) string
Source: middleware/keyauth/keyauth.go:170
TokenFromContext returns the bearer token from the request context. It accepts fiber.CustomCtx, fiber.Ctx, *fasthttp.RequestCtx, and context.Context. It returns an empty string if the token does not exist.
Types
type Config
Source: middleware/keyauth/config.go:19
Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c fiber.Ctx) bool
// SuccessHandler defines a function which is executed for a valid key.
//
// Optional. Default: c.Next()
SuccessHandler fiber.Handler
// ErrorHandler defines a function which is executed for an invalid key.
// It may be used to define a custom error.
//
// Optional. Default: 401 with the body "missing or invalid API Key" (ErrMissingOrMalformedAPIKey)
ErrorHandler fiber.ErrorHandler
// Validator is a function to validate the key.
//
// Required.
Validator func(c fiber.Ctx, key string) (bool, error)
// Realm defines the protected area for WWW-Authenticate responses.
// This is used to set the `WWW-Authenticate` header when authentication fails.
//
// Optional. Default value "Restricted".
Realm string
// Challenge defines the full `WWW-Authenticate` header value used when
// the middleware responds with 401 and no Authorization scheme is
// present.
//
// Optional. Default: `ApiKey realm="<Realm>"` when no Authorization scheme
// is configured.
Challenge string
// Error is the RFC 6750 `error` parameter appended to Bearer
// `WWW-Authenticate` challenges when validation fails. Allowed values
// are `invalid_request`, `invalid_token`, or `insufficient_scope`.
// It is omitted, together with ErrorDescription, ErrorURI and Scope,
// when the request carried no credentials or used an Authorization
// scheme other than Bearer, as RFC 6750 section 3 asks.
//
// Optional. Default: "".
Error string
// ErrorDescription is the RFC 6750 `error_description` parameter
// appended to Bearer `WWW-Authenticate` challenges when validation
// fails. This field requires that `Error` is also set.
//
// Optional. Default: "".
ErrorDescription string
// ErrorURI is the RFC 6750 `error_uri` parameter appended to Bearer
// `WWW-Authenticate` challenges when validation fails. This field
// requires that `Error` is also set.
//
// Optional. Default: "".
ErrorURI string
// Scope is the RFC 6750 `scope` parameter appended to Bearer
// challenges when the `error` is `insufficient_scope`. This field
// requires that `Error` is set to `insufficient_scope`.
//
// Optional. Default: "".
Scope string
// Extractor is a function to extract the key from the request.
//
// Optional. Default: extractors.FromAuthHeader("Bearer")
Extractor extractors.Extractor
}Fields
Next func(c fiber.Ctx) boolNext defines a function to skip this middleware when returned true.
Optional. Default: nil
SuccessHandler fiber.HandlerSuccessHandler defines a function which is executed for a valid key.
Optional. Default: c.Next()
ErrorHandler fiber.ErrorHandlerErrorHandler defines a function which is executed for an invalid key. It may be used to define a custom error.
Optional. Default: 401 with the body "missing or invalid API Key" (ErrMissingOrMalformedAPIKey)
Validator func(c fiber.Ctx, key string) (bool, error)Validator is a function to validate the key.
Required.
Realm stringRealm defines the protected area for WWW-Authenticate responses. This is used to set the
WWW-Authenticateheader when authentication fails.Optional. Default value "Restricted".
Challenge stringChallenge defines the full
WWW-Authenticateheader value used when the middleware responds with 401 and no Authorization scheme is present.Optional. Default:
ApiKey realm="<Realm>"when no Authorization scheme is configured.Error stringError is the RFC 6750
errorparameter appended to BearerWWW-Authenticatechallenges when validation fails. Allowed values areinvalid_request,invalid_token, orinsufficient_scope. It is omitted, together with ErrorDescription, ErrorURI and Scope, when the request carried no credentials or used an Authorization scheme other than Bearer, as RFC 6750 section 3 asks.Optional. Default: "".
ErrorDescription stringErrorDescription is the RFC 6750
error_descriptionparameter appended to BearerWWW-Authenticatechallenges when validation fails. This field requires thatErroris also set.Optional. Default: "".
ErrorURI stringErrorURI is the RFC 6750
error_uriparameter appended to BearerWWW-Authenticatechallenges when validation fails. This field requires thatErroris also set.Optional. Default: "".
Scope stringScope is the RFC 6750
scopeparameter appended to Bearer challenges when theerrorisinsufficient_scope. This field requires thatErroris set toinsufficient_scope.Optional. Default: "".
Extractor extractors.ExtractorExtractor is a function to extract the key from the request.
Optional. Default: extractors.FromAuthHeader("Bearer")