1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use rive_models::{
    mfa::{MFAMethod, MFARecoveryCode, MFAStatus, MFATicket, TOTPSecret},
    payload::{CreateMFATicketPayload, EnableTOTP2FAPayload},
};

use crate::prelude::*;

impl Client {
    /// Create a new MFA ticket or validate an existing one.
    pub async fn create_mfa_ticket(&self, payload: CreateMFATicketPayload) -> Result<MFATicket> {
        Ok(self
            .client
            .put(ep!(self, "/auth/mfa/ticket"))
            .auth(&self.authentication)
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Fetch MFA status of an account.
    pub async fn fetch_mfa_status(&self) -> Result<MFAStatus> {
        Ok(self
            .client
            .get(ep!(self, "/auth/mfa/"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Fetch recovery codes for an account.
    pub async fn fetch_recovery_codes(&self) -> Result<Vec<MFARecoveryCode>> {
        Ok(self
            .client
            .post(ep!(self, "/auth/mfa/recovery"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Re-generate recovery codes for an account.
    pub async fn generate_recovery_codes(&self) -> Result<Vec<MFARecoveryCode>> {
        Ok(self
            .client
            .patch(ep!(self, "/auth/mfa/recovery"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Fetch available MFA methods.
    pub async fn get_mfa_methods(&self) -> Result<Vec<MFAMethod>> {
        Ok(self
            .client
            .get(ep!(self, "/auth/mfa/methods"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Enable TOTP 2FA for an account.
    pub async fn enable_totp_2fa(&self, payload: EnableTOTP2FAPayload) -> Result<()> {
        self.client
            .put(ep!(self, "/auth/mfa/totp"))
            .json(&payload)
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Generate a new secret for TOTP.
    pub async fn generate_totp_secret(&self) -> Result<TOTPSecret> {
        Ok(self
            .client
            .post(ep!(self, "/auth/mfa/totp"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Disable TOTP 2FA for an account.
    pub async fn disable_totp_2fa(&self) -> Result<()> {
        self.client
            .delete(ep!(self, "/auth/mfa/totp"))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }
}