From 869f8d65f46e8b7b642b4e87c905159ba7d3200f Mon Sep 17 00:00:00 2001 From: MMelchor Date: Fri, 6 Oct 2023 14:02:42 +0200 Subject: [PATCH] wip --- src/web/api/v1/contexts/torrent/handlers.rs | 6 +- src/web/api/v1/extractors/mod.rs | 1 + src/web/api/v1/extractors/user_id.rs | 70 +++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/web/api/v1/extractors/user_id.rs diff --git a/src/web/api/v1/contexts/torrent/handlers.rs b/src/web/api/v1/contexts/torrent/handlers.rs index 6c0754f0..62273e45 100644 --- a/src/web/api/v1/contexts/torrent/handlers.rs +++ b/src/web/api/v1/contexts/torrent/handlers.rs @@ -23,6 +23,7 @@ use crate::services::torrent_file::generate_random_torrent; use crate::utils::parse_torrent; use crate::web::api::v1::auth::get_optional_logged_in_user; use crate::web::api::v1::extractors::bearer_token::Extract; +// use crate::web::api::v1::extractors::user_id::ExtractAuthenticatedUser; use crate::web::api::v1::responses::OkResponseData; use crate::web::api::v1::routes::API_VERSION_URL_PREFIX; @@ -38,12 +39,13 @@ use crate::web::api::v1::routes::API_VERSION_URL_PREFIX; pub async fn upload_torrent_handler( State(app_data): State>, Extract(maybe_bearer_token): Extract, + // ExtractAuthenticatedUser(user_id): ExtractAuthenticatedUser, multipart: Multipart, ) -> Response { - let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { + let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { Ok(user_id) => user_id, Err(error) => return error.into_response(), - }; + }; let add_torrent_form = match build_add_torrent_request_from_payload(multipart).await { Ok(torrent_request) => torrent_request, diff --git a/src/web/api/v1/extractors/mod.rs b/src/web/api/v1/extractors/mod.rs index 36d737ca..2c55e042 100644 --- a/src/web/api/v1/extractors/mod.rs +++ b/src/web/api/v1/extractors/mod.rs @@ -1 +1,2 @@ pub mod bearer_token; +pub mod user_id; diff --git a/src/web/api/v1/extractors/user_id.rs b/src/web/api/v1/extractors/user_id.rs new file mode 100644 index 00000000..e61debe7 --- /dev/null +++ b/src/web/api/v1/extractors/user_id.rs @@ -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); + +#[derive(Deserialize, Debug)] +pub struct UserId(String); + +impl UserId { + #[must_use] + pub fn value(&self) -> String { + self.0.clone() + } +} + +#[async_trait] +impl FromRequestParts for Extract +where + S: Send + Sync, +{ + type Rejection = Response; + + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + 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> + + 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 = 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)), +}*/