|
1 | 1 | use std::usize; |
2 | 2 |
|
3 | | -use crate::GlobalConfig; |
| 3 | +use crate::{GlobalConfig, errors::HeaderValidationError}; |
4 | 4 | use axum::{ |
5 | 5 | extract::{Path, Request, State}, |
6 | | - http::{HeaderMap, StatusCode, header}, |
| 6 | + http::{HeaderMap, header}, |
7 | 7 | middleware::Next, |
8 | | - response::{IntoResponse, Response}, |
| 8 | + response::Response, |
9 | 9 | }; |
10 | 10 | use hmac::{Hmac, Mac}; |
11 | 11 |
|
12 | | -#[derive(Debug)] |
13 | | -pub enum HeaderValidationError { |
14 | | - MissingHeader(&'static str), |
15 | | - InvalidSignature, |
16 | | - InvalidUserAgent, |
17 | | - WebhookNotFound, |
18 | | - AxumError(axum::Error), |
19 | | -} |
20 | | - |
21 | | -impl IntoResponse for HeaderValidationError { |
22 | | - fn into_response(self) -> Response { |
23 | | - let (status, message) = match self { |
24 | | - HeaderValidationError::MissingHeader(header) => ( |
25 | | - StatusCode::BAD_REQUEST, |
26 | | - format!("Missing required header: {header}"), |
27 | | - ), |
28 | | - HeaderValidationError::InvalidSignature => { |
29 | | - (StatusCode::UNAUTHORIZED, "Invalid signature".to_string()) |
30 | | - } |
31 | | - HeaderValidationError::InvalidUserAgent => { |
32 | | - (StatusCode::BAD_REQUEST, "Invalid User-Agent".to_string()) |
33 | | - } |
34 | | - HeaderValidationError::WebhookNotFound => { |
35 | | - (StatusCode::NOT_FOUND, "Webhook not configured".to_string()) |
36 | | - } |
37 | | - HeaderValidationError::AxumError(error) => (StatusCode::BAD_REQUEST, error.to_string()), |
38 | | - }; |
39 | | - (status, message).into_response() |
40 | | - } |
41 | | -} |
42 | | - |
43 | 12 | pub async fn validate_headers( |
44 | 13 | request: Request, |
45 | 14 | next: Next, |
@@ -132,7 +101,7 @@ fn validate_signature( |
132 | 101 | let expected_signature = hex::encode(mac.finalize().into_bytes()); |
133 | 102 | (expected_signature, signature) |
134 | 103 | } |
135 | | - None => { |
| 104 | + _ => { |
136 | 105 | let signature = headers |
137 | 106 | .get("X-Hub-Signature") |
138 | 107 | .and_then(|v| v.to_str().ok()) |
|
0 commit comments