Skip to main content

Crate fastauth_plugin_two_factor

Crate fastauth_plugin_two_factor 

Source
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§

BackupCodesOutput
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 first verify-totp, or at once with skip_verification_on_enable.
EnableInput
Body of /two-factor/enable.
EnableOutput
GenerateBackupCodes
POST /two-factor/generate-backup-codes: replaces every backup code with new ones.
GetTotpUri
POST /two-factor/get-totp-uri: the current secret’s URI, for showing the QR code again.
PasswordInput
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.
TotpUriOutput
TwoFactor
The two-factor plugin: .plugin(TwoFactor::new().issuer("Acme")).
TwoFactorApi
Calls the two-factor endpoints without HTTP. headers are the request’s: their cookies carry the session, or the pending sign-in for verify_totp.
TwoFactorDisabled
Emitted when a user turns two-factor off.
TwoFactorEnabled
Emitted when a user’s two-factor turns on.
TwoFactorRow
A row of two_factor: one user’s TOTP secret and backup codes.
TwoFactorUser
The column two-factor adds to user. Apps read it with session.user.plugin::<TwoFactor, _>().
VerifyBackupCode
POST /two-factor/verify-backup-code: accepts each backup code once.
VerifyInput
Body of the /two-factor/verify-* endpoints.
VerifyOtp
POST /two-factor/verify-otp: checks an emailed code. Five wrong codes burn it.
VerifyOutput
What a successful verification answers: the session token and the user.
VerifyTotp
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§

TwoFactorError
Why a two-factor request failed. Codes and statuses match Better Auth’s two-factor plugin.

Traits§

TwoFactorExt
auth.two_factor(): the server API. Only compiles on an Auth with TwoFactor registered.