forked from torrust/torrust-index
-
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.
- Loading branch information
MMelchor
committed
Oct 6, 2023
1 parent
bf989a1
commit 869f8d6
Showing
3 changed files
with
75 additions
and
2 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,70 @@ | ||
use axum::async_trait; | ||
use axum::extract::{FromRequestParts, State}; | ||
use axum::http::request::Parts; | ||
use axum::response::Response; | ||
use serde::Deserialize; | ||
|
||
use super::bearer_token::{self, BearerToken}; | ||
use crate::common::AppData; | ||
use crate::web::api::v1::auth::{get_optional_logged_in_user, parse_token}; | ||
use crate::{app, common}; | ||
|
||
pub struct Extract(pub Option<UserId>); | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct UserId(String); | ||
|
||
impl UserId { | ||
#[must_use] | ||
pub fn value(&self) -> String { | ||
self.0.clone() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<S> FromRequestParts<S> for Extract | ||
where | ||
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) => Some(maybe_bearer_token), | ||
Err(_) => None, | ||
}; | ||
// State(app_data): State<Arc<AppData>> | ||
|
||
let app_data = match axum::extract::State::from_request_parts(parts, state).await { | ||
Ok(app_state) => Some(app_state), | ||
Err(_) => None, | ||
}; | ||
|
||
// Ok(Extract(UserId(maybe_bearer_token))) | ||
Ok(Extract(None)) | ||
} | ||
} | ||
|
||
/* | ||
//Refactor? Should a extractor be used here? | ||
// let header = parts.headers.get("Authorization"); | ||
// let bearer_token: String; | ||
/* match header { | ||
Some(header_value) => bearer_token = parse_token(header_value), | ||
None => bearer_token = parse_token(None), | ||
} | ||
*/ | ||
/* | ||
match user_id { | ||
Some(user_id) => Ok(ExtractAuthenticatedUser(Some(UserId(userId)))), | ||
None => OK(ExtractAuthenticatedUser(None)), | ||
} | ||
*/ | ||
/* */ let user_id: Option<i64> = get_optional_logged_in_user(Extract(maybe_bearer_token), State(app_data)); | ||
//get_optional_logged_in_user(maybe_bearer_token, _state); | ||
// if () | ||
match user_id { | ||
Some(user_id) => Ok(ExtractAuthenticatedUser(Some(UserId(userId)))), | ||
None => OK(ExtractAuthenticatedUser(None)), | ||
}*/ |