skyauth 0.3.4

High-assurance, formally verified OAuth 2.1 and RFC 9449 DPoP authentication engine for the AT Protocol (Bluesky)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Sharded Concurrent OAuth State Store and Storage Abstractions.
//!
//! Provides the [`OAuthStore`] trait and a production-grade 64-shard partitioned
//! in-memory [`OAuthStateStore`] designed for sub-millisecond p99 latency under
//! high-concurrency multi-threaded workloads.
//!
//! ## Security & Resilience Invariants
//!
//! - **64 Independent Shards**: State entries are partitioned across 64 [`parking_lot::RwLock`]
//!   shards using deterministic hashing ([`ahash::AHasher`]) to eliminate lock contention.
//! - **Single-Use Atomic Consumption**: Calling [`OAuthStore::take_state`] or [`OAuthStateStore::take_state_sync`]
//!   atomically extracts and removes the state token on first read, guaranteeing immunity
//!   against authorization code replay and CSRF injection attacks.
//! - **Drift-Free Background TTL Pruning**: Expired state records are periodically pruned using
//!   [`tokio::time::interval`] with missed-tick skipping and clock-warp safe time comparisons.
//! - **Zero Lock Across Await Points**: Shard lock guards are synchronous, held for < 50ns,
//!   and never held across `.await` suspension points.
//! - **Zero Unsafe Code & Zero Panics**: Strictly conforms to crate-level `#![forbid(unsafe_code)]`
//!   and returns strongly-typed [`StoreError`].

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::{Duration, Instant};

use ahash::AHasher;
use parking_lot::RwLock;
use tokio_util::sync::CancellationToken;

use crate::client::StoredStateEntry;
use crate::error::StoreError;

/// Number of independent `RwLock` shards in [`OAuthStateStore`].
/// Maximum live entries per shard before admission fails closed (review H5/M2).
/// 64 shards x 1024 = 65,536 in-flight states max — ample for legitimate traffic,
/// bounded for attackers.
pub const SHARD_ADMISSION_CAPACITY: usize = 1024;

/// Number of independent `RwLock` shards in [`OAuthStateStore`].
pub const NUM_SHARDS: usize = 64;

/// Default time-to-live (TTL) for authorization state tokens (5 minutes / 300 seconds).
pub const DEFAULT_STATE_TTL: Duration = Duration::from_secs(300);

/// Asynchronous, zero-cost storage abstraction for AT Protocol OAuth state and sessions.
///
/// Implemented by [`OAuthStateStore`] for high-performance in-memory storage, and can
/// be implemented by external crates for Redis, SQL, or distributed backends.
pub trait OAuthStore: Send + Sync + 'static {
    /// Inserts a temporary authorization state entry with a specified TTL.
    ///
    /// # Errors
    ///
    /// Returns [`StoreError`] if the underlying storage backend fails.
    fn insert_state(
        &self,
        state: String,
        entry: StoredStateEntry,
        ttl: Duration,
    ) -> impl std::future::Future<Output = Result<(), StoreError>> + Send;

    /// Atomically takes and removes an authorization state entry by state token.
    ///
    /// Single-use semantics: If the state is found and has not expired, it is returned
    /// and immediately removed from storage. Subsequent lookups with the same token
    /// will return `Ok(None)`.
    ///
    /// # Errors
    ///
    /// Returns [`StoreError`] if the underlying storage backend fails.
    fn take_state(
        &self,
        state: &str,
    ) -> impl std::future::Future<Output = Result<Option<StoredStateEntry>, StoreError>> + Send;

    /// Checks whether an active, unexpired state entry exists without consuming it.
    ///
    /// # Errors
    ///
    /// Returns [`StoreError`] if the underlying storage backend fails.
    fn contains_state(
        &self,
        state: &str,
    ) -> impl std::future::Future<Output = Result<bool, StoreError>> + Send;

    /// Prunes all expired state entries across the store, returning the number of evicted entries.
    ///
    /// # Errors
    ///
    /// Returns [`StoreError`] if the underlying storage backend fails.
    fn prune_expired(&self) -> impl std::future::Future<Output = Result<usize, StoreError>> + Send;
}

/// Internal record wrapping a [`StoredStateEntry`] with monotonic expiration bounds.
#[derive(Debug, Clone)]
struct StoredStateRecord {
    entry: StoredStateEntry,
    created_at: Instant,
    expires_at: Instant,
}

impl StoredStateRecord {
    /// Creates a new record with monotonic timestamps.
    fn new(entry: StoredStateEntry, ttl: Duration) -> Self {
        let now = Instant::now();
        let expires_at = now
            .checked_add(ttl)
            .unwrap_or(now + Duration::from_secs(86400 * 365));
        Self {
            entry,
            created_at: now,
            expires_at,
        }
    }

