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
- Variables
- func Code(secret []byte, step int64) string
- func DecodeSecret(s string) ([]byte, error)
- func EncodeSecret(secret []byte) string
- func HashRecoveryCode(code string) []byte
- func NewRecoveryCodes() (codes []string, hashes [][]byte)
- func NewSecret() []byte
- 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 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 )
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 HashRecoveryCode ¶
HashRecoveryCode normalizes a recovery code as typed (case, spaces and dashes do not matter) and returns its hash.
func NewRecoveryCodes ¶
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 URI ¶
URI returns the otpauth:// URI for a QR code. issuer names your application; account names the user (usually their email address).
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.