From adec8218ef6dde8b90da1044dc46f0293996cc28 Mon Sep 17 00:00:00 2001 From: Mario Date: Tue, 30 Jan 2024 00:59:33 +0100 Subject: [PATCH 1/5] feat: [#445] new user id extractor --- src/web/api/server/v1/extractors/mod.rs | 1 + src/web/api/server/v1/extractors/user_id.rs | 37 +++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/web/api/server/v1/extractors/user_id.rs diff --git a/src/web/api/server/v1/extractors/mod.rs b/src/web/api/server/v1/extractors/mod.rs index 36d737ca..2c55e042 100644 --- a/src/web/api/server/v1/extractors/mod.rs +++ b/src/web/api/server/v1/extractors/mod.rs @@ -1 +1,2 @@ pub mod bearer_token; +pub mod user_id; diff --git a/src/web/api/server/v1/extractors/user_id.rs b/src/web/api/server/v1/extractors/user_id.rs new file mode 100644 index 00000000..71cdad1f --- /dev/null +++ b/src/web/api/server/v1/extractors/user_id.rs @@ -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 FromRequestParts for ExtractLoggedInUser +where + Arc: FromRef, + 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) => 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()), + } + } +} From b5da54714e83f45b6714927235c9af397e7be899 Mon Sep 17 00:00:00 2001 From: Mario Date: Tue, 30 Jan 2024 01:03:09 +0100 Subject: [PATCH 2/5] refactor: [#445] new return error type for user id extractor --- src/web/api/server/v1/extractors/user_id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web/api/server/v1/extractors/user_id.rs b/src/web/api/server/v1/extractors/user_id.rs index 71cdad1f..145e94b9 100644 --- a/src/web/api/server/v1/extractors/user_id.rs +++ b/src/web/api/server/v1/extractors/user_id.rs @@ -31,7 +31,7 @@ where 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()), + Err(_) => Err(ServiceError::Unauthorized.into_response()), } } } From 1cecc59e49ef9b39e2a45dec642bcd79b521966d Mon Sep 17 00:00:00 2001 From: Mario Date: Tue, 30 Jan 2024 20:48:55 +0100 Subject: [PATCH 3/5] feat: [#445] new custom error and minor refactor to extractor --- src/errors.rs | 4 ++++ src/web/api/server/v1/extractors/user_id.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 301d841c..274e07fd 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -148,6 +148,9 @@ pub enum ServiceError { #[display(fmt = "Database error.")] DatabaseError, + #[display(fmt = "You must be logged in!.")] + LoggedInUserNotFound, + // Begin tracker errors #[display(fmt = "Sorry, we have an error with our tracker connection.")] TrackerOffline, @@ -311,6 +314,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode { ServiceError::TrackerUnknownResponse => StatusCode::INTERNAL_SERVER_ERROR, ServiceError::TorrentNotFoundInTracker => StatusCode::NOT_FOUND, ServiceError::InvalidTrackerToken => StatusCode::INTERNAL_SERVER_ERROR, + ServiceError::LoggedInUserNotFound => StatusCode::UNAUTHORIZED, } } diff --git a/src/web/api/server/v1/extractors/user_id.rs b/src/web/api/server/v1/extractors/user_id.rs index 145e94b9..4ea81900 100644 --- a/src/web/api/server/v1/extractors/user_id.rs +++ b/src/web/api/server/v1/extractors/user_id.rs @@ -23,7 +23,7 @@ where 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) => maybe_bearer_token.0, - Err(_) => return Err(ServiceError::Unauthorized.into_response()), + Err(_) => return Err(ServiceError::TokenNotFound.into_response()), }; //Extracts the app state @@ -31,7 +31,7 @@ where match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { Ok(user_id) => Ok(ExtractLoggedInUser(user_id)), - Err(_) => Err(ServiceError::Unauthorized.into_response()), + Err(_) => Err(ServiceError::LoggedInUserNotFound.into_response()), } } } From 2f288c6c2699e94a26be8e4411fa068f9d579dbe Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 31 Jan 2024 00:04:47 +0100 Subject: [PATCH 4/5] refactor: [#445] new custom error message --- src/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/errors.rs b/src/errors.rs index 274e07fd..cad0a384 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -148,7 +148,7 @@ pub enum ServiceError { #[display(fmt = "Database error.")] DatabaseError, - #[display(fmt = "You must be logged in!.")] + #[display(fmt = "Please sign in!")] LoggedInUserNotFound, // Begin tracker errors From 59b1cced740f0126b10cf1a7982e6c9c4fa4e402 Mon Sep 17 00:00:00 2001 From: Mario Date: Mon, 5 Feb 2024 21:54:42 +0100 Subject: [PATCH 5/5] refactor: [#445] new more descriptive error message --- src/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/errors.rs b/src/errors.rs index cad0a384..b750a852 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -148,7 +148,7 @@ pub enum ServiceError { #[display(fmt = "Database error.")] DatabaseError, - #[display(fmt = "Please sign in!")] + #[display(fmt = "Authentication error, please sign in")] LoggedInUserNotFound, // Begin tracker errors