webauth

package
v0.99.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package webauth provides password hashing, TOTP, and encryption helpers for web UI login.

Index

Constants

View Source
const (
	// MinPasswordLength is the minimum allowed password length for web accounts.
	MinPasswordLength = 12
	// MinBcryptCost is the minimum accepted bcrypt cost.
	MinBcryptCost = 10
	// BackupCodeCount is how many one-time backup codes are issued at enroll time.
	BackupCodeCount = 10
	// BackupCodeBytes is the entropy used per backup code before encoding.
	BackupCodeBytes = 5
	// PendingSessionTTL is the max lifetime of pending_2fa / pending_enroll cookies.
	PendingSessionTTL = 5 * time.Minute
	// FullSessionTTL is the accessToken lifetime after successful 2FA.
	FullSessionTTL = 24 * time.Hour
)
View Source
const (
	KindPending2FA       = "pending_2fa"
	KindPendingEnroll    = "pending_enroll"
	KindPendingBackupAck = "pending_backup_ack"
	KindFull             = "full"
)

Session kinds stored in parameter params["kind"].

View Source
const (
	CookieAccessToken = "accessToken"
	CookiePending     = "pendingAuth"
)

Cookie names for web auth.

Variables

This section is empty.

Functions

func CheckPassword

func CheckPassword(hash, password string) bool

CheckPassword reports whether password matches hash.

func CodeAt

func CodeAt(secret string, now time.Time) (string, error)

CodeAt returns the 6-digit TOTP for secret at the given time (exact step, no window).

func GenerateTOTPSecret

func GenerateTOTPSecret() (string, error)

GenerateTOTPSecret returns a new base32-encoded TOTP secret (20 bytes).

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns a bcrypt hash of password.

func LooksLikeTOTPCode

func LooksLikeTOTPCode(code string) bool

LooksLikeTOTPCode reports whether code is a 6-digit TOTP shape (not a backup code).

func TOTPProvisioningURI

func TOTPProvisioningURI(secret, username, issuer string) string

TOTPProvisioningURI builds an otpauth URI for authenticator apps.

func UIDForUsername

func UIDForUsername(username string) string

UIDForUsername returns the stable web uid for a username.

func ValidatePasswordStrength

func ValidatePasswordStrength(username, password string) error

ValidatePasswordStrength rejects short or known-weak passwords.

func VerifyTOTP

func VerifyTOTP(secret, code string, now time.Time) (step int64, ok bool)

VerifyTOTP validates a 6-digit code for secret at the current time (±1 step). Returns the matched time step (unix/30) so callers can reject replays.

Types

type Encryptor

type Encryptor struct {
	// contains filtered or unexported fields
}

Encryptor holds the AES-256-GCM key used for TOTP secrets and backup-code pepper.

func LoadEncryptor

func LoadEncryptor(encryptionKey, keyDir string) (enc *Encryptor, fromFile, created bool, err error)

LoadEncryptor resolves the encryption key from explicit config or a persistent file. When encryptionKey is empty, a key file is created/read under keyDir (0600) and a warning should be logged by the caller (FromFile reports created=true).

func (*Encryptor) ConsumeBackupCode

func (e *Encryptor) ConsumeBackupCode(hashes []string, code string) (remaining []string, ok bool)

ConsumeBackupCode returns updated hashes with matching code removed, or ok=false.

func (*Encryptor) Decrypt

func (e *Encryptor) Decrypt(ciphertext, nonce []byte) ([]byte, error)

Decrypt decrypts ciphertext produced by Encrypt.

func (*Encryptor) Encrypt

func (e *Encryptor) Encrypt(plaintext []byte) (ciphertext, nonce []byte, err error)

Encrypt encrypts plaintext with AES-256-GCM. Returns ciphertext and nonce.

func (*Encryptor) GenerateBackupCodes

func (e *Encryptor) GenerateBackupCodes(n int) (codes, hashes []string, err error)

GenerateBackupCodes returns plaintext codes and their hashes.

func (*Encryptor) HashBackupCode

func (e *Encryptor) HashBackupCode(code string) string

HashBackupCode returns a HMAC-SHA256 hex digest of code using the encryptor key as pepper.

Jump to

Keyboard shortcuts

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