Skip to main content

openauth_plugins/two_factor/
options.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::sync::Arc;
4
5use openauth_core::api::ApiRequest;
6use openauth_core::db::User;
7use openauth_core::error::OpenAuthError;
8
9pub type SendOtpFuture = Pin<Box<dyn Future<Output = Result<(), OpenAuthError>> + Send>>;
10pub type SendOtp = Arc<dyn Fn(TwoFactorOtpMessage) -> SendOtpFuture + Send + Sync>;
11
12#[derive(Clone)]
13pub struct TwoFactorOtpMessage {
14    pub user: User,
15    pub otp: String,
16    pub request: ApiRequest,
17}
18
19#[derive(Clone)]
20pub struct TwoFactorOptions {
21    pub issuer: Option<String>,
22    pub two_factor_table: String,
23    pub totp: TotpOptions,
24    pub otp: OtpOptions,
25    pub backup_codes: BackupCodeOptions,
26    pub skip_verification_on_enable: bool,
27    pub allow_passwordless: bool,
28    pub two_factor_cookie_max_age: u64,
29    pub trust_device_max_age: u64,
30}
31
32impl Default for TwoFactorOptions {
33    fn default() -> Self {
34        Self {
35            issuer: None,
36            two_factor_table: "twoFactor".to_owned(),
37            totp: TotpOptions::default(),
38            otp: OtpOptions::default(),
39            backup_codes: BackupCodeOptions::default(),
40            skip_verification_on_enable: false,
41            allow_passwordless: false,
42            two_factor_cookie_max_age: 10 * 60,
43            trust_device_max_age: 30 * 24 * 60 * 60,
44        }
45    }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct TotpOptions {
50    pub digits: u32,
51    pub period: u64,
52    pub disabled: bool,
53}
54
55impl Default for TotpOptions {
56    fn default() -> Self {
57        Self {
58            digits: 6,
59            period: 30,
60            disabled: false,
61        }
62    }
63}
64
65#[derive(Clone)]
66pub struct OtpOptions {
67    pub period_seconds: u64,
68    pub digits: usize,
69    pub allowed_attempts: u32,
70    pub storage: OtpStorage,
71    pub send_otp: Option<SendOtp>,
72}
73
74impl Default for OtpOptions {
75    fn default() -> Self {
76        Self {
77            period_seconds: 3 * 60,
78            digits: 6,
79            allowed_attempts: 5,
80            storage: OtpStorage::Plain,
81            send_otp: None,
82        }
83    }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub enum OtpStorage {
88    Plain,
89    Encrypted,
90    Hashed,
91}
92
93#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct BackupCodeOptions {
95    pub amount: usize,
96    pub length: usize,
97    pub storage: BackupCodeStorage,
98}
99
100impl Default for BackupCodeOptions {
101    fn default() -> Self {
102        Self {
103            amount: 10,
104            length: 10,
105            storage: BackupCodeStorage::Encrypted,
106        }
107    }
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111pub enum BackupCodeStorage {
112    Plain,
113    Encrypted,
114}