Skip to content

Commit a14004e

Browse files
committed
Merge #447: #445 mandatory user id extractor
59b1cce refactor: [#445] new more descriptive error message (Mario) 2f288c6 refactor: [#445] new custom error message (Mario) 1cecc59 feat: [#445] new custom error and minor refactor to extractor (Mario) b5da547 refactor: [#445] new return error type for user id extractor (Mario) adec821 feat: [#445] new user id extractor (Mario) Pull request description: Resolves #445 ACKs for top commit: josecelano: ACK 59b1cce Tree-SHA512: 87858403b5c3e60c9df500a078d48f1a04d5a57c4985b7343dd139f539b6cc18dbf4ec3300fc7b0e344caf09aa1f58bb95c5532060872be8486b4624c98d6229
2 parents 663bd6e + 59b1cce commit a14004e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ pub enum ServiceError {
148148
#[display(fmt = "Database error.")]
149149
DatabaseError,
150150

151+
#[display(fmt = "Authentication error, please sign in")]
152+
LoggedInUserNotFound,
153+
151154
// Begin tracker errors
152155
#[display(fmt = "Sorry, we have an error with our tracker connection.")]
153156
TrackerOffline,
@@ -311,6 +314,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
311314
ServiceError::TrackerUnknownResponse => StatusCode::INTERNAL_SERVER_ERROR,
312315
ServiceError::TorrentNotFoundInTracker => StatusCode::NOT_FOUND,
313316
ServiceError::InvalidTrackerToken => StatusCode::INTERNAL_SERVER_ERROR,
317+
ServiceError::LoggedInUserNotFound => StatusCode::UNAUTHORIZED,
314318
}
315319
}
316320

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod bearer_token;
2+
pub mod user_id;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::sync::Arc;
2+
3+
use async_trait::async_trait;
4+
use axum::extract::{FromRef, FromRequestParts};
5+
use axum::http::request::Parts;
6+
use axum::response::{IntoResponse, Response};
7+
8+
use super::bearer_token;
9+
use crate::common::AppData;
10+
use crate::errors::ServiceError;
11+
use crate::models::user::UserId;
12+
13+
pub struct ExtractLoggedInUser(pub UserId);
14+
15+
#[async_trait]
16+
impl<S> FromRequestParts<S> for ExtractLoggedInUser
17+
where
18+
Arc<AppData>: FromRef<S>,
19+
S: Send + Sync,
20+
{
21+
type Rejection = Response;
22+
23+
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
24+
let maybe_bearer_token = match bearer_token::Extract::from_request_parts(parts, state).await {
25+
Ok(maybe_bearer_token) => maybe_bearer_token.0,
26+
Err(_) => return Err(ServiceError::TokenNotFound.into_response()),
27+
};
28+
29+
//Extracts the app state
30+
let app_data = Arc::from_ref(state);
31+
32+
match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
33+
Ok(user_id) => Ok(ExtractLoggedInUser(user_id)),
34+
Err(_) => Err(ServiceError::LoggedInUserNotFound.into_response()),
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)