rust-x402 0.3.0

HTTP-native micropayments with x402 protocol
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
//! Storage trait for facilitator nonce tracking
//!
//! This module provides a trait-based storage abstraction for tracking
//! processed nonces to prevent replay attacks.

use crate::Result;
use async_trait::async_trait;

/// Trait for storing and retrieving nonce information
///
/// This trait allows different storage backends to be used by the facilitator,
/// enabling flexibility in deployment scenarios.
#[async_trait]
pub trait NonceStorage: Send + Sync {
    /// Check if a nonce has been processed
    async fn has_nonce(&self, nonce: &str) -> Result<bool>;

    /// Mark a nonce as processed
    async fn mark_nonce(&self, nonce: &str) -> Result<()>;

    /// Remove a nonce (optional cleanup)
    async fn remove_nonce(&self, nonce: &str) -> Result<()>;
}

/// In-memory storage implementation
///
/// This is the default storage implementation that uses an in-memory HashMap.
/// Data is lost when the server restarts.
#[derive(Debug, Clone)]
pub struct InMemoryStorage {
    nonces: std::sync::Arc<tokio::sync::RwLock<std::collections::HashMap<String, bool>>>,
}

impl InMemoryStorage {
    /// Create a new in-memory storage instance
    pub fn new() -> Self {
        Self {
            nonces: std::sync::Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new())),
        }
    }
}

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

#[async_trait]
impl NonceStorage for InMemoryStorage {
    async fn has_nonce(&self, nonce: &str) -> Result<bool> {
        let nonces = self.nonces.read().await;
        Ok(nonces.contains_key(nonce))
    }

    async fn mark_nonce(&self, nonce: &str) -> Result<()> {
        let mut nonces = self.nonces.write().await;
        nonces.insert(nonce.to_string(), true);
        Ok(())
    }

    async fn remove_nonce(&self, nonce: &str) -> Result<()> {
        let mut nonces = self.nonces.write().await;
        nonces.remove(nonce);
        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_in_memory_storage_creation() {
        let storage = InMemoryStorage::new();
        assert!(!storage.has_nonce("test").await.unwrap());
    }

    #[tokio::test]
    async fn test_in_memory_storage_has_nonce() {
        let storage = InMemoryStorage::new();
        let test_nonce = "test_nonce_123";

        // Initially, nonce should not exist
        let exists = storage.has_nonce(test_nonce).await.unwrap();
        assert!(!exists, "Nonce should not exist initially");

        // Mark nonce as processed
        storage.mark_nonce(test_nonce).await.unwrap();

        // Now nonce should exist
        let exists = storage.has_nonce(test_nonce).await.unwrap();
        assert!(exists, "Nonce should exist after marking");
    }

    #[tokio::test]
    async fn test_in_memory_storage_mark_nonce() {
        let storage = InMemoryStorage::new();
        let test_nonce = "test_nonce_mark_456";

        // Mark nonce should succeed
        let result = storage.mark_nonce(test_nonce).await;
        assert!(result.is_ok(), "mark_nonce should succeed");

        // Verify nonce was marked
        let exists = storage.has_nonce(test_nonce).await.unwrap();
        assert!(exists, "Nonce should exist after marking");
    }

    #[tokio::test]
    async fn test_in_memory_storage_remove_nonce() {
        let storage = InMemoryStorage::new();
        let test_nonce = "test_nonce_remove_789";

        // Mark nonce first
        storage.mark_nonce(test_nonce).await.unwrap();
        assert!(storage.has_nonce(test_nonce).await.unwrap());

        // Remove nonce
        let result = storage.remove_nonce(test_nonce).await;
        assert!(result.is_ok(), "remove_nonce should succeed");

        // Verify nonce was removed
        let exists = storage.has_nonce(test_nonce).await.unwrap();
        assert!(!exists, "Nonce should not exist after removal");
    }

    #[tokio::test]
    async fn test_in_memory_storage_replay_protection() {
        let storage = InMemoryStorage::new();
        let test_nonce = "test_nonce_replay_abc";

        // First mark should succeed
        assert!(!storage.has_nonce(test_nonce).await.unwrap());
        storage.mark_nonce(test_nonce).await.unwrap();

        // Second mark should still work (idempotent), but has_nonce should return true
        storage.mark_nonce(test_nonce).await.unwrap();
        assert!(
            storage.has_nonce(test_nonce).await.unwrap(),
            "Nonce should still exist after second mark"
        );
    }

    #[tokio::test]
    async fn test_in_memory_storage_multiple_nonces() {
        let storage = InMemoryStorage::new();

        let nonce1 = "nonce1";
        let nonce2 = "nonce2";
        let nonce3 = "nonce3";

        // Mark multiple nonces
        storage.mark_nonce(nonce1).await.unwrap();
        storage.mark_nonce(nonce2).await.unwrap();
        storage.mark_nonce(nonce3).await.unwrap();

        // Verify all exist
        assert!(storage.has_nonce(nonce1).await.unwrap());
        assert!(storage.has_nonce(nonce2).await.unwrap());
        assert!(storage.has_nonce(nonce3).await.unwrap());

        // Remove one
        storage.remove_nonce(nonce2).await.unwrap();
        assert!(!storage.has_nonce(nonce2).await.unwrap());
        assert!(storage.has_nonce(nonce1).await.unwrap());
        assert!(storage.has_nonce(nonce3).await.unwrap());
    }
}

#[cfg(feature = "redis")]
pub mod redis_storage {
    use super::{NonceStorage, Result};
    use redis::{AsyncCommands, Client};

    /// Redis-based storage implementation
    ///
    /// This implementation uses Redis for persistent nonce storage,
    /// allowing data to survive server restarts and enabling distributed
    /// facilitator deployments.
    #[derive(Debug, Clone)]
    pub struct RedisStorage {
        client: Client,
        key_prefix: String,
    }

    impl RedisStorage {
        /// Create a new Redis storage instance
        ///
        /// # Arguments
        ///
        /// * `redis_url` - Redis connection URL (e.g., "redis://localhost:6379")
        /// * `key_prefix` - Optional prefix for Redis keys (default: "x402:nonce:")
        pub async fn new(redis_url: &str, key_prefix: Option<&str>) -> Result<Self> {
            let client = Client::open(redis_url).map_err(|e| {
                crate::X402Error::config(format!("Failed to connect to Redis: {}", e))
            })?;

            let key_prefix = key_prefix.unwrap_or("x402:nonce:").to_string();

            Ok(Self { client, key_prefix })
        }

        fn make_key(&self, nonce: &str) -> String {
            format!("{}{}", self.key_prefix, nonce)
        }
    }

    #[async_trait::async_trait]
    impl NonceStorage for RedisStorage {
        async fn has_nonce(&self, nonce: &str) -> Result<bool> {
            let mut conn = self
                .client
                .get_multiplexed_async_connection()
                .await
                .map_err(|e| {
                    crate::X402Error::config(format!("Failed to get Redis connection: {}", e))
                })?;

            let key = self.make_key(nonce);
            let exists: bool = conn.exists(&key).await.map_err(|e| {
                crate::X402Error::config(format!("Redis EXISTS command failed: {}", e))
            })?;

            Ok(exists)
        }

        async fn mark_nonce(&self, nonce: &str) -> Result<()> {
            let mut conn = self
                .client
                .get_multiplexed_async_connection()
                .await
                .map_err(|e| {
                    crate::X402Error::config(format!("Failed to get Redis connection: {}", e))
                })?;

            let key = self.make_key(nonce);
            // Set with TTL of 24 hours to prevent unbounded growth
            conn.set_ex::<_, _, ()>(&key, "1", 86400)
                .await
                .map_err(|e| {
                    crate::X402Error::config(format!("Redis SET command failed: {}", e))
                })?;

            Ok(())
        }

        async fn remove_nonce(&self, nonce: &str) -> Result<()> {
            let mut conn = self
                .client
                .get_multiplexed_async_connection()
                .await
                .map_err(|e| {
                    crate::X402Error::config(format!("Failed to get Redis connection: {}", e))
                })?;

            let key = self.make_key(nonce);
            conn.del::<_, ()>(&key).await.map_err(|e| {
                crate::X402Error::config(format!("Redis DEL command failed: {}", e))
            })?;

            Ok(())
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use std::env;

        /// Helper function to check if Redis is available
        /// Tests will be skipped if Redis is not available
        async fn check_redis_available(redis_url: &str) -> bool {
            match Client::open(redis_url) {
                Ok(client) => {
                    match client.get_multiplexed_async_connection().await {
                        Ok(mut conn) => {
                            // Try to ping Redis using AsyncCommands
                            match conn.get::<&str, Option<String>>("__test_key__").await {
                                Ok(_) => true,
                                Err(_) => {
                                    // If GET fails but connection works, try EXISTS
                                    match conn.exists::<&str, bool>("__test_key__").await {
                                        Ok(_) => true,
                                        Err(_) => false,
                                    }
                                }
                            }
                        }
                        Err(_) => false,
                    }
                }
                Err(_) => false,
            }
        }

        #[tokio::test]
        async fn test_redis_storage_creation() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            let storage = RedisStorage::new(&redis_url, None).await;
            assert!(storage.is_ok(), "RedisStorage creation should succeed");

            let storage = storage.unwrap();
            assert_eq!(storage.key_prefix, "x402:nonce:");
        }

        #[tokio::test]
        async fn test_redis_storage_custom_prefix() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            let storage = RedisStorage::new(&redis_url, Some("test:prefix:")).await;
            assert!(storage.is_ok());

            let storage = storage.unwrap();
            assert_eq!(storage.key_prefix, "test:prefix:");
        }

        #[tokio::test]
        async fn test_redis_storage_has_nonce() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            // Use a unique prefix for this test to avoid conflicts
            let test_prefix = format!("test:{}:", uuid::Uuid::new_v4());
            let storage = RedisStorage::new(&redis_url, Some(&test_prefix))
                .await
                .unwrap();

            let test_nonce = "test_nonce_123";

            // Initially, nonce should not exist
            let exists = storage.has_nonce(test_nonce).await.unwrap();
            assert!(!exists, "Nonce should not exist initially");

