capnweb-core 0.1.0

Core protocol implementation for Cap'n Web RPC - capability-based security with promise pipelining
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
// Resume Tokens for Cap'n Web Protocol Session Recovery
// Enables session suspension and resumption with full state preservation

use super::ids::IdAllocator;
use super::tables::{ExportTable, ImportTable, Value};
use super::variable_state::VariableStateManager;
use base64::{engine::general_purpose, Engine as _};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

/// Serializable session state for resume tokens
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionSnapshot {
    /// Session metadata
    pub session_id: String,
    pub created_at: u64,
    pub last_activity: u64,
    pub version: u32,

    /// ID allocation state
    pub next_positive_id: i64,
    pub next_negative_id: i64,

    /// Import table state
    pub imports: HashMap<i64, SerializableImportValue>,

    /// Export table state
    pub exports: HashMap<i64, SerializableExportValue>,

    /// Variable state
    pub variables: HashMap<String, Value>,

    /// Session configuration
    pub max_age_seconds: u64,
    pub capabilities: Vec<String>, // Capability identifiers
}

/// Serializable import value (excludes non-serializable stubs/promises)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SerializableImportValue {
    Value(Value),
    StubReference(String),    // Reference ID only
    PromiseReference(String), // Reference ID only
}

/// Serializable export value
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SerializableExportValue {
    Resolved(Value),
    Rejected(Value),
    StubReference(String),    // Reference ID only
    PromiseReference(String), // Reference ID only
}

/// Resume token containing encrypted and signed session state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResumeToken {
    /// Base64-encoded encrypted session data
    pub token_data: String,
    /// Session ID for quick identification
    pub session_id: String,
    /// Token creation timestamp
    pub issued_at: u64,
    /// Token expiration timestamp
    pub expires_at: u64,
}

/// Resume token manager for session persistence
#[derive(Debug)]
pub struct ResumeTokenManager {
    /// Secret key for token encryption/signing
    secret_key: Vec<u8>,
    /// Default token lifetime in seconds
    default_ttl: u64,
    /// Maximum session age before forced expiry
    max_session_age: u64,
}

impl ResumeTokenManager {
    /// Create a new resume token manager
    pub fn new(secret_key: Vec<u8>) -> Self {
        Self {
            secret_key,
            default_ttl: 3600,      // 1 hour default
            max_session_age: 86400, // 24 hours max
        }
    }

    /// Create a resume token manager with custom settings
    pub fn with_settings(secret_key: Vec<u8>, default_ttl: u64, max_session_age: u64) -> Self {
        Self {
            secret_key,
            default_ttl,
            max_session_age,
        }
    }

    /// Generate a secure random secret key
    pub fn generate_secret_key() -> Vec<u8> {
        use rand::RngCore;
        let mut key = vec![0u8; 32];
        rand::rng().fill_bytes(&mut key);
        key
    }

    /// Create a session snapshot from current session state
    pub async fn create_snapshot(
        &self,
        session_id: String,
        _allocator: &Arc<IdAllocator>,
        _imports: &Arc<ImportTable>,
        _exports: &Arc<ExportTable>,
        variables: Option<&VariableStateManager>,
    ) -> Result<SessionSnapshot, ResumeTokenError> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("System time should be after UNIX epoch")
            .as_secs();

        // Extract import table state
        let serializable_imports = HashMap::new();

        // Note: In a real implementation, we'd iterate over the actual import table
        // For now, we'll create a minimal snapshot structure
        tracing::info!(session_id = %session_id, "Creating session snapshot");

        // Extract variable state
        let variables_map = if let Some(var_mgr) = variables {
            var_mgr.export_variables().await
        } else {
            HashMap::new()
        };

        let snapshot = SessionSnapshot {
            session_id: session_id.clone(),
            created_at: now,
            last_activity: now,
            version: 1, // Protocol version

            // ID allocation state (simplified - would need actual state from allocator)
            next_positive_id: 1,
            next_negative_id: -1,

            imports: serializable_imports,
            exports: HashMap::new(), // Would extract from export table

            variables: variables_map,

            max_age_seconds: self.max_session_age,
            capabilities: Vec::new(), // Would list registered capabilities
        };

