1use crate::api::auth::types::CreateRegistrationChallengeResponse;
4use crate::error::DfnsError;
5use crate::models::generic::DfnsBaseApiOptions;
6use crate::signer::{
7 FirstFactorAssertion, RecoveryKeyAssertion, SecondFactorAssertion, UserActionChallenge,
8};
9use crate::store::{FirstFactorAttestation, RecoveryFactorAttestation, SecondFactorAttestation};
10use crate::utils::fetch::{simple_fetch, FetchOptions, HttpMethod};
11use serde::{Deserialize, Serialize};
12
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14#[serde(rename_all = "kebab-case")]
15pub struct CreateUserActionChallengeRequest {
16 pub user_action_payload: String,
17 pub user_action_http_method: HttpMethod,
18 pub user_action_http_path: String,
19 pub user_action_server_kind: String,
20}
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23#[serde(rename_all = "kebab-case")]
24pub struct SignUserActionChallengeRequest {
25 pub challenge_identifier: String,
26 pub first_factor: FirstFactorAssertion,
27 pub second_factor: Option<SecondFactorAssertion>,
28}
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31#[serde(rename_all = "kebab-case")]
32pub struct UserActionResponse {
33 pub user_action: String,
34}
35
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37#[serde(rename_all = "kebab-case")]
38pub struct CreateUserLoginChallengeRequest {
39 pub username: String,
40 pub org_id: String,
41}
42
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44#[serde(rename_all = "kebab-case")]
45pub struct UserLoginResponse {
46 pub token: String,
47}
48
49#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
50#[serde(rename_all = "kebab-case")]
51pub struct CreateUserRegistrationChallengeRequest {
52 pub org_id: String,
53 pub username: String,
54 pub registration_code: String,
55}
56
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58#[serde(rename_all = "kebab-case")]
59pub struct CreateUserRegistrationRequest {
60 pub first_factor_credential: FirstFactorAttestation,
61 pub second_factor_credential: Option<SecondFactorAttestation>,
62 pub recovery_credential: Option<RecoveryFactorAttestation>,
63}
64
65#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
66#[serde(rename_all = "kebab-case")]
67pub struct UserRegistrationResponse {
68 pub credential: CredentialInfo,
69 pub user: UserInfo,
70}
71
72#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
73#[serde(rename_all = "kebab-case")]
74pub struct CredentialInfo {
75 pub uuid: String,
76 pub kind: String,
77 pub name: String,
78}
79
80#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
81#[serde(rename_all = "kebab-case")]
82pub struct UserInfo {
83 pub id: String,
84 pub username: String,
85 pub org_id: String,
86}
87
88#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
89#[serde(rename_all = "kebab-case")]
90pub struct CreateUserRecoveryRequest {
91 pub recovery: RecoveryKeyAssertion,
92 pub new_credentials: NewCredentials,
93}
94
95#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
96#[serde(rename_all = "kebab-case")]
97pub struct NewCredentials {
98 pub first_factor_credential: FirstFactorAttestation,
99 pub second_factor_credential: Option<SecondFactorAttestation>,
100 pub recovery_credential: Option<RecoveryFactorAttestation>,
101}
102
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104#[serde(rename_all = "kebab-case")]
105pub struct BaseAuthApi;
106
107impl BaseAuthApi {
108 pub async fn create_user_action_challenge(
109 request: CreateUserActionChallengeRequest,
110 options: DfnsBaseApiOptions,
111 ) -> Result<UserActionChallenge, DfnsError> {
112 let fetch_options = FetchOptions {
113 method: HttpMethod::POST,
114 headers: None,
115 body: Some(serde_json::to_value(&request)?),
116 api_options: options,
117 };
118
119 simple_fetch("/auth/action/init", fetch_options).await
120 }
121
122 pub async fn sign_user_action_challenge(
123 request: SignUserActionChallengeRequest,
124 options: DfnsBaseApiOptions,
125 ) -> Result<UserActionResponse, DfnsError> {
126 let fetch_options = FetchOptions {
127 method: HttpMethod::POST,
128 headers: None,
129 body: Some(serde_json::to_value(&request)?),
130 api_options: options,
131 };
132
133 simple_fetch("/auth/action", fetch_options).await
134 }
135
136 pub async fn create_user_login_challenge(
137 request: CreateUserLoginChallengeRequest,
138 options: DfnsBaseApiOptions,
139 ) -> Result<UserActionChallenge, DfnsError> {
140 let fetch_options = FetchOptions {
141 method: HttpMethod::POST,
142 headers: None,
143 body: Some(serde_json::to_value(&request)?),
144 api_options: options,
145 };
146
147 simple_fetch("/auth/login/init", fetch_options).await
148 }
149
150 pub async fn create_user_login(
151 request: SignUserActionChallengeRequest,
152 options: DfnsBaseApiOptions,
153 ) -> Result<UserLoginResponse, DfnsError> {
154 let fetch_options = FetchOptions {
155 method: HttpMethod::POST,
156 headers: None,
157 body: Some(serde_json::to_value(&request)?),
158 api_options: options,
159 };
160
161 simple_fetch("/auth/login", fetch_options).await
162 }
163
164 pub async fn user_logout(options: DfnsBaseApiOptions) -> Result<(), DfnsError> {
165 if options.auth_token.is_none() {
166 return Err(DfnsError::new(
167 400,
168 "authToken is required".to_string(),
169 None,
170 ));
171 }
172
173 let fetch_options = FetchOptions {
174 method: HttpMethod::PUT,
175 headers: None,
176 body: None,
177 api_options: options,
178 };
179
180 simple_fetch("/auth/logout", fetch_options).await
181 }
182
183 pub async fn create_user_registration_challenge(
184 request: CreateUserRegistrationChallengeRequest,
185 options: DfnsBaseApiOptions,
186 ) -> Result<CreateRegistrationChallengeResponse, DfnsError> {
187 let fetch_options = FetchOptions {
188 method: HttpMethod::POST,
189 headers: None,
190 body: Some(serde_json::to_value(&request)?),
191 api_options: options,
192 };
193
194 simple_fetch("/auth/registration/init", fetch_options).await
195 }
196
197 pub async fn create_user_registration(
198 request: CreateUserRegistrationRequest,
199 options: DfnsBaseApiOptions,
200 ) -> Result<UserRegistrationResponse, DfnsError> {
201 let fetch_options = FetchOptions {
202 method: HttpMethod::POST,
203 headers: None,
204 body: Some(serde_json::to_value(&request)?),
205 api_options: options,
206 };
207
208 simple_fetch("/auth/registration", fetch_options).await
209 }
210
211 pub async fn create_user_recovery(
212 request: CreateUserRecoveryRequest,
213 options: DfnsBaseApiOptions,
214 ) -> Result<UserRegistrationResponse, DfnsError> {
215 let fetch_options = FetchOptions {
216 method: HttpMethod::POST,
217 headers: None,
218 body: Some(serde_json::to_value(&request)?),
219 api_options: options,
220 };
221
222 simple_fetch("/auth/recover/user", fetch_options).await
223 }
224}