v2keyauth

package module
v0.0.0-...-c45d584 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 5 Imported by: 0

README

fiber keyauth v3 -> v2 backport

I needed to extend the fiber keyauth middleware to support multiple key sources. These changes were contributed to fiber upstream for the next v3 version in this pr. As my project still depends on v2 for the time being, this repo hosts a minimally-backported version for all to use

Documentation

Overview

Special thanks to Echo: https://github.com/labstack/echo/blob/master/middleware/key_auth.go

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	SuccessHandler: func(c *fiber.Ctx) error {
		return c.Next()
	},
	ErrorHandler: func(c *fiber.Ctx, err error) error {
		if errors.Is(err, ErrMissingOrMalformedAPIKey) {
			return c.Status(fiber.StatusUnauthorized).SendString(err.Error())
		}
		return c.Status(fiber.StatusUnauthorized).SendString("Invalid or expired API Key")
	},
	KeyLookup:       "header:" + fiber.HeaderAuthorization,
	CustomKeyLookup: nil,
	AuthScheme:      "Bearer",
}

ConfigDefault is the default config

View Source
var ErrMissingOrMalformedAPIKey = errors.New("missing or malformed API Key")

When there is no request of the key thrown ErrMissingOrMalformedAPIKey

Functions

func New

func New(config ...Config) fiber.Handler

New creates a new middleware 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 Config

type Config struct {
	// Next defines a function to skip middleware.
	// Optional. Default: nil
	Next func(*fiber.Ctx) bool

	// SuccessHandler defines a function which is executed for a valid key.
	// Optional. Default: nil
	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 Invalid or expired key
	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:Authorization".
	// Possible values:
	// - "header:<name>"
	// - "query:<name>"
	// - "form:<name>"
	// - "param:<name>"
	// - "cookie:<name>"
	KeyLookup string

	CustomKeyLookup KeyLookupFunc

	// 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)
}

Config defines the config for middleware.

type KeyLookupFunc

type KeyLookupFunc func(c *fiber.Ctx) (string, error)

func DefaultKeyLookup

func DefaultKeyLookup(keyLookup, authScheme string) (KeyLookupFunc, error)

func KeyFromCookie

func KeyFromCookie(name string) KeyLookupFunc

keyFromCookie returns a function that extracts api key from the named cookie.

func KeyFromForm

func KeyFromForm(param string) KeyLookupFunc

keyFromForm returns a function that extracts api key from the form.

func KeyFromHeader

func KeyFromHeader(header, authScheme string) KeyLookupFunc

keyFromHeader returns a function that extracts api key from the request header.

func KeyFromParam

func KeyFromParam(param string) KeyLookupFunc

keyFromParam returns a function that extracts api key from the url param string.

func KeyFromQuery

func KeyFromQuery(param string) KeyLookupFunc

keyFromQuery returns a function that extracts api key from the query string.

func MultipleKeySourceLookup

func MultipleKeySourceLookup(keyLookups []string, authScheme string) (KeyLookupFunc, error)

MultipleKeySourceLookup creates a CustomKeyLookup function that checks multiple sources until one is found Each element should be specified according to the format used in KeyLookup

Jump to

Keyboard shortcuts

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