chore: Use core implementation to extract the signature and public key
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
f4df5b26d3
commit
5ca23c8af5
2 changed files with 21 additions and 13 deletions
|
@ -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);
|
||||||
|
|
|
@ -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"),
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue