Skip to main content

openauth_plugins/phone_number/
options.rs

1use std::sync::Arc;
2
3use openauth_core::error::OpenAuthError;
4
5pub type PhoneNumberSender =
6    Arc<dyn Fn(&str, &str) -> Result<(), OpenAuthError> + Send + Sync + 'static>;
7pub type PhoneNumberVerifier =
8    Arc<dyn Fn(&str, &str) -> Result<bool, OpenAuthError> + Send + Sync + 'static>;
9pub type PhoneNumberValidator =
10    Arc<dyn Fn(&str) -> Result<bool, OpenAuthError> + Send + Sync + 'static>;
11pub type PhoneNumberCallback =
12    Arc<dyn Fn(&str, &str) -> Result<(), OpenAuthError> + Send + Sync + 'static>;
13pub type PhoneNumberTempValue = Arc<dyn Fn(&str) -> String + Send + Sync + 'static>;
14
15#[derive(Clone)]
16pub struct SignUpOnVerification {
17    pub get_temp_email: PhoneNumberTempValue,
18    pub get_temp_name: Option<PhoneNumberTempValue>,
19}
20
21#[derive(Clone)]
22pub struct PhoneNumberOptions {
23    pub otp_length: usize,
24    pub expires_in: u64,
25    pub allowed_attempts: u32,
26    pub require_verification: bool,
27    pub send_otp: Option<PhoneNumberSender>,
28    pub verify_otp: Option<PhoneNumberVerifier>,
29    pub send_password_reset_otp: Option<PhoneNumberSender>,
30    pub callback_on_verification: Option<PhoneNumberCallback>,
31    pub phone_number_validator: Option<PhoneNumberValidator>,
32    pub sign_up_on_verification: Option<SignUpOnVerification>,
33}
34
35impl Default for PhoneNumberOptions {
36    fn default() -> Self {
37        Self {
38            otp_length: 6,
39            expires_in: 300,
40            allowed_attempts: 3,
41            require_verification: false,
42            send_otp: None,
43            verify_otp: None,
44            send_password_reset_otp: None,
45            callback_on_verification: None,
46            phone_number_validator: None,
47            sign_up_on_verification: None,
48        }
49    }
50}
51
52impl PhoneNumberOptions {
53    pub(crate) fn with_defaults(mut self) -> Self {
54        if self.otp_length == 0 {
55            self.otp_length = 6;
56        }
57        if self.expires_in == 0 {
58            self.expires_in = 300;
59        }
60        if self.allowed_attempts == 0 {
61            self.allowed_attempts = 3;
62        }
63        self
64    }
65
66    #[must_use]
67    pub fn send_otp<F>(mut self, sender: F) -> Self
68    where
69        F: Fn(&str, &str) -> Result<(), OpenAuthError> + Send + Sync + 'static,
70    {
71        self.send_otp = Some(Arc::new(sender));
72        self
73    }
74
75    #[must_use]
76    pub fn verify_otp<F>(mut self, verifier: F) -> Self
77    where
78        F: Fn(&str, &str) -> Result<bool, OpenAuthError> + Send + Sync + 'static,
79    {
80        self.verify_otp = Some(Arc::new(verifier));
81        self
82    }
83
84    #[must_use]
85    pub fn send_password_reset_otp<F>(mut self, sender: F) -> Self
86    where
87        F: Fn(&str, &str) -> Result<(), OpenAuthError> + Send + Sync + 'static,
88    {
89        self.send_password_reset_otp = Some(Arc::new(sender));
90        self
91    }
92
93    #[must_use]
94    pub fn phone_number_validator<F>(mut self, validator: F) -> Self
95    where
96        F: Fn(&str) -> Result<bool, OpenAuthError> + Send + Sync + 'static,
97    {
98        self.phone_number_validator = Some(Arc::new(validator));
99        self
100    }
101
102    #[must_use]
103    pub fn callback_on_verification<F>(mut self, callback: F) -> Self
104    where
105        F: Fn(&str, &str) -> Result<(), OpenAuthError> + Send + Sync + 'static,
106    {
107        self.callback_on_verification = Some(Arc::new(callback));
108        self
109    }
110
111    #[must_use]
112    pub fn sign_up_on_verification(mut self, options: SignUpOnVerification) -> Self {
113        self.sign_up_on_verification = Some(options);
114        self
115    }
116
117    #[must_use]
118    pub fn require_verification(mut self, require_verification: bool) -> Self {
119        self.require_verification = require_verification;
120        self
121    }
122}