feat: add endpoints for Verify MFA Login and Send MFA SMS
This commit is contained in:
parent
bf2608fb39
commit
cd637fbb6b
|
@ -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.
|
||||||
|
@ -32,7 +34,7 @@ impl Instance {
|
||||||
// instances' limits to pass them on as user_rate_limits later.
|
// instances' limits to pass them on as user_rate_limits later.
|
||||||
let mut user =
|
let mut user =
|
||||||
ChorusUser::shell(Arc::new(RwLock::new(self.clone())), "None".to_string()).await;
|
ChorusUser::shell(Arc::new(RwLock::new(self.clone())), "None".to_string()).await;
|
||||||
|
|
||||||
let login_result = chorus_request
|
let login_result = chorus_request
|
||||||
.deserialize_response::<LoginResult>(&mut user)
|
.deserialize_response::<LoginResult>(&mut user)
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue