use std::collections::HashMap;
use std::fmt;
use std::path::Path;
use std::sync::{Arc, Mutex};
use adk_core::Clock;
use base64ct::{Base64UrlUnpadded, Encoding as _};
use chrono::{DateTime, Duration, Utc};
use p256::ecdsa::signature::Signer as _;
use p256::ecdsa::{Signature, SigningKey};
use sha2::{Digest as _, Sha256};
use super::error::MobileError;
const TOKEN_LIFETIME: Duration = Duration::hours(12);
const RESIGN_AFTER: Duration = Duration::hours(1);
#[derive(Clone)]
pub struct PushKey {
signing: SigningKey,
public: String,
id: String,
}
impl fmt::Debug for PushKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "PushKey({})", self.id)
}
}
impl PushKey {
pub const FILE: &'static str = "push.key";
pub fn generate() -> Result<Self, MobileError> {
loop {
let mut bytes = [0u8; 32];
getrandom::fill(&mut bytes).map_err(|error| MobileError::PushKey {
reason: format!("the operating system's random source failed: {error}"),
})?;
if let Ok(key) = Self::from_bytes(bytes) {
return Ok(key);
}
}
}
pub fn from_bytes(bytes: [u8; 32]) -> Result<Self, MobileError> {
let signing = SigningKey::from_bytes(&bytes.into()).map_err(|_| MobileError::PushKey {
reason: "the bytes are not a P-256 secret key".into(),
})?;
let point = signing.verifying_key().to_encoded_point(false);
let public = Base64UrlUnpadded::encode_string(point.as_bytes());
let digest = Sha256::digest(point.as_bytes());
let id = Base64UrlUnpadded::encode_string(&digest[..12]);
Ok(Self { signing, public, id })
}
pub fn load_or_create(path: &Path) -> Result<Self, MobileError> {
let unusable = |error: std::io::Error| MobileError::PushKey {
reason: format!("{}: {error}", path.display()),
};
match std::fs::read_to_string(path) {
Ok(text) if text.trim().is_empty() => {
let key = Self::generate()?;
let mut file = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(path)
.map_err(unusable)?;
key.write_to(&mut file).map_err(unusable)?;
return Ok(key);
}
Ok(text) => return Self::parse(path, &text),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(unusable(error)),
}
let key = Self::generate()?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(unusable)?;
}
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
match options.open(path) {
Ok(mut file) => {
key.write_to(&mut file).map_err(unusable)?;
Ok(key)
}
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
Self::parse(path, &std::fs::read_to_string(path).map_err(unusable)?)
}
Err(error) => Err(unusable(error)),
}
}
fn write_to(&self, file: &mut std::fs::File) -> std::io::Result<()> {
use std::io::Write as _;
let bytes = self.signing.to_bytes();
let hex: String = bytes.iter().map(|byte| format!("{byte:02x}")).collect();
writeln!(file, "{hex}")?;
file.sync_all()
}
fn parse(path: &Path, text: &str) -> Result<Self, MobileError> {
let text = text.trim();
let invalid = || MobileError::PushKey {
reason: format!("{} does not hold 64 hex digits", path.display()),
};
if text.len() != 64 || !text.is_ascii() {
return Err(invalid());
}
let mut bytes = [0u8; 32];
for (index, byte) in bytes.iter_mut().enumerate() {
*byte =
u8::from_str_radix(&text[index * 2..index * 2 + 2], 16).map_err(|_| invalid())?;
}
Self::from_bytes(bytes)
}
pub fn id(&self) -> &str {
&self.id
}
pub fn public_key(&self) -> &str {
&self.public
}
fn sign(&self, message: &[u8]) -> Vec<u8> {
let signature: Signature = self.signing.sign(message);
signature.to_bytes().to_vec()
}
}
#[derive(Clone)]
pub(crate) struct Vapid {
key: PushKey,
subject: String,
clock: Arc<dyn Clock>,
signed: Arc<Mutex<Signed>>,
}
type Signed = HashMap<String, (String, DateTime<Utc>)>;
impl fmt::Debug for Vapid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Vapid").field("key", &self.key).field("subject", &self.subject).finish()
}
}
impl Vapid {
pub(crate) fn new(key: PushKey, subject: String, clock: Arc<dyn Clock>) -> Self {
Self { key, subject, clock, signed: Arc::default() }
}
pub(crate) fn key(&self) -> &PushKey {
&self.key
}
pub(crate) fn authorization(&self, origin: &str) -> String {
format!("vapid t={}, k={}", self.token(origin), self.key.public_key())
}
fn token(&self, origin: &str) -> String {
let now = self.clock.now();
let mut signed = self.signed.lock().unwrap_or_else(|e| e.into_inner());
if let Some((token, at)) = signed.get(origin)
&& now - *at < RESIGN_AFTER
&& now >= *at
{
return token.clone();
}
let header = Base64UrlUnpadded::encode_string(br#"{"typ":"JWT","alg":"ES256"}"#);
let claims = serde_json::json!({
"aud": origin,
"exp": (now + TOKEN_LIFETIME).timestamp(),
"sub": self.subject,
});
let claims = Base64UrlUnpadded::encode_string(claims.to_string().as_bytes());
let input = format!("{header}.{claims}");
let signature = Base64UrlUnpadded::encode_string(&self.key.sign(input.as_bytes()));
let token = format!("{input}.{signature}");
signed.insert(origin.to_string(), (token.clone(), now));
token
}
}
#[cfg(test)]
mod tests {
use adk_core::ManualClock;
use chrono::TimeZone as _;
use p256::ecdsa::VerifyingKey;
use p256::ecdsa::signature::Verifier as _;
use super::*;
fn clock() -> Arc<ManualClock> {
Arc::new(ManualClock::new(Utc.with_ymd_and_hms(2026, 10, 24, 18, 0, 0).unwrap()))
}
fn parts(header: &str) -> (String, serde_json::Value, Vec<u8>, String) {
let (token, key) = header
.strip_prefix("vapid t=")
.and_then(|rest| rest.split_once(", k="))
.expect("vapid t=…, k=…");
let (input, signature) = token.rsplit_once('.').unwrap();
let claims = input.split_once('.').unwrap().1;
let claims =
serde_json::from_slice(&Base64UrlUnpadded::decode_vec(claims).unwrap()).unwrap();
(
input.to_string(),
claims,
Base64UrlUnpadded::decode_vec(signature).unwrap(),
key.to_string(),
)
}
#[test]
fn a_token_is_es256_for_the_origin() {
let clock = clock();
let key = PushKey::generate().unwrap();
let vapid = Vapid::new(key.clone(), "https://home.example.ts.net".into(), clock.clone());
let (input, claims, signature, public) =
parts(&vapid.authorization("https://web.push.apple.com"));
assert_eq!(public, key.public_key());
assert_eq!(claims["aud"], "https://web.push.apple.com");
assert_eq!(claims["sub"], "https://home.example.ts.net");
assert_eq!(claims["exp"], (clock.now() + Duration::hours(12)).timestamp());
let verifying = VerifyingKey::from_sec1_bytes(
&Base64UrlUnpadded::decode_vec(key.public_key()).unwrap(),
)
.unwrap();
let signature = Signature::from_slice(&signature).unwrap();
verifying.verify(input.as_bytes(), &signature).unwrap();
}
#[test]
fn a_token_is_reused_for_an_hour_per_origin() {
let clock = clock();
let vapid = Vapid::new(
PushKey::generate().unwrap(),
"mailto:ana@example.com".into(),
clock.clone(),
);
let apple = vapid.authorization("https://web.push.apple.com");
let google = vapid.authorization("https://fcm.googleapis.com");
assert_ne!(apple, google);
clock.advance(std::time::Duration::from_secs(59 * 60));
assert_eq!(vapid.authorization("https://web.push.apple.com"), apple);
clock.advance(std::time::Duration::from_secs(60));
assert_ne!(vapid.authorization("https://web.push.apple.com"), apple);
}
#[test]
fn a_key_file_that_is_not_a_key_is_refused() {
let home = tempfile::tempdir().unwrap();
let path = home.path().join(PushKey::FILE);
std::fs::write(&path, "not a key\n").unwrap();
let error = PushKey::load_or_create(&path).unwrap_err();
assert!(matches!(error, MobileError::PushKey { .. }), "{error:?}");
}
#[cfg(unix)]
#[test]
fn a_new_key_file_is_its_owner_s_only() {
use std::os::unix::fs::PermissionsExt as _;
let home = tempfile::tempdir().unwrap();
let path = home.path().join(PushKey::FILE);
PushKey::load_or_create(&path).unwrap();
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
}
}