auth

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package auth provides authentication primitives: identity context, password hashing, and JWT token management (in the jwt sub-package).

Index

Constants

View Source
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

func ComparePassword(hashed, plain string) error

ComparePassword compares a bcrypt hashed password with a plaintext candidate. Returns nil on match, error on mismatch.

func HashPassword

func HashPassword(password string) (string, error)

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

func HashPasswordCost(password string, cost int) (string, error)

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.

func WithPrincipal

func WithPrincipal(ctx context.Context, p Principal) context.Context

WithPrincipal stores a Principal in the context.

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

func PrincipalFrom(ctx context.Context) (Principal, bool)

PrincipalFrom retrieves the Principal from the context. Returns false if no Principal is present (unauthenticated request).

func (Principal) HasRole

func (p Principal) HasRole(role string) bool

HasRole reports whether the principal has the given role.

Directories

Path Synopsis
Package jwt provides an instance-based JWT manager using HS256.
Package jwt provides an instance-based JWT manager using HS256.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL