diff --git a/src/error.rs b/src/error.rs index 5b32cf3..3b776a7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,7 +18,7 @@ pub enum AuthError { WrongPassword, #[error("Failed to authenticate: account not found")] - NotFound, + UsernameUnknown, #[error("Failed to authenticate: account not verified")] NotVerified, @@ -53,3 +53,21 @@ pub enum RegisterError { #[error("Failed to register: password does not meet the criteria")] 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, +} diff --git a/src/lib.rs b/src/lib.rs index c11a2d4..bf2a836 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ -mod error; +pub mod error; pub mod request; pub mod response; -use crate::error::{AuthError, RegisterError}; +use crate::error::{AuthError, RegisterError, VerifyAccountError}; use anyhow::Result; use reqwest::StatusCode; use std::fmt::Debug; @@ -62,7 +62,7 @@ impl Session { StatusCode::BAD_REQUEST => Err(AuthError::BadRequest), StatusCode::UNAUTHORIZED => Err(AuthError::WrongPassword), StatusCode::FORBIDDEN => Err(AuthError::Forbidden), - StatusCode::NOT_FOUND => Err(AuthError::NotFound), + StatusCode::NOT_FOUND => Err(AuthError::UsernameUnknown), StatusCode::FAILED_DEPENDENCY => Err(AuthError::NotVerified), _ => 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::() + .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 { self.auth_token.is_some() } diff --git a/src/request.rs b/src/request.rs index 70ad992..b6ad028 100644 --- a/src/request.rs +++ b/src/request.rs @@ -12,3 +12,8 @@ pub struct AccountRegister { pub password: String, pub email: String, } + +#[derive(Debug, Serialize, Clone)] +pub struct AccountVerify { + pub token: String, +} diff --git a/src/response.rs b/src/response.rs index fb8235b..01723df 100644 --- a/src/response.rs +++ b/src/response.rs @@ -19,3 +19,8 @@ pub enum AccountRegisterUnprocessable { Email, Password, } + +#[derive(Debug, Deserialize, Clone)] +pub struct AccountVerify { + pub token: String, +}