Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User authentication should be done in middleware #369

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 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::ExtractLoggedInUser;
use crate::web::api::v1::responses::OkResponseData;
use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;

Expand All @@ -37,14 +38,9 @@ use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;
#[allow(clippy::unused_async)]
pub async fn upload_torrent_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mario-nt I think this is non-optional Acum extractor that returns an optional value. That's not the same as an optional extractor. See #39 (comment)

multipart: Multipart,
) -> Response {
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,
Err(error) => return error.into_response(),
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;
37 changes: 37 additions & 0 deletions src/web/api/v1/extractors/user_id.rs
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::Unauthorized.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(error) => Err(error.into_response()),
}
}
}
Loading