From c1124f09414e55a3aabf438bbe46d8fd19ac0316 Mon Sep 17 00:00:00 2001 From: Artemy Date: Tue, 26 Jul 2022 20:27:05 +0300 Subject: [PATCH] feat: signup api (OS-3) --- src/core.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/routes.rs | 16 ++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/src/core.rs b/src/core.rs index 7113c73..b9f9167 100644 --- a/src/core.rs +++ b/src/core.rs @@ -37,6 +37,57 @@ impl Core { self.get_collection(&self.apps).await } + pub async fn signup(&self, user: &Json) -> serde_json::Value { + let jwt_info = JwtInfo { + name: user.name.clone(), + role: "user".to_string(), + exp: Utc::now().timestamp() + 604800, //week + }; + + let token = encode( + &Header::default(), + &jwt_info, + &EncodingKey::from_secret(self.jwt_secret.as_ref()), + ); + + match token { + Ok(token) => { + let auth_info = doc! { + "name": &user.name, + "password": self.hash(user.name.clone() + &user.password), + "email": &user.email, + "role": "user".to_string(), + }; + let response = self.users.insert_one(&auth_info, None).await; + match response { + Ok(_) => { + json! ({ + "code":"ok", + "token":token + }) + } + Err(_) => { + json! ({ + "code":"err", + "msg":"User with this name already exist" + }) + } + } + } + Err(_) => { + json! ({ + "code":"err", + "msg":"Some problem with jwt generation" + }) + } + } + } + fn hash(&self, to_hash: String) -> String { + let mut hasher = Sha3_256::new(); + hasher.update(to_hash + &self.salt); + let hash = hasher.finalize(); + format!("{:x}", hash) + } async fn get_collection(&self, collection: &Collection) -> Vec { let options = FindOptions::builder().projection(doc! {"_id" : 0}).build(); let cursor = match collection.find(None, options).await { diff --git a/src/main.rs b/src/main.rs index 5a4c031..7e0ed25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,7 @@ async fn main() -> std::io::Result<()> { })) .wrap(cors) .service(routes::apps) + .service(routes::signup) }) .bind(("0.0.0.0", port)) .expect("Can not bind to port") diff --git a/src/routes.rs b/src/routes.rs index f4ec0bb..1c0de04 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,7 +1,23 @@ use crate::types::*; use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder}; use actix_web_grants::proc_macro::{has_any_permission, has_permissions}; +#[post("/signup")] +pub async fn signup(app_data: web::Data, user: web::Json) -> impl Responder { + response(app_data.core.signup(&user).await) +} #[get("/apps")] pub async fn apps(app_data: web::Data) -> impl Responder { HttpResponse::Ok().json(app_data.core.get_apps().await) } +fn response(result: serde_json::Value) -> impl Responder { + if result["code"] == "ok" { + HttpResponse::Ok().json(result) + } else if result["code"] == "ok_body" { + HttpResponse::Ok().json(&result["body"]) + } else if result["code"] == "denied" { + HttpResponse::Forbidden().json(result) + } else { + println!("{}", result["code"]); + HttpResponse::InternalServerError().json(result) + } +}