1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-10-18 07:22:39 +00:00

Add ValidatedJson

This commit is contained in:
Mo 2022-12-27 00:50:03 +01:00
parent 3ac410372f
commit 42d97b5959
2 changed files with 29 additions and 17 deletions

View file

@ -6,13 +6,12 @@ use axum::{
http::request::{Parts, Request}, http::request::{Parts, Request},
}; };
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
use serde_json::Value;
use sha2::Sha256; use sha2::Sha256;
use crate::{errors::AppError, states}; use crate::{errors::AppError, states};
pub struct ReceivedSignature { pub struct ReceivedSignature(pub Vec<u8>);
pub inner: Vec<u8>,
}
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for ReceivedSignature impl<S> FromRequestParts<S> for ReceivedSignature
@ -33,9 +32,7 @@ where
let received_signature = hex::decode(received_signature) let received_signature = hex::decode(received_signature)
.context("Can not hex decode the received signature!")?; .context("Can not hex decode the received signature!")?;
Ok(ReceivedSignature { Ok(ReceivedSignature(received_signature.to_vec()))
inner: received_signature.to_vec(),
})
} }
} }
@ -46,13 +43,11 @@ impl ReceivedSignature {
mac.update(body); mac.update(body);
let expected_signature = mac.finalize().into_bytes(); let expected_signature = mac.finalize().into_bytes();
self.inner[..] == expected_signature[..] self.0[..] == expected_signature[..]
} }
} }
pub struct ValidatedBody { pub struct ValidatedBody(pub Bytes);
pub inner: Bytes,
}
#[async_trait] #[async_trait]
impl<S, B> FromRequest<S, B> for ValidatedBody impl<S, B> FromRequest<S, B> for ValidatedBody
@ -81,6 +76,27 @@ where
return Err("Invalid signature!".into()); return Err("Invalid signature!".into());
} }
Ok(Self { inner: body }) Ok(Self(body))
}
}
pub struct ValidatedJson(pub Value);
#[async_trait]
impl<S, B> FromRequest<S, B> for ValidatedJson
where
Bytes: FromRequest<S, B>,
B: Send + 'static,
S: Send + Sync + states::ConfigState,
{
type Rejection = AppError;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
let body = ValidatedBody::from_request(req, state).await?.0;
let json: Value =
serde_json::from_slice(&body).context("Can not parse the request body into JSON!")?;
Ok(Self(json))
} }
} }

View file

@ -5,12 +5,11 @@ use axum::{
response::Response, response::Response,
}; };
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value;
use std::sync::Arc; use std::sync::Arc;
use tokio::task; use tokio::task;
use tracing::info; use tracing::info;
use crate::{db, errors, extractors, mailer, states, templates, webhook}; use crate::{db, errors, extractors::ValidatedJson, mailer, states, templates, webhook};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct IndexQuery { pub struct IndexQuery {
@ -40,13 +39,10 @@ pub async fn trigger(
State(db): State<Arc<states::DB>>, State(db): State<Arc<states::DB>>,
State(state_config): State<Arc<states::StateConfig>>, State(state_config): State<Arc<states::StateConfig>>,
State(mailer): State<Arc<mailer::Mailer>>, State(mailer): State<Arc<mailer::Mailer>>,
body: extractors::ValidatedBody, ValidatedJson(json): ValidatedJson,
) -> Result<Response, errors::AppError> { ) -> Result<Response, errors::AppError> {
info!("Trigger called"); info!("Trigger called");
let json: Value =
serde_json::from_slice(&body.inner).context("Can not parse the request body into JSON!")?;
let repo = json let repo = json
.get("repository") .get("repository")
.context("Can not get the repository value from the request body!")?; .context("Can not get the repository value from the request body!")?;