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.
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);

View file

@ -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<Self, StatusError> {
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<Self, StatusError> {
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"),
)
})
}
}