Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MMelchor committed Oct 6, 2023
1 parent bf989a1 commit 869f8d6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,12 +39,13 @@ use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;
pub async fn upload_torrent_handler(
State(app_data): State<Arc<AppData>>,
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,
Expand Down
1 change: 1 addition & 0 deletions src/web/api/v1/extractors/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod bearer_token;
pub mod user_id;
70 changes: 70 additions & 0 deletions src/web/api/v1/extractors/user_id.rs
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)),
}*/

0 comments on commit 869f8d6

Please sign in to comment.