Skip to content

Commit

Permalink
fix: allow auth token to access general token API
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzyct committed Sep 28, 2024
1 parent 738bc4d commit 96613f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/core/middlewares/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl FromRequest for AuthMiddleware {

let token = token_extractor(auth_str);
let token_data =
decode_token(&token.to_string()).map_err(|_| APIError::Unauthorized)?;
decode_token_auth(&token.to_string()).map_err(|_| APIError::Unauthorized)?;

let user_id = di.auth_repository.verify_token(&token_data).map_err(|_| {
APIError::UnauthorizedMessage {
Expand All @@ -86,7 +86,7 @@ impl FromRequest for AuthMiddleware {
}
}

pub fn decode_token(jwt: &str) -> AppResult<TokenData<AuthToken>> {
pub fn decode_token_auth(jwt: &str) -> AppResult<TokenData<AuthToken>> {
let bytes_public_key = general_purpose::STANDARD
.decode(dotenv!("ACCESS_TOKEN_PUBLIC_KEY"))
.unwrap();
Expand Down
19 changes: 16 additions & 3 deletions src/core/middlewares/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use dotenv_codegen::dotenv;
use jsonwebtoken::{Algorithm, DecodingKey, TokenData, Validation};

use crate::{
core::{constants::AUTHORIZATION, error::APIError, types::AppResult},
core::{constants::AUTHORIZATION, error::APIError, middlewares::auth::decode_token_auth, types::AppResult},
features::auth::data::models::general_token::GeneralToken,
utils::token_helper::{is_auth_header_valid, token_extractor},
};
Expand Down Expand Up @@ -46,8 +46,21 @@ impl FromRequest for GeneralMiddleware {
})?;

let token = token_extractor(auth_str);
let token_data = decode_token(&token).map_err(|_| APIError::Unauthorized)?;
Ok(GeneralMiddleware { data: token_data })
// check general token first
let _ = decode_token(&token).map_err(|_|
decode_token_auth(&token).map_err(|_| APIError::Unauthorized));

let mock_data = TokenData {
header: Default::default(),
claims: GeneralToken {
// Add fields of GeneralToken here
aud: "".to_string(),
exp: 0,
iat: 0,
},

};
Ok(GeneralMiddleware { data: mock_data })
})
}
}
Expand Down

0 comments on commit 96613f2

Please sign in to comment.