Expand description
Two-factor authentication for fastauth: TOTP
authenticator apps, one-time codes by email, backup codes and trusted devices. The routes
and JSON match Better Auth’s two-factor plugin, so its twoFactorClient works unchanged.
use fastauth_core::prelude::*;
use fastauth_plugin_two_factor::{TwoFactor, TwoFactorExt};
let auth: Auth<MemoryAdapter, (), plugins![TwoFactor]> = FastAuth::builder()
.secret(Secret::from_bytes(vec![1u8; 32]))
.base_url("http://localhost:3000")
.database(MemoryAdapter::new())
.email_password(EmailPassword::default())
.plugin(TwoFactor::new().issuer("Acme"))
.build()?;
let _two_factor = auth.two_factor();Without the plugin registered, auth.two_factor() doesn’t compile:
ⓘ
use fastauth_core::prelude::*;
use fastauth_plugin_two_factor::TwoFactorExt;
let auth: Auth<MemoryAdapter> = FastAuth::builder()
.secret(Secret::from_bytes(vec![1u8; 32]))
.base_url("http://localhost:3000")
.database(MemoryAdapter::new())
.email_password(EmailPassword::default())
.build()
.unwrap();
let _two_factor = auth.two_factor();Signing in as a user with two-factor on answers
{ "twoFactorRedirect": true, "twoFactorMethods": ["totp", "otp"] } ("otp" only with a
mailer) and sets a
signed {prefix}.two_factor cookie naming the pending user. One of verify-totp,
verify-otp (after send-otp) or verify-backup-code then creates the session.
Modules§
- totp
- TOTP (RFC 6238) over HMAC-SHA1, and the base32 encoding authenticator apps read secrets in.
Structs§
- Backup
Codes Output - Disable
POST /two-factor/disable: turns two-factor off and deletes the secret and backup codes.- Enable
POST /two-factor/enable: a new TOTP secret and backup codes, replacing any old ones. Two-factor turns on after the firstverify-totp, or at once withskip_verification_on_enable.- Enable
Input - Body of
/two-factor/enable. - Enable
Output - Generate
Backup Codes POST /two-factor/generate-backup-codes: replaces every backup code with new ones.- GetTotp
Uri POST /two-factor/get-totp-uri: the current secret’s URI, for showing the QR code again.- Password
Input - Body of the endpoints that only ask for the password again.
- SendOtp
POST /two-factor/send-otp: emails a 6-digit code to the pending or signed-in user.- Totp
UriOutput - TwoFactor
- The two-factor plugin:
.plugin(TwoFactor::new().issuer("Acme")). - TwoFactor
Api - Calls the two-factor endpoints without HTTP.
headersare the request’s: their cookies carry the session, or the pending sign-in forverify_totp. - TwoFactor
Disabled - Emitted when a user turns two-factor off.
- TwoFactor
Enabled - Emitted when a user’s two-factor turns on.
- TwoFactor
Row - A row of
two_factor: one user’s TOTP secret and backup codes. - TwoFactor
User - The column two-factor adds to
user. Apps read it withsession.user.plugin::<TwoFactor, _>(). - Verify
Backup Code POST /two-factor/verify-backup-code: accepts each backup code once.- Verify
Input - Body of the
/two-factor/verify-*endpoints. - Verify
Otp POST /two-factor/verify-otp: checks an emailed code. Five wrong codes burn it.- Verify
Output - What a successful verification answers: the session token and the user.
- Verify
Totp POST /two-factor/verify-totp: checks an authenticator code. Finishes a pending sign-in, or, for a signed-in user, turns two-factor on the first time.
Enums§
- TwoFactor
Error - Why a two-factor request failed. Codes and statuses match Better Auth’s two-factor plugin.
Traits§
- TwoFactor
Ext auth.two_factor(): the server API. Only compiles on anAuthwithTwoFactorregistered.