Documentation
¶
Overview ¶
Package auth provides authentication primitives: identity context, password hashing, and JWT token management (in the jwt sub-package).
Index ¶
Constants ¶
const DefaultPasswordCost = 12
DefaultPasswordCost is the bcrypt cost chok uses for HashPassword. 12 is a reasonable 2024 default — roughly 250ms on modern x86 per hash, slow enough to frustrate brute force, fast enough to serve login traffic. bcrypt.DefaultCost (10) is below the current OWASP recommendation. Tests that feel slow can override via HashPasswordCost.
Variables ¶
This section is empty.
Functions ¶
func ComparePassword ¶
ComparePassword compares a bcrypt hashed password with a plaintext candidate. Returns nil on match, error on mismatch.
func HashPassword ¶
HashPassword returns a bcrypt hash of the password using the default cost (DefaultPasswordCost). Returns an error if password exceeds bcrypt's 72-byte input limit, since bcrypt silently truncates longer inputs.
func HashPasswordCost ¶
HashPasswordCost hashes password at the given bcrypt cost. Use this escape hatch when a specific deployment requires a different cost (lower for tests that run HashPassword in tight loops, higher for paranoid deployments). cost is clamped to bcrypt.MinCost..MaxCost.
Types ¶
type Principal ¶
type Principal struct {
Subject string // Primary identifier (e.g. userID, RID).
Name string // Display name (optional).
Roles []string // Role list (optional; simple RBAC without external authorizer).
Claims map[string]any // Extension fields from the token (optional).
}
Principal represents an authenticated request subject.
func PrincipalFrom ¶
PrincipalFrom retrieves the Principal from the context. Returns false if no Principal is present (unauthenticated request).