Middleware

middleware/keyauth

github.com/gofiber/fiber/v3/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) bool

    Next defines a function to skip this middleware when returned true.

    Optional. Default: nil

  • SuccessHandler fiber.Handler

    SuccessHandler defines a function which is executed for a valid key.

    Optional. Default: c.Next()

  • ErrorHandler fiber.ErrorHandler

    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)

  • Validator func(c fiber.Ctx, key string) (bool, error)

    Validator is a function to validate the key.

    Required.

  • Realm string

    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".

  • Challenge 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.

  • Error 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: "".

  • ErrorDescription 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: "".

  • ErrorURI 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: "".

  • Scope 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: "".

  • Extractor extractors.Extractor

    Extractor is a function to extract the key from the request.

    Optional. Default: extractors.FromAuthHeader("Bearer")