Skip to Content
Signet

Second factor with TOTP

TOTP enrollment, Backup codes, Two-factor challenges and the JWT exchange.

Second factor is opt-in. It needs an application-supplied TOTPStore and a dedicated 32-byte TOTPKey; without either, New rejects the configuration with ErrTOTPNotConfigured and the Go API returns ErrTOTPNotConfigured for 2FA operations. TOTPConfig sets the code parameters.

cfg.TOTPStore = myStore // implements Get, Put and Delete keyed by User ID
cfg.TOTPKey = key       // exactly 32 bytes, distinct from CSRFKey
cfg.TOTP = &signet.TOTPConfig{Digits: 6, Period: 30, Window: 1}

The secret is encrypted at rest with the dedicated key. Backup codes are stored only as digests. TokenTwoFactor is the single-use Token kind used for challenges.

Reference schema

TOTPStore is keyed by User ID — one enrollment per User, Put upserts. Your store persists what it is given: the secret arrives already encrypted with TOTPKey, and the backup codes arrive as digests.

-- PostgreSQL
CREATE TABLE IF NOT EXISTS signet_totp_states (
    user_id TEXT PRIMARY KEY,       -- one enrollment per User
    secret TEXT NOT NULL,           -- encrypted with TOTPKey before it reaches you
    backup_codes TEXT NOT NULL,     -- the ten digests, serialized however you choose
    verified BOOLEAN NOT NULL DEFAULT FALSE,
    FOREIGN KEY (user_id) REFERENCES signet_users(id) ON DELETE CASCADE
);

For SQLite: BOOLEAN → INTEGER and TEXT timestamps stay TEXT, as in the bundled adapters. A missing row means "not enrolled"; delete it when the User disables Second factor. The stdlib example implements this schema as a worked TOTPStore (examples/stdlib/totpstore.go) and wires the endpoints end to end.

Enrollment

EnableTwoFactor(ctx, EnableTwoFactorInput) (*EnableTwoFactorResult, error) requires the current password. It returns a totp_uri for authenticator apps and ten backup_codes formatted XXXXX-XXXXX. Enrollment stays pending until the first successful TOTP code.

enabled, err := auth.EnableTwoFactor(ctx, signet.EnableTwoFactorInput{
    UserID: userID, Password: "correct horse",
})
// Confirm with enabled.Challenge and a code from the authenticator app.
_, err = auth.VerifyTwoFactor(ctx, signet.VerifyTwoFactorInput{
    Challenge: enabled.Challenge, Code: "123456",
})

Signing in with a Second factor

Once enrollment exists, a password-only sign-in returns a TwoFactorChallenge instead of a Session. Exchange it with VerifyTwoFactor(ctx, VerifyTwoFactorInput) (*SignInResult, error):

first, err := auth.SignIn(ctx, signet.SignInInput{Email: "ada@example.com", Password: "correct horse"})
if first.TwoFactorChallenge != "" {
    result, err := auth.VerifyTwoFactor(ctx, signet.VerifyTwoFactorInput{
        Challenge: first.TwoFactorChallenge, Code: "123456",
    })
}

Challenges expire after five minutes and are single-use; replay returns ErrTokenInvalid. Wrong codes return ErrTOTPInvalid (invalid_code, 401). Five failed attempts per challenge are throttled through the existing rate limiter.

A pending enrollment is confirmed by verifying its first TOTP code. Backup codes work only after confirmation, are single-use, and a used code is removed. Wrong TOTP and Backup codes both return ErrTOTPInvalid.

Backup codes and disabling

fresh, err := auth.RegenerateBackupCodes(ctx, signet.RegenerateBackupCodesInput{
    UserID: userID, SessionToken: sessionToken, Password: "correct horse",
})
err = auth.DisableTwoFactor(ctx, signet.DisableTwoFactorInput{
    UserID: userID, Password: "correct horse",
})

Regeneration requires the current password and an active enrollment; a pending enrollment returns ErrTOTPNotEnabled. Regeneration replaces all ten codes. Disabling removes the TOTP state immediately. Both require a Session within Accounts.FreshWindow when called over HTTP.

JWT exchange

The /token endpoint cannot bypass Second factor. A password-only exchange for an enrolled User returns a TokenPair with only two_factor_challenge and no tokens. Post that challenge and a code back through TokenInput to receive the real pair:

pair, _ := auth.ExchangeToken(ctx, signet.TokenInput{Email: email, Password: password})
final, err := auth.ExchangeToken(ctx, signet.TokenInput{
    Challenge: pair.TwoFactorChallenge, Code: code,
})

HTTP endpoints

EndpointMethodAuthPurpose
enable-two-factorPOSTyesStart enrollment; returns URI and Backup codes.
verify-two-factorPOSTanonymousExchange challenge and code for a Session.
disable-two-factorPOSTyesRemove enrollment.
regenerate-backup-codesPOSTyesReplace all Backup codes.

See Errors.

On this page