-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(http): extract Axum extractor for the URL path param key
- Loading branch information
1 parent
94fbdeb
commit 20436be
Showing
5 changed files
with
73 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::panic::Location; | ||
|
||
use axum::async_trait; | ||
use axum::extract::{FromRequestParts, Path}; | ||
use axum::http::request::Parts; | ||
use axum::response::{IntoResponse, Response}; | ||
|
||
use crate::http::axum_implementation::handlers::auth::{self, KeyIdParam}; | ||
use crate::http::axum_implementation::responses; | ||
use crate::tracker::auth::KeyId; | ||
|
||
pub struct ExtractKeyId(pub KeyId); | ||
|
||
impl ExtractKeyId { | ||
#[must_use] | ||
pub fn value(&self) -> KeyId { | ||
self.0.clone() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<S> FromRequestParts<S> for ExtractKeyId | ||
where | ||
S: Send + Sync, | ||
{ | ||
type Rejection = Response; | ||
|
||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
match Path::<KeyIdParam>::from_request_parts(parts, state).await { | ||
Ok(key_id_param) => { | ||
let Ok(key_id) = key_id_param.0.value().parse::<KeyId>() else { | ||
return Err(responses::error::Error::from( | ||
auth::Error::InvalidKeyFormat { | ||
location: Location::caller() | ||
}) | ||
.into_response()) | ||
}; | ||
Ok(ExtractKeyId(key_id)) | ||
} | ||
Err(rejection) => match rejection { | ||
axum::extract::rejection::PathRejection::FailedToDeserializePathParams(_) => { | ||
return Err(responses::error::Error::from(auth::Error::InvalidKeyFormat { | ||
location: Location::caller(), | ||
}) | ||
.into_response()) | ||
} | ||
axum::extract::rejection::PathRejection::MissingPathParams(_) => { | ||
return Err(responses::error::Error::from(auth::Error::MissingAuthKey { | ||
location: Location::caller(), | ||
}) | ||
.into_response()) | ||
} | ||
_ => { | ||
return Err(responses::error::Error::from(auth::Error::CannotExtractKeyParam { | ||
location: Location::caller(), | ||
}) | ||
.into_response()) | ||
} | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod announce_request; | ||
pub mod key; | ||
pub mod peer_ip; | ||
pub mod remote_client_ip; | ||
pub mod scrape_request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters