oxirs-did 0.2.4

W3C DID and Verifiable Credentials implementation with Signed RDF Graphs for OxiRS
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! # DID:web Method Resolver
//!
//! Resolves DID identifiers using the `did:web` method (W3C DID Web Method).
//! Converts `did:web:example.com:path` to `https://example.com/path/did.json`
//! and fetches the DID Document.
//!
//! ## Features
//!
//! - **URL construction**: Correct domain/path decoding per spec
//! - **DID Document parsing**: JSON-LD DID Document deserialization
//! - **Verification method extraction**: Parse public keys and services
//! - **Caching**: In-memory cache with TTL for resolved documents
//! - **Validation**: Structural validation of resolved DID Documents
//! - **Metadata**: Resolution metadata including content type, duration

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

// ─────────────────────────────────────────────
// Configuration
// ─────────────────────────────────────────────

/// Configuration for the DID:web resolver.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DidWebConfig {
    /// Cache TTL for resolved documents (default: 300s).
    pub cache_ttl: Duration,
    /// Maximum cache entries (default: 1000).
    pub max_cache_entries: usize,
    /// Request timeout (default: 10s).
    pub request_timeout: Duration,
    /// Whether to require HTTPS (default: true).
    pub require_https: bool,
    /// Whether to validate DID Document structure (default: true).
    pub validate_documents: bool,
}

impl Default for DidWebConfig {
    fn default() -> Self {
        Self {
            cache_ttl: Duration::from_secs(300),
            max_cache_entries: 1000,
            request_timeout: Duration::from_secs(10),
            require_https: true,
            validate_documents: true,
        }
    }
}

// ─────────────────────────────────────────────
// DID Document types
// ─────────────────────────────────────────────

/// A resolved DID Document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DidDocument {
    /// The DID identifier.
    pub id: String,
    /// JSON-LD context.
    pub context: Vec<String>,
    /// Verification methods (public keys).
    pub verification_method: Vec<VerificationMethod>,
    /// Authentication methods.
    pub authentication: Vec<String>,
    /// Assertion methods.
    pub assertion_method: Vec<String>,
    /// Service endpoints.
    pub service: Vec<ServiceEndpoint>,
    /// Controller of the DID.
    pub controller: Option<String>,
    /// Also-known-as aliases.
    pub also_known_as: Vec<String>,
}

/// A verification method (public key).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationMethod {
    /// Verification method ID.
    pub id: String,
    /// Type (e.g., Ed25519VerificationKey2020).
    pub method_type: String,
    /// Controller DID.
    pub controller: String,
    /// Public key in multibase encoding.
    pub public_key_multibase: Option<String>,
    /// Public key in JWK format.
    pub public_key_jwk: Option<serde_json::Value>,
}

/// A service endpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceEndpoint {
    /// Service ID.
    pub id: String,
    /// Service type.
    pub service_type: String,
    /// Endpoint URL.
    pub service_endpoint: String,
}

/// Resolution metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolutionMetadata {
    /// Content type of the resolved document.
    pub content_type: String,
    /// Time taken to resolve (ms).
    pub duration_ms: u64,
    /// Whether the result came from cache.
    pub from_cache: bool,
    /// The URL that was fetched.
    pub resolved_url: String,
    /// HTTP status code (if fetched).
    pub http_status: Option<u16>,
}

/// Validation result for a DID Document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
    /// Whether the document is valid.
    pub valid: bool,
    /// Validation errors.
    pub errors: Vec<String>,
    /// Validation warnings.
    pub warnings: Vec<String>,
}

// ─────────────────────────────────────────────
// Cache Entry
// ─────────────────────────────────────────────

#[derive(Debug, Clone)]
struct CacheEntry {
    document: DidDocument,
    cached_at: Instant,
}

// ─────────────────────────────────────────────
// Statistics
// ─────────────────────────────────────────────

/// Statistics for the DID:web resolver.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DidWebStats {
    pub total_resolutions: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
    pub validation_errors: u64,
    pub url_parse_errors: u64,
}

// ─────────────────────────────────────────────
// URL Construction
// ─────────────────────────────────────────────

