Skip to main content

haste_repository/types/
mfa.rs

1use crate::types::scope::UserId;
2use haste_jwt::TenantId;
3use sqlx::types::time::OffsetDateTime;
4
5#[derive(sqlx::FromRow, Clone)]
6pub struct UserMFACredential {
7    pub id: Option<String>,
8    pub tenant: TenantId,
9    pub user_id: String,
10    pub credential_type: String,
11    pub secret_ciphertext: Vec<u8>,
12    pub secret_nonce: Vec<u8>,
13    pub key_id: String,
14    pub totp_algorithm: String,
15    pub totp_digits: i16,
16    pub totp_period: i16,
17    pub totp_skew: i16,
18    pub is_active: bool,
19
20    pub created_at: OffsetDateTime,
21    // pub activated_at: Option<OffsetDateTime>,
22}
23
24pub struct UserMFASearchClaims {
25    pub tenant: TenantId,
26    pub user_id: UserId,
27    pub is_active: Option<bool>,
28}
29
30#[derive(Clone)]
31pub enum MFACredentialType {
32    TOTP,
33}
34
35impl From<MFACredentialType> for &str {
36    fn from(credential_type: MFACredentialType) -> Self {
37        match credential_type {
38            MFACredentialType::TOTP => "totp",
39        }
40    }
41}
42
43#[derive(Clone)]
44pub struct UserMFACredentialCreate {
45    pub user_id: UserId,
46    pub credential_type: MFACredentialType,
47    pub secret_ciphertext: Vec<u8>,
48    pub secret_nonce: Vec<u8>,
49    pub key_id: String,
50    pub totp_algorithm: Option<String>,
51    pub totp_digits: Option<i16>,
52    pub totp_period: Option<i16>,
53    pub totp_skew: Option<i16>,
54}
55
56// Update model right now is just about activation and deactivation of the MFA credential.
57// If we need to update other fields in the future, we can add them here.
58pub struct UserMFACredentialUpdate {
59    pub id: String,
60    pub user_id: UserId,
61    pub activated_at: Option<OffsetDateTime>,
62    pub is_active: bool,
63}
64
65pub struct MFAId(pub String);
66
67pub struct MFAKey(UserId, MFAId);
68impl MFAKey {
69    #[must_use]
70    pub fn new(user_id: UserId, mfa_id: String) -> Self {
71        MFAKey(user_id, MFAId(mfa_id))
72    }
73    #[must_use]
74    pub fn user_id(&self) -> &UserId {
75        &self.0
76    }
77
78    #[must_use]
79    pub fn mfa_id(&self) -> &MFAId {
80        &self.1
81    }
82}