matterbridge/vendor/github.com/labstack/echo/v4/middleware/key_auth.go

181 lines
5.7 KiB
Go
Raw Normal View History

2017-02-18 14:00:46 -08:00
package middleware
import (
"errors"
"github.com/labstack/echo/v4"
2022-03-12 10:41:07 -08:00
"net/http"
2017-02-18 14:00:46 -08:00
)
type (
// KeyAuthConfig defines the config for KeyAuth middleware.
KeyAuthConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
2022-03-12 10:41:07 -08:00
// KeyLookup is a string in the form of "<source>:<name>" or "<source>:<name>,<source>:<name>" that is used
2017-02-18 14:00:46 -08:00
// to extract key from the request.
// Optional. Default value "header:Authorization".
// Possible values:
2022-03-12 10:41:07 -08:00
// - "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 `.
2017-02-18 14:00:46 -08:00
// - "query:<name>"
2018-02-20 15:48:10 -08:00
// - "form:<name>"
// - "cookie:<name>"
2022-03-12 10:41:07 -08:00
// Multiple sources example:
// - "header:Authorization,header:X-Api-Key"
KeyLookup string
2017-02-18 14:00:46 -08:00
// AuthScheme to be used in the Authorization header.
// Optional. Default value "Bearer".
AuthScheme string
// Validator is a function to validate key.
// Required.
Validator KeyAuthValidator
2021-05-29 15:25:30 -07:00
// ErrorHandler defines a function which is executed for an invalid key.
// It may be used to define a custom error.
ErrorHandler KeyAuthErrorHandler
2022-03-12 10:41:07 -08:00
// 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
2017-02-18 14:00:46 -08:00
}
// KeyAuthValidator defines a function to validate KeyAuth credentials.
2022-03-12 10:41:07 -08:00
KeyAuthValidator func(auth string, c echo.Context) (bool, error)
2021-05-29 15:25:30 -07:00
// KeyAuthErrorHandler defines a function which is executed for an invalid key.
2022-03-12 10:41:07 -08:00
KeyAuthErrorHandler func(err error, c echo.Context) error
2017-02-18 14:00:46 -08:00
)
var (
// DefaultKeyAuthConfig is the default KeyAuth middleware config.
DefaultKeyAuthConfig = KeyAuthConfig{
Skipper: DefaultSkipper,
KeyLookup: "header:" + echo.HeaderAuthorization,
AuthScheme: "Bearer",
}
)
2022-03-12 10:41:07 -08:00
// ErrKeyAuthMissing is error type when KeyAuth middleware is unable to extract value from lookups
type ErrKeyAuthMissing struct {
Err error
}
// Error returns errors text
func (e *ErrKeyAuthMissing) Error() string {
return e.Err.Error()
}
// Unwrap unwraps error
func (e *ErrKeyAuthMissing) Unwrap() error {
return e.Err
}
2017-02-18 14:00:46 -08:00
// KeyAuth returns an KeyAuth middleware.
//
// For valid key it calls the next handler.
// For invalid key, it sends "401 - Unauthorized" response.
// For missing key, it sends "400 - Bad Request" response.
func KeyAuth(fn KeyAuthValidator) echo.MiddlewareFunc {
c := DefaultKeyAuthConfig
c.Validator = fn
return KeyAuthWithConfig(c)
}
// KeyAuthWithConfig returns an KeyAuth middleware with config.
// See `KeyAuth()`.
func KeyAuthWithConfig(config KeyAuthConfig) echo.MiddlewareFunc {
// Defaults
if config.Skipper == nil {
config.Skipper = DefaultKeyAuthConfig.Skipper
}
// Defaults
if config.AuthScheme == "" {
config.AuthScheme = DefaultKeyAuthConfig.AuthScheme
}
if config.KeyLookup == "" {
config.KeyLookup = DefaultKeyAuthConfig.KeyLookup
}
if config.Validator == nil {
2017-12-07 14:00:56 -08:00
panic("echo: key-auth middleware requires a validator function")
2017-02-18 14:00:46 -08:00
}
extractors, cErr := createExtractors(config.KeyLookup, config.AuthScheme)
if cErr != nil {
panic(cErr)
2017-02-18 14:00:46 -08:00
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
2022-03-12 10:41:07 -08:00
var lastExtractorErr error
var lastValidatorErr error
for _, extractor := range extractors {
keys, err := extractor(c)
if err != nil {
lastExtractorErr = err
continue
}
for _, key := range keys {
valid, err := config.Validator(key, c)
if err != nil {
lastValidatorErr = err
continue
}
if valid {
return next(c)
}
lastValidatorErr = errors.New("invalid key")
2021-05-29 15:25:30 -07:00
}
2017-02-18 14:00:46 -08:00
}
2022-03-12 10:41:07 -08:00
// we are here only when we did not successfully extract and validate any of keys
err := lastValidatorErr
if err == nil { // prioritize validator errors over extracting errors
// ugly part to preserve backwards compatible errors. someone could rely on them
if lastExtractorErr == errQueryExtractorValueMissing {
err = errors.New("missing key in the query string")
} else if lastExtractorErr == errCookieExtractorValueMissing {
err = errors.New("missing key in cookies")
} else if lastExtractorErr == errFormExtractorValueMissing {
err = errors.New("missing key in the form")
} else if lastExtractorErr == errHeaderExtractorValueMissing {
err = errors.New("missing key in request header")
} else if lastExtractorErr == errHeaderExtractorValueInvalid {
err = errors.New("invalid key in the request header")
} else {
err = lastExtractorErr
2021-05-29 15:25:30 -07:00
}
2022-03-12 10:41:07 -08:00
err = &ErrKeyAuthMissing{Err: err}
}
if config.ErrorHandler != nil {
tmpErr := config.ErrorHandler(err, c)
if config.ContinueOnIgnoredError && tmpErr == nil {
return next(c)
}
return tmpErr
}
if lastValidatorErr != nil { // prioritize validator errors over extracting errors
2019-06-16 14:33:25 -07:00
return &echo.HTTPError{
Code: http.StatusUnauthorized,
2022-03-12 10:41:07 -08:00
Message: "Unauthorized",
Internal: lastValidatorErr,
2019-06-16 14:33:25 -07:00
}
2017-02-18 14:00:46 -08:00
}
2022-03-12 10:41:07 -08:00
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
}
}