/// Convert a `did:web:...` identifier to the corresponding HTTPS URL.
///
/// Per the DID Web Method spec:
/// - `did:web:example.com` → `https://example.com/.well-known/did.json`
/// - `did:web:example.com:path:to:resource` → `https://example.com/path/to/resource/did.json`
/// - `%3A` in the domain is decoded to `:` (for ports)
pub fn did_web_to_url(did: &str) -> Result<String, String> {
    if !did.starts_with("did:web:") {
        return Err(format!("Not a did:web identifier: {did}"));
    }

    let method_specific = &did[8..]; // After "did:web:"
    if method_specific.is_empty() {
        return Err("Empty method-specific identifier".into());
    }

    let parts: Vec<&str> = method_specific.split(':').collect();

    // First part is the domain (with optional port via %3A)
    let domain = parts[0].replace("%3A", ":");

    if parts.len() == 1 {
        // Root DID: https://domain/.well-known/did.json
        Ok(format!("https://{domain}/.well-known/did.json"))
    } else {
        // Path-based DID: https://domain/path1/path2/.../did.json
        let path = parts[1..].join("/");
        Ok(format!("https://{domain}/{path}/did.json"))
    }
}

/// Parse a DID:web identifier into its components.
pub fn parse_did_web(did: &str) -> Result<(String, Vec<String>), String> {
    if !did.starts_with("did:web:") {
        return Err(format!("Not a did:web identifier: {did}"));
    }

    let method_specific = &did[8..];
    if method_specific.is_empty() {
        return Err("Empty method-specific identifier".into());
    }

    let parts: Vec<&str> = method_specific.split(':').collect();
    let domain = parts[0].replace("%3A", ":");
    let path: Vec<String> = parts[1..].iter().map(|s| s.to_string()).collect();

    Ok((domain, path))
}

/// Construct a did:web identifier from domain and path.
pub fn construct_did_web(domain: &str, path: &[&str]) -> String {
    let encoded_domain = domain.replace(':', "%3A");
    if path.is_empty() {
        format!("did:web:{encoded_domain}")
    } else {
        format!("did:web:{encoded_domain}:{}", path.join(":"))
    }
}

// ─────────────────────────────────────────────
// DID Document Validation
// ─────────────────────────────────────────────

/// Validate a DID Document structure.
pub fn validate_did_document(doc: &DidDocument) -> ValidationResult {
    let mut errors = Vec::new();
    let mut warnings = Vec::new();

    if doc.id.is_empty() {
        errors.push("DID Document must have an 'id' field".into());
    } else if !doc.id.starts_with("did:") {
        errors.push(format!("Invalid DID format: {}", doc.id));
    }

    if doc.context.is_empty() {
        errors.push("DID Document must have a '@context' field".into());
    } else if !doc
        .context
        .iter()
        .any(|c| c.contains("did/v1") || c.contains("www.w3.org"))
    {
        warnings.push("Context should include W3C DID context".into());
    }

    if doc.verification_method.is_empty() {
        warnings.push("No verification methods found".into());
    }

    for vm in &doc.verification_method {
        if vm.id.is_empty() {
            errors.push("Verification method must have an 'id'".into());
        }
        if vm.method_type.is_empty() {
            errors.push("Verification method must have a 'type'".into());
        }
        if vm.public_key_multibase.is_none() && vm.public_key_jwk.is_none() {
            warnings.push(format!(
                "Verification method '{}' has no public key material",
                vm.id
            ));
        }
    }

    for svc in &doc.service {
        if svc.id.is_empty() {
            errors.push("Service endpoint must have an 'id'".into());
        }
        if svc.service_endpoint.is_empty() {
            errors.push("Service endpoint must have a URL".into());
        }
    }

    ValidationResult {
        valid: errors.is_empty(),
        errors,
        warnings,
    }
}

// ─────────────────────────────────────────────
// DID:web Resolver
// ─────────────────────────────────────────────

/// Resolver for DID:web identifiers.
pub struct DidWebResolver {
    config: DidWebConfig,
    cache: HashMap<String, CacheEntry>,
    stats: DidWebStats,
}

impl DidWebResolver {
    /// Create a new resolver.
    pub fn new(config: DidWebConfig) -> Self {
        Self {
            config,
            cache: HashMap::new(),
            stats: DidWebStats::default(),
        }
    }

