Skip to main content

hyprstream_core/services/oauth/
replay_key.rs

1//! Fixed-size keys for OAuth replay barriers.
2//!
3//! A replay barrier keeps one map entry and one heap node for each live key.
4//! On the supported 64-bit targets, a `[u8; 32]` key plus `Entry<()>` is 56
5//! bytes and a heap node is 56 bytes. `HashMap` needs at most eight buckets
6//! for seven live entries, so the map contributes at most 64 bytes per live
7//! key before allocation growth; together that is 120 bytes. Capacity planning
8//! rounds this to 128 bytes per live key before allocator slack. Unlike a
9//! `String`, every retained part of this key is fixed-size.
10//!
11//! Replay identifiers are controlled by remote callers. Keeping their raw
12//! strings in a bounded entry-count cache would still leave retained memory
13//! unbounded, and `TtlCache` stores each key in both its map and expiry heap.
14//! A BLAKE3-256 digest makes retained key storage exactly 32 bytes per copy.
15
16/// BLAKE3-256 replay-barrier key.
17pub type ReplayKey = [u8; 32];
18
19/// Conservative per-live-entry planning size for a no-refresh replay barrier.
20///
21/// See this module's documentation for the 64-bit layout derivation. The
22/// cache's geometric allocations make its exact resident size a fixed function
23/// of its entry cap, rather than an exact multiple of this number.
24pub const REPLAY_BARRIER_ENTRY_BYTES: usize = 128;
25
26fn digest(domain: &[u8], components: &[&[u8]]) -> ReplayKey {
27    let mut hasher = blake3::Hasher::new();
28    hasher.update(b"hyprstream:replay-key:v1\0");
29    hasher.update(&(domain.len() as u64).to_be_bytes());
30    hasher.update(domain);
31    for component in components {
32        hasher.update(&(component.len() as u64).to_be_bytes());
33        hasher.update(component);
34    }
35    *hasher.finalize().as_bytes()
36}
37
38/// Digest a DPoP proof JTI in its own replay domain.
39pub fn dpop_jti(jti: &str) -> ReplayKey {
40    digest(b"dpop", &[jti.as_bytes()])
41}
42
43/// Digest a one-use mount ticket JTI in its own replay domain.
44pub fn mount_ticket_jti(jti: &str) -> ReplayKey {
45    digest(b"mount-ticket", &[jti.as_bytes()])
46}
47
48/// Digest a length-framed client-ID/JTI tuple in the client-assertion domain.
49pub fn client_assertion_jti(client_id: &str, jti: &str) -> ReplayKey {
50    digest(b"client-assertion", &[client_id.as_bytes(), jti.as_bytes()])
51}
52
53/// Digest a length-framed issuer/JTI tuple in the ATProto assertion domain.
54pub fn atproto_service_assertion_jti(issuer: &str, jti: &str) -> ReplayKey {
55    digest(
56        b"atproto-service-assertion",
57        &[issuer.as_bytes(), jti.as_bytes()],
58    )
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn keys_are_fixed_size_domain_separated_and_tuple_framed() {
67        assert_eq!(std::mem::size_of::<ReplayKey>(), 32);
68        assert_eq!(REPLAY_BARRIER_ENTRY_BYTES, 128);
69        assert_ne!(dpop_jti("mount-ticket:x"), mount_ticket_jti("x"));
70        assert_ne!(dpop_jti("same"), mount_ticket_jti("same"));
71        assert_ne!(
72            client_assertion_jti("a", "b\x1fc"),
73            client_assertion_jti("a\x1fb", "c")
74        );
75        assert_ne!(
76            atproto_service_assertion_jti("a", "b\x1fc"),
77            atproto_service_assertion_jti("a\x1fb", "c")
78        );
79    }
80}