        Ok(snapshot)
    }

    /// Generate a resume token from a session snapshot
    pub fn generate_token(
        &self,
        snapshot: SessionSnapshot,
    ) -> Result<ResumeToken, ResumeTokenError> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("System time should be after UNIX epoch")
            .as_secs();

        let expires_at = now + self.default_ttl;

        // Serialize the snapshot
        let snapshot_data = serde_json::to_vec(&snapshot)
            .map_err(|e| ResumeTokenError::SerializationError(e.to_string()))?;

        // Create a simple signed token (in production, use proper encryption)
        let signature = self.sign_data(&snapshot_data);
        let token_payload = TokenPayload {
            snapshot: snapshot_data,
            issued_at: now,
            expires_at,
            signature,
        };

        let token_bytes = serde_json::to_vec(&token_payload)
            .map_err(|e| ResumeTokenError::SerializationError(e.to_string()))?;

        let token_data = general_purpose::STANDARD.encode(&token_bytes);

        Ok(ResumeToken {
            token_data,
            session_id: snapshot.session_id,
            issued_at: now,
            expires_at,
        })
    }

    /// Parse and validate a resume token
    pub fn parse_token(&self, token: &ResumeToken) -> Result<SessionSnapshot, ResumeTokenError> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("System time should be after UNIX epoch")
            .as_secs();

        // Check expiration
        if now > token.expires_at {
            return Err(ResumeTokenError::TokenExpired);
        }

        // Decode the token
        let token_bytes = general_purpose::STANDARD
            .decode(&token.token_data)
            .map_err(|e| ResumeTokenError::InvalidToken(e.to_string()))?;

        let token_payload: TokenPayload = serde_json::from_slice(&token_bytes)
            .map_err(|e| ResumeTokenError::InvalidToken(e.to_string()))?;

        // Verify signature
        let expected_signature = self.sign_data(&token_payload.snapshot);
        if token_payload.signature != expected_signature {
            return Err(ResumeTokenError::InvalidSignature);
        }

        // Deserialize snapshot
        let snapshot: SessionSnapshot = serde_json::from_slice(&token_payload.snapshot)
            .map_err(|e| ResumeTokenError::InvalidToken(e.to_string()))?;

        // Additional validation
        if snapshot.created_at + snapshot.max_age_seconds < now {
            return Err(ResumeTokenError::SessionTooOld);
        }

        Ok(snapshot)
    }

    /// Restore session state from a snapshot
    pub async fn restore_session(
        &self,
        snapshot: SessionSnapshot,
        _allocator: &Arc<IdAllocator>,
        _imports: &Arc<ImportTable>,
        _exports: &Arc<ExportTable>,
        variables: Option<&VariableStateManager>,
    ) -> Result<(), ResumeTokenError> {
        tracing::info!(
            session_id = %snapshot.session_id,
            imports_count = snapshot.imports.len(),
            exports_count = snapshot.exports.len(),
            variables_count = snapshot.variables.len(),
            "Restoring session from snapshot"
        );

        // Restore variable state
        if let Some(var_mgr) = variables {
            var_mgr
                .import_variables(snapshot.variables)
                .await
                .map_err(|e| ResumeTokenError::RestoreError(e.to_string()))?;
        }

        // Note: In a full implementation, we'd restore:
        // - Import table entries (with careful stub/promise handling)
        // - Export table entries
        // - ID allocator state
        // - Registered capabilities

        tracing::info!(session_id = %snapshot.session_id, "Session restoration completed");
        Ok(())
    }

    /// Sign data using the secret key
    fn sign_data(&self, data: &[u8]) -> String {
        let mut hasher = Sha256::new();
        hasher.update(&self.secret_key);
        hasher.update(data);
        general_purpose::STANDARD.encode(hasher.finalize())
    }
}

/// Internal token payload structure
#[derive(Debug, Serialize, Deserialize)]
struct TokenPayload {
    snapshot: Vec<u8>,
    issued_at: u64,
    expires_at: u64,
    signature: String,
}

/// Errors related to resume token operations
#[derive(Debug, thiserror::Error)]
pub enum ResumeTokenError {
    #[error("Serialization error: {0}")]
    SerializationError(String),

    #[error("Invalid token: {0}")]
    InvalidToken(String),

    #[error("Token has expired")]
    TokenExpired,

    #[error("Invalid token signature")]
    InvalidSignature,

    #[error("Session too old to resume")]
    SessionTooOld,

    #[error("Session restoration error: {0}")]
    RestoreError(String),

    #[error("Variable state error: {0}")]
    VariableStateError(#[from] super::variable_state::VariableError),
}

/// Session manager that integrates resume token functionality
#[derive(Debug)]
pub struct PersistentSessionManager {
    token_manager: ResumeTokenManager,
    active_sessions: Arc<tokio::sync::RwLock<HashMap<String, SessionInfo>>>,
}

#[derive(Debug, Clone)]
struct SessionInfo {
    _session_id: String,
    last_activity: u64,
    _variable_manager: Option<Arc<VariableStateManager>>,
}

impl PersistentSessionManager {
    /// Create a new persistent session manager
    pub fn new(token_manager: ResumeTokenManager) -> Self {
        Self {
            token_manager,
            active_sessions: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
        }
    }

    /// Create a session snapshot for the given session
    pub async fn snapshot_session(
        &self,
        session_id: &str,
        _allocator: &Arc<IdAllocator>,
        _imports: &Arc<ImportTable>,
        _exports: &Arc<ExportTable>,
        variables: Option<&VariableStateManager>,
    ) -> Result<ResumeToken, ResumeTokenError> {
        let snapshot = self
            .token_manager
            .create_snapshot(
                session_id.to_string(),
                _allocator,
                _imports,
                _exports,
                variables,
            )
            .await?;

        self.token_manager.generate_token(snapshot)
    }

