authn

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package authn implements sign-in primitives: argon2id password hashing, opaque session and API tokens (only SHA-256 hashes are stored), TOTP, recovery codes, the optional breached-password check, passkeys and OIDC.

Index

Constants

View Source
const (
	APIKeyPrefix = "rc_live_"
	AgentPrefix  = "rc_agent_"
	TestPrefix   = "rc_test_"
	SMTPPrefix   = "rc_smtp_"
)

Prefixes of API keys and MCP agent tokens. Test-mode transactional keys start with rc_test_ and SMTP credentials with rc_smtp_, so a key says what it is wherever it is pasted.

Variables

View Source
var ErrInvalidHash = errors.New("authn: invalid password hash")

ErrInvalidHash is returned for a malformed stored hash.

Functions

func EqualHash

func EqualHash(a, b []byte) bool

EqualHash compares two hashes in constant time.

func GroupSecret

func GroupSecret(secret string) string

GroupSecret formats a secret for manual entry: "JBSW Y3DP EHPK 3PXP".

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns a PHC-formatted argon2id hash.

func HashToken

func HashToken(token string) []byte

HashToken returns the SHA-256 of a token. Tokens carry 256 bits of entropy, so a fast hash is enough (no salt or KDF needed).

func LooksLikeAPIKey

func LooksLikeAPIKey(s string) bool

LooksLikeAPIKey reports whether a bearer token has an API key or agent prefix.

func MaskAPIKey

func MaskAPIKey(key string) string

MaskAPIKey shows the prefix, the first 4 and the last 4 characters.

func MatchTOTP

func MatchTOTP(secret, code string, now time.Time) (int64, bool)

MatchTOTP checks a 6-digit code, allowing one 30-second step of clock skew either way, and returns the time step (Unix time / 30 s) the code belongs to. A caller that accepts each step once makes every code single use. Spaces in pasted codes ("123 456") are ignored.

func NeedsRehash

func NeedsRehash(encoded string) bool

NeedsRehash reports whether a hash was made with weaker parameters.

func NewAPIKey

func NewAPIKey(prefix string) (key string, hash []byte, masked string)

NewAPIKey returns a new key such as rc_live_8f2c…a91d (64 hex chars after the prefix), its hash, and the masked form shown in lists.

func NewRecoveryCodes

func NewRecoveryCodes(n int) []string

NewRecoveryCodes returns n one-time codes formatted "b4xk-9q2m-7hdn".

func NewToken

func NewToken() (token string, hash []byte)

NewToken returns a random URL-safe token (32 bytes of entropy) and its SHA-256 hash. Only the hash is stored; the token goes to the client once.

func NormalizeRecoveryCode

func NormalizeRecoveryCode(s string) string

NormalizeRecoveryCode lower-cases and strips spaces so pasted codes match.

func PasswordProblem

func PasswordProblem(password string, minLength int) string

PasswordProblem explains why a password is rejected, or "" when it's fine, in English (the CLI's).

func PasswordProblemIn

func PasswordProblemIn(l i18n.Lang, password string, minLength int) string

PasswordProblemIn explains why a password is rejected in a language, or "" when it's fine.

func VerifyDummy

func VerifyDummy(password string)

VerifyDummy burns the same time as a real verification.

func VerifyPassword

func VerifyPassword(password, encoded string) (bool, error)

VerifyPassword reports whether password matches the stored hash, in constant time.

func VerifyTOTP

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

VerifyTOTP checks a 6-digit code, allowing one 30-second step of clock skew. Spaces in pasted codes ("123 456") are ignored. It doesn't say which step matched: sign-in uses MatchTOTP so that a code works once.

Types

type BreachChecker

type BreachChecker struct {
	Client  *http.Client
	BaseURL string
}

BreachChecker asks the Have I Been Pwned range API whether a password is known from breaches. Only the first five hex characters of the SHA-1 leave the server (k-anonymity). Instance admins can turn it off for offline installs.

func NewBreachChecker

func NewBreachChecker() *BreachChecker

NewBreachChecker returns a checker with a short timeout.

func (*BreachChecker) Breached

func (c *BreachChecker) Breached(ctx context.Context, password string) (bool, error)

Breached reports whether the password appears in the breach corpus.

type TOTPSecret

type TOTPSecret struct {
	Secret string // base32, no padding
	URL    string // otpauth:// URL for the QR code
}

TOTPSecret holds a freshly generated authenticator secret.

func NewTOTP

func NewTOTP(issuer, account string) (TOTPSecret, error)

NewTOTP generates a secret for the given issuer (instance name) and account.

Jump to

Keyboard shortcuts

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