Key Auth Middleware

Key auth middleware provides a key based authentication.

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

Usage

  1. e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
  2. return key == "valid-key", nil
  3. }))

Custom Configuration

Usage

  1. e := echo.New()
  2. e.Use(middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{
  3. KeyLookup: "query:api-key",
  4. }))

Configuration

  1. KeyAuthConfig struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper Skipper
  4. // KeyLookup is a string in the form of "<source>:<name>" that is used
  5. // to extract key from the request.
  6. // Optional. Default value "header:Authorization".
  7. // Possible values:
  8. // - "header:<name>"
  9. // - "query:<name>"
  10. KeyLookup string `json:"key_lookup"`
  11. // AuthScheme to be used in the Authorization header.
  12. // Optional. Default value "Bearer".
  13. AuthScheme string
  14. // Validator is a function to validate key.
  15. // Required.
  16. Validator KeyAuthValidator
  17. }

Default Configuration

  1. DefaultKeyAuthConfig = KeyAuthConfig{
  2. Skipper: DefaultSkipper,
  3. KeyLookup: "header:" + echo.HeaderAuthorization,
  4. AuthScheme: "Bearer",
  5. }