2025-01-24 04:15:06 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2025-01-11 17:29:18 +03:00
|
|
|
use axum::{
|
2025-01-20 03:10:42 +03:00
|
|
|
extract::Path,
|
2025-01-11 17:29:18 +03:00
|
|
|
http::StatusCode,
|
|
|
|
response::IntoResponse,
|
|
|
|
routing::{delete, get, patch, post},
|
2025-01-24 04:15:06 +03:00
|
|
|
Extension, Json, Router,
|
2025-01-11 17:29:18 +03:00
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2025-01-23 21:34:54 +03:00
|
|
|
use crate::feature::{auth::OneTimePassword, login::Login, user::User, user_contact::UserContact};
|
|
|
|
|
2025-01-26 04:39:11 +03:00
|
|
|
use super::middleware::{user_and_token_then_insert, UserAndAuthorizationToken};
|
2025-01-24 04:15:06 +03:00
|
|
|
|
2025-01-23 21:34:54 +03:00
|
|
|
const CONTACT_EMAIL_DEFAULT_ID: i64 = 0;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct CreateOneTimePassword {
|
|
|
|
pub user_id: i64,
|
|
|
|
}
|
2025-01-11 17:29:18 +03:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct CreateLogin {
|
2025-01-24 04:15:06 +03:00
|
|
|
user_id: i64,
|
|
|
|
one_time_password: String,
|
2025-01-11 17:29:18 +03:00
|
|
|
}
|
|
|
|
|
2025-01-20 03:10:42 +03:00
|
|
|
pub fn route() -> Router {
|
2025-01-11 17:29:18 +03:00
|
|
|
Router::new()
|
2025-01-23 21:34:54 +03:00
|
|
|
.route("/one_time_password", post(create_one_time_password))
|
2025-01-11 17:29:18 +03:00
|
|
|
.route("/", post(create))
|
2025-01-23 21:34:54 +03:00
|
|
|
.route("/users/{user_id}/tokens/{token}", get(read))
|
2025-01-24 04:15:06 +03:00
|
|
|
.route(
|
|
|
|
"/",
|
2025-01-26 04:39:11 +03:00
|
|
|
patch(update).route_layer(axum::middleware::from_fn(user_and_token_then_insert)),
|
2025-01-24 04:15:06 +03:00
|
|
|
)
|
2025-01-23 21:34:54 +03:00
|
|
|
.route("/users/{user_id}/tokens/{token}", delete(delete_))
|
|
|
|
.route("/users/{user_id}", get(read_all_for_user))
|
|
|
|
.route("/users/{user_id}", delete(delete_all_for_user))
|
|
|
|
.route("/count/users/{user_id}", get(count_all_for_user))
|
|
|
|
}
|
|
|
|
async fn create_one_time_password(
|
|
|
|
Json(create_one_time_password): Json<CreateOneTimePassword>,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
//todo get user from middleware or something
|
|
|
|
let user = User::read(&create_one_time_password.user_id).await.unwrap();
|
2025-01-26 04:39:11 +03:00
|
|
|
match UserContact::read(&user, &CONTACT_EMAIL_DEFAULT_ID).await {
|
2025-01-23 21:34:54 +03:00
|
|
|
Ok(user_email) => match OneTimePassword::new(&user, &user_email.contact_value).await {
|
|
|
|
Ok(_) => (StatusCode::CREATED, Json(serde_json::json!(""))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
}
|
2025-01-11 17:29:18 +03:00
|
|
|
}
|
2025-01-20 03:10:42 +03:00
|
|
|
async fn create(Json(create_login): Json<CreateLogin>) -> impl IntoResponse {
|
2025-01-24 04:15:06 +03:00
|
|
|
let one_time_password = OneTimePassword {
|
|
|
|
user_id: create_login.user_id,
|
|
|
|
one_time_password: create_login.one_time_password,
|
|
|
|
};
|
|
|
|
|
|
|
|
match OneTimePassword::verify(&one_time_password).await {
|
|
|
|
true => match Login::create(&one_time_password.user_id).await {
|
2025-01-20 03:10:42 +03:00
|
|
|
Ok(login) => (StatusCode::CREATED, Json(serde_json::json!(login))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
},
|
2025-01-11 17:29:18 +03:00
|
|
|
false => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(
|
|
|
|
"One Time Password Authentication Failed".to_string()
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 03:10:42 +03:00
|
|
|
async fn read(Path((user_id, token)): Path<(i64, String)>) -> impl IntoResponse {
|
|
|
|
match Login::read(&user_id, &token).await {
|
2025-01-11 17:29:18 +03:00
|
|
|
Ok(login) => (StatusCode::OK, Json(serde_json::json!(login))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-25 23:50:45 +03:00
|
|
|
async fn update(
|
|
|
|
Extension(user_and_authorization_token): Extension<Arc<UserAndAuthorizationToken>>,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
match Login::update(
|
|
|
|
&user_and_authorization_token.user.user_id,
|
|
|
|
&user_and_authorization_token.authorization_token,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
2025-01-11 17:29:18 +03:00
|
|
|
Ok(login) => (StatusCode::ACCEPTED, Json(serde_json::json!(login))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 03:10:42 +03:00
|
|
|
async fn delete_(Path((user_id, token)): Path<(i64, String)>) -> impl IntoResponse {
|
|
|
|
match Login::delete(&user_id, &token).await {
|
2025-01-11 17:29:18 +03:00
|
|
|
Ok(login) => (StatusCode::NO_CONTENT, Json(serde_json::json!(login))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 03:10:42 +03:00
|
|
|
async fn read_all_for_user(Path(user_id): Path<i64>) -> impl IntoResponse {
|
|
|
|
match Login::read_all_for_user(&user_id).await {
|
2025-01-11 17:29:18 +03:00
|
|
|
Ok(logins) => (StatusCode::OK, Json(serde_json::json!(logins))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 03:10:42 +03:00
|
|
|
async fn delete_all_for_user(Path(user_id): Path<i64>) -> impl IntoResponse {
|
|
|
|
match Login::delete_all_for_user(&user_id).await {
|
2025-01-11 17:29:18 +03:00
|
|
|
Ok(logins) => (StatusCode::OK, Json(serde_json::json!(logins))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 03:10:42 +03:00
|
|
|
async fn count_all_for_user(Path(user_id): Path<i64>) -> impl IntoResponse {
|
|
|
|
match Login::count_all_for_user(&user_id).await {
|
2025-01-11 17:29:18 +03:00
|
|
|
Ok(login_count) => (StatusCode::OK, Json(serde_json::json!(login_count))),
|
|
|
|
Err(err_val) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(serde_json::json!(err_val.to_string())),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|