            // Mark nonce as processed
            storage.mark_nonce(test_nonce).await.unwrap();

            // Now nonce should exist
            let exists = storage.has_nonce(test_nonce).await.unwrap();
            assert!(exists, "Nonce should exist after marking");

            // Clean up
            storage.remove_nonce(test_nonce).await.unwrap();
        }

        #[tokio::test]
        async fn test_redis_storage_mark_nonce() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            let test_prefix = format!("test:{}:", uuid::Uuid::new_v4());
            let storage = RedisStorage::new(&redis_url, Some(&test_prefix))
                .await
                .unwrap();

            let test_nonce = "test_nonce_mark_456";

            // Mark nonce should succeed
            let result = storage.mark_nonce(test_nonce).await;
            assert!(result.is_ok(), "mark_nonce should succeed");

            // Verify nonce was marked
            let exists = storage.has_nonce(test_nonce).await.unwrap();
            assert!(exists, "Nonce should exist after marking");

            // Clean up
            storage.remove_nonce(test_nonce).await.unwrap();
        }

        #[tokio::test]
        async fn test_redis_storage_remove_nonce() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            let test_prefix = format!("test:{}:", uuid::Uuid::new_v4());
            let storage = RedisStorage::new(&redis_url, Some(&test_prefix))
                .await
                .unwrap();

            let test_nonce = "test_nonce_remove_789";

            // Mark nonce first
            storage.mark_nonce(test_nonce).await.unwrap();
            assert!(storage.has_nonce(test_nonce).await.unwrap());

            // Remove nonce
            let result = storage.remove_nonce(test_nonce).await;
            assert!(result.is_ok(), "remove_nonce should succeed");

            // Verify nonce was removed
            let exists = storage.has_nonce(test_nonce).await.unwrap();
            assert!(!exists, "Nonce should not exist after removal");
        }

        #[tokio::test]
        async fn test_redis_storage_replay_protection() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            let test_prefix = format!("test:{}:", uuid::Uuid::new_v4());
            let storage = RedisStorage::new(&redis_url, Some(&test_prefix))
                .await
                .unwrap();

            let test_nonce = "test_nonce_replay_abc";

            // First mark should succeed
            assert!(storage.has_nonce(test_nonce).await.unwrap() == false);
            storage.mark_nonce(test_nonce).await.unwrap();

            // Second mark should still work (idempotent), but has_nonce should return true
            storage.mark_nonce(test_nonce).await.unwrap();
            assert!(
                storage.has_nonce(test_nonce).await.unwrap(),
                "Nonce should still exist after second mark"
            );

            // Clean up
            storage.remove_nonce(test_nonce).await.unwrap();
        }

        #[tokio::test]
        async fn test_redis_storage_ttl() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            let test_prefix = format!("test:{}:", uuid::Uuid::new_v4());
            let storage = RedisStorage::new(&redis_url, Some(&test_prefix))
                .await
                .unwrap();

            let test_nonce = "test_nonce_ttl_xyz";

            // Mark nonce (should have TTL of 24 hours)
            storage.mark_nonce(test_nonce).await.unwrap();

            // Verify key exists and has TTL
            let mut conn = storage
                .client
                .get_multiplexed_async_connection()
                .await
                .unwrap();
            let key = storage.make_key(test_nonce);
            let ttl: i64 = conn.ttl(&key).await.unwrap();

            // TTL should be positive (less than 86400 seconds = 24 hours)
            assert!(ttl > 0, "Key should have a positive TTL");
            assert!(
                ttl <= 86400,
                "TTL should be at most 24 hours (86400 seconds)"
            );

            // Clean up
            storage.remove_nonce(test_nonce).await.unwrap();
        }

        #[tokio::test]
        async fn test_redis_storage_multiple_nonces() {
            let redis_url =
                env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

            if !check_redis_available(&redis_url).await {
                println!("Skipping Redis test: Redis not available at {}", redis_url);
                return;
            }

            let test_prefix = format!("test:{}:", uuid::Uuid::new_v4());
            let storage = RedisStorage::new(&redis_url, Some(&test_prefix))
                .await
                .unwrap();

            let nonce1 = "nonce1";
            let nonce2 = "nonce2";
            let nonce3 = "nonce3";

            // Mark multiple nonces
            storage.mark_nonce(nonce1).await.unwrap();
            storage.mark_nonce(nonce2).await.unwrap();
            storage.mark_nonce(nonce3).await.unwrap();

            // Verify all exist
            assert!(storage.has_nonce(nonce1).await.unwrap());
            assert!(storage.has_nonce(nonce2).await.unwrap());
            assert!(storage.has_nonce(nonce3).await.unwrap());

            // Remove one
            storage.remove_nonce(nonce2).await.unwrap();
            assert!(!storage.has_nonce(nonce2).await.unwrap());
            assert!(storage.has_nonce(nonce1).await.unwrap());
            assert!(storage.has_nonce(nonce3).await.unwrap());

            // Clean up
            storage.remove_nonce(nonce1).await.unwrap();
            storage.remove_nonce(nonce3).await.unwrap();
        }
    }
}