    /// Create with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(DidWebConfig::default())
    }

    /// Resolve a did:web identifier to its URL (without HTTP fetch).
    pub fn resolve_url(&mut self, did: &str) -> Result<String, String> {
        self.stats.total_resolutions += 1;
        did_web_to_url(did).map_err(|e| {
            self.stats.url_parse_errors += 1;
            e
        })
    }

    /// Check cache for a resolved document.
    pub fn get_cached(&mut self, did: &str) -> Option<&DidDocument> {
        if let Some(entry) = self.cache.get(did) {
            if entry.cached_at.elapsed() <= self.config.cache_ttl {
                self.stats.cache_hits += 1;
                return Some(&entry.document);
            }
        }
        self.stats.cache_misses += 1;
        None
    }

    /// Store a resolved document in cache.
    pub fn cache_document(&mut self, did: &str, document: DidDocument) {
        if self.cache.len() >= self.config.max_cache_entries {
            // Evict oldest
            if let Some(oldest_key) = self
                .cache
                .iter()
                .min_by_key(|(_, e)| e.cached_at)
                .map(|(k, _)| k.clone())
            {
                self.cache.remove(&oldest_key);
            }
        }
        self.cache.insert(
            did.to_string(),
            CacheEntry {
                document,
                cached_at: Instant::now(),
            },
        );
    }

    /// Validate a DID Document.
    pub fn validate(&mut self, doc: &DidDocument) -> ValidationResult {
        let result = validate_did_document(doc);
        if !result.valid {
            self.stats.validation_errors += 1;
        }
        result
    }

    /// Clear the cache.
    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }

    /// Get statistics.
    pub fn stats(&self) -> &DidWebStats {
        &self.stats
    }

    /// Get configuration.
    pub fn config(&self) -> &DidWebConfig {
        &self.config
    }

    /// Number of cached entries.
    pub fn cache_size(&self) -> usize {
        self.cache.len()
    }
}

