use axum::extract::FromRequestParts;
use axum::http::request::Parts;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
#[derive(Debug)]
pub struct Signature {
pub key: String,
}
#[derive(Debug)]
pub struct SignatureRejection {
message: &'static str,
}
impl IntoResponse for SignatureRejection {
fn into_response(self) -> Response {
(StatusCode::BAD_REQUEST, self.message).into_response()
}
}
impl<S> FromRequestParts<S> for Signature
where
S: Send + Sync,
{
type Rejection = SignatureRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
match parts.headers.get("x-line-signature") {
Some(value) => match value.to_str() {
Ok(key) => Ok(Signature {
key: key.to_string(),
}),
Err(_) => Err(SignatureRejection {
message: "x-line-signature contains invalid characters",
}),
},
None => Err(SignatureRejection {
message: "x-line-signature is missing",
}),
}
}
}