middleware/basicauth
import "github.com/gofiber/fiber/v3/middleware/basicauth"
Variables
Source: middleware/basicauth/config.go:21
var (
ErrInvalidSHA256PasswordLength = errors.New("decode SHA256 password: invalid length")
ErrInvalidSHA512PasswordLength = errors.New("decode SHA512 password: invalid length")
)ConfigDefault is the default config
Source: middleware/basicauth/config.go:106
var ConfigDefault = Config{
Next: nil,
Users: map[string]string{},
Realm: "Restricted",
Charset: "UTF-8",
HeaderLimit: 8192,
Authorizer: nil,
Unauthorized: nil,
BadRequest: nil,
}Functions
func New(config ...Config) fiber.Handler
Source: middleware/basicauth/basicauth.go:34
New creates a new middleware handler
func UsernameFromContext(ctx any) string
Source: middleware/basicauth/basicauth.go:228
UsernameFromContext returns the username found in the context. It accepts fiber.CustomCtx, fiber.Ctx, *fasthttp.RequestCtx, and context.Context. It returns an empty string if the username does not exist.
Types
type Config
Source: middleware/basicauth/config.go:48
Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c fiber.Ctx) bool
// Users defines the allowed credentials
//
// Required. Default: map[string]string{}
Users map[string]string
// Authorizer defines a function you can pass
// to check the credentials however you want.
// It will be called with a username, password and
// the current fiber context and is expected to return
// true or false to indicate that the credentials were
// approved or not.
//
// Optional. Default: nil.
Authorizer func(string, string, fiber.Ctx) bool
// Unauthorized defines the response body for unauthorized responses.
// It runs with the 401 status, WWW-Authenticate, Cache-Control: no-store and Vary already set.
//
// Optional. Default: nil
Unauthorized fiber.Handler
// BadRequest defines the response body for malformed Authorization headers,
// a request carrying the field on more than one line included.
// It runs with the 400 status already set, and no WWW-Authenticate header.
//
// Optional. Default: nil
BadRequest fiber.Handler
// Realm is a string to define realm attribute of BasicAuth.
// the realm identifies the system to authenticate against
// and can be used by clients to save credentials
//
// Optional. Default: "Restricted".
Realm string
// Charset defines the value for the charset parameter in the
// WWW-Authenticate header. According to RFC 7617 clients can use
// this value to interpret credentials correctly. Only the value
// "UTF-8" is allowed; any other value will panic.
//
// Optional. Default: "UTF-8".
Charset string
// HeaderLimit specifies the maximum allowed length of the
// Authorization header. Requests exceeding this limit will
// be rejected.
//
// Optional. Default: 8192.
HeaderLimit int
}Fields
Next func(c fiber.Ctx) boolNext defines a function to skip this middleware when returned true.
Optional. Default: nil
Users map[string]stringUsers defines the allowed credentials
Required. Default: map[string]string{}
Authorizer func(string, string, fiber.Ctx) boolAuthorizer defines a function you can pass to check the credentials however you want. It will be called with a username, password and the current fiber context and is expected to return true or false to indicate that the credentials were approved or not.
Optional. Default: nil.
Unauthorized fiber.HandlerUnauthorized defines the response body for unauthorized responses. It runs with the 401 status, WWW-Authenticate, Cache-Control: no-store and Vary already set.
Optional. Default: nil
BadRequest fiber.HandlerBadRequest defines the response body for malformed Authorization headers, a request carrying the field on more than one line included. It runs with the 400 status already set, and no WWW-Authenticate header.
Optional. Default: nil
Realm stringRealm is a string to define realm attribute of BasicAuth. the realm identifies the system to authenticate against and can be used by clients to save credentials
Optional. Default: "Restricted".
Charset stringCharset defines the value for the charset parameter in the WWW-Authenticate header. According to RFC 7617 clients can use this value to interpret credentials correctly. Only the value "UTF-8" is allowed; any other value will panic.
Optional. Default: "UTF-8".
HeaderLimit intHeaderLimit specifies the maximum allowed length of the Authorization header. Requests exceeding this limit will be rejected.
Optional. Default: 8192.