// ─────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────

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

    fn sample_document() -> DidDocument {
        DidDocument {
            id: "did:web:example.com".into(),
            context: vec!["https://www.w3.org/ns/did/v1".into()],
            verification_method: vec![VerificationMethod {
                id: "did:web:example.com#key-1".into(),
                method_type: "Ed25519VerificationKey2020".into(),
                controller: "did:web:example.com".into(),
                public_key_multibase: Some("z6Mkf...".into()),
                public_key_jwk: None,
            }],
            authentication: vec!["did:web:example.com#key-1".into()],
            assertion_method: vec![],
            service: vec![ServiceEndpoint {
                id: "did:web:example.com#service-1".into(),
                service_type: "LinkedDomains".into(),
                service_endpoint: "https://example.com".into(),
            }],
            controller: None,
            also_known_as: vec![],
        }
    }

    #[test]
    fn test_did_web_to_url_root() {
        let url = did_web_to_url("did:web:example.com").expect("parse failed");
        assert_eq!(url, "https://example.com/.well-known/did.json");
    }

    #[test]
    fn test_did_web_to_url_path() {
        let url = did_web_to_url("did:web:example.com:users:alice").expect("parse failed");
        assert_eq!(url, "https://example.com/users/alice/did.json");
    }

    #[test]
    fn test_did_web_to_url_port() {
        let url = did_web_to_url("did:web:example.com%3A8080").expect("parse failed");
        assert_eq!(url, "https://example.com:8080/.well-known/did.json");
    }

    #[test]
    fn test_did_web_to_url_port_with_path() {
        let url = did_web_to_url("did:web:example.com%3A8080:path").expect("parse failed");
        assert_eq!(url, "https://example.com:8080/path/did.json");
    }

    #[test]
    fn test_did_web_to_url_invalid() {
        assert!(did_web_to_url("did:key:abc").is_err());
        assert!(did_web_to_url("not-a-did").is_err());
    }

    #[test]
    fn test_did_web_to_url_empty() {
        assert!(did_web_to_url("did:web:").is_err());
    }

    #[test]
    fn test_parse_did_web() {
        let (domain, path) =
            parse_did_web("did:web:example.com:users:alice").expect("parse failed");
        assert_eq!(domain, "example.com");
        assert_eq!(path, vec!["users", "alice"]);
    }

    #[test]
    fn test_parse_did_web_root() {
        let (domain, path) = parse_did_web("did:web:example.com").expect("parse failed");
        assert_eq!(domain, "example.com");
        assert!(path.is_empty());
    }

    #[test]
    fn test_construct_did_web() {
        let did = construct_did_web("example.com", &["users", "alice"]);
        assert_eq!(did, "did:web:example.com:users:alice");
    }

    #[test]
    fn test_construct_did_web_root() {
        let did = construct_did_web("example.com", &[]);
        assert_eq!(did, "did:web:example.com");
    }

    #[test]
    fn test_construct_did_web_port() {
        let did = construct_did_web("example.com:8080", &[]);
        assert_eq!(did, "did:web:example.com%3A8080");
    }

    #[test]
    fn test_validate_valid_document() {
        let result = validate_did_document(&sample_document());
        assert!(result.valid);
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_validate_missing_id() {
        let mut doc = sample_document();
        doc.id = String::new();
        let result = validate_did_document(&doc);
        assert!(!result.valid);
    }

    #[test]
    fn test_validate_missing_context() {
        let mut doc = sample_document();
        doc.context.clear();
        let result = validate_did_document(&doc);
        assert!(!result.valid);
    }

    #[test]
    fn test_validate_no_verification_methods() {
        let mut doc = sample_document();
        doc.verification_method.clear();
        let result = validate_did_document(&doc);
        assert!(result.valid); // Warning, not error
        assert!(!result.warnings.is_empty());
    }

    #[test]
    fn test_validate_empty_vm_id() {
        let mut doc = sample_document();
        doc.verification_method[0].id = String::new();
        let result = validate_did_document(&doc);
        assert!(!result.valid);
    }

    #[test]
    fn test_validate_empty_service_endpoint() {
        let mut doc = sample_document();
        doc.service[0].service_endpoint = String::new();
        let result = validate_did_document(&doc);
        assert!(!result.valid);
    }

    #[test]
    fn test_resolver_url() {
        let mut resolver = DidWebResolver::with_defaults();
        let url = resolver
            .resolve_url("did:web:example.com")
            .expect("resolve failed");
        assert_eq!(url, "https://example.com/.well-known/did.json");
    }

    #[test]
    fn test_resolver_cache() {
        let mut resolver = DidWebResolver::with_defaults();
        let doc = sample_document();
        resolver.cache_document("did:web:example.com", doc);
        assert_eq!(resolver.cache_size(), 1);

        let cached = resolver.get_cached("did:web:example.com");
        assert!(cached.is_some());
        assert_eq!(resolver.stats().cache_hits, 1);
    }

    #[test]
    fn test_resolver_cache_miss() {
        let mut resolver = DidWebResolver::with_defaults();
        assert!(resolver.get_cached("did:web:unknown.com").is_none());
        assert_eq!(resolver.stats().cache_misses, 1);
    }

    #[test]
    fn test_resolver_cache_ttl() {
        let mut resolver = DidWebResolver::new(DidWebConfig {
            cache_ttl: Duration::from_millis(50),
            ..Default::default()
        });
        resolver.cache_document("did:web:example.com", sample_document());
        std::thread::sleep(Duration::from_millis(100));
        assert!(resolver.get_cached("did:web:example.com").is_none());
    }

    #[test]
    fn test_resolver_clear_cache() {
        let mut resolver = DidWebResolver::with_defaults();
        resolver.cache_document("did:web:a.com", sample_document());
        resolver.cache_document("did:web:b.com", sample_document());
        resolver.clear_cache();
        assert_eq!(resolver.cache_size(), 0);
    }

    #[test]
    fn test_resolver_validate() {
        let mut resolver = DidWebResolver::with_defaults();
        let result = resolver.validate(&sample_document());
        assert!(result.valid);
    }

    #[test]
    fn test_config_defaults() {
        let config = DidWebConfig::default();
        assert_eq!(config.cache_ttl, Duration::from_secs(300));
        assert!(config.require_https);
        assert!(config.validate_documents);
    }

    #[test]
    fn test_config_serialization() {
        let config = DidWebConfig::default();
        let json = serde_json::to_string(&config).expect("serialize failed");
        assert!(json.contains("cache_ttl"));
    }

    #[test]
    fn test_document_serialization() {
        let doc = sample_document();
        let json = serde_json::to_string(&doc).expect("serialize failed");
        let deser: DidDocument = serde_json::from_str(&json).expect("deser failed");
        assert_eq!(deser.id, doc.id);
    }

    #[test]
    fn test_stats_tracking() {
        let mut resolver = DidWebResolver::with_defaults();
        let _ = resolver.resolve_url("did:web:a.com");
        let _ = resolver.resolve_url("did:web:b.com");
        assert_eq!(resolver.stats().total_resolutions, 2);
    }

    #[test]
    fn test_stats_url_parse_errors() {
        let mut resolver = DidWebResolver::with_defaults();
        let _ = resolver.resolve_url("invalid");
        assert_eq!(resolver.stats().url_parse_errors, 1);
    }

    #[test]
    fn test_cache_eviction() {
        let mut resolver = DidWebResolver::new(DidWebConfig {
            max_cache_entries: 2,
            ..Default::default()
        });
        resolver.cache_document("did:web:a.com", sample_document());
        resolver.cache_document("did:web:b.com", sample_document());
        resolver.cache_document("did:web:c.com", sample_document());
        assert!(resolver.cache_size() <= 2);
    }

    #[test]
    fn test_roundtrip_construct_parse() {
        let did = construct_did_web("example.com", &["users", "alice"]);
        let (domain, path) = parse_did_web(&did).expect("parse failed");
        assert_eq!(domain, "example.com");
        assert_eq!(path, vec!["users", "alice"]);
    }

    #[test]
    fn test_validation_result_serialization() {
        let result = ValidationResult {
            valid: true,
            errors: vec![],
            warnings: vec!["test".into()],
        };
        let json = serde_json::to_string(&result).expect("serialize failed");
        assert!(json.contains("warnings"));
    }
}