From 67d64ec4b368c48188c746f2dba2967ec4615fe5 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Mon, 24 Jun 2024 14:54:54 +0100 Subject: feat: add user account verification (#2190) * add verified column to users table * add database functions to check if verified, or to verify * getting there * verification check * use base64 urlsafe no pad * add verification client * clippy * correct docs * fix integration tests --- crates/atuin-client/src/api_client.rs | 36 +++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'crates/atuin-client/src') diff --git a/crates/atuin-client/src/api_client.rs b/crates/atuin-client/src/api_client.rs index 60f2d300..53f24424 100644 --- a/crates/atuin-client/src/api_client.rs +++ b/crates/atuin-client/src/api_client.rs @@ -11,8 +11,9 @@ use reqwest::{ use atuin_common::{ api::{ AddHistoryRequest, ChangePasswordRequest, CountResponse, DeleteHistoryRequest, - ErrorResponse, LoginRequest, LoginResponse, MeResponse, RegisterResponse, StatusResponse, - SyncHistoryResponse, + ErrorResponse, LoginRequest, LoginResponse, MeResponse, RegisterResponse, + SendVerificationResponse, StatusResponse, SyncHistoryResponse, VerificationTokenRequest, + VerificationTokenResponse, }, record::RecordStatus, }; @@ -403,4 +404,35 @@ impl<'a> Client<'a> { bail!("Unknown error"); } } + + // Either request a verification email if token is null, or validate a token + pub async fn verify(&self, token: Option) -> Result<(bool, bool)> { + // could dedupe this a bit, but it's simple at the moment + let (email_sent, verified) = if let Some(token) = token { + let url = format!("{}/api/v0/account/verify", self.sync_addr); + let url = Url::parse(url.as_str())?; + + let resp = self + .client + .post(url) + .json(&VerificationTokenRequest { token }) + .send() + .await?; + let resp = handle_resp_error(resp).await?; + let resp = resp.json::().await?; + + (false, resp.verified) + } else { + let url = format!("{}/api/v0/account/send-verification", self.sync_addr); + let url = Url::parse(url.as_str())?; + + let resp = self.client.post(url).send().await?; + let resp = handle_resp_error(resp).await?; + let resp = resp.json::().await?; + + (resp.email_sent, resp.verified) + }; + + Ok((email_sent, verified)) + } } -- cgit v1.3.1