use std::time::Duration;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use hmac::{Hmac, KeyInit, Mac};
use sha2::Sha256;
use crate::{Error, Result};
pub const SECRET_PREFIX: &str = "whsec_";
pub fn generate_secret() -> String {
let mut bytes = [0u8; 24];
rand::fill(&mut bytes);
format!("{SECRET_PREFIX}{}", STANDARD.encode(bytes))
}
pub fn decode_secret(secret: &str) -> Result<Vec<u8>> {
let encoded = secret.strip_prefix(SECRET_PREFIX).unwrap_or(secret);
STANDARD.decode(encoded.trim()).map_err(|_| {
Error::from(anyhow::anyhow!(
"the webhook secret is not valid base64 (expected `whsec_<base64>`)"
))
})
}
fn mac(key: &[u8], id: &str, timestamp: &str, body: &[u8]) -> Hmac<Sha256> {
let mut mac =
<Hmac<Sha256> as KeyInit>::new_from_slice(key).expect("HMAC accepts keys of any length");
mac.update(id.as_bytes());
mac.update(b".");
mac.update(timestamp.as_bytes());
mac.update(b".");
mac.update(body);
mac
}
pub fn sign(secret: &str, id: &str, timestamp: i64, body: &[u8]) -> Result<String> {
let key = decode_secret(secret)?;
let digest = mac(&key, id, ×tamp.to_string(), body)
.finalize()
.into_bytes();
Ok(format!("v1,{}", STANDARD.encode(digest)))
}
pub fn verify(
secret: &str,
id: &str,
timestamp_header: &str,
signature_header: &str,
body: &[u8],
tolerance: Duration,
) -> bool {
let timestamp_header = timestamp_header.trim();
let Ok(timestamp) = timestamp_header.parse::<i64>() else {
return false;
};
if (crate::clock::unix_secs() - timestamp).unsigned_abs() > tolerance.as_secs() {
return false;
}
let Ok(key) = decode_secret(secret) else {
return false;
};
let expected = mac(&key, id, timestamp_header, body)
.finalize()
.into_bytes();
signature_header
.split(' ')
.filter_map(|part| part.strip_prefix("v1,"))
.filter_map(|b64| STANDARD.decode(b64).ok())
.fold(false, |ok, sig| {
let equal = sig.len() == expected.len()
&& sig
.iter()
.zip(expected.iter())
.fold(0u8, |acc, (x, y)| acc | (x ^ y))
== 0;
ok | equal
})
}
pub struct SignedRequest<T>(pub T);
fn unauthorized() -> axum::response::Response {
use axum::response::IntoResponse;
(axum::http::StatusCode::UNAUTHORIZED, "Unauthorized").into_response()
}
impl<T> axum::extract::FromRequest<crate::AppState> for SignedRequest<T>
where
T: serde::de::DeserializeOwned + Send,
{
type Rejection = axum::response::Response;
async fn from_request(
req: axum::extract::Request,
state: &crate::AppState,
) -> std::result::Result<Self, Self::Rejection> {
let headers = {
let header = |name: &str| {
req.headers()
.get(name)
.and_then(|v| v.to_str().ok())
.map(str::to_owned)
};
(
header("webhook-key-id"),
header("webhook-id"),
header("webhook-timestamp"),
header("webhook-signature"),
)
};
let (Some(key_id), Some(id), Some(timestamp), Some(signature)) = headers else {
return Err(unauthorized());
};
let Some(secret) = state.signing_keys.get(&key_id) else {
return Err(unauthorized());
};
let body = axum::body::Bytes::from_request(req, state)
.await
.map_err(|_| unauthorized())?;
let tolerance = Duration::from_secs(state.config.webhook_tolerance_secs);
if !verify(secret, &id, ×tamp, &signature, &body, tolerance) {
return Err(unauthorized());
}
let fresh = state
.cache
.add(
&format!("webhook-replay:{key_id}:{id}"),
&true,
Some(tolerance * 2),
)
.await
.map_err(|_| unauthorized())?;
if !fresh {
return Err(unauthorized());
}
let value = serde_json::from_slice(&body).map_err(|_| unauthorized())?;
Ok(Self(value))
}
}
#[cfg(test)]
mod tests {
use super::*;
const SECRET: &str = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw";
const ID: &str = "msg_p5jXN8AQM9LWM0D4loKWxJek";
const BODY: &[u8] = br#"{"test": 2432232314}"#;
const VECTOR: &str = "v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=";
const WINDOW: Duration = Duration::from_secs(300);
#[test]
fn matches_the_specification_test_vector() {
assert_eq!(sign(SECRET, ID, 1614265330, BODY).unwrap(), VECTOR);
}
#[test]
fn verifies_within_the_window_and_rejects_tampering() {
let now = crate::clock::unix_secs();
let signature = sign(SECRET, ID, now, BODY).unwrap();
let ts = now.to_string();
assert!(verify(SECRET, ID, &ts, &signature, BODY, WINDOW));
assert!(!verify(SECRET, ID, &ts, &signature, b"{}", WINDOW));
assert!(!verify(SECRET, "msg_other", &ts, &signature, BODY, WINDOW));
assert!(!verify(
&generate_secret(),
ID,
&ts,
&signature,
BODY,
WINDOW
));
assert!(!verify(
SECRET,
ID,
"not a number",
&signature,
BODY,
WINDOW
));
}
#[test]
fn rejects_old_and_future_timestamps() {
let now = crate::clock::unix_secs();
for ts in [now - 301, now + 301] {
let signature = sign(SECRET, ID, ts, BODY).unwrap();
assert!(!verify(
SECRET,
ID,
&ts.to_string(),
&signature,
BODY,
WINDOW
));
}
}
#[test]
fn accepts_any_of_several_signatures_and_skips_other_versions() {
let now = crate::clock::unix_secs();
let good = sign(SECRET, ID, now, BODY).unwrap();
let other = sign(&generate_secret(), ID, now, BODY).unwrap();
let ts = now.to_string();
let header = format!("{other} {good}");
assert!(verify(SECRET, ID, &ts, &header, BODY, WINDOW));
let header = format!("v2,abc {good}");
assert!(verify(SECRET, ID, &ts, &header, BODY, WINDOW));
assert!(!verify(SECRET, ID, &ts, "v1,!!! v2,abc", BODY, WINDOW));
assert!(!verify(SECRET, ID, &ts, "", BODY, WINDOW));
}
#[test]
fn generated_secrets_decode_and_differ() {
let a = generate_secret();
assert!(a.starts_with(SECRET_PREFIX));
assert_eq!(decode_secret(&a).unwrap().len(), 24);
assert_ne!(a, generate_secret());
assert!(decode_secret("whsec_***").is_err());
}
}