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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use reqwest::Client;
use crate::{
errors::ChorusResult,
instance::{ChorusUser, Token},
ratelimiter::ChorusRequest,
types::{
BeginWebAuthnAuthenticatorCreationReturn, EnableTotpMfaResponse, EnableTotpMfaSchema,
FinishWebAuthnAuthenticatorCreationReturn, FinishWebAuthnAuthenticatorCreationSchema,
GetBackupCodesSchema, LimitType, MfaAuthenticator, MfaBackupCode,
ModifyWebAuthnAuthenticatorSchema, SendBackupCodesChallengeReturn,
SendBackupCodesChallengeSchema, SmsMfaRouteSchema, Snowflake,
},
};
impl ChorusUser {
/// Enables TOTP based multi-factor authentication for the current user.
///
/// # Notes
/// Fires a [`UserUpdate`](crate::types::UserUpdate) gateway event.
///
/// Updates the authorization token for the current session.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#enable-totp-mfa>
pub async fn enable_totp_mfa(
&mut self,
schema: EnableTotpMfaSchema,
) -> ChorusResult<EnableTotpMfaResponse> {
let request = Client::new()
.post(format!(
"{}/users/@me/mfa/totp/enable",
self.belongs_to.read().unwrap().urls.api
))
.json(&schema);
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_headers_for(self);
let response: EnableTotpMfaResponse = chorus_request.send_and_deserialize_response(self).await?;
self.token = response.token.clone();
Ok(response)
}
/// Disables TOTP based multi-factor authentication for the current user.
///
/// Updates the authorization token for the current session and returns the new token.
///
/// # Notes
/// Requires MFA.
///
/// MFA cannot be disabled for administrators of guilds with published creator monetization listings.
///
/// Fires a [`UserUpdate`](crate::types::UserUpdate) gateway event.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#disable-totp-mfa>
pub async fn disable_totp_mfa(&mut self) -> ChorusResult<Token> {
let request = Client::new().post(format!(
"{}/users/@me/mfa/totp/disable",
self.belongs_to.read().unwrap().urls.api
));
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_maybe_mfa(&self.mfa_token)
.with_headers_for(self);
let response: Token = chorus_request.send_and_deserialize_response(self).await?;
self.token = response.token.clone();
Ok(response)
}
/// Enables SMS based multi-factor authentication for the current user.
///
/// Requires that TOTP based MFA is already enabled and the user has a verified phone number.
///
/// # Notes
/// Requires MFA.
///
/// Fires a [`UserUpdate`](crate::types::UserUpdate) gateway event.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#enable-sms-mfa>
pub async fn enable_sms_mfa(&mut self, schema: SmsMfaRouteSchema) -> ChorusResult<()> {
let request = Client::new()
.post(format!(
"{}/users/@me/mfa/sms/enable",
self.belongs_to.read().unwrap().urls.api
))
.json(&schema);
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_maybe_mfa(&self.mfa_token)
.with_headers_for(self);
chorus_request.send_and_handle_as_result(self).await
}
/// Disables SMS based multi-factor authentication for the current user.
///
/// # Notes
/// Requires MFA.
///
/// Fires a [`UserUpdate`](crate::types::UserUpdate) gateway event.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#disable-sms-mfa>
pub async fn disable_sms_mfa(&mut self, schema: SmsMfaRouteSchema) -> ChorusResult<()> {
let request = Client::new()
.post(format!(
"{}/users/@me/mfa/sms/disable",
self.belongs_to.read().unwrap().urls.api
))
.json(&schema);
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_maybe_mfa(&self.mfa_token)
.with_headers_for(self);
chorus_request.send_and_handle_as_result(self).await
}
/// Fetches a list of [WebAuthn](crate::types::MfaAuthenticatorType::WebAuthn)
/// [MfaAuthenticator]s for the current user.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#get-webauthn-authenticators>
pub async fn get_webauthn_authenticators(&mut self) -> ChorusResult<Vec<MfaAuthenticator>> {
let request = Client::new().get(format!(
"{}/users/@me/mfa/webauthn/credentials",
self.belongs_to.read().unwrap().urls.api
));
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_headers_for(self);
chorus_request.send_and_deserialize_response(self).await
}
/// Begins creation of a [WebAuthn](crate::types::MfaAuthenticatorType::WebAuthn)
/// [MfaAuthenticator] for the current user.
///
/// Returns [BeginWebAuthnAuthenticatorCreationReturn], which includes the MFA ticket
/// and a stringified JSON object of the public key credential challenge.
///
/// Once you have obtained the credential from the user, you should call
/// [ChorusUser::finish_webauthn_authenticator_creation]
///
/// # Notes
/// Requires MFA.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#create-webauthn-authenticator>
///
/// Note: for an easier to use API, we've split this one route into two methods
pub async fn begin_webauthn_authenticator_creation(
&mut self,
) -> ChorusResult<BeginWebAuthnAuthenticatorCreationReturn> {
let request = Client::new().post(format!(
"{}/users/@me/mfa/webauthn/credentials",
self.belongs_to.read().unwrap().urls.api
));
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_maybe_mfa(&self.mfa_token)
.with_headers_for(self);
chorus_request.send_and_deserialize_response(self).await
}
/// Finishes creation of a [WebAuthn](crate::types::MfaAuthenticatorType::WebAuthn)
/// [MfaAuthenticator] for the current user.
///
/// Returns [FinishWebAuthnAuthenticatorCreationReturn], which includes the created
/// authenticator and a list of backup codes.
///
/// To create a Webauthn authenticator from start to finish, call
/// [ChorusUser::begin_webauthn_authenticator_creation] first.
///
/// # Notes
/// Requires MFA.
///
/// Fires [AuthenticatorCreate](crate::types::AuthenticatorCreate) and
/// [UserUpdate](crate::types::UserUpdate) events.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#create-webauthn-authenticator>
///
/// Note: for an easier to use API, we've split this one route into two methods
pub async fn finish_webauthn_authenticator_creation(
&mut self,
schema: FinishWebAuthnAuthenticatorCreationSchema,
) -> ChorusResult<FinishWebAuthnAuthenticatorCreationReturn> {
let request = Client::new()
.post(format!(
"{}/users/@me/mfa/webauthn/credentials",
self.belongs_to.read().unwrap().urls.api
))
.json(&schema);
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_maybe_mfa(&self.mfa_token)
.with_headers_for(self);
chorus_request.send_and_deserialize_response(self).await
}
/// Modifies a [WebAuthn](crate::types::MfaAuthenticatorType::WebAuthn)
/// [MfaAuthenticator] (currently just renames) for the current user.
///
/// Returns the updated authenticator.
///
/// # Notes
/// Requires MFA.
///
/// Fires an [AuthenticatorUpdate](crate::types::AuthenticatorUpdate) event.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#modify-webauthn-authenticator>
pub async fn modify_webauthn_authenticator(
&mut self,
authenticator_id: Snowflake,
schema: ModifyWebAuthnAuthenticatorSchema,
) -> ChorusResult<MfaAuthenticator> {
let request = Client::new()
.patch(format!(
"{}/users/@me/mfa/webauthn/credentials/{}",
self.belongs_to.read().unwrap().urls.api,
authenticator_id
))
.json(&schema);
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_maybe_mfa(&self.mfa_token)
.with_headers_for(self);
chorus_request.send_and_deserialize_response(self).await
}
/// Deletes a [WebAuthn](crate::types::MfaAuthenticatorType::WebAuthn)
/// [MfaAuthenticator] for the current user.
///
/// # Notes
/// Requires MFA.
///
/// Fires [AuthenticatorDelete](crate::types::AuthenticatorDelete) and
/// [UserUpdate](crate::types::UserUpdate) events.
///
/// If this is the last remaining authenticator, this disables MFA for the current user.
///
/// MFA cannot be disabled for administrators of guilds with published creator monetization listings.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#delete-webauthn-authenticator>
pub async fn delete_webauthn_authenticator(
&mut self,
authenticator_id: Snowflake,
) -> ChorusResult<()> {
let request = Client::new().delete(format!(
"{}/users/@me/mfa/webauthn/credentials/{}",
self.belongs_to.read().unwrap().urls.api,
authenticator_id
));
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_maybe_mfa(&self.mfa_token)
.with_headers_for(self);
chorus_request.send_and_handle_as_result(self).await
}
/// Sends an email to the current user with a verification code
/// that allows them to view or regenerate their backup codes.
///
/// For the request to actually view the backup codes, see [ChorusUser::get_backup_codes].
///
/// # Notes
/// The two returned nonces can only be used once and expire after 30 minutes.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#send-backup-codes-challenge>
pub async fn send_backup_codes_challenge(
&mut self,
schema: SendBackupCodesChallengeSchema,
) -> ChorusResult<SendBackupCodesChallengeReturn> {
let request = Client::new()
.post(format!(
"{}/auth/verify/view-backup-codes-challenge",
self.belongs_to.read().unwrap().urls.api,
))
.json(&schema);
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_headers_for(self);
chorus_request.send_and_deserialize_response(self).await
}
/// Fetches the user's [MfaBackupCode]s.
///
/// Before using this endpoint, you must use [ChorusUser::send_backup_codes_challenge] and
/// obtain a key from the user's email.
///
/// # Notes
/// The nonces in the schema are returned by the [ChorusUser::send_backup_codes_challenge]
/// endpoint.
///
/// If regenerate is set to true, the nonce in the schema must be the regenerate_nonce.
/// Otherwise it should be the view_nonce.
///
/// Each nonce can only be used once and expires after 30 minutes.
///
/// # Reference
/// See <https://docs.discord.food/resources/user#get-backup-codes>
pub async fn get_backup_codes(
&mut self,
schema: GetBackupCodesSchema,
) -> ChorusResult<Vec<MfaBackupCode>> {
let request = Client::new()
.post(format!(
"{}/users/@me/mfa/codes-verification",
self.belongs_to.read().unwrap().urls.api,
))
.json(&schema);
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
}
.with_headers_for(self);
chorus_request.send_and_deserialize_response(self).await
}
}