diff --git a/src/xrpc/com_atproto_server.rs b/src/xrpc/com_atproto_server.rs index 552eddf..de6a898 100644 --- a/src/xrpc/com_atproto_server.rs +++ b/src/xrpc/com_atproto_server.rs @@ -10,6 +10,8 @@ use axum::response::{IntoResponse, Response}; use axum::{Extension, Json, debug_handler, extract, extract::Request}; use serde::{Deserialize, Serialize}; use serde_json; +use sqlx::Error; +use sqlx::sqlite::SqliteQueryResult; use tracing::log; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -155,13 +157,19 @@ pub async fn update_email( // Email update asked for if email_auth_update { let email = payload.email.clone(); - let email_confirmed = sqlx::query_as::<_, (String,)>( + let email_confirmed = match sqlx::query_as::<_, (String,)>( "SELECT did FROM account WHERE emailConfirmedAt IS NOT NULL AND email = ?", ) .bind(&email) .fetch_optional(&state.account_pool) .await - .map_err(|_| StatusCode::BAD_REQUEST)?; + { + Ok(row) => row, + Err(err) => { + log::error!("Error checking if email is confirmed: {err}"); + return Err(StatusCode::BAD_REQUEST); + } + }; //Since the email is already confirmed we can enable 2fa return match email_confirmed { @@ -184,23 +192,35 @@ pub async fn update_email( if !email_auth_update && !email_auth_not_set { //User wants auth turned off and has a token if let Some(token) = &payload.token { - let token_found = sqlx::query_as::<_, (String,)>( + let token_found = match sqlx::query_as::<_, (String,)>( "SELECT token FROM email_token WHERE token = ? AND did = ? AND purpose = 'update_email'", ) .bind(token) .bind(&did.0) .fetch_optional(&state.account_pool) - .await - .map_err(|_| StatusCode::BAD_REQUEST)?; + .await{ + Ok(token) => token, + Err(err) => { + log::error!("Error checking if token is valid: {err}"); + return Err(StatusCode::BAD_REQUEST); + } + }; return if token_found.is_some() { - let _ = sqlx::query( + //TODO I think there may be a bug here and need to do some retry logic + // First try was erroring, seconds was allowing + match sqlx::query( "INSERT INTO two_factor_accounts (did, required) VALUES (?, 0) ON CONFLICT(did) DO UPDATE SET required = 0", ) .bind(&did.0) .execute(&state.pds_gatekeeper_pool) - .await - .map_err(|_| StatusCode::BAD_REQUEST)?; + .await { + Ok(_) => {} + Err(err) => { + log::error!("Error updating email auth: {err}"); + return Err(StatusCode::BAD_REQUEST); + } + } Ok(StatusCode::OK.into_response()) } else { @@ -275,9 +295,7 @@ pub async fn create_account( ); // Rewrite the URI to point at the upstream PDS; keep headers, method, and body intact - *req.uri_mut() = uri - .parse() - .map_err(|_| StatusCode::BAD_REQUEST)?; + *req.uri_mut() = uri.parse().map_err(|_| StatusCode::BAD_REQUEST)?; let proxied = state .reverse_proxy_client