2024-06-07 01:24:42 +00:00
|
|
|
pub mod db;
|
|
|
|
|
|
2024-06-06 18:21:12 +00:00
|
|
|
use axum::{
|
2024-06-07 14:30:16 +00:00
|
|
|
extract::Path,
|
|
|
|
|
routing::{get, post},
|
|
|
|
|
Json,
|
|
|
|
|
Router
|
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 14:30:16 +00:00
|
|
|
use crate::AppError;
|
2024-06-07 15:37:00 +00:00
|
|
|
use db::{get_users, User};
|
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-06 18:21:12 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
type AuthenticationSession = AuthSession<User, ObjectId, SessionMongoPool, Client>;
|
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())
|
|
|
|
|
.route("/",
|
|
|
|
|
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 15:37:00 +00:00
|
|
|
pub async fn get_sign_in(Path(user_id): Path<ObjectId>) -> Result<String, AppError> {
|
|
|
|
|
let db = get_users().await?;
|
|
|
|
|
let query = doc! { "_id" : user_id};
|
|
|
|
|
|
|
|
|
|
match db.find_one(query, None).await? {
|
|
|
|
|
Some(user) => {
|
|
|
|
|
//TODO: Return User salt
|
|
|
|
|
Ok(String::new())
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
//TODO: Return User does not exist
|
|
|
|
|
Ok(String::new())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-07 14:30:16 +00:00
|
|
|
|
|
|
|
|
pub async fn post_sign_in(Path(user_id): Path<ObjectId>, auth: AuthenticationSession, body: String) -> Result<String, AppError> {
|
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) => {
|
|
|
|
|
auth.login_user(user_id);
|
|
|
|
|
auth.remember_user(true);
|
2024-06-07 15:37:00 +00:00
|
|
|
//TODO: Return API Key or Auth Key and User ID
|
2024-06-07 14:30:16 +00:00
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
//TODO: Return or Redirect Unauthorized
|
|
|
|
|
},
|
|
|
|
|
};
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
Ok(String::new())
|
|
|
|
|
}
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
pub async fn post_sign_up(Json(body): Json<User>) -> Result<(), AppError> {
|
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 15:37:00 +00:00
|
|
|
match db.find_one(query, None).await? {
|
|
|
|
|
Some(_user) => {
|
2024-06-07 14:30:16 +00:00
|
|
|
//TODO: Return or Redirect User Exists
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
//TODO: Reutrn UsedId
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-07 01:24:42 +00:00
|
|
|
|
2024-06-07 14:30:16 +00:00
|
|
|
pub async fn post_sign_out(auth: AuthenticationSession) {
|
|
|
|
|
match auth.is_authenticated() {
|
|
|
|
|
true => auth.logout_user(),
|
|
|
|
|
false => {
|
|
|
|
|
//TODO: Redirect Not Logged in
|
|
|
|
|
},
|
|
|
|
|
}
|
2024-06-07 01:24:42 +00:00
|
|
|
}
|
2024-06-07 14:30:16 +00:00
|
|
|
|
|
|
|
|
pub async fn post_backup() {} //TODO: Backup
|
|
|
|
|
|
|
|
|
|
pub async fn get_restore() {} //TODO: Restore
|