Skip to main content

vac_sidecar/
revocation.rs

1use std::collections::HashSet;
2use crate::error::VacError;
3
4/// Revocation filter for efficient token revocation checking
5/// 
6/// Phase 3: Using HashSet for simplicity. In production, this should use a Bloom Filter
7/// for memory efficiency (100k sessions → ~100KB vs ~3.2MB with HashSet).
8/// 
9/// For Phase 3, HashSet provides O(1) lookup with no false positives (simpler to debug).
10pub struct RevocationFilter {
11    revoked_tokens: HashSet<[u8; 32]>, // Set of revoked token IDs
12}
13
14impl RevocationFilter {
15    /// Create a new revocation filter
16    pub fn new() -> Self {
17        Self {
18            revoked_tokens: HashSet::new(),
19        }
20    }
21    
22    /// Check if a token ID is revoked
23    pub fn is_revoked(&self, token_id: &[u8]) -> bool {
24        if token_id.len() != 32 {
25            // Invalid token ID format, reject to be safe
26            return true;
27        }
28        
29        let mut hash = [0u8; 32];
30        hash.copy_from_slice(token_id);
31        self.revoked_tokens.contains(&hash)
32    }
33    
34    /// Add a token ID to the revocation list
35    pub fn revoke(&mut self, token_id: &[u8]) -> Result<(), VacError> {
36        if token_id.len() != 32 {
37            return Err(VacError::InternalError(
38                format!("Invalid token ID length: expected 32 bytes, got {}", token_id.len())
39            ));
40        }
41        
42        let mut hash = [0u8; 32];
43        hash.copy_from_slice(token_id);
44        self.revoked_tokens.insert(hash);
45        Ok(())
46    }
47    
48    /// Update the filter with a list of revoked token IDs (from heartbeat response)
49    pub fn update_from_ids(&mut self, revoked_ids: Vec<[u8; 32]>) {
50        for id in revoked_ids {
51            self.revoked_tokens.insert(id);
52        }
53    }
54    
55    /// Get the number of revoked tokens
56    pub fn revoked_count(&self) -> usize {
57        self.revoked_tokens.len()
58    }
59}
60
61impl Default for RevocationFilter {
62    fn default() -> Self {
63        Self::new()
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn revocation_filter_empty_not_revoked() {
73        let f = RevocationFilter::new();
74        let id = [0u8; 32];
75        assert!(!f.is_revoked(&id));
76        assert_eq!(f.revoked_count(), 0);
77    }
78
79    #[test]
80    fn revocation_filter_revoke_then_is_revoked() {
81        let mut f = RevocationFilter::new();
82        let id = [1u8; 32];
83        assert!(!f.is_revoked(&id));
84        f.revoke(&id).unwrap();
85        assert!(f.is_revoked(&id));
86        assert_eq!(f.revoked_count(), 1);
87    }
88
89    #[test]
90    fn revocation_filter_update_from_ids() {
91        let mut f = RevocationFilter::new();
92        let id1 = [1u8; 32];
93        let id2 = [2u8; 32];
94        f.update_from_ids(vec![id1, id2]);
95        assert!(f.is_revoked(&id1));
96        assert!(f.is_revoked(&id2));
97        assert_eq!(f.revoked_count(), 2);
98    }
99
100    #[test]
101    fn revocation_filter_revoke_invalid_length() {
102        let mut f = RevocationFilter::new();
103        let short = [0u8; 16];
104        let err = f.revoke(&short).unwrap_err();
105        assert!(matches!(err, VacError::InternalError(_)));
106    }
107
108    #[test]
109    fn revocation_filter_is_revoked_invalid_length_rejects() {
110        let f = RevocationFilter::new();
111        let short = [0u8; 16];
112        assert!(f.is_revoked(&short));
113    }
114
115    #[test]
116    fn extract_token_id_deterministic() {
117        let id1 = extract_token_id("abc").unwrap();
118        let id2 = extract_token_id("abc").unwrap();
119        assert_eq!(id1, id2);
120        assert_eq!(id1.len(), 32);
121    }
122
123    #[test]
124    fn extract_token_id_different_inputs_different_ids() {
125        let id1 = extract_token_id("abc").unwrap();
126        let id2 = extract_token_id("xyz").unwrap();
127        assert_ne!(id1, id2);
128    }
129}
130
131/// Extract token ID from a Root Biscuit
132/// 
133/// The token ID is derived from the biscuit's signature/public key.
134/// For simplicity in Phase 3, we use the first 32 bytes of the base64-encoded biscuit.
135pub fn extract_token_id(biscuit_base64: &str) -> Result<[u8; 32], VacError> {
136    use sha2::{Sha256, Digest};
137    
138    // Hash the biscuit to get a consistent 32-byte ID
139    let mut hasher = Sha256::new();
140    hasher.update(biscuit_base64.as_bytes());
141    let hash = hasher.finalize();
142    
143    let mut token_id = [0u8; 32];
144    token_id.copy_from_slice(&hash);
145    Ok(token_id)
146}