-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
59b1cce refactor: [#445] new more descriptive error message (Mario) 2f288c6 refactor: [#445] new custom error message (Mario) 1cecc59 feat: [#445] new custom error and minor refactor to extractor (Mario) b5da547 refactor: [#445] new return error type for user id extractor (Mario) adec821 feat: [#445] new user id extractor (Mario) Pull request description: Resolves #445 ACKs for top commit: josecelano: ACK 59b1cce Tree-SHA512: 87858403b5c3e60c9df500a078d48f1a04d5a57c4985b7343dd139f539b6cc18dbf4ec3300fc7b0e344caf09aa1f58bb95c5532060872be8486b4624c98d6229
- Loading branch information
Showing
3 changed files
with
42 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
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 bearer_token; | ||
pub mod user_id; |
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,37 @@ | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use axum::extract::{FromRef, FromRequestParts}; | ||
use axum::http::request::Parts; | ||
use axum::response::{IntoResponse, Response}; | ||
|
||
use super::bearer_token; | ||
use crate::common::AppData; | ||
use crate::errors::ServiceError; | ||
use crate::models::user::UserId; | ||
|
||
pub struct ExtractLoggedInUser(pub UserId); | ||
|
||
#[async_trait] | ||
impl<S> FromRequestParts<S> for ExtractLoggedInUser | ||
where | ||
Arc<AppData>: FromRef<S>, | ||
S: Send + Sync, | ||
{ | ||
type Rejection = Response; | ||
|
||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
let maybe_bearer_token = match bearer_token::Extract::from_request_parts(parts, state).await { | ||
Ok(maybe_bearer_token) => maybe_bearer_token.0, | ||
Err(_) => return Err(ServiceError::TokenNotFound.into_response()), | ||
}; | ||
|
||
//Extracts the app state | ||
let app_data = Arc::from_ref(state); | ||
|
||
match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { | ||
Ok(user_id) => Ok(ExtractLoggedInUser(user_id)), | ||
Err(_) => Err(ServiceError::LoggedInUserNotFound.into_response()), | ||
} | ||
} | ||
} |