keyauth

package
v0.0.0-...-a9bbbbd Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHeaderExtractorValueMissing = &ValueExtractorError{message: "missing value in request header"}
	ErrHeaderExtractorValueInvalid = &ValueExtractorError{message: "invalid value in request header"}
	ErrQueryExtractorValueMissing  = &ValueExtractorError{message: "missing value in query string"}
	ErrPathExtractorValueMissing   = &ValueExtractorError{message: "missing value in path params"}
	ErrFormExtractorValueMissing   = &ValueExtractorError{message: "missing value in form"}
	ErrCtxExtractorValueMissing    = &ValueExtractorError{message: "missing value in ctx"}
)
View Source
var (
	ErrInvalidKey = errors.New("invalid key")
	ErrMissingKey = errors.New("missing key")
)

Functions

func Middleware

func Middleware(cfg *Config) gin.HandlerFunc

Types

type Config

type Config struct {
	// KeyLookup is a string in the form of "<source>:<name>" or "<source>:<name>,<source>:<name>" that is used
	// to extract key from the request.
	// Optional. Default value "header:Authorization".
	// Possible values:
	// - "header:<name>" or "header:<name>:<cut-prefix>"
	// 			`<cut-prefix>` is argument value to cut/trim prefix of the extracted value. This is useful if header
	//			value has static prefix like `Authorization: <auth-scheme> <authorisation-parameters>` where part that we
	//			want to cut is `<auth-scheme> ` note the space at the end.
	//			In case of basic authentication `Authorization: Basic <credentials>` prefix we want to remove is `Basic `.
	// - "query:<name>"
	// - "form:<name>"
	// - "cookie:<name>"
	// Multiple sources example:
	// - "header:Authorization,header:X-Api-Key"
	KeyLookup string `mapstructure:"key_lookup"`

	// ContinueOnIgnoredError allows the next middleware/handler to be called when ErrorHandler decides to
	// ignore the error (by returning `nil`).
	// This is useful when parts of your site/api allow public access and some authorized routes provide extra functionality.
	// In that case you can use ErrorHandler to set a default public key auth value in the request context
	// and continue. Some logic down the remaining execution chain needs to check that (public) key auth value then.
	ContinueOnIgnoredError bool `mapstructure:"continue_on_ignored_error"`

	ExcludeRoutes []string `mapstructure:"exclude_routes"`

	// Validator is a function to validate key.
	// Required.
	Validator Validator

	// ErrorHandler defines a function which is executed for an invalid key.
	// It may be used to define a custom error.
	ErrorHandler ErrorHandler
}

func (*Config) InitDefaults

func (cfg *Config) InitDefaults()

type ErrorHandler

type ErrorHandler func(c *gin.Context, err error) error

ErrorHandler defines a function which is executed for an invalid key.

type ExtractorSource

type ExtractorSource string
const (
	ExtractorSourceHeader ExtractorSource = "header"
	ExtractorSourceQuery  ExtractorSource = "query"
	ExtractorSourcePath   ExtractorSource = "path"
	ExtractorSourceForm   ExtractorSource = "form"
	ExtractorSourceCtx    ExtractorSource = "ctx"
)

type Validator

type Validator func(c *gin.Context, key string, source ExtractorSource) (bool, error)

Validator defines a function to validate KeyAuth credentials.

type ValueExtractorError

type ValueExtractorError struct {
	// contains filtered or unexported fields
}

func (*ValueExtractorError) Error

func (e *ValueExtractorError) Error() string

type ValuesExtractor

type ValuesExtractor func(c *gin.Context) ([]string, ExtractorSource, error)

func CreateExtractors

func CreateExtractors(lookups string) ([]ValuesExtractor, error)

func ValuesFromCtx

func ValuesFromCtx(name string) ValuesExtractor

func ValuesFromForm

func ValuesFromForm(name string) ValuesExtractor

func ValuesFromHeader

func ValuesFromHeader(header string, valuePrefix string) ValuesExtractor

func ValuesFromPath

func ValuesFromPath(param string) ValuesExtractor

func ValuesFromQuery

func ValuesFromQuery(param string) ValuesExtractor

Jump to

Keyboard shortcuts

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