api_key_middleware

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingOrMalformedAPIKey = errors.New("missing or malformed API Key")
	ErrWrongAPIKey              = errors.New("wrong api key")
)
View Source
var AuthMiddlewareConfigDefault = ApiKeyMiddlewareConfig{
	SuccessHandler: func(c fiber.Ctx) error {
		return c.Next()
	},

	ErrorHandler: func(c fiber.Ctx, err error) error {
		log.Errorf("[ApiKeyMiddleware] error: %s", err)

		resp := core_dtos.NewResponse(c)

		if errors.Is(err, ErrMissingOrMalformedAPIKey) {
			resp.SetStatus(fiber.StatusUnauthorized)
			resp.SetError(101, errCodesMessages[101])
		} else if errors.Is(err, ErrWrongAPIKey) {
			resp.SetStatus(fiber.StatusUnauthorized)
			resp.SetError(100, errCodesMessages[100])
		} else {
			resp.SetStatus(fiber.StatusInternalServerError)
		}

		return resp.JSON()
	},
}

Functions

func NewApiKeyMiddleware

func NewApiKeyMiddleware(config ApiKeyMiddlewareConfig) fiber.Handler

func TokenFromContext

func TokenFromContext(c fiber.Ctx) string

TokenFromContext returns the bearer token from the request context. returns an empty string if the token does not exist

Types

type ApiKeyMiddlewareConfig

type ApiKeyMiddlewareConfig struct {
	Pass           bool
	Next           func(fiber.Ctx) bool
	SuccessHandler fiber.Handler
	ErrorHandler   fiber.ErrorHandler

	// KeyLookup is a string in the form of "<source>:<name>" that is used to extract key from the request.
	// Optional. Default value "header:X-API-KEY".
	// Possible values:
	// - "header:<name>"
	// - "query:<name>"
	// - "form:<name>"
	// - "param:<name>"
	// - "cookie:<name>"
	KeyLookup string

	// AuthScheme to be used in the Authorization header.
	// Optional. Default value "Bearer".
	AuthScheme string

	// Validator is a function to validate key.
	Validator func(fiber.Ctx, string) (bool, error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL