adk-awp 0.8.2

Agentic Web Protocol (AWP) implementation for ADK-Rust
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
//! Consent capture, check, and revocation framework.
//!
//! Provides two implementations:
//! - [`InMemoryConsentService`] — ephemeral, for development and testing
//! - [`FileConsentService`] — JSON file-backed, for production (GDPR/KPA compliance)

use std::path::{Path, PathBuf};

use async_trait::async_trait;
use awp_types::AwpError;
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};

/// Trait for managing user consent records.
#[async_trait]
pub trait ConsentService: Send + Sync {
    /// Record consent for a subject and purpose.
    async fn capture_consent(&self, subject: &str, purpose: &str) -> Result<(), AwpError>;

    /// Check whether consent is currently active for a subject and purpose.
    async fn check_consent(&self, subject: &str, purpose: &str) -> Result<bool, AwpError>;

    /// Revoke previously captured consent.
    async fn revoke_consent(&self, subject: &str, purpose: &str) -> Result<(), AwpError>;
}

// ---------------------------------------------------------------------------
// In-memory implementation
// ---------------------------------------------------------------------------

/// A consent record with timestamp and revocation state.
#[derive(Debug, Clone)]
struct ConsentRecord {
    _captured_at: DateTime<Utc>,
    revoked: bool,
}

/// In-memory consent service backed by [`DashMap`].
///
/// Keys are `(subject, purpose)` tuples. Capturing consent on an already-
/// captured pair re-activates it (clears the revoked flag).
///
/// Records are lost on process restart. Use [`FileConsentService`] for
/// durable storage.
pub struct InMemoryConsentService {
    records: DashMap<(String, String), ConsentRecord>,
}

impl InMemoryConsentService {
    /// Create a new empty consent service.
    pub fn new() -> Self {
        Self { records: DashMap::new() }
    }
}

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

#[async_trait]
impl ConsentService for InMemoryConsentService {
    async fn capture_consent(&self, subject: &str, purpose: &str) -> Result<(), AwpError> {
        let key = (subject.to_string(), purpose.to_string());
        self.records.insert(key, ConsentRecord { _captured_at: Utc::now(), revoked: false });
        Ok(())
    }

    async fn check_consent(&self, subject: &str, purpose: &str) -> Result<bool, AwpError> {
        let key = (subject.to_string(), purpose.to_string());
        Ok(self.records.get(&key).is_some_and(|r| !r.revoked))
    }

