From 5ca23c8af5eb3a23767b91cca3c59a5f82ec0d15 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Sun, 28 Jul 2024 09:46:14 +0300 Subject: [PATCH] chore: Use core implementation to extract the signature and public key Signed-off-by: Awiteb --- .../oxidetalis/src/middlewares/signature.rs | 6 ++-- .../oxidetalis_core/src/types/impl_openapi.rs | 28 +++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/oxidetalis/src/middlewares/signature.rs b/crates/oxidetalis/src/middlewares/signature.rs index f108f4a..e5d019b 100644 --- a/crates/oxidetalis/src/middlewares/signature.rs +++ b/crates/oxidetalis/src/middlewares/signature.rs @@ -16,10 +16,12 @@ //! Request signature middleware. +use oxidetalis_core::types::{PublicKey, Signature}; use salvo::{ handler, http::{Body, StatusCode}, Depot, + Extractible, FlowCtrl, Request, 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, Err(err) => { 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, Err(err) => { write_err(&err.to_string(), UNAUTHORIZED); diff --git a/crates/oxidetalis_core/src/types/impl_openapi.rs b/crates/oxidetalis_core/src/types/impl_openapi.rs index b22fbfe..eea4d05 100644 --- a/crates/oxidetalis_core/src/types/impl_openapi.rs +++ b/crates/oxidetalis_core/src/types/impl_openapi.rs @@ -111,20 +111,26 @@ impl EndpointArgRegister for CorePublicKey { impl<'ex> Extractible<'ex> for Signature { fn metadata() -> &'ex ExtractMetadata { - unreachable!( - " - `Extractible` is required to implement `ToParameters` for `Signature`, but \ - Salvo does not need it actually, see https://github.com/salvo-rs/salvo/issues/838" - ) + static METADATA: ExtractMetadata = ExtractMetadata::new(""); + &METADATA } #[allow(refining_impl_trait)] - async fn extract(_: &'ex mut Request) -> Result { - unreachable!( - " - `Extractible` is required to implement `ToParameters` for `Signature`, but \ - Salvo does not need it actually, see https://github.com/salvo-rs/salvo/issues/838" - ) + async fn extract(req: &'ex mut Request) -> Result { + extract_header(req, crate::SIGNATURE_HEADER) + .and_then(|sig| { + Signature::from_str(sig).map_err(|err| { + 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"), + ) + }) } }