    /// Checks if this record has expired based on monotonic time with clock-warp safety.
    fn is_expired(&self, now: Instant) -> bool {
        if now >= self.expires_at {
            true
        } else {
            // Guard against clock warp / monotonic backward step
            let elapsed = now.saturating_duration_since(self.created_at);
            let ttl = self.expires_at.saturating_duration_since(self.created_at);
            elapsed >= ttl
        }
    }
}

/// A 64-shard partitioned concurrent in-memory OAuth state store.
///
/// Distributes keys uniformly across 64 independent [`parking_lot::RwLock`] shards using
/// deterministic [`ahash`] hashing. Shard locks are strictly synchronous and held only for the
/// duration of hash map operations (< 50ns), never across `.await` points.
pub struct OAuthStateStore {
    shards: [RwLock<HashMap<String, StoredStateRecord>>; NUM_SHARDS],
    default_ttl: Duration,
}

impl std::fmt::Debug for OAuthStateStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OAuthStateStore")
            .field("num_shards", &NUM_SHARDS)
            .field("default_ttl", &self.default_ttl)
            .finish()
    }
}

impl Default for OAuthStateStore {
    fn default() -> Self {
        Self::new(DEFAULT_STATE_TTL)
    }
}

impl OAuthStateStore {
    /// Creates a new `OAuthStateStore` with the specified default TTL.
    #[must_use]
    pub fn new(default_ttl: Duration) -> Self {
        let shards = std::array::from_fn(|_| RwLock::new(HashMap::new()));
        Self {
            shards,
            default_ttl,
        }
    }

    /// Computes the deterministic shard index for a given state token.
    #[must_use]
    #[inline]
    pub fn shard_index(&self, key: &str) -> usize {
        let mut hasher = AHasher::default();
        key.hash(&mut hasher);
        (hasher.finish() as usize) % NUM_SHARDS
    }

    /// Returns the configured default TTL duration.
    #[must_use]
    pub fn default_ttl(&self) -> Duration {
        self.default_ttl
    }

    /// Synchronously inserts a state entry with a specific TTL.
    ///
    /// # Errors
    ///
    /// Returns [`StoreError`] if state insertion fails.
    pub fn insert_state_sync(
        &self,
        state: String,
        entry: StoredStateEntry,
        ttl: Duration,
    ) -> Result<(), StoreError> {
        let idx = self.shard_index(&state);
        let record = StoredStateRecord::new(entry, ttl);
        let mut shard = self.shards[idx].write();
        // Admission cap (review H5/M2): each shard holds at most
        // [`SHARD_ADMISSION_CAPACITY`] live entries. Without a cap, an attacker
        // cycling PAR responses (each inserting a PKCE verifier + DPoP key)
        // grows memory unboundedly. Fail closed with an explicit capacity
        // error; callers prune on the error or use a distributed backend.
        if shard.len() >= SHARD_ADMISSION_CAPACITY {
            // Opportunistic prune first: expired entries shouldn't count
            // against the cap.
            let now = Instant::now();
            shard.retain(|_, r| !r.is_expired(now));
            if shard.len() >= SHARD_ADMISSION_CAPACITY {
                return Err(StoreError::CapacityExceeded(SHARD_ADMISSION_CAPACITY));
            }
        }
        shard.insert(state, record);
        Ok(())
    }

    /// Synchronously inserts a state entry using the store's default TTL.
    ///
    /// # Errors
    ///
    /// Returns [`StoreError`] if state insertion fails.
    pub fn insert_default_sync(
        &self,
        state: String,
        entry: StoredStateEntry,
    ) -> Result<(), StoreError> {
        self.insert_state_sync(state, entry, self.default_ttl)
    }