    async fn revoke_consent(&self, subject: &str, purpose: &str) -> Result<(), AwpError> {
        let key = (subject.to_string(), purpose.to_string());
        if let Some(mut entry) = self.records.get_mut(&key) {
            entry.revoked = true;
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// File-backed implementation
// ---------------------------------------------------------------------------

/// Serializable consent record for file persistence.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct FileConsentRecord {
    subject: String,
    purpose: String,
    captured_at: DateTime<Utc>,
    revoked: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    revoked_at: Option<DateTime<Utc>>,
}

/// Serializable consent store for JSON file persistence.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
struct FileConsentStore {
    records: Vec<FileConsentRecord>,
}

/// JSON file-backed consent service for durable storage.
///
/// Persists consent records to a JSON file on every mutation (capture/revoke).
/// Loads existing records from the file on construction. Safe for single-process
/// deployments; for multi-process, use a database-backed implementation.
///
/// # Example
///
/// ```rust,ignore
/// use adk_awp::FileConsentService;
///
/// let consent = FileConsentService::new("data/consent.json")?;
/// consent.capture_consent("visitor-123", "analytics").await?;
/// assert!(consent.check_consent("visitor-123", "analytics").await?);
/// ```
pub struct FileConsentService {
    path: PathBuf,
    records: DashMap<(String, String), FileConsentRecord>,
}

impl FileConsentService {
    /// Create a new file-backed consent service.
    ///
    /// If the file exists, records are loaded from it. If it doesn't exist,
    /// an empty store is created and the file is written on first mutation.
    ///
    /// # Errors
    ///
    /// Returns [`AwpError::InternalError`] if the file exists but cannot be
    /// read or parsed.
    pub fn new(path: impl AsRef<Path>) -> Result<Self, AwpError> {
        let path = path.as_ref().to_path_buf();
        let records = DashMap::new();

        if path.exists() {
            let content = std::fs::read_to_string(&path).map_err(|e| {
                AwpError::InternalError(format!(
                    "failed to read consent file {}: {e}",
                    path.display()
                ))
            })?;
            let store: FileConsentStore = serde_json::from_str(&content).map_err(|e| {
                AwpError::InternalError(format!(
                    "failed to parse consent file {}: {e}",
                    path.display()
                ))
            })?;
            for record in store.records {
                let key = (record.subject.clone(), record.purpose.clone());
                records.insert(key, record);
            }
        }

        Ok(Self { path, records })
    }

    /// Persist all records to the JSON file.
    fn flush(&self) -> Result<(), AwpError> {
        let records: Vec<FileConsentRecord> =
            self.records.iter().map(|entry| entry.value().clone()).collect();
        let store = FileConsentStore { records };
        let json = serde_json::to_string_pretty(&store).map_err(|e| {
            AwpError::InternalError(format!("failed to serialize consent records: {e}"))
        })?;

        // Create parent directories if needed
        if let Some(parent) = self.path.parent() {
            if !parent.exists() {
                std::fs::create_dir_all(parent).map_err(|e| {
                    AwpError::InternalError(format!(
                        "failed to create consent directory {}: {e}",
                        parent.display()
                    ))
                })?;
            }
        }

        std::fs::write(&self.path, json).map_err(|e| {
            AwpError::InternalError(format!(
                "failed to write consent file {}: {e}",
                self.path.display()
            ))
        })?;
        Ok(())
    }
}

#[async_trait]
impl ConsentService for FileConsentService {
    async fn capture_consent(&self, subject: &str, purpose: &str) -> Result<(), AwpError> {
        let key = (subject.to_string(), purpose.to_string());
        self.records.insert(
            key,
            FileConsentRecord {
                subject: subject.to_string(),
                purpose: purpose.to_string(),
                captured_at: Utc::now(),
                revoked: false,
                revoked_at: None,
            },
        );
        self.flush()
    }

    async fn check_consent(&self, subject: &str, purpose: &str) -> Result<bool, AwpError> {
        let key = (subject.to_string(), purpose.to_string());
        Ok(self.records.get(&key).is_some_and(|r| !r.revoked))
    }

    async fn revoke_consent(&self, subject: &str, purpose: &str) -> Result<(), AwpError> {
        let key = (subject.to_string(), purpose.to_string());
        if let Some(mut entry) = self.records.get_mut(&key) {
            entry.revoked = true;
            entry.revoked_at = Some(Utc::now());
        }
        self.flush()
    }
}

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

    // --- InMemoryConsentService tests ---

    #[tokio::test]
    async fn test_capture_and_check() {
        let svc = InMemoryConsentService::new();
        svc.capture_consent("user1", "analytics").await.unwrap();
        assert!(svc.check_consent("user1", "analytics").await.unwrap());
    }

    #[tokio::test]
    async fn test_check_without_capture_returns_false() {
        let svc = InMemoryConsentService::new();
        assert!(!svc.check_consent("user1", "analytics").await.unwrap());
    }

    #[tokio::test]
    async fn test_revoke_consent() {
        let svc = InMemoryConsentService::new();
        svc.capture_consent("user1", "analytics").await.unwrap();
        assert!(svc.check_consent("user1", "analytics").await.unwrap());

        svc.revoke_consent("user1", "analytics").await.unwrap();
        assert!(!svc.check_consent("user1", "analytics").await.unwrap());
    }

    #[tokio::test]
    async fn test_recapture_after_revoke() {
        let svc = InMemoryConsentService::new();
        svc.capture_consent("user1", "analytics").await.unwrap();
        svc.revoke_consent("user1", "analytics").await.unwrap();
        assert!(!svc.check_consent("user1", "analytics").await.unwrap());

        svc.capture_consent("user1", "analytics").await.unwrap();
        assert!(svc.check_consent("user1", "analytics").await.unwrap());
    }

    #[tokio::test]
    async fn test_different_subjects_independent() {
        let svc = InMemoryConsentService::new();
        svc.capture_consent("user1", "analytics").await.unwrap();
        assert!(!svc.check_consent("user2", "analytics").await.unwrap());
    }

    #[tokio::test]
    async fn test_different_purposes_independent() {
        let svc = InMemoryConsentService::new();
        svc.capture_consent("user1", "analytics").await.unwrap();
        assert!(!svc.check_consent("user1", "marketing").await.unwrap());
    }

    #[tokio::test]
    async fn test_revoke_nonexistent_is_noop() {
        let svc = InMemoryConsentService::new();
        svc.revoke_consent("user1", "analytics").await.unwrap();
        assert!(!svc.check_consent("user1", "analytics").await.unwrap());
    }

    // --- FileConsentService tests ---

    #[tokio::test]
    async fn test_file_capture_and_check() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");
        let svc = FileConsentService::new(&path).unwrap();

        svc.capture_consent("user1", "analytics").await.unwrap();
        assert!(svc.check_consent("user1", "analytics").await.unwrap());

        // File should exist
        assert!(path.exists());
    }

