mfa

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 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.Key), and stores recovery codes only as SHA-256 hashes.

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
)

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 HashRecoveryCode

func HashRecoveryCode(code string) []byte

HashRecoveryCode normalizes a recovery code as typed (case, spaces and dashes do not matter) and returns its hash.

func NewRecoveryCodes

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

NewRecoveryCodes returns RecoveryCodeCount new recovery codes, to show the user once, and their hashes, to store. Each code has 50 random bits, formatted "xxxxx-xxxxx".

func NewSecret

func NewSecret() []byte

NewSecret returns a new random TOTP secret.

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 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 SHA-256 hashes of the unused recovery codes.
	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