use axum::{body::Body, http::Request};
use pubky_common::crypto::PublicKey;
pub(crate) fn extract_legacy_pubky(req: &Request<Body>) -> Option<PublicKey> {
let mut pubky = None;
for header in ["host", "pubky-host"].iter() {
if let Some(val) = req.headers().get(*header) {
if let Ok(s) = val.to_str() {
if PublicKey::is_pubky_prefixed(s) {
continue;
}
if let Ok(key) = PublicKey::try_from_z32(s) {
pubky = Some(key);
}
}
}
}
if pubky.is_none() {
pubky = req.uri().query().and_then(|query| {
query.split('&').find_map(|pair| {
let mut parts = pair.splitn(2, '=');
if let (Some(key), Some(val)) = (parts.next(), parts.next()) {
if key == "pubky-host" {
if PublicKey::is_pubky_prefixed(val) {
return None;
}
return PublicKey::try_from_z32(val).ok();
}
}
None
})
});
}
pubky
}