    #[tokio::test]
    async fn test_file_persistence_across_instances() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");

        // First instance: capture consent
        {
            let svc = FileConsentService::new(&path).unwrap();
            svc.capture_consent("user1", "analytics").await.unwrap();
            svc.capture_consent("user2", "marketing").await.unwrap();
        }

        // Second instance: records should be loaded from file
        {
            let svc = FileConsentService::new(&path).unwrap();
            assert!(svc.check_consent("user1", "analytics").await.unwrap());
            assert!(svc.check_consent("user2", "marketing").await.unwrap());
            assert!(!svc.check_consent("user3", "analytics").await.unwrap());
        }
    }

    #[tokio::test]
    async fn test_file_revoke_persists() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");

        {
            let svc = FileConsentService::new(&path).unwrap();
            svc.capture_consent("user1", "analytics").await.unwrap();
            svc.revoke_consent("user1", "analytics").await.unwrap();
        }

        {
            let svc = FileConsentService::new(&path).unwrap();
            assert!(!svc.check_consent("user1", "analytics").await.unwrap());
        }
    }

    #[tokio::test]
    async fn test_file_recapture_after_revoke_persists() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");

        {
            let svc = FileConsentService::new(&path).unwrap();
            svc.capture_consent("user1", "analytics").await.unwrap();
            svc.revoke_consent("user1", "analytics").await.unwrap();
            svc.capture_consent("user1", "analytics").await.unwrap();
        }

        {
            let svc = FileConsentService::new(&path).unwrap();
            assert!(svc.check_consent("user1", "analytics").await.unwrap());
        }
    }

    #[tokio::test]
    async fn test_file_nonexistent_path_creates_on_write() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("subdir").join("consent.json");
        assert!(!path.exists());

        let svc = FileConsentService::new(&path).unwrap();
        svc.capture_consent("user1", "analytics").await.unwrap();
        assert!(path.exists());
    }

    #[test]
    fn test_file_invalid_json_returns_error() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");
        std::fs::write(&path, "not valid json").unwrap();

        let result = FileConsentService::new(&path);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_file_check_without_capture_returns_false() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");
        let svc = FileConsentService::new(&path).unwrap();
        assert!(!svc.check_consent("user1", "analytics").await.unwrap());
    }

    #[tokio::test]
    async fn test_file_revoke_nonexistent_is_noop() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");
        let svc = FileConsentService::new(&path).unwrap();
        svc.revoke_consent("user1", "analytics").await.unwrap();
        assert!(!svc.check_consent("user1", "analytics").await.unwrap());
    }

    #[tokio::test]
    async fn test_file_consent_json_structure() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("consent.json");
        let svc = FileConsentService::new(&path).unwrap();
        svc.capture_consent("user1", "analytics").await.unwrap();

        let content = std::fs::read_to_string(&path).unwrap();
        let json: serde_json::Value = serde_json::from_str(&content).unwrap();
        assert!(json["records"].is_array());
        let record = &json["records"][0];
        assert_eq!(record["subject"], "user1");
        assert_eq!(record["purpose"], "analytics");
        assert_eq!(record["revoked"], false);
    }
}