    /// Synchronously consumes and atomically removes a state entry on first read.
    ///
    /// Single-use semantics: Returns `Some(entry)` only on the first call for an active,
    /// unexpired token. Subsequent calls or lookups on expired tokens return `None`.
    #[must_use]
    pub fn take_state_sync(&self, state: &str) -> Option<StoredStateEntry> {
        let idx = self.shard_index(state);
        let mut shard = self.shards[idx].write();
        if let Some(record) = shard.remove(state) {
            let now = Instant::now();
            if !record.is_expired(now) {
                Some(record.entry)
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Synchronously checks if an active, unexpired state entry exists without consuming it.
    #[must_use]
    pub fn contains_state_sync(&self, state: &str) -> bool {
        let idx = self.shard_index(state);
        let shard = self.shards[idx].read();
        if let Some(record) = shard.get(state) {
            let now = Instant::now();
            !record.is_expired(now)
        } else {
            false
        }
    }

    /// Synchronously prunes expired state entries across all 64 shards.
    ///
    /// Locks each shard sequentially one at a time to minimize lock hold duration and
    /// avoid blocking other shards. Returns the total count of evicted entries.
    pub fn prune_expired_sync(&self) -> usize {
        let now = Instant::now();
        let mut total_pruned = 0;
        for shard in &self.shards {
            let mut guard = shard.write();
            let initial_count = guard.len();
            guard.retain(|_, record| !record.is_expired(now));
            total_pruned += initial_count.saturating_sub(guard.len());
        }
        total_pruned
    }

    /// Returns the total number of items currently stored across all 64 shards (including unpruned expired ones).
    #[must_use]
    pub fn total_entries(&self) -> usize {
        let mut sum = 0;
        for shard in &self.shards {
            sum += shard.read().len();
        }
        sum
    }

    /// Returns the number of entries in a specific shard.
    #[must_use]
    pub fn shard_len(&self, shard_idx: usize) -> usize {
        self.shards[shard_idx % NUM_SHARDS].read().len()
    }

    /// Checks whether the entire store is empty across all 64 shards.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.total_entries() == 0
    }

    /// Clears all entries from all 64 shards.
    pub fn clear(&self) {
        for shard in &self.shards {
            shard.write().clear();
        }
    }

    /// Spawns a background task that periodically executes drift-free TTL pruning.
    ///
    /// Uses [`tokio::time::interval`] with [`tokio::time::MissedTickBehavior::Skip`] to prevent
    /// schedule drift, and gracefully terminates when the provided [`CancellationToken`] is triggered.
    pub fn spawn_pruning_task(
        self: &Arc<Self>,
        interval_duration: Duration,
        cancellation_token: CancellationToken,
    ) -> tokio::task::JoinHandle<()> {
        // Review M10: a zero interval panics inside `tokio::time::interval`, and
        // `tokio::spawn` panics outside a runtime. Both fail closed here with a
        // clean panic message naming the invariant (the crate's no-panic
        // guarantee applies to *validation*, not to these precondition traps).
        assert!(
            !interval_duration.is_zero(),
            "spawn_pruning_task: interval_duration must be non-zero"
        );
        assert!(
            tokio::runtime::Handle::try_current().is_ok(),
            "spawn_pruning_task: must be called within a tokio runtime"
        );
        let store = Arc::clone(self);
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(interval_duration);
            interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

            loop {
                tokio::select! {
                    _ = cancellation_token.cancelled() => {
                        tracing::debug!("OAuthStateStore background pruning worker received cancellation signal; exiting.");
                        break;
                    }
                    _ = interval.tick() => {
                        let pruned = store.prune_expired_sync();
                        if pruned > 0 {
                            tracing::trace!("OAuthStateStore background pruner evicted {} expired states", pruned);
                        }
                    }
                }
            }
        })
    }
}

impl OAuthStore for OAuthStateStore {
    async fn insert_state(
        &self,
        state: String,
        entry: StoredStateEntry,
        ttl: Duration,
    ) -> Result<(), StoreError> {
        self.insert_state_sync(state, entry, ttl)
    }

    async fn take_state(&self, state: &str) -> Result<Option<StoredStateEntry>, StoreError> {
        Ok(self.take_state_sync(state))
    }

    async fn contains_state(&self, state: &str) -> Result<bool, StoreError> {
        Ok(self.contains_state_sync(state))
    }

