feat: add endpoints for Verify MFA Login and Send MFA SMS

This commit is contained in:
xystrive 2024-07-25 13:17:59 +01:00
parent bf2608fb39
commit cd637fbb6b
No known key found for this signature in database
GPG Key ID: 5EC5BBA34DEE94B7
1 changed files with 59 additions and 2 deletions

View File

@ -11,7 +11,9 @@ use crate::errors::ChorusResult;
use crate::gateway::Gateway; use crate::gateway::Gateway;
use crate::instance::{ChorusUser, Instance}; use crate::instance::{ChorusUser, Instance};
use crate::ratelimiter::ChorusRequest; use crate::ratelimiter::ChorusRequest;
use crate::types::{GatewayIdentifyPayload, LimitType, LoginResult, LoginSchema, User}; use crate::types::{
AuthenticatorType, GatewayIdentifyPayload, LimitType, LoginResult, LoginSchema, SendMfaSmsResponse, SendMfaSmsSchema, User, VerifyMFALoginResponse, VerifyMFALoginSchema
};
impl Instance { impl Instance {
/// Logs into an existing account on the spacebar server. /// Logs into an existing account on the spacebar server.
@ -48,4 +50,59 @@ impl Instance {
Ok(user) Ok(user)
} }
/// Verifies a multi-factor authentication login
///
/// # Reference
/// See <https://docs.discord.sex/authentication#verify-mfa-login>
pub async fn verify_mfa_login(
&mut self,
authenticator: AuthenticatorType,
schema: VerifyMFALoginSchema,
) -> ChorusResult<ChorusUser> {
let endpoint_url = self.urls.api.clone() + &authenticator.to_string();
let chorus_request = ChorusRequest {
request: Client::new()
.post(endpoint_url)
.header("Content-Type", "application/json")
.json(&schema),
limit_type: LimitType::AuthLogin,
};
let mut user =
ChorusUser::shell(Arc::new(RwLock::new(self.clone())), "None".to_string()).await;
match chorus_request.deserialize_response::<VerifyMFALoginResponse>(&mut user).await? {
VerifyMFALoginResponse::Success { token, user_settings: _ } => user.set_token(token),
VerifyMFALoginResponse::UserSuspended { suspended_user_token } => return Err(crate::errors::ChorusError::SuspendUser { token: suspended_user_token }),
}
let mut identify = GatewayIdentifyPayload::common();
identify.token = user.token();
user.gateway.send_identify(identify).await;
Ok(user)
}
/// Sends a multi-factor authentication code to the user's phone number
///
/// # Reference
/// See <https://docs.discord.sex/authentication#send-mfa-sms>
pub async fn send_mfa_sms(&mut self, schema: SendMfaSmsSchema) -> ChorusResult<SendMfaSmsResponse> {
let endpoint_url = self.urls.api.clone() + "/auth/mfa/sms/send";
let chorus_request = ChorusRequest {
request: Client::new()
.post(endpoint_url)
.header("Content-Type", "application/json")
.json(&schema),
limit_type: LimitType::Ip
};
let mut chorus_user = ChorusUser::shell(Arc::new(RwLock::new(self.clone())), "None".to_string()).await;
let send_mfa_sms_response = chorus_request.deserialize_response::<SendMfaSmsResponse>(&mut chorus_user).await?;
Ok(send_mfa_sms_response)
}
} }