refactor: Use PublicKey and Signature as parameters #33

Manually merged
awiteb merged 9 commits from awiteb/public-key_and_siganture_as_parameters into master 2024-07-28 11:56:46 +02:00 AGit
2 changed files with 21 additions and 13 deletions
Showing only changes of commit 5ca23c8af5 - Show all commits

View file

@ -16,10 +16,12 @@
//! Request signature middleware. //! Request signature middleware.
use oxidetalis_core::types::{PublicKey, Signature};
use salvo::{ use salvo::{
handler, handler,
http::{Body, StatusCode}, http::{Body, StatusCode},
Depot, Depot,
Extractible,
FlowCtrl, FlowCtrl,
Request, Request,
Response, Response,
@ -54,7 +56,7 @@ pub async fn signature_check(
} }
}; };
let signature = match utils::extract_signature(req) { let signature = match Signature::extract(req).await {
Ok(s) => s, Ok(s) => s,
Err(err) => { Err(err) => {
write_err(&err.to_string(), UNAUTHORIZED); write_err(&err.to_string(), UNAUTHORIZED);
@ -62,7 +64,7 @@ pub async fn signature_check(
} }
}; };
let sender_public_key = match utils::extract_public_key(req) { let sender_public_key = match PublicKey::extract(req).await {
Ok(k) => k, Ok(k) => k,
Err(err) => { Err(err) => {
write_err(&err.to_string(), UNAUTHORIZED); write_err(&err.to_string(), UNAUTHORIZED);

View file

@ -111,20 +111,26 @@ impl EndpointArgRegister for CorePublicKey {
impl<'ex> Extractible<'ex> for Signature { impl<'ex> Extractible<'ex> for Signature {
fn metadata() -> &'ex ExtractMetadata { fn metadata() -> &'ex ExtractMetadata {
unreachable!( static METADATA: ExtractMetadata = ExtractMetadata::new("");
" &METADATA
`Extractible` is required to implement `ToParameters` for `Signature`, but \
Salvo does not need it actually, see https://github.com/salvo-rs/salvo/issues/838"
)
} }
#[allow(refining_impl_trait)] #[allow(refining_impl_trait)]
async fn extract(_: &'ex mut Request) -> Result<Self, StatusError> { async fn extract(req: &'ex mut Request) -> Result<Self, StatusError> {
unreachable!( extract_header(req, crate::SIGNATURE_HEADER)
" .and_then(|sig| {
`Extractible` is required to implement `ToParameters` for `Signature`, but \ Signature::from_str(sig).map_err(|err| {
Salvo does not need it actually, see https://github.com/salvo-rs/salvo/issues/838" StatusError::unauthorized()
) .brief("Invalid signature")
.cause(err.to_string())
})
})
.map_err(|err| {
StatusError::unauthorized().brief(err.brief).cause(
err.cause
.expect("The cause was set when we extract the header"),
)
})
} }
} }