Skip to main content

rustauth_plugins/two_factor/
otp_storage.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::sync::Arc;
4
5use rustauth_core::error::RustAuthError;
6
7pub type OtpTransformFuture = Pin<Box<dyn Future<Output = Result<String, RustAuthError>> + Send>>;
8pub type OtpHashFn = Arc<dyn Fn(String) -> OtpTransformFuture + Send + Sync>;
9pub type OtpEncryptFn = Arc<dyn Fn(String) -> OtpTransformFuture + Send + Sync>;
10pub type OtpDecryptFn = Arc<dyn Fn(String) -> OtpTransformFuture + Send + Sync>;
11
12#[derive(Clone, Default)]
13pub enum OtpStorage {
14    #[default]
15    Plain,
16    Encrypted,
17    Hashed,
18    CustomHash(OtpHashFn),
19    CustomEncrypt {
20        encrypt: OtpEncryptFn,
21        decrypt: OtpDecryptFn,
22    },
23}
24
25impl std::fmt::Debug for OtpStorage {
26    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            Self::Plain => formatter.write_str("Plain"),
29            Self::Encrypted => formatter.write_str("Encrypted"),
30            Self::Hashed => formatter.write_str("Hashed"),
31            Self::CustomHash(_) => formatter.write_str("CustomHash(<hash>)"),
32            Self::CustomEncrypt { .. } => formatter.write_str("CustomEncrypt(<encrypt/decrypt>)"),
33        }
34    }
35}