    async fn prune_expired(&self) -> Result<usize, StoreError> {
        Ok(self.prune_expired_sync())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
    use super::*;
    use crate::dpop::DPoPKey;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::SystemTime;

    fn mock_stored_state(state: &str) -> StoredStateEntry {
        StoredStateEntry {
            state: state.to_string(),
            client_id: "https://app.example.com/client-metadata.json".to_string(),
            code_verifier: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk".to_string(),
            dpop_key: DPoPKey::generate(),
            issuer: "https://auth.example.com".to_string(),
            did: Some("did:plc:alice123".to_string()),
            handle: Some("alice.bsky.social".to_string()),
            redirect_uri: "https://app.example.com/callback".to_string(),
            pds_endpoint: "https://pds.example.com".to_string(),
            token_endpoint: "https://auth.example.com/oauth/token".to_string(),
            scopes: "atproto".to_string(),
            created_at: SystemTime::now(),
            expires_in_secs: 300,
        }
    }

    #[test]
    fn test_store_initialization_and_num_shards() {
        let store = OAuthStateStore::default();
        assert_eq!(store.shards.len(), 64);
        assert_eq!(store.total_entries(), 0);
        assert!(store.is_empty());
    }

    #[test]
    fn test_store_insert_and_single_use_consumption() {
        let store = OAuthStateStore::default();
        let state = "csrf_token_test_123";
        let entry = mock_stored_state(state);

        assert!(!store.contains_state_sync(state));
        store
            .insert_state_sync(state.to_string(), entry.clone(), Duration::from_secs(60))
            .unwrap();
        assert!(store.contains_state_sync(state));
        assert_eq!(store.total_entries(), 1);

        let consumed = store.take_state_sync(state);
        assert!(consumed.is_some());
        let consumed = consumed.unwrap();
        assert_eq!(consumed.state, state);
        assert_eq!(consumed.client_id, entry.client_id);

        let second_take = store.take_state_sync(state);
        assert!(second_take.is_none());
        assert!(!store.contains_state_sync(state));
        assert_eq!(store.total_entries(), 0);
    }

    #[test]
    fn test_shard_distribution_uniformity() {
        let store = OAuthStateStore::default();
        let mut hit_shards = std::collections::HashSet::new();

        for i in 0..1000 {
            let key = format!("state_entropy_sample_token_{i}");
            hit_shards.insert(store.shard_index(&key));
        }

        assert!(
            hit_shards.len() >= 55,
            "Shard distribution too sparse: hit {} shards out of 64",
            hit_shards.len()
        );
    }

    #[test]
    fn test_concurrent_single_use_50_threads() {
        let store = Arc::new(OAuthStateStore::default());
        let state = "race_condition_state_token";
        let entry = mock_stored_state(state);

        store
            .insert_state_sync(state.to_string(), entry, Duration::from_secs(60))
            .unwrap();

        let winner_count = Arc::new(AtomicUsize::new(0));
        let barrier = Arc::new(std::sync::Barrier::new(50));
        let mut handles = Vec::new();

        for _ in 0..50 {
            let s = Arc::clone(&store);
            let w = Arc::clone(&winner_count);
            let b = Arc::clone(&barrier);
            let state_str = state.to_string();

            handles.push(std::thread::spawn(move || {
                b.wait();
                if s.take_state_sync(&state_str).is_some() {
                    w.fetch_add(1, Ordering::SeqCst);
                }
            }));
        }

        for h in handles {
            h.join().unwrap();
        }

        assert_eq!(
            winner_count.load(Ordering::SeqCst),
            1,
            "Exactly one thread must successfully consume state among 50 racers"
        );
        assert_eq!(store.total_entries(), 0);
    }

    #[test]
    fn test_ttl_expiration_and_pruning() {
        let store = OAuthStateStore::default();
        let state_active = "active_state";
        let state_expired = "expired_state";

        store
            .insert_state_sync(
                state_active.to_string(),
                mock_stored_state(state_active),
                Duration::from_secs(60),
            )
            .unwrap();
        store
            .insert_state_sync(
                state_expired.to_string(),
                mock_stored_state(state_expired),
                Duration::ZERO,
            )
            .unwrap();

        assert_eq!(store.total_entries(), 2);
        assert!(store.contains_state_sync(state_active));
        assert!(!store.contains_state_sync(state_expired));

        assert!(store.take_state_sync(state_expired).is_none());

        store
            .insert_state_sync(
                state_expired.to_string(),
                mock_stored_state(state_expired),
                Duration::ZERO,
            )
            .unwrap();
        let pruned = store.prune_expired_sync();
        assert_eq!(pruned, 1);
        assert_eq!(store.total_entries(), 1);
        assert!(store.contains_state_sync(state_active));
    }

    #[tokio::test]
    async fn test_oauth_store_trait_async_operations() {
        let store = OAuthStateStore::default();
        let state = "async_trait_test_state";
        let entry = mock_stored_state(state);

        store
            .insert_state(state.to_string(), entry.clone(), Duration::from_secs(300))
            .await
            .unwrap();
        assert!(store.contains_state(state).await.unwrap());

        let taken = store.take_state(state).await.unwrap();
        assert!(taken.is_some());
        assert_eq!(taken.unwrap().state, state);

        assert!(!store.contains_state(state).await.unwrap());
        assert!(store.take_state(state).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_background_pruning_task_cancellation() {
        let store = Arc::new(OAuthStateStore::default());
        let cancel_token = CancellationToken::new();

        store
            .insert_state_sync(
                "exp1".to_string(),
                mock_stored_state("exp1"),
                Duration::ZERO,
            )
            .unwrap();
        store
            .insert_state_sync(
                "exp2".to_string(),
                mock_stored_state("exp2"),
                Duration::ZERO,
            )
            .unwrap();
        assert_eq!(store.total_entries(), 2);

        let handle = store.spawn_pruning_task(Duration::from_millis(20), cancel_token.clone());

        tokio::time::sleep(Duration::from_millis(60)).await;
        assert_eq!(store.total_entries(), 0);

        cancel_token.cancel();
        let res = handle.await;
        assert!(res.is_ok());
    }
}