BasicAuth

Basic Authentication middleware for Fiber that provides an HTTP basic authentication. It calls the next handler for valid credentials and 401 Unauthorized or a custom response for missing or invalid credentials.

Signatures

  1. func New(config Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/basicauth"
  4. )

After you initiate your Fiber app, you can use the following possibilities:

  1. // Provide a minimal config
  2. app.Use(basicauth.New(basicauth.Config{
  3. Users: map[string]string{
  4. "john": "doe",
  5. "admin": "123456",
  6. },
  7. }))
  8. // Or extend your config for customization
  9. app.Use(basicauth.New(basicauth.Config{
  10. Users: map[string]string{
  11. "john": "doe",
  12. "admin": "123456",
  13. },
  14. Realm: "Forbidden",
  15. Authorizer: func(user, pass string) bool {
  16. if user == "john" && pass == "doe" {
  17. return true
  18. }
  19. if user == "admin" && pass == "123456" {
  20. return true
  21. }
  22. return false
  23. },
  24. Unauthorized: func(c *fiber.Ctx) error {
  25. return c.SendFile("./unauthorized.html")
  26. },
  27. ContextUsername: "_user",
  28. ContextPassword: "_pass",
  29. }))

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Next defines a function to skip this middleware when returned true.
  4. //
  5. // Optional. Default: nil
  6. Next func(c *fiber.Ctx) bool
  7. // Users defines the allowed credentials
  8. //
  9. // Required. Default: map[string]string{}
  10. Users map[string]string
  11. // Realm is a string to define realm attribute of BasicAuth.
  12. // the realm identifies the system to authenticate against
  13. // and can be used by clients to save credentials
  14. //
  15. // Optional. Default: "Restricted".
  16. Realm string
  17. // Authorizer defines a function you can pass
  18. // to check the credentials however you want.
  19. // It will be called with a username and password
  20. // and is expected to return true or false to indicate
  21. // that the credentials were approved or not.
  22. //
  23. // Optional. Default: nil.
  24. Authorizer func(string, string) bool
  25. // Unauthorized defines the response body for unauthorized responses.
  26. // By default it will return with a 401 Unauthorized and the correct
  27. // WWW-Auth header
  28. //
  29. // Optional. Default: nil
  30. Unauthorized fiber.Handler
  31. // ContextUser is the key to store the username in Locals
  32. //
  33. // Optional. Default: "username"
  34. ContextUsername string
  35. // ContextPass is the key to store the password in Locals
  36. //
  37. // Optional. Default: "password"
  38. ContextPassword string
  39. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. Users: map[string]string{},
  4. Realm: "Restricted",
  5. Authorizer: nil,
  6. Unauthorized: nil,
  7. ContextUsername: "username",
  8. ContextPassword: "password",
  9. }