-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from indnb/feature/add-login-admin
Feature/add login admin
- Loading branch information
Showing
15 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod create_jwt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
use crate::models::admin::admin_jwt; | ||
use log::info; | ||
use rocket::get; | ||
|
||
#[allow(dead_code)] | ||
#[get("/admin/get")] | ||
pub async fn get(claims: admin_jwt::AdminJwt) { | ||
info!("Welcome, user with ID: {}", claims.admin_name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,40 @@ | ||
use crate::dto::request::admin::login_admin::LoginAdminData; | ||
use crate::error::api_error::ApiError; | ||
use crate::models::admin::admin_jwt; | ||
use crate::utils::env_configuration::EnvConfiguration; | ||
use crate::utils::prelude_api::*; | ||
use crate::utils::security; | ||
use chrono::{Duration, Utc}; | ||
use rocket::post; | ||
|
||
#[post("/admin/login", data = "<data>")] | ||
pub async fn login(data: Json<LoginAdminData>) -> Result<String, ApiError> { | ||
let data = data.into_inner(); | ||
let LoginAdminData { | ||
admin_name, | ||
admin_password, | ||
} = data; | ||
let password_env = EnvConfiguration::get().admin_password.to_owned(); | ||
let name_env = EnvConfiguration::get().admin_name.to_owned(); | ||
if name_env != admin_name { | ||
return Err(ApiError::ValidationError( | ||
"Error validation admin name".to_string(), | ||
)); | ||
} | ||
|
||
if password_env != admin_password { | ||
return Err(ApiError::ValidationError( | ||
"Error validation admin password".to_string(), | ||
)); | ||
} | ||
let my_claims = admin_jwt::AdminJwt { | ||
admin_password, | ||
admin_name, | ||
exp: (Utc::now() + Duration::hours(24)).timestamp() as u64, | ||
}; | ||
|
||
match security::encoded_data(&my_claims) { | ||
Ok(token) => Ok(token), | ||
Err(err) => Err(ApiError::TokenGenerationError(err.to_string())), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use serde::Deserialize; | ||
#[derive(Deserialize)] | ||
#[allow(dead_code)] | ||
pub struct CheckJwtAdminData { | ||
jwt: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
pub struct LoginAdminData { | ||
pub admin_password: String, | ||
pub admin_name: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod check_jwt; | ||
pub mod login_admin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod admin; | ||
pub mod hackathon_2024; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::models::admin::admin_jwt; | ||
use crate::utils::security; | ||
use rocket::http::Status; | ||
use rocket::request::FromRequest; | ||
use rocket::{request, Request}; | ||
|
||
#[rocket::async_trait] | ||
impl<'r> FromRequest<'r> for admin_jwt::AdminJwt { | ||
type Error = (); | ||
|
||
#[allow(dead_code)] | ||
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> { | ||
let token = req | ||
.headers() | ||
.get_one("Authorization") | ||
.and_then(|header| header.strip_prefix("Bearer ")); | ||
|
||
match token { | ||
Some(token) => match security::decoded_data::<admin_jwt::AdminJwt>(token) { | ||
Ok(token_data) => request::Outcome::Success(token_data.claims), | ||
Err(_) => request::Outcome::Error((Status::Unauthorized, ())), | ||
}, | ||
None => request::Outcome::Error((Status::Unauthorized, ())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod admin_match; | ||
pub mod claims; | ||
pub mod get_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct AdminJwt { | ||
pub admin_password: String, | ||
pub admin_name: String, | ||
pub exp: u64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod admin_jwt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod admin; | ||
pub mod hackathon_2024; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters