2024-06-07 01:24:42 +00:00
|
|
|
pub mod db;
|
|
|
|
|
|
2024-06-06 18:21:12 +00:00
|
|
|
use axum::{
|
2024-06-07 17:59:35 +00:00
|
|
|
body::Body,
|
|
|
|
|
extract::Path,
|
2024-06-07 14:30:16 +00:00
|
|
|
Json,
|
2024-06-07 17:59:35 +00:00
|
|
|
response::Response,
|
|
|
|
|
Router,
|
|
|
|
|
routing::{get, post}
|
2024-06-06 18:21:12 +00:00
|
|
|
};
|
2024-06-07 01:24:42 +00:00
|
|
|
use axum_session_auth::AuthSession;
|
|
|
|
|
use axum_session_mongo::SessionMongoPool;
|
2024-06-07 17:59:35 +00:00
|
|
|
use chrono::Utc;
|
2024-06-07 14:30:16 +00:00
|
|
|
use crate::AppError;
|
2024-06-07 15:37:00 +00:00
|
|
|
use db::{get_users, User};
|
2024-06-07 17:59:35 +00:00
|
|
|
use http::StatusCode;
|
2024-06-07 14:30:16 +00:00
|
|
|
use mongodb::{
|
2024-06-07 15:37:00 +00:00
|
|
|
bson::{doc, oid::ObjectId, to_document},
|
|
|
|
|
Client
|
2024-06-07 14:30:16 +00:00
|
|
|
};
|
2024-06-07 17:59:35 +00:00
|
|
|
use rlg::macro_info_log;
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
type AuthenticationSession = AuthSession<User, ObjectId, SessionMongoPool, Client>;
|
2024-06-07 17:59:35 +00:00
|
|
|
type ApiResult = Result<Response, AppError>;
|
2024-06-06 19:43:32 +00:00
|
|
|
|
2024-06-06 18:21:12 +00:00
|
|
|
pub fn router() -> Router {
|
|
|
|
|
Router::new()
|
|
|
|
|
.nest("/:user_id", user_router())
|
2024-06-07 17:59:35 +00:00
|
|
|
.route("/sign-up",
|
2024-06-06 18:21:12 +00:00
|
|
|
post(post_sign_up)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn user_router() -> Router {
|
|
|
|
|
Router::new()
|
|
|
|
|
.route("/sign-in",
|
|
|
|
|
get(get_sign_in)
|
|
|
|
|
.post(post_sign_in)
|
|
|
|
|
)
|
2024-06-07 14:30:16 +00:00
|
|
|
.route("/sign-out",
|
|
|
|
|
post(post_sign_out))
|
2024-06-06 18:21:12 +00:00
|
|
|
.route("/backup",
|
|
|
|
|
post(post_backup)
|
|
|
|
|
)
|
|
|
|
|
.route("/restore",
|
|
|
|
|
get(get_restore)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
pub async fn get_sign_in(Path(user_id): Path<ObjectId>) -> ApiResult {
|
|
|
|
|
let id = format!("user_id: {}", user_id);
|
|
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Salt request initiated");
|
|
|
|
|
|
2024-06-07 15:37:00 +00:00
|
|
|
let db = get_users().await?;
|
|
|
|
|
let query = doc! { "_id" : user_id};
|
|
|
|
|
|
|
|
|
|
match db.find_one(query, None).await? {
|
|
|
|
|
Some(user) => {
|
2024-06-07 17:59:35 +00:00
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Salt request successful");
|
|
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::CREATED)
|
|
|
|
|
.body(Body::from(user.auth.salt.unwrap()))?)
|
2024-06-07 15:37:00 +00:00
|
|
|
},
|
|
|
|
|
None => {
|
2024-06-07 17:59:35 +00:00
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Salt Request unsuccessful: username does not exist");
|
|
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
|
.body(Body::from("User does not exist"))?)
|
2024-06-07 15:37:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-07 14:30:16 +00:00
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
pub async fn post_sign_in(Path(user_id): Path<ObjectId>, auth: AuthenticationSession, body: String) -> ApiResult {
|
|
|
|
|
let id = format!("user_id: {}", user_id);
|
|
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-in request initiated");
|
|
|
|
|
|
2024-06-07 15:37:00 +00:00
|
|
|
let db = get_users().await?;
|
|
|
|
|
let query = doc! { "_id": &user_id, "_auth._hash": body };
|
|
|
|
|
|
|
|
|
|
match db.find_one(query, None).await? {
|
2024-06-07 14:30:16 +00:00
|
|
|
Some(_user) => {
|
2024-06-07 17:59:35 +00:00
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-in request successful");
|
|
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
auth.login_user(user_id);
|
|
|
|
|
auth.remember_user(true);
|
2024-06-07 17:59:35 +00:00
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.body(Body::from(format!("{}", user_id)))?)
|
2024-06-07 14:30:16 +00:00
|
|
|
},
|
|
|
|
|
None => {
|
2024-06-07 17:59:35 +00:00
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-in request unsuccessful: incorrect credentials");
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::UNAUTHORIZED)
|
|
|
|
|
.body(Body::from("Username or password is incorrect"))?)
|
|
|
|
|
},
|
|
|
|
|
}
|
2024-06-07 14:30:16 +00:00
|
|
|
}
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
pub async fn post_sign_up(Json(body): Json<User>) -> ApiResult {
|
|
|
|
|
let id = "guest_user";
|
|
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-up request initiated");
|
|
|
|
|
|
2024-06-07 15:37:00 +00:00
|
|
|
let db = get_users().await?;
|
|
|
|
|
let query = to_document(&body)?;
|
2024-06-07 14:30:16 +00:00
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
match db.find_one(query.clone(), None).await? {
|
2024-06-07 15:37:00 +00:00
|
|
|
Some(_user) => {
|
2024-06-07 17:59:35 +00:00
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-up request unsuccessful: username already exists");
|
|
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_ACCEPTABLE)
|
|
|
|
|
.body(Body::from("Username is already taken"))?)
|
2024-06-07 14:30:16 +00:00
|
|
|
},
|
|
|
|
|
None => {
|
2024-06-07 17:59:35 +00:00
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-up request successful");
|
|
|
|
|
|
|
|
|
|
db.insert_one(body, None).await?;
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::CREATED)
|
|
|
|
|
.body(Body::from("Account created successfully"))?)
|
2024-06-07 14:30:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 01:24:42 +00:00
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
pub async fn post_sign_out(Path(user_id): Path<ObjectId>, auth: AuthenticationSession) -> ApiResult {
|
|
|
|
|
let id = format!("user_id: {}", user_id);
|
|
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-out request initiated");
|
|
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
match auth.is_authenticated() {
|
2024-06-07 17:59:35 +00:00
|
|
|
true => {
|
|
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-out request successful");
|
|
|
|
|
|
|
|
|
|
auth.logout_user();
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.body(Body::from("Sign out successful"))?)
|
|
|
|
|
},
|
2024-06-07 14:30:16 +00:00
|
|
|
false => {
|
2024-06-07 17:59:35 +00:00
|
|
|
macro_info_log!(&Utc::now().to_string(), &id, "Sign-out request unsuccessful: user was not logged in");
|
|
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_ACCEPTABLE)
|
|
|
|
|
.body(Body::from("No user is not logged in"))?)
|
2024-06-07 14:30:16 +00:00
|
|
|
},
|
|
|
|
|
}
|
2024-06-07 01:24:42 +00:00
|
|
|
}
|
2024-06-07 14:30:16 +00:00
|
|
|
|
|
|
|
|
pub async fn post_backup() {} //TODO: Backup
|
|
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
pub async fn get_restore() {} //TODO: restore
|