Skip to content

Commit

Permalink
feat: create token helper to fix duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzyct committed Sep 28, 2024
1 parent 8edd150 commit 738bc4d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
20 changes: 3 additions & 17 deletions src/core/middlewares/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::future::Future;
use std::pin::Pin;

use actix_web::dev::Payload;
use actix_web::http::header::HeaderValue;
use actix_web::{FromRequest, HttpRequest};
use base64::engine::general_purpose;
use base64::Engine;
Expand All @@ -26,6 +25,7 @@ use crate::{
entity::user_response::UserResponse, repository::user_repository::UserRepositoryImpl,
},
},
utils::token_helper::{is_auth_header_valid, token_extractor},
};

pub struct AuthMiddleware {
Expand All @@ -35,7 +35,7 @@ pub struct AuthMiddleware {

impl FromRequest for AuthMiddleware {
type Error = APIError;
type Future = Pin<Box<dyn Future<Output = Result<AuthMiddleware, Self::Error>>>>;
type Future = Pin<Box<dyn Future<Output=Result<AuthMiddleware, Self::Error>>>>;

// act as auth middleware
fn from_request(request: &HttpRequest, _: &mut Payload) -> Self::Future {
Expand Down Expand Up @@ -98,19 +98,5 @@ pub fn decode_token(jwt: &str) -> AppResult<TokenData<AuthToken>> {
&DecodingKey::from_rsa_pem(decoded_public_key.as_bytes()).unwrap(),
&validation,
)
.map_err(|_e| APIError::Unauthorized)
}

pub fn token_extractor(auth: &str) -> String {
let bearer_str = auth.split(' ').collect::<Vec<&str>>();
let token_prefix = bearer_str[1].split('.').collect::<Vec<&str>>();

token_prefix[1..].join(".")
}

pub fn is_auth_header_valid(auth_header: &HeaderValue) -> bool {
if let Ok(auth_str) = auth_header.to_str() {
return auth_str.starts_with("bearer") || auth_str.starts_with("Bearer");
}
false
.map_err(|_e| APIError::Unauthorized)
}
20 changes: 3 additions & 17 deletions src/core/middlewares/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::future::Future;
use std::pin::Pin;

use actix_web::dev::Payload;
use actix_web::http::header::HeaderValue;
use actix_web::{FromRequest, HttpRequest};
use base64::engine::general_purpose;
use base64::Engine;
Expand All @@ -12,6 +11,7 @@ use jsonwebtoken::{Algorithm, DecodingKey, TokenData, Validation};
use crate::{
core::{constants::AUTHORIZATION, error::APIError, types::AppResult},
features::auth::data::models::general_token::GeneralToken,
utils::token_helper::{is_auth_header_valid, token_extractor},
};

#[allow(dead_code)]
Expand All @@ -21,7 +21,7 @@ pub struct GeneralMiddleware {

impl FromRequest for GeneralMiddleware {
type Error = APIError;
type Future = Pin<Box<dyn Future<Output = Result<GeneralMiddleware, Self::Error>>>>;
type Future = Pin<Box<dyn Future<Output=Result<GeneralMiddleware, Self::Error>>>>;

// act as auth middleware
fn from_request(request: &HttpRequest, _: &mut Payload) -> Self::Future {
Expand Down Expand Up @@ -64,19 +64,5 @@ pub fn decode_token(jwt: &str) -> AppResult<TokenData<GeneralToken>> {
&DecodingKey::from_rsa_pem(decoded_public_key.as_bytes()).unwrap(),
&validation,
)
.map_err(|_| APIError::Unauthorized)
}

pub fn token_extractor(auth: &str) -> String {
let bearer_str = auth.split(' ').collect::<Vec<&str>>();
let token_prefix = bearer_str[1].split('.').collect::<Vec<&str>>();

token_prefix[1..].join(".")
}

pub fn is_auth_header_valid(auth_header: &HeaderValue) -> bool {
if let Ok(auth_str) = auth_header.to_str() {
return auth_str.starts_with("bearer") || auth_str.starts_with("Bearer");
}
false
.map_err(|_| APIError::Unauthorized)
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod handler;
pub mod macros;
pub mod token_helper;
15 changes: 15 additions & 0 deletions src/utils/token_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use actix_web::http::header::HeaderValue;

pub fn token_extractor(auth: &str) -> String {
let bearer_str = auth.split(' ').collect::<Vec<&str>>();
let token_prefix = bearer_str[1].split('.').collect::<Vec<&str>>();

token_prefix[1..].join(".")
}

pub fn is_auth_header_valid(auth_header: &HeaderValue) -> bool {
if let Ok(auth_str) = auth_header.to_str() {
return auth_str.starts_with("bearer") || auth_str.starts_with("Bearer");
}
false
}

0 comments on commit 738bc4d

Please sign in to comment.