dfns_sdk_rs/client/
base_auth_api.rs

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// @dfns-sdk-rs/src/client/base_auth_api.rs

use crate::api::auth::types::CreateRegistrationChallengeResponse;
use crate::error::DfnsError;
use crate::models::generic::DfnsBaseApiOptions;
use crate::signer::{
    FirstFactorAssertion, RecoveryKeyAssertion, SecondFactorAssertion, UserActionChallenge,
};
use crate::store::{FirstFactorAttestation, RecoveryFactorAttestation, SecondFactorAttestation};
use crate::utils::fetch::{simple_fetch, FetchOptions, HttpMethod};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CreateUserActionChallengeRequest {
    pub user_action_payload: String,
    pub user_action_http_method: HttpMethod,
    pub user_action_http_path: String,
    pub user_action_server_kind: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SignUserActionChallengeRequest {
    pub challenge_identifier: String,
    pub first_factor: FirstFactorAssertion,
    pub second_factor: Option<SecondFactorAssertion>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct UserActionResponse {
    pub user_action: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CreateUserLoginChallengeRequest {
    pub username: String,
    pub org_id: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct UserLoginResponse {
    pub token: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CreateUserRegistrationChallengeRequest {
    pub org_id: String,
    pub username: String,
    pub registration_code: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CreateUserRegistrationRequest {
    pub first_factor_credential: FirstFactorAttestation,
    pub second_factor_credential: Option<SecondFactorAttestation>,
    pub recovery_credential: Option<RecoveryFactorAttestation>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct UserRegistrationResponse {
    pub credential: CredentialInfo,
    pub user: UserInfo,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CredentialInfo {
    pub uuid: String,
    pub kind: String,
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct UserInfo {
    pub id: String,
    pub username: String,
    pub org_id: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CreateUserRecoveryRequest {
    pub recovery: RecoveryKeyAssertion,
    pub new_credentials: NewCredentials,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct NewCredentials {
    pub first_factor_credential: FirstFactorAttestation,
    pub second_factor_credential: Option<SecondFactorAttestation>,
    pub recovery_credential: Option<RecoveryFactorAttestation>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct BaseAuthApi;

impl BaseAuthApi {
    pub async fn create_user_action_challenge(
        request: CreateUserActionChallengeRequest,
        options: DfnsBaseApiOptions,
    ) -> Result<UserActionChallenge, DfnsError> {
        let fetch_options = FetchOptions {
            method: HttpMethod::POST,
            headers: None,
            body: Some(serde_json::to_value(&request)?),
            api_options: options,
        };

        simple_fetch("/auth/action/init", fetch_options).await
    }

    pub async fn sign_user_action_challenge(
        request: SignUserActionChallengeRequest,
        options: DfnsBaseApiOptions,
    ) -> Result<UserActionResponse, DfnsError> {
        let fetch_options = FetchOptions {
            method: HttpMethod::POST,
            headers: None,
            body: Some(serde_json::to_value(&request)?),
            api_options: options,
        };

        simple_fetch("/auth/action", fetch_options).await
    }

    pub async fn create_user_login_challenge(
        request: CreateUserLoginChallengeRequest,
        options: DfnsBaseApiOptions,
    ) -> Result<UserActionChallenge, DfnsError> {
        let fetch_options = FetchOptions {
            method: HttpMethod::POST,
            headers: None,
            body: Some(serde_json::to_value(&request)?),
            api_options: options,
        };

        simple_fetch("/auth/login/init", fetch_options).await
    }

    pub async fn create_user_login(
        request: SignUserActionChallengeRequest,
        options: DfnsBaseApiOptions,
    ) -> Result<UserLoginResponse, DfnsError> {
        let fetch_options = FetchOptions {
            method: HttpMethod::POST,
            headers: None,
            body: Some(serde_json::to_value(&request)?),
            api_options: options,
        };

        simple_fetch("/auth/login", fetch_options).await
    }

    pub async fn user_logout(options: DfnsBaseApiOptions) -> Result<(), DfnsError> {
        if options.auth_token.is_none() {
            return Err(DfnsError::new(
                400,
                "authToken is required".to_string(),
                None,
            ));
        }

        let fetch_options = FetchOptions {
            method: HttpMethod::PUT,
            headers: None,
            body: None,
            api_options: options,
        };

        simple_fetch("/auth/logout", fetch_options).await
    }

    pub async fn create_user_registration_challenge(
        request: CreateUserRegistrationChallengeRequest,
        options: DfnsBaseApiOptions,
    ) -> Result<CreateRegistrationChallengeResponse, DfnsError> {
        let fetch_options = FetchOptions {
            method: HttpMethod::POST,
            headers: None,
            body: Some(serde_json::to_value(&request)?),
            api_options: options,
        };

        simple_fetch("/auth/registration/init", fetch_options).await
    }

    pub async fn create_user_registration(
        request: CreateUserRegistrationRequest,
        options: DfnsBaseApiOptions,
    ) -> Result<UserRegistrationResponse, DfnsError> {
        let fetch_options = FetchOptions {
            method: HttpMethod::POST,
            headers: None,
            body: Some(serde_json::to_value(&request)?),
            api_options: options,
        };

        simple_fetch("/auth/registration", fetch_options).await
    }

    pub async fn create_user_recovery(
        request: CreateUserRecoveryRequest,
        options: DfnsBaseApiOptions,
    ) -> Result<UserRegistrationResponse, DfnsError> {
        let fetch_options = FetchOptions {
            method: HttpMethod::POST,
            headers: None,
            body: Some(serde_json::to_value(&request)?),
            api_options: options,
        };

        simple_fetch("/auth/recover/user", fetch_options).await
    }
}