mfa

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 13 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mfa implements time-based one-time passwords (TOTP, RFC 6238) and recovery codes, and defines the Store that keeps a subject's factor.

Codes use the parameters every authenticator app supports: HMAC-SHA1, 6 digits, 30-second steps. IAM seals TOTP secrets before they reach the Store (see iam.MFAConfig.Keys), and stores recovery codes only as hashes, keyed when MFAConfig.Keys is set.

Index

Constants

View Source
const (
	// Digits is the length of a TOTP code.
	Digits = 6

	// Period is the length of a TOTP time step.
	Period = 30 * time.Second

	// SecretSize is the size of a new TOTP secret (160 bits, RFC 4226).
	SecretSize = 20

	// RecoveryCodeCount is how many recovery codes are issued at a time.
	RecoveryCodeCount = 10
)
View Source
const MaxRecoveryCodeLen = 64

MaxRecoveryCodeLen bounds what is hashed when checking a typed code.

View Source
const RecoveryCodeBits = 80

RecoveryCodeBits is the entropy of a code from GenerateRecoveryCodes.

Variables

View Source
var (
	// ErrNotFound is returned by stores when a subject has no factor.
	ErrNotFound = errors.New("mfa: not found")
)

Functions

func Code

func Code(secret []byte, step int64) string

Code returns the TOTP code for a time step.

func DecodeSecret

func DecodeSecret(s string) ([]byte, error)

DecodeSecret parses the base32 form (as EncodeSecret produces, or as a user types it: case and spaces do not matter).

func EncodeSecret

func EncodeSecret(secret []byte) string

EncodeSecret returns the base32 form users type into authenticator apps.

func GenerateRecoveryCodes added in v2.5.0

func GenerateRecoveryCodes() []string

GenerateRecoveryCodes returns RecoveryCodeCount new recovery codes, to show the user once. Each has RecoveryCodeBits random bits, formatted "xxxx-xxxx-xxxx-xxxx". iam stores them as keyed hashes (or, with only the legacy MFA key configured, HashRecoveryCode).

func HashRecoveryCode

func HashRecoveryCode(code string) []byte

HashRecoveryCode returns the unkeyed SHA-256 hash of a recovery code as typed (case, spaces and dashes do not matter). It is how codes were stored before keyed hashes, and how they still are when only the legacy MFA key is configured.

func NewRecoveryCodes deprecated

func NewRecoveryCodes() (codes []string, hashes [][]byte)

NewRecoveryCodes returns RecoveryCodeCount new 50-bit recovery codes, formatted "xxxxx-xxxxx", and their HashRecoveryCode hashes.

Deprecated: iam no longer uses it. Use GenerateRecoveryCodes, which has RecoveryCodeBits of entropy.

func NewSecret

func NewSecret() []byte

NewSecret returns a new random TOTP secret.

func NormalizeRecoveryCode added in v2.5.0

func NormalizeRecoveryCode(code string) string

NormalizeRecoveryCode returns a recovery code as typed in the form that is hashed: without dashes or spaces, lower case.

func Step

func Step(t time.Time) int64

Step returns the TOTP time step of t.

func URI

func URI(issuer, account string, secret []byte) string

URI returns the otpauth:// URI for a QR code. issuer names your application; account names the user (usually their email address).

func Validate

func Validate(secret []byte, code string, now time.Time, skew int) (int64, bool)

Validate checks a code against the steps around now (skew steps either side) and returns the matching step. It compares in constant time and does not prevent replay: the caller must only accept a step greater than the last one used (Store.AdvanceTOTP).

Types

type SecretResealer added in v2.5.0

type SecretResealer interface {
	// ResealTOTP replaces the subject's Secret with sealed only if it
	// currently equals old, and reports whether it did. Nothing else about
	// the factor changes. Missing is (false, nil). Of two concurrent calls
	// with the same old, at most one returns true.
	ResealTOTP(ctx context.Context, subjectID string, old, sealed []byte) (bool, error)
}

SecretResealer is an optional Store extension that lets iam move a sealed secret to a new key after MFA key rotation, without rewriting the rest of the factor (a whole-factor PutTOTP could undo a concurrent AdvanceTOTP or UseRecoveryCode).

type Store

type Store interface {
	// GetTOTP returns the subject's factor, or ErrNotFound.
	GetTOTP(ctx context.Context, subjectID string) (*TOTP, error)

	// PutTOTP creates or replaces the subject's factor.
	PutTOTP(ctx context.Context, t *TOTP) error

	// DeleteTOTP removes the subject's factor. Missing is not an error.
	DeleteTOTP(ctx context.Context, subjectID string) error

	// AdvanceTOTP sets LastStep to step only if step is greater than the
	// stored LastStep, and reports whether it did. Of two concurrent calls
	// with the same step, at most one returns true.
	AdvanceTOTP(ctx context.Context, subjectID string, step int64) (bool, error)

	// UseRecoveryCode removes one recovery-code hash and reports whether
	// it was present. Of two concurrent calls, at most one returns true.
	UseRecoveryCode(ctx context.Context, subjectID string, hash []byte) (bool, error)
}

Store persists TOTP factors, one per subject. Implementations must be safe for concurrent use; AdvanceTOTP and UseRecoveryCode must be atomic.

type TOTP

type TOTP struct {
	SubjectID string

	// Secret is the sealed secret: opaque to stores, never the raw key.
	Secret []byte

	// Confirmed is false between enrollment and the first valid code. Only
	// confirmed factors are required at login.
	Confirmed bool

	// LastStep is the last time step accepted, for replay protection.
	LastStep int64

	// RecoveryCodes are the hashes of the unused recovery codes: keyed
	// tags (keyring.Tag) or, from before keyed hashes or with only the
	// legacy MFA key, HashRecoveryCode. Opaque to stores.
	RecoveryCodes [][]byte

	CreatedAt time.Time
}

TOTP is a subject's TOTP factor as stored.

Jump to

Keyboard shortcuts

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