Skip to main content

Overview

The basicauth middleware provides HTTP Basic Authentication, a simple authentication scheme built into the HTTP protocol. It prompts users for a username and password, which are sent with each request. Use it when you need:
  • Simple password protection for admin areas
  • Quick authentication for internal tools
  • Development/staging environment protection

Installation

Quick Start

Configuration

Options

ValidatorFunc

Examples

Static Credentials

Custom Realm

Custom Validator

Validate against a database or external service:

Custom Error Handler

Route-Specific Protection

Group Protection

API Reference

Functions

Types

Technical Details

The basicauth middleware implements HTTP Basic Authentication according to RFC 7617. Here are the key implementation details:

Authentication Flow

  1. Header Extraction: Extracts the Authorization header from the incoming request
  2. Scheme Validation: Verifies the header starts with β€œBasic ” prefix
  3. Base64 Decoding: Decodes the base64-encoded credentials using base64.StdEncoding.DecodeString()
  4. Credential Parsing: Splits the decoded string on the first colon (:) to separate username and password
  5. Validation: Calls the configured validator function to verify credentials
  6. Response: Either proceeds to the next handler or returns 401 Unauthorized with WWW-Authenticate header

Security Features

Constant-Time Comparison: The built-in validator uses secureCompare() function to prevent timing attacks:
This approach:
  • Hashes both strings using SHA-256 to normalize lengths
  • Uses crypto/subtle.ConstantTimeCompare() to prevent timing side-channels
  • Protects against timing attacks that could leak password information
WWW-Authenticate Header: On authentication failure, the middleware sets the WWW-Authenticate header with the configured realm, prompting the browser to display a login dialog.

Error Handling

The middleware returns 401 Unauthorized in these cases:
  • Missing Authorization header
  • Invalid authorization scheme (not β€œBasic”)
  • Invalid base64 encoding
  • Malformed credentials (no colon separator)
  • Validator function returns false
Custom error handlers can be provided via Options.ErrorHandler to customize the response format (e.g., JSON instead of plain text).

Security Considerations

  1. Always use HTTPS - Basic auth sends credentials base64-encoded (not encrypted)
  2. Use strong passwords - Avoid dictionary words and short passwords
  3. Hash stored passwords - Never store plain-text passwords in your validator
  4. Consider rate limiting - Combine with ratelimit to prevent brute force attacks
  5. Timing attacks - The built-in validator uses constant-time comparison

Best Practices

  • Use Basic Auth for simple internal tools, not production user-facing auth
  • Combine with HTTPS redirect middleware
  • Add rate limiting to prevent brute force attacks
  • Use Bearer auth or session-based auth for APIs

Testing

The basicauth middleware includes comprehensive test coverage. Below are all test cases: