New() — fiber Function Reference
Architecture documentation for the New() function in basicauth.go from the fiber codebase.
Entity Profile
Dependency Diagram
graph TD 91f86705_80f9_8221_0e56_b7e4986da753["New()"] 0169cc3d_e89d_3f0a_2e41_1b964fca25f9["basicauth.go"] 91f86705_80f9_8221_0e56_b7e4986da753 -->|defined in| 0169cc3d_e89d_3f0a_2e41_1b964fca25f9 04229ef2_36f4_5c8c_0c3d_f445fa3ac309["containsInvalidHeaderChars()"] 91f86705_80f9_8221_0e56_b7e4986da753 -->|calls| 04229ef2_36f4_5c8c_0c3d_f445fa3ac309 a53e4565_404a_a05c_787e_e4395b4f4936["containsCTL()"] 91f86705_80f9_8221_0e56_b7e4986da753 -->|calls| a53e4565_404a_a05c_787e_e4395b4f4936 style 91f86705_80f9_8221_0e56_b7e4986da753 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
middleware/basicauth/basicauth.go lines 27–111
func New(config ...Config) fiber.Handler {
// Set default config
cfg := configDefault(config...)
var cerr base64.CorruptInputError
// Return new handler
return func(c fiber.Ctx) error {
// Don't execute middleware if Next returns true
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
// Get authorization header and ensure it matches the Basic scheme
rawAuth := c.Get(fiber.HeaderAuthorization)
if rawAuth == "" {
return cfg.Unauthorized(c)
}
if len(rawAuth) > cfg.HeaderLimit {
return c.SendStatus(fiber.StatusRequestHeaderFieldsTooLarge)
}
if containsInvalidHeaderChars(rawAuth) {
return cfg.BadRequest(c)
}
auth := utils.TrimSpace(rawAuth)
if auth == "" {
return cfg.Unauthorized(c)
}
if len(auth) < len(basicScheme) || !utils.EqualFold(auth[:len(basicScheme)], basicScheme) {
return cfg.Unauthorized(c)
}
rest := auth[len(basicScheme):]
if len(rest) < 2 || rest[0] != ' ' || rest[1] == ' ' {
return cfg.BadRequest(c)
}
rest = rest[1:]
if strings.IndexFunc(rest, unicode.IsSpace) != -1 {
return cfg.BadRequest(c)
}
// Decode the header contents
raw, err := base64.StdEncoding.DecodeString(rest)
if err != nil {
if errors.As(err, &cerr) {
raw, err = base64.RawStdEncoding.DecodeString(rest)
}
if err != nil {
return cfg.BadRequest(c)
}
}
if !utf8.Valid(raw) {
return cfg.BadRequest(c)
}
if !norm.NFC.IsNormal(raw) {
raw = norm.NFC.Bytes(raw)
}
// Get the credentials
var creds string
if c.App().Config().Immutable {
creds = string(raw)
} else {
creds = utils.UnsafeString(raw)
}
// Check if the credentials are in the correct form
// which is "username:password".
username, password, found := strings.Cut(creds, ":")
if !found {
return cfg.BadRequest(c)
}
if containsCTL(username) || containsCTL(password) {
return cfg.BadRequest(c)
}
if cfg.Authorizer(username, password, c) {
c.Locals(usernameKey, username)
return c.Next()
}
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does New() do?
New() is a function in the fiber codebase, defined in middleware/basicauth/basicauth.go.
Where is New() defined?
New() is defined in middleware/basicauth/basicauth.go at line 27.
What does New() call?
New() calls 2 function(s): containsCTL, containsInvalidHeaderChars.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free