use std::fmt;
use std::sync::Arc;
use chrono::{DateTime, Utc};
use hmac::{Hmac, KeyInit, Mac};
use http::Uri;
use http::request::Parts;
use navree_contracts::Clock;
use navree_kernel::{AppContext, BoxFuture};
use sha2::Sha256;
use crate::{AppError, Guard};
#[derive(Clone)]
pub struct UrlSigner {
key: Vec<u8>,
}
impl UrlSigner {
pub fn new(key: &[u8]) -> Self {
Self { key: key.to_vec() }
}
pub fn sign(&self, path_and_query: &str, expires_at: DateTime<Utc>) -> String {
let separator = if path_and_query.contains('?') {
'&'
} else {
'?'
};
let unsigned = format!(
"{path_and_query}{separator}expires={}",
expires_at.timestamp()
);
let signature = hex::encode(self.mac(&unsigned).finalize().into_bytes());
format!("{unsigned}&signature={signature}")
}
pub fn verify(&self, uri: &Uri, now: DateTime<Utc>) -> bool {
let Some(query) = uri.query() else {
return false;
};
let mut signature = None;
let mut expires = None;
let mut rest = Vec::new();
for pair in query.split('&') {
match pair.split_once('=') {
Some(("signature", value)) => {
if signature.replace(value).is_some() {
return false;
}
}
Some(("expires", value)) => {
expires = value.parse::<i64>().ok();
rest.push(pair);
}
_ => rest.push(pair),
}
}
let (Some(signature), Some(expires)) = (signature, expires) else {
return false;
};
if now.timestamp() >= expires {
return false;
}
let Ok(signature) = hex::decode(signature) else {
return false;
};
let unsigned = format!("{}?{}", uri.path(), rest.join("&"));
self.mac(&unsigned).verify_slice(&signature).is_ok()
}
pub(crate) fn derived_from_app_key(app_key: &str) -> Self {
let mut mac = <Hmac<Sha256> as KeyInit>::new_from_slice(app_key.as_bytes())
.expect("HMAC accepts keys of any length");
mac.update(b"navree-http/url-signing/v1");
Self::new(&mac.finalize().into_bytes())
}
fn mac(&self, message: &str) -> Hmac<Sha256> {
let mut mac = <Hmac<Sha256> as KeyInit>::new_from_slice(&self.key)
.expect("HMAC accepts keys of any length");
mac.update(message.as_bytes());
mac
}
}
impl fmt::Debug for UrlSigner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UrlSigner")
.field("key", &"[redacted]")
.finish()
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct ValidSignature;
impl ValidSignature {
pub fn guard() -> Arc<dyn Guard> {
Arc::new(Self)
}
}
impl Guard for ValidSignature {
fn check<'a>(&'a self, parts: &'a mut Parts) -> BoxFuture<'a, Result<(), AppError>> {
Box::pin(async move {
let ctx = parts
.extensions
.get::<AppContext>()
.cloned()
.ok_or_else(|| {
AppError::internal("ValidSignature runs outside the navree pipeline")
})?;
let signer = ctx.get::<UrlSigner>().await?;
let now = match ctx.try_get::<dyn Clock>().await {
Some(clock) => clock.now(),
None => Utc::now(),
};
if signer.verify(&parts.uri, now) {
Ok(())
} else {
Err(AppError::new(
http::StatusCode::UNAUTHORIZED,
crate::codes::INVALID_SIGNATURE,
"Invalid signature.",
))
}
})
}
}
#[cfg(test)]
mod tests {
use chrono::Duration;
use super::*;
fn uri(s: &str) -> Uri {
s.parse().unwrap()
}
#[test]
fn signed_url_roundtrip_and_expiry() {
let signer = UrlSigner::new(b"key-1");
let now = Utc::now();
let url = signer.sign("/verify/1?hash=abc", now + Duration::seconds(60));
assert!(url.starts_with("/verify/1?hash=abc&expires="));
assert!(signer.verify(&uri(&url), now));
assert!(signer.verify(&uri(&url), now + Duration::seconds(59)));
assert!(
!signer.verify(&uri(&url), now + Duration::seconds(60)),
"expired"
);
let no_query = signer.sign("/download", now + Duration::seconds(60));
assert!(no_query.starts_with("/download?expires="));
assert!(signer.verify(&uri(&no_query), now));
}
#[test]
fn tampering_or_another_key_fails() {
let signer = UrlSigner::new(b"key-1");
let now = Utc::now();
let url = signer.sign("/verify/1?hash=abc", now + Duration::seconds(60));
assert!(!signer.verify(&uri(&url.replace("/verify/1", "/verify/2")), now));
assert!(!signer.verify(&uri(&url.replace("hash=abc", "hash=abd")), now));
let later = (now + Duration::days(1)).timestamp().to_string();
let expires = url
.split("expires=")
.nth(1)
.unwrap()
.split('&')
.next()
.unwrap();
assert!(!signer.verify(&uri(&url.replace(expires, &later)), now));
assert!(!UrlSigner::new(b"key-2").verify(&uri(&url), now));
assert!(!signer.verify(&uri("/verify/1?hash=abc"), now), "unsigned");
assert!(
!signer.verify(&uri(&format!("{url}&signature=00")), now),
"two signatures"
);
assert!(!signer.verify(&uri(&url.replace("signature=", "signature=zz")), now));
}
#[test]
fn debug_hides_the_key() {
assert!(!format!("{:?}", UrlSigner::new(b"hunter2")).contains("hunter2"));
}
}