From a1ae83fa5fcca7c1802d11dd82812ebc1b8390bc Mon Sep 17 00:00:00 2001 From: core Date: Tue, 21 Nov 2023 22:54:21 -0500 Subject: [PATCH] new api work --- trifid-api/src/routes/v1/auth/mod.rs | 1 + trifid-api/src/routes/v1/auth/totp.rs | 21 +++++++++++++++++++ .../src/routes/v1/auth/verify_magic_link.rs | 19 ++++++----------- .../src/routes/v1/totp_authenticators.rs | 9 +------- 4 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 trifid-api/src/routes/v1/auth/totp.rs diff --git a/trifid-api/src/routes/v1/auth/mod.rs b/trifid-api/src/routes/v1/auth/mod.rs index 57e96d5..947d083 100644 --- a/trifid-api/src/routes/v1/auth/mod.rs +++ b/trifid-api/src/routes/v1/auth/mod.rs @@ -1,2 +1,3 @@ pub mod magic_link; pub mod verify_magic_link; +pub mod totp; diff --git a/trifid-api/src/routes/v1/auth/totp.rs b/trifid-api/src/routes/v1/auth/totp.rs new file mode 100644 index 0000000..dcd75db --- /dev/null +++ b/trifid-api/src/routes/v1/auth/totp.rs @@ -0,0 +1,21 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize)] +pub struct TotpAuthReq { + pub code: String +} + +#[derive(Serialize)] +pub struct TotpAuthRespMeta {} + +#[derive(Serialize)] +pub struct TotpAuthRespData { + #[serde(rename = "authToken")] + pub auth_token: String, +} + +#[derive(Serialize)] +pub struct TotpAuthResp { + pub data: TotpAuthRespData, + pub metadata: TotpAuthRespMeta, +} \ No newline at end of file diff --git a/trifid-api/src/routes/v1/auth/verify_magic_link.rs b/trifid-api/src/routes/v1/auth/verify_magic_link.rs index cf6f2aa..e09da20 100644 --- a/trifid-api/src/routes/v1/auth/verify_magic_link.rs +++ b/trifid-api/src/routes/v1/auth/verify_magic_link.rs @@ -1,6 +1,6 @@ use crate::models::{MagicLink, SessionToken}; use crate::response::JsonAPIResponse; -use crate::schema::session_tokens; +use crate::schema::{magic_links, session_tokens}; use crate::{randid, AppState}; use actix_web::http::StatusCode; use actix_web::post; @@ -8,6 +8,7 @@ use actix_web::web::{Data, Json}; use diesel::{ExpressionMethods, QueryDsl, SelectableHelper}; use diesel_async::RunQueryDsl; use serde::{Deserialize, Serialize}; +use diesel::result::OptionalExtension; use std::time::{Duration, SystemTime}; #[derive(Deserialize)] @@ -34,20 +35,12 @@ pub async fn verify_link_req( req: Json, state: Data, ) -> JsonAPIResponse { - use crate::schema::magic_links::dsl::*; + let mut conn = handle_error!(state.pool.get().await); - let tokens = handle_error!( - magic_links - .filter(id.eq(&req.magic_link_token)) - .select(MagicLink::as_select()) - .load(&mut conn) - .await - ); - - let token = match tokens.get(0) { - Some(token) => token, + let token = match handle_error!(magic_links::table.find(&req.magic_link_token).first::(&mut conn).await.optional()) { + Some(t) => t, None => { err!( StatusCode::BAD_REQUEST, @@ -71,7 +64,7 @@ pub async fn verify_link_req( ) } - handle_error!(diesel::delete(token).execute(&mut conn).await); + handle_error!(diesel::delete(&token).execute(&mut conn).await); let new_token = SessionToken { id: randid!(token "sess"), diff --git a/trifid-api/src/routes/v1/totp_authenticators.rs b/trifid-api/src/routes/v1/totp_authenticators.rs index 78ad6bf..d692092 100644 --- a/trifid-api/src/routes/v1/totp_authenticators.rs +++ b/trifid-api/src/routes/v1/totp_authenticators.rs @@ -44,14 +44,7 @@ pub async fn create_totp_auth_req( let auth_info = auth!(req_info, conn); let session_token = enforce!(sess auth_info); - let users_vec = handle_error!( - users::dsl::users - .filter(users::dsl::id.eq(&session_token.user_id)) - .select(User::as_select()) - .load(&mut conn) - .await - ); - let user = handle_error!(users_vec.get(0).ok_or("impossible relation")); + let user = handle_error!(users::table.find(&session_token.user_id).first::(&mut conn).await); let secret = Secret::generate_secret();