Home / File/ basicauth.go — fiber Source File

basicauth.go — fiber Source File

Architecture documentation for basicauth.go, a go file in the fiber codebase. 1 imports, 0 dependents.

File go FiberCore Adapters 1 imports 4 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  0169cc3d_e89d_3f0a_2e41_1b964fca25f9["basicauth.go"]
  eddbea11_a101_02f6_6172_741abd636ae7["base64"]
  0169cc3d_e89d_3f0a_2e41_1b964fca25f9 --> eddbea11_a101_02f6_6172_741abd636ae7
  style 0169cc3d_e89d_3f0a_2e41_1b964fca25f9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

package basicauth

import (
	"encoding/base64"
	"errors"
	"strings"
	"unicode"
	"unicode/utf8"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/utils/v2"
	"golang.org/x/text/unicode/norm"
)

// The contextKey type is unexported to prevent collisions with context keys defined in
// other packages.
type contextKey int

// The key for the username value stored in the context
const (
	usernameKey contextKey = iota
)

const basicScheme = "Basic"

// New creates a new middleware handler
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)
// ... (72 more lines)

Domain

Subdomains

Classes

Types

Dependencies

  • base64

Frequently Asked Questions

What does basicauth.go do?
basicauth.go is a source file in the fiber codebase, written in go. It belongs to the FiberCore domain, Adapters subdomain.
What functions are defined in basicauth.go?
basicauth.go defines 4 function(s): New, UsernameFromContext, containsCTL, containsInvalidHeaderChars.
What does basicauth.go depend on?
basicauth.go imports 1 module(s): base64.
Where is basicauth.go in the architecture?
basicauth.go is located at middleware/basicauth/basicauth.go (domain: FiberCore, subdomain: Adapters, directory: middleware/basicauth).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free