Documentation
¶
Overview ¶
Package mfa adds multi-factor auth + account protection to the togo auth family: a challenge state-machine (login → challenge token → factor → session), TOTP 2FA with recovery codes, email OTP, magic-link login, account lockout, and password policy.
m, _ := mfa.FromKernel(k)
if m.Required(userID) { // issue a challenge instead of a session
tok, _ := m.IssueChallenge(userID)
}
Index ¶
- type Config
- type Lockout
- type OTPCode
- type RecoveryCode
- type Service
- func (s *Service) Cfg() Config
- func (s *Service) DisableTOTP(userID string)
- func (s *Service) EnrollTOTP(userID, issuer string) (secret, uri string, err error)
- func (s *Service) GenerateRecoveryCodes(userID string, n int) []string
- func (s *Service) IsLocked(userID string) (bool, time.Time)
- func (s *Service) IssueChallenge(userID string) (string, error)
- func (s *Service) MagicLinkToken(userID string) string
- func (s *Service) RecordFailure(userID string)
- func (s *Service) Required(userID string) bool
- func (s *Service) Reset(userID string)
- func (s *Service) SendOTP(userID string) (string, error)
- func (s *Service) ValidatePassword(pw string) error
- func (s *Service) ValidateTOTPAt(secret, code string, at time.Time) (bool, error)
- func (s *Service) VerifyChallenge(token string) (string, error)
- func (s *Service) VerifyMagicLink(token string) (string, error)
- func (s *Service) VerifyOTP(userID, code string) bool
- func (s *Service) VerifyRecoveryCode(userID, code string) bool
- func (s *Service) VerifyTOTP(userID, code string) bool
- func (s *Service) WithStore(store Store) *Service
- type Store
- type TOTPSecret
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
ChallengeSecret []byte
ChallengeTTL time.Duration
OTPTTL time.Duration
OTPRatePer10Min int
LockoutThreshold int
LockoutMinutes int
PwMinLen int
}
Config is read from env on boot.
type RecoveryCode ¶
RecoveryCode is a single-use backup code (stored hashed).
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the MFA runtime stored on the kernel (k.Get("mfa")).
func FromKernel ¶
FromKernel returns the MFA Service.
func (*Service) DisableTOTP ¶
DisableTOTP removes a user's TOTP enrollment.
func (*Service) EnrollTOTP ¶
EnrollTOTP generates a new (inactive) TOTP secret for a user and returns the otpauth:// provisioning URI (render it as a QR code client-side).
func (*Service) GenerateRecoveryCodes ¶
GenerateRecoveryCodes creates n single-use codes, stores their hashes, and returns the plaintext codes (shown to the user ONCE).
func (*Service) IssueChallenge ¶
IssueChallenge mints a short-lived signed challenge token (NOT a session).
func (*Service) MagicLinkToken ¶
MagicLinkToken mints a signed login token for a user (reuses the challenge signing with a distinct purpose).
func (*Service) RecordFailure ¶
RecordFailure increments a user's failure counter; locks after the threshold.
func (*Service) SendOTP ¶
SendOTP generates a 6-digit code for the user (rate-limited). The caller is responsible for delivery (email/SMS); the code is returned for the caller to send (and logged in dev). Returns the code + an error if rate-limited.
func (*Service) ValidatePassword ¶
ValidatePassword checks a password against the configured policy.
func (*Service) ValidateTOTPAt ¶
ValidateTOTPAt verifies a code at a specific time (used by tests).
func (*Service) VerifyChallenge ¶
VerifyChallenge validates a challenge token and returns its user id.
func (*Service) VerifyMagicLink ¶
VerifyMagicLink validates a magic-link token, returning the user id.
func (*Service) VerifyRecoveryCode ¶
VerifyRecoveryCode consumes a single-use recovery code.
func (*Service) VerifyTOTP ¶
VerifyTOTP checks a code; on the first valid code it activates the secret.
type Store ¶
type Store interface {
SetTOTP(TOTPSecret)
GetTOTP(userID string) (TOTPSecret, bool)
DeleteTOTP(userID string)
AddRecoveryCodes(userID string, hashes []string)
ConsumeRecovery(userID, hash string) bool // returns true if a matching unused code was consumed
SetOTP(OTPCode)
GetOTP(userID string) (OTPCode, bool)
DeleteOTP(userID string)
OTPSendsSince(userID string, since time.Time) int
RecordOTPSend(userID string, at time.Time)
GetLockout(userID string) Lockout
SetLockout(userID string, l Lockout)
}
Store is the MFA persistence seam (swap the in-memory default for a DB one).