new api work
This commit is contained in:
parent
4180bdd162
commit
a1ae83fa5f
|
@ -1,2 +1,3 @@
|
||||||
pub mod magic_link;
|
pub mod magic_link;
|
||||||
pub mod verify_magic_link;
|
pub mod verify_magic_link;
|
||||||
|
pub mod totp;
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::models::{MagicLink, SessionToken};
|
use crate::models::{MagicLink, SessionToken};
|
||||||
use crate::response::JsonAPIResponse;
|
use crate::response::JsonAPIResponse;
|
||||||
use crate::schema::session_tokens;
|
use crate::schema::{magic_links, session_tokens};
|
||||||
use crate::{randid, AppState};
|
use crate::{randid, AppState};
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::post;
|
use actix_web::post;
|
||||||
|
@ -8,6 +8,7 @@ use actix_web::web::{Data, Json};
|
||||||
use diesel::{ExpressionMethods, QueryDsl, SelectableHelper};
|
use diesel::{ExpressionMethods, QueryDsl, SelectableHelper};
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use diesel::result::OptionalExtension;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -34,20 +35,12 @@ pub async fn verify_link_req(
|
||||||
req: Json<VerifyLinkReq>,
|
req: Json<VerifyLinkReq>,
|
||||||
state: Data<AppState>,
|
state: Data<AppState>,
|
||||||
) -> JsonAPIResponse<VerifyLinkResp> {
|
) -> JsonAPIResponse<VerifyLinkResp> {
|
||||||
use crate::schema::magic_links::dsl::*;
|
|
||||||
|
|
||||||
let mut conn = handle_error!(state.pool.get().await);
|
let mut conn = handle_error!(state.pool.get().await);
|
||||||
|
|
||||||
let tokens = handle_error!(
|
let token = match handle_error!(magic_links::table.find(&req.magic_link_token).first::<MagicLink>(&mut conn).await.optional()) {
|
||||||
magic_links
|
Some(t) => t,
|
||||||
.filter(id.eq(&req.magic_link_token))
|
|
||||||
.select(MagicLink::as_select())
|
|
||||||
.load(&mut conn)
|
|
||||||
.await
|
|
||||||
);
|
|
||||||
|
|
||||||
let token = match tokens.get(0) {
|
|
||||||
Some(token) => token,
|
|
||||||
None => {
|
None => {
|
||||||
err!(
|
err!(
|
||||||
StatusCode::BAD_REQUEST,
|
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 {
|
let new_token = SessionToken {
|
||||||
id: randid!(token "sess"),
|
id: randid!(token "sess"),
|
||||||
|
|
|
@ -44,14 +44,7 @@ pub async fn create_totp_auth_req(
|
||||||
let auth_info = auth!(req_info, conn);
|
let auth_info = auth!(req_info, conn);
|
||||||
let session_token = enforce!(sess auth_info);
|
let session_token = enforce!(sess auth_info);
|
||||||
|
|
||||||
let users_vec = handle_error!(
|
let user = handle_error!(users::table.find(&session_token.user_id).first::<User>(&mut conn).await);
|
||||||
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 secret = Secret::generate_secret();
|
let secret = Secret::generate_secret();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue