feat: added support for account verification

This commit is contained in:
antifallobst 2023-11-01 21:12:51 +01:00
parent f85081e449
commit 44095c4235
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
4 changed files with 61 additions and 4 deletions

View File

@ -18,7 +18,7 @@ pub enum AuthError {
WrongPassword, WrongPassword,
#[error("Failed to authenticate: account not found")] #[error("Failed to authenticate: account not found")]
NotFound, UsernameUnknown,
#[error("Failed to authenticate: account not verified")] #[error("Failed to authenticate: account not verified")]
NotVerified, NotVerified,
@ -53,3 +53,21 @@ pub enum RegisterError {
#[error("Failed to register: password does not meet the criteria")] #[error("Failed to register: password does not meet the criteria")]
PasswordInvalid, PasswordInvalid,
} }
#[derive(Debug, Error)]
pub enum VerifyAccountError {
#[error("Failed to verify account: the API complains about a malformed request")]
BadRequest,
#[error("Failed to verify account: unprocessable API response")]
BadResponse,
#[error("Failed to verify account: failed to send request")]
RequestSend(reqwest::Error),
#[error("Failed to verify account: blocked for security reasons")]
Forbidden,
#[error("Failed to verify account: unknown verification token")]
UnknownToken,
}

View File

@ -1,8 +1,8 @@
mod error; pub mod error;
pub mod request; pub mod request;
pub mod response; pub mod response;
use crate::error::{AuthError, RegisterError}; use crate::error::{AuthError, RegisterError, VerifyAccountError};
use anyhow::Result; use anyhow::Result;
use reqwest::StatusCode; use reqwest::StatusCode;
use std::fmt::Debug; use std::fmt::Debug;
@ -62,7 +62,7 @@ impl Session {
StatusCode::BAD_REQUEST => Err(AuthError::BadRequest), StatusCode::BAD_REQUEST => Err(AuthError::BadRequest),
StatusCode::UNAUTHORIZED => Err(AuthError::WrongPassword), StatusCode::UNAUTHORIZED => Err(AuthError::WrongPassword),
StatusCode::FORBIDDEN => Err(AuthError::Forbidden), StatusCode::FORBIDDEN => Err(AuthError::Forbidden),
StatusCode::NOT_FOUND => Err(AuthError::NotFound), StatusCode::NOT_FOUND => Err(AuthError::UsernameUnknown),
StatusCode::FAILED_DEPENDENCY => Err(AuthError::NotVerified), StatusCode::FAILED_DEPENDENCY => Err(AuthError::NotVerified),
_ => Err(AuthError::BadResponse), _ => Err(AuthError::BadResponse),
} }
@ -117,6 +117,35 @@ impl Session {
} }
} }
pub async fn verify_account(&mut self, token: String) -> Result<(), VerifyAccountError> {
let url = format!("{}/account/verify", &self.base_url);
let body = request::AccountVerify { token };
let request = self.client.post(&url).json(&body);
let response = request
.send()
.await
.map_err(|e| VerifyAccountError::RequestSend(e))?;
match response.status() {
StatusCode::OK => {
self.auth_token = Some(
response
.json::<response::AccountVerify>()
.await
.map_err(|_| VerifyAccountError::BadResponse)?
.token,
);
Ok(())
}
StatusCode::BAD_REQUEST => Err(VerifyAccountError::BadRequest),
StatusCode::FORBIDDEN => Err(VerifyAccountError::Forbidden),
StatusCode::NOT_FOUND => Err(VerifyAccountError::UnknownToken),
_ => Err(VerifyAccountError::BadResponse),
}
}
pub fn is_authenticated(&self) -> bool { pub fn is_authenticated(&self) -> bool {
self.auth_token.is_some() self.auth_token.is_some()
} }

View File

@ -12,3 +12,8 @@ pub struct AccountRegister {
pub password: String, pub password: String,
pub email: String, pub email: String,
} }
#[derive(Debug, Serialize, Clone)]
pub struct AccountVerify {
pub token: String,
}

View File

@ -19,3 +19,8 @@ pub enum AccountRegisterUnprocessable {
Email, Email,
Password, Password,
} }
#[derive(Debug, Deserialize, Clone)]
pub struct AccountVerify {
pub token: String,
}