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
- Variables
- func Code(secret []byte, step int64) string
- func DecodeSecret(s string) ([]byte, error)
- func EncodeSecret(secret []byte) string
- func GenerateRecoveryCodes() []string
- func HashRecoveryCode(code string) []byte
- func NewRecoveryCodes() (codes []string, hashes [][]byte)deprecated
- func NewSecret() []byte
- func NormalizeRecoveryCode(code string) string
- func Step(t time.Time) int64
- func URI(issuer, account string, secret []byte) string
- func Validate(secret []byte, code string, now time.Time, skew int) (int64, bool)
- type SecretResealer
- type Store
- type TOTP
Constants ¶
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 )
const MaxRecoveryCodeLen = 64
MaxRecoveryCodeLen bounds what is hashed when checking a typed code.
const RecoveryCodeBits = 80
RecoveryCodeBits is the entropy of a code from GenerateRecoveryCodes.
Variables ¶
var ( // ErrNotFound is returned by stores when a subject has no factor. ErrNotFound = errors.New("mfa: not found") )
Functions ¶
func DecodeSecret ¶
DecodeSecret parses the base32 form (as EncodeSecret produces, or as a user types it: case and spaces do not matter).
func EncodeSecret ¶
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 ¶
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 NormalizeRecoveryCode ¶ added in v2.5.0
NormalizeRecoveryCode returns a recovery code as typed in the form that is hashed: without dashes or spaces, lower case.
func URI ¶
URI returns the otpauth:// URI for a QR code. issuer names your application; account names the user (usually their email address).
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.