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,
|
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-08 15:36:06 +00:00
|
|
|
use crate::ApiResult;
|
2024-06-07 15:37:00 +00:00
|
|
|
use db::{get_users, User};
|
2024-06-07 21:29:49 +00:00
|
|
|
use http::{header::HeaderMap, StatusCode};
|
|
|
|
|
use mongodb::bson::{doc, oid::ObjectId};
|
2024-06-06 18:21:12 +00:00
|
|
|
|
2024-06-06 19:43:32 +00:00
|
|
|
|
2024-06-06 18:21:12 +00:00
|
|
|
pub fn router() -> Router {
|
|
|
|
|
Router::new()
|
|
|
|
|
.route("/sign-in",
|
|
|
|
|
get(get_sign_in)
|
|
|
|
|
.post(post_sign_in)
|
|
|
|
|
)
|
2024-06-07 21:29:49 +00:00
|
|
|
.route("/sign-up",
|
|
|
|
|
post(post_sign_up))
|
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 21:29:49 +00:00
|
|
|
pub async fn get_sign_in(Json(body): Json<User>) -> ApiResult {
|
2024-06-07 15:37:00 +00:00
|
|
|
let db = get_users().await?;
|
2024-06-07 21:29:49 +00:00
|
|
|
let query = doc! {
|
|
|
|
|
"$expr": { "$eq": ["$username", body.username] }
|
|
|
|
|
};
|
2024-06-07 15:37:00 +00:00
|
|
|
|
|
|
|
|
match db.find_one(query, None).await? {
|
2024-06-07 21:29:49 +00:00
|
|
|
Some(user) => {
|
2024-06-07 17:59:35 +00:00
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::CREATED)
|
2024-06-07 21:29:49 +00:00
|
|
|
.body(Body::from(user.auth.unwrap().salt.unwrap()))?)
|
2024-06-07 15:37:00 +00:00
|
|
|
},
|
|
|
|
|
None => {
|
2024-06-07 17:59:35 +00:00
|
|
|
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 21:29:49 +00:00
|
|
|
pub async fn post_sign_in(Json(body): Json<User>) -> ApiResult {
|
2024-06-07 15:37:00 +00:00
|
|
|
let db = get_users().await?;
|
2024-06-07 21:29:49 +00:00
|
|
|
let api = ObjectId::new();
|
|
|
|
|
let query = doc! {
|
|
|
|
|
"$expr": {
|
|
|
|
|
"$and": [
|
|
|
|
|
{ "$eq": ["$username", body.username]},
|
|
|
|
|
{ "$eq": ["$_auth._hash", body.auth.unwrap_or_default().hash]}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let update = doc! {
|
|
|
|
|
"$set": {
|
|
|
|
|
"_auth._api": api
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-06-07 15:37:00 +00:00
|
|
|
|
2024-06-07 21:29:49 +00:00
|
|
|
match db.find_one(query.clone(), None).await? {
|
2024-06-07 14:30:16 +00:00
|
|
|
Some(_user) => {
|
2024-06-07 21:29:49 +00:00
|
|
|
db.update_one(query, update, None).await?;
|
2024-06-07 17:59:35 +00:00
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
2024-06-07 21:29:49 +00:00
|
|
|
.body(Body::from(api.to_string()))?)
|
2024-06-07 14:30:16 +00:00
|
|
|
},
|
|
|
|
|
None => {
|
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 {
|
2024-06-07 15:37:00 +00:00
|
|
|
let db = get_users().await?;
|
2024-06-07 21:29:49 +00:00
|
|
|
let auth = body.clone().auth.unwrap_or_default();
|
|
|
|
|
let query = doc! {
|
|
|
|
|
"$expr": {
|
|
|
|
|
"$and": [
|
|
|
|
|
{ "$eq": ["$username", &body.username] },
|
|
|
|
|
{ "$eq": ["$_auth._hash", &auth.hash]},
|
|
|
|
|
{ "$eq": ["$_auth._salt", &auth.salt]}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-06-07 14:30:16 +00:00
|
|
|
|
2024-06-07 21:29:49 +00:00
|
|
|
match db.find_one(query, None).await? {
|
2024-06-07 15:37:00 +00:00
|
|
|
Some(_user) => {
|
2024-06-07 17:59:35 +00:00
|
|
|
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
|
|
|
db.insert_one(body, None).await?;
|
2024-06-07 21:29:49 +00:00
|
|
|
|
2024-06-07 17:59:35 +00:00
|
|
|
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 21:29:49 +00:00
|
|
|
pub async fn post_sign_out(headers: HeaderMap) -> ApiResult {
|
|
|
|
|
let db = get_users().await?;
|
|
|
|
|
let api = headers["api_key"].to_str()?;
|
|
|
|
|
|
|
|
|
|
let query = doc! {
|
|
|
|
|
"$expr": { "$eq": ["$_auth._api", ObjectId::parse_str(api)?] }
|
|
|
|
|
};
|
|
|
|
|
let update = doc! {
|
|
|
|
|
"$unset": {
|
|
|
|
|
"_auth._api": ObjectId::new()
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-06-07 17:59:35 +00:00
|
|
|
|
2024-06-07 21:29:49 +00:00
|
|
|
match db.find_one(query.clone(), None).await? {
|
|
|
|
|
Some(_user) => {
|
|
|
|
|
db.update_one(query, update, None).await?;
|
2024-06-07 17:59:35 +00:00
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.body(Body::from("Sign out successful"))?)
|
|
|
|
|
},
|
2024-06-07 21:29:49 +00:00
|
|
|
None => {
|
2024-06-07 17:59:35 +00:00
|
|
|
Ok(Response::builder()
|
|
|
|
|
.status(StatusCode::NOT_ACCEPTABLE)
|
2024-06-07 21:29:49 +00:00
|
|
|
.body(Body::from("User does not exist"))?)
|
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
|