From b2379f2a438ccdb595f2b27199b461d0eaa65693 Mon Sep 17 00:00:00 2001 From: Artemy Date: Tue, 26 Jul 2022 20:30:45 +0300 Subject: [PATCH] feat: signin api (OS-2) --- src/core.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/routes.rs | 9 ++++++++ src/types.rs | 8 +++++++ 4 files changed, 76 insertions(+) diff --git a/src/core.rs b/src/core.rs index b9f9167..1631cf6 100644 --- a/src/core.rs +++ b/src/core.rs @@ -37,6 +37,64 @@ impl Core { self.get_collection(&self.apps).await } + pub async fn signin(&self, name: &String, password: &String) -> serde_json::Value { + let response = self.users.find_one(doc! {"name":name}, None).await; + match response { + Ok(user) => { + match user { + Some(user) => { + let pass_hash = self.hash(name.clone() + &password); + if user.get_str("password").unwrap() == pass_hash { + let jwt_info = JwtInfo { + name: name.clone(), + role: user.get_str("role").unwrap().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) => { + json! ({ + "code":"ok", + "token":token + }) + } + Err(_) => { + json! ({ + "code":"err", + "msg":"Some problem with jwt generation" + }) + } + } + } else { + json! ({ + "code":"denied", + "msg":"Wrong password" + }) + } + } + None => { + json! ({ + "code":"err", + "msg":"User does not exist" + }) + } + } + } + Err(_) => { + json! ({ + "code":"err", + "msg":"User does not exist" + }) + } + } + } + pub async fn signup(&self, user: &Json) -> serde_json::Value { let jwt_info = JwtInfo { name: user.name.clone(), diff --git a/src/main.rs b/src/main.rs index 7e0ed25..5b8f22d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ async fn main() -> std::io::Result<()> { .wrap(cors) .service(routes::apps) .service(routes::signup) + .service(routes::signin), }) .bind(("0.0.0.0", port)) .expect("Can not bind to port") diff --git a/src/routes.rs b/src/routes.rs index 1c0de04..1b88266 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -5,6 +5,15 @@ use actix_web_grants::proc_macro::{has_any_permission, has_permissions}; pub async fn signup(app_data: web::Data, user: web::Json) -> impl Responder { response(app_data.core.signup(&user).await) } + +#[post("/signin")] +pub async fn signin( + app_data: web::Data, + user: web::Json, +) -> impl Responder { + response(app_data.core.signin(&user.name, &user.password).await) +} + #[get("/apps")] pub async fn apps(app_data: web::Data) -> impl Responder { HttpResponse::Ok().json(app_data.core.get_apps().await) diff --git a/src/types.rs b/src/types.rs index 98cfd40..55d8f6e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -15,6 +15,14 @@ pub struct User { pub password: String, pub email: String, } + +#[derive(Debug, Serialize, Deserialize)] +pub struct UserAuth { + pub name: String, + pub password: String, +} + +#[derive(Debug, Serialize, Deserialize)] pub struct JwtInfo { pub name: String, pub role: String,