    /// Restore a session from a resume token
    pub async fn restore_session(
        &self,
        token: &ResumeToken,
        _allocator: &Arc<IdAllocator>,
        _imports: &Arc<ImportTable>,
        _exports: &Arc<ExportTable>,
        variables: Option<&VariableStateManager>,
    ) -> Result<String, ResumeTokenError> {
        let snapshot = self.token_manager.parse_token(token)?;

        self.token_manager
            .restore_session(snapshot.clone(), _allocator, _imports, _exports, variables)
            .await?;

        // Register the restored session
        let mut sessions = self.active_sessions.write().await;
        sessions.insert(
            snapshot.session_id.clone(),
            SessionInfo {
                _session_id: snapshot.session_id.clone(),
                last_activity: SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .expect("System time should be after UNIX epoch")
                    .as_secs(),
                _variable_manager: None, // Note: Variable manager integration would be handled separately
            },
        );

        Ok(snapshot.session_id)
    }

    /// Clean up expired sessions
    pub async fn cleanup_expired_sessions(&self) -> usize {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("System time should be after UNIX epoch")
            .as_secs();

        let mut sessions = self.active_sessions.write().await;
        let initial_count = sessions.len();

        sessions.retain(|_, info| {
            now - info.last_activity < 3600 // Keep sessions active for 1 hour
        });

        let cleaned_count = initial_count - sessions.len();
        if cleaned_count > 0 {
            tracing::info!(
                cleaned_sessions = cleaned_count,
                "Cleaned up expired sessions"
            );
        }

        cleaned_count
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::Number;

    #[tokio::test]
    async fn test_basic_resume_token_flow() {
        let secret_key = ResumeTokenManager::generate_secret_key();
        let manager = ResumeTokenManager::new(secret_key);

        // Create a simple snapshot
        let mut variables = HashMap::new();
        variables.insert("test_var".to_string(), Value::Number(Number::from(42)));

        let snapshot = SessionSnapshot {
            session_id: "test-session".to_string(),
            created_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            last_activity: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            version: 1,
            next_positive_id: 5,
            next_negative_id: -3,
            imports: HashMap::new(),
            exports: HashMap::new(),
            variables,
            max_age_seconds: 3600,
            capabilities: vec!["calculator".to_string()],
        };

        // Generate token
        let token = manager.generate_token(snapshot.clone()).unwrap();
        assert_eq!(token.session_id, "test-session");

        // Parse token back
        let restored_snapshot = manager.parse_token(&token).unwrap();
        assert_eq!(restored_snapshot.session_id, snapshot.session_id);
        assert_eq!(restored_snapshot.variables.len(), 1);

        if let Some(Value::Number(n)) = restored_snapshot.variables.get("test_var") {
            assert_eq!(n.as_i64(), Some(42));
        } else {
            panic!("Expected test_var to be number 42");
        }
    }

    #[tokio::test]
    async fn test_token_expiration() {
        let secret_key = ResumeTokenManager::generate_secret_key();
        let manager = ResumeTokenManager::with_settings(secret_key, 0, 3600); // 0 second TTL

        let snapshot = SessionSnapshot {
            session_id: "test-session".to_string(),
            created_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            last_activity: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            version: 1,
            next_positive_id: 1,
            next_negative_id: -1,
            imports: HashMap::new(),
            exports: HashMap::new(),
            variables: HashMap::new(),
            max_age_seconds: 3600,
            capabilities: Vec::new(),
        };

        let token = manager.generate_token(snapshot).unwrap();

        // Wait a moment for the token to expire (TTL is 0 seconds)
        tokio::time::sleep(std::time::Duration::from_millis(1100)).await;

        let result = manager.parse_token(&token);
        assert!(matches!(result, Err(ResumeTokenError::TokenExpired)));
    }

    #[tokio::test]
    async fn test_invalid_signature() {
        let secret_key1 = ResumeTokenManager::generate_secret_key();
        let secret_key2 = ResumeTokenManager::generate_secret_key();

        let manager1 = ResumeTokenManager::new(secret_key1);
        let manager2 = ResumeTokenManager::new(secret_key2);

        let snapshot = SessionSnapshot {
            session_id: "test-session".to_string(),
            created_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            last_activity: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            version: 1,
            next_positive_id: 1,
            next_negative_id: -1,
            imports: HashMap::new(),
            exports: HashMap::new(),
            variables: HashMap::new(),
            max_age_seconds: 3600,
            capabilities: Vec::new(),
        };

        // Generate token with manager1
        let token = manager1.generate_token(snapshot).unwrap();

        // Try to parse with manager2 (different key)
        let result = manager2.parse_token(&token);
        assert!(matches!(result, Err(ResumeTokenError::InvalidSignature)));
    }

    #[tokio::test]
    async fn test_persistent_session_manager() {
        let secret_key = ResumeTokenManager::generate_secret_key();
        let token_manager = ResumeTokenManager::new(secret_key);
        let session_manager = PersistentSessionManager::new(token_manager);

        // Test session cleanup
        let cleaned = session_manager.cleanup_expired_sessions().await;
        assert_eq!(cleaned, 0); // No sessions to clean initially
    }
}