Basic Auth

Basic auth middleware provides an HTTP basic authentication.

  • For valid credentials it calls the next handler.
  • For missing or invalid credentials, it sends “401 - Unauthorized” response.

Usage

  1. e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
  2. if username == "joe" && password == "secret" {
  3. return true, nil
  4. }
  5. return false, nil
  6. }))

Custom Configuration

Usage

  1. e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{}))

Configuration

  1. BasicAuthConfig struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper Skipper
  4. // Validator is a function to validate BasicAuth credentials.
  5. // Required.
  6. Validator BasicAuthValidator
  7. // Realm is a string to define realm attribute of BasicAuth.
  8. // Default value "Restricted".
  9. Realm string
  10. }

Default Configuration

  1. DefaultBasicAuthConfig = BasicAuthConfig{
  2. Skipper: DefaultSkipper,
  3. }