anp 0.8.7

Rust SDK for Agent Network Protocol (ANP)
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
use std::collections::BTreeMap;
use std::fs;

use anp::authentication::{
    build_agent_message_service, build_agent_message_service_with_options,
    build_anp_message_service, build_group_message_service, create_did_wba_document,
    extract_signature_metadata, generate_auth_header, generate_http_signature_headers,
    validate_did_document_binding, verify_auth_header_signature, verify_federated_http_request,
    verify_http_message_signature, AnpMessageServiceOptions, AuthMode, DIDWbaAuthHeader,
    DidDocumentOptions, DidProfile, DidWbaVerifier, DidWbaVerifierConfig,
    FederatedVerificationOptions,
};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
use ed25519_dalek::SigningKey;
use serde_json::json;
use tempfile::tempdir;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

#[test]
fn test_create_did_document_profiles() {
    let e1 = create_did_wba_document(
        "example.com",
        DidDocumentOptions {
            path_segments: vec!["user".to_string(), "alice".to_string()],
            ..DidDocumentOptions::default()
        },
    )
    .expect("e1 DID creation should succeed");
    assert!(e1.did_document["id"].as_str().unwrap().contains(":e1_"));
    assert_eq!(
        e1.did_document["proof"]["type"],
        json!("DataIntegrityProof")
    );

    let k1 = create_did_wba_document(
        "example.com",
        DidDocumentOptions {
            path_segments: vec!["user".to_string(), "alice".to_string()],
            did_profile: DidProfile::K1,
            ..DidDocumentOptions::default()
        },
    )
    .expect("k1 DID creation should succeed");
    assert!(k1.did_document["id"].as_str().unwrap().contains(":k1_"));

    let legacy = create_did_wba_document(
        "example.com",
        DidDocumentOptions {
            path_segments: vec!["user".to_string(), "alice".to_string()],
            did_profile: DidProfile::PlainLegacy,
            ..DidDocumentOptions::default()
        },
    )
    .expect("legacy DID creation should succeed");
    assert_eq!(
        legacy.did_document["proof"]["type"],
        json!("EcdsaSecp256k1Signature2019")
    );

    let bare = create_did_wba_document("example.com", DidDocumentOptions::default())
        .expect("bare DID creation should succeed");
    assert_eq!(bare.did_document["id"], json!("did:wba:example.com"));
}

#[test]
fn test_validate_did_document_binding_rejects_e1_without_assertion_method_authorization() {
    let bundle = create_did_wba_document(
        "example.com",
        DidDocumentOptions {
            path_segments: vec!["user".to_string(), "alice".to_string()],
            ..DidDocumentOptions::default()
        },
    )
    .expect("e1 DID creation should succeed");

    let mut document = bundle.did_document.clone();
    let assertion_method = document
        .get_mut("assertionMethod")
        .and_then(serde_json::Value::as_array_mut)
        .expect("assertionMethod should exist");
    assertion_method.clear();

    assert!(
        !validate_did_document_binding(&document, false),
        "e1 DID binding should require proof.verificationMethod authorization in assertionMethod",
    );
}

#[test]
fn test_validate_did_document_binding_rejects_e1_with_tampered_thumbprint() {
    let bundle = create_did_wba_document(
        "example.com",
        DidDocumentOptions {
            path_segments: vec!["user".to_string(), "alice".to_string()],
            ..DidDocumentOptions::default()
        },
    )
    .expect("e1 DID creation should succeed");

    let mut document = bundle.did_document.clone();
    let original_did = document["id"]
        .as_str()
        .expect("DID should be a string")
        .to_string();
    let tampered_did = format!("{original_did}x");
    document["id"] = json!(tampered_did);

    assert!(
        !validate_did_document_binding(&document, false),
        "e1 DID binding should fail when the path thumbprint no longer matches the proof key",
    );
}

#[test]
fn test_build_anp_message_service_helpers() {
    let agent =
        build_agent_message_service("did:wba:example.com:user:alice", "https://example.com/rpc");
    assert_eq!(agent["type"], json!("ANPMessageService"));
    assert_eq!(agent["id"], json!("did:wba:example.com:user:alice#message"));
    assert_eq!(agent["profiles"][1], json!("anp.direct.base.v1"));
    assert_eq!(agent["securityProfiles"][1], json!("direct-e2ee"));

    let group =
        build_group_message_service("did:wba:example.com:groups:test", "https://example.com/rpc");
    assert_eq!(group["type"], json!("ANPMessageService"));
    assert_eq!(group["profiles"][1], json!("anp.group.base.v1"));
    assert_eq!(group["securityProfiles"][1], json!("group-e2ee"));

    let service_ref = build_anp_message_service(
        "#message",
        "https://example.com/rpc",
        AnpMessageServiceOptions::default(),
    );
    assert_eq!(service_ref["id"], json!("#message"));
}

#[test]
fn test_legacy_auth_header_generation_and_verification() {
    let bundle = create_did_wba_document(
        "example.com",
        DidDocumentOptions {
            path_segments: vec!["user".to_string(), "alice".to_string()],
            did_profile: DidProfile::K1,
            ..DidDocumentOptions::default()
        },
    )
    .expect("DID creation should succeed");
    let private_key = anp::PrivateKeyMaterial::from_pem(&bundle.keys["key-1"].private_key_pem)
        .expect("private key should load");
    let header = generate_auth_header(&bundle.did_document, "api.example.com", &private_key, "1.1")
        .expect("auth header generation should succeed");
    verify_auth_header_signature(&header, &bundle.did_document, "api.example.com")
        .expect("verification should succeed");
}

#[test]
fn test_http_signature_verification_rejects_tampered_body() {
    let bundle = create_did_wba_document("example.com", DidDocumentOptions::default())
        .expect("DID creation should succeed");
    let private_key = anp::PrivateKeyMaterial::from_pem(&bundle.keys["key-1"].private_key_pem)
        .expect("private key should load");
    let headers = generate_http_signature_headers(
        &bundle.did_document,
        "https://api.example.com/orders",
        "POST",
        &private_key,
        None,
        Some(br#"{"item":"book"}"#),
        Default::default(),
    )
    .expect("HTTP signature generation should succeed");
    assert!(verify_http_message_signature(
        &bundle.did_document,
        "POST",
        "https://api.example.com/orders",
        &headers,
        Some(br#"{"item":"book"}"#),
    )
    .is_ok());
    assert!(verify_http_message_signature(
        &bundle.did_document,
        "POST",
        "https://api.example.com/orders",
        &headers,
        Some(br#"{"item":"music"}"#),
    )
    .is_err());
}

#[test]
fn test_build_agent_message_service_with_service_did() {
    let service = build_agent_message_service_with_options(
        "did:wba:example.com:agents:alice:e1_demo",
        "https://example.com/anp",
        AnpMessageServiceOptions::default().with_service_did("did:wba:example.com"),
    );
    assert_eq!(service["serviceDid"], json!("did:wba:example.com"));
}

#[tokio::test]
async fn test_verify_federated_http_request_with_did_wba_service_did() {
    let sender = create_did_wba_document(
        "a.example.com",
        DidDocumentOptions {
            path_segments: vec!["agents".to_string(), "alice".to_string()],
            ..DidDocumentOptions::default()
        },
    )
    .expect("sender DID creation should succeed");
    let service_identity = create_did_wba_document("a.example.com", DidDocumentOptions::default())
        .expect("service DID creation should succeed");
    let private_key =
        anp::PrivateKeyMaterial::from_pem(&service_identity.keys["key-1"].private_key_pem)
            .expect("private key should load");
    let mut sender_document = sender.did_document.clone();
    sender_document
        .as_object_mut()
        .expect("sender document should be object")
        .insert(
            "service".to_string(),
            json!([build_agent_message_service_with_options(
                sender.did_document["id"].as_str().unwrap(),
                "https://a.example.com/anp",
                AnpMessageServiceOptions::default().with_service_did("did:wba:a.example.com"),
            )]),
        );
    let headers = generate_http_signature_headers(
        &service_identity.did_document,
        "https://b.example.com/anp",
        "POST",
        &private_key,
        None,
        Some(br#"{"message":"hello"}"#),
        Default::default(),
    )
    .expect("headers should generate");

    let result = verify_federated_http_request(
        sender.did_document["id"].as_str().unwrap(),
        "POST",
        "https://b.example.com/anp",
        &headers,
        Some(br#"{"message":"hello"}"#),
        FederatedVerificationOptions {
            sender_did_document: Some(sender_document),
            service_did_document: Some(service_identity.did_document.clone()),
            ..FederatedVerificationOptions::default()
        },
    )
    .await
    .expect("federated verification should succeed");

    assert_eq!(result.service_did, "did:wba:a.example.com");
    assert_eq!(
        result.signature_metadata.keyid,
        "did:wba:a.example.com#key-1"
    );
}

#[tokio::test]
async fn test_verify_federated_http_request_with_did_web_service_did() {
    let sender = create_did_wba_document(
        "a.example.com",
        DidDocumentOptions {
            path_segments: vec!["agents".to_string(), "alice".to_string()],
            ..DidDocumentOptions::default()
        },
    )
    .expect("sender DID creation should succeed");
    let mut sender_document = sender.did_document.clone();
    sender_document
        .as_object_mut()
        .expect("sender document should be object")
        .insert(
            "service".to_string(),
            json!([build_agent_message_service_with_options(
                sender.did_document["id"].as_str().unwrap(),
                "https://a.example.com/anp",
                AnpMessageServiceOptions::default().with_service_did("did:web:a.example.com"),
            )]),
        );

    let signing_key = SigningKey::generate(&mut rand::rngs::OsRng);
    let verifying_key = signing_key.verifying_key();
    let service_document = json!({
        "@context": ["https://www.w3.org/ns/did/v1"],
        "id": "did:web:a.example.com",
        "verificationMethod": [{
            "id": "did:web:a.example.com#key-1",
            "type": "Ed25519VerificationKey2020",
            "controller": "did:web:a.example.com",
            "publicKeyJwk": {
                "kty": "OKP",
                "crv": "Ed25519",
                "x": URL_SAFE_NO_PAD.encode(verifying_key.as_bytes()),
            }
        }],
        "authentication": ["did:web:a.example.com#key-1"]
    });
    let private_key = anp::PrivateKeyMaterial::Ed25519(signing_key);
    let headers = generate_http_signature_headers(
        &service_document,
        "https://b.example.com/anp",
        "POST",
        &private_key,
        None,
        Some(br#"{"message":"hello"}"#),
        Default::default(),
    )
    .expect("headers should generate");

    let result = verify_federated_http_request(
        sender.did_document["id"].as_str().unwrap(),
        "POST",
        "https://b.example.com/anp",
        &headers,
        Some(br#"{"message":"hello"}"#),
        FederatedVerificationOptions {
            sender_did_document: Some(sender_document),
            service_did_document: Some(service_document),
            ..FederatedVerificationOptions::default()
        },
    )
    .await
    .expect("federated verification should succeed");

    assert_eq!(result.service_did, "did:web:a.example.com");
    assert_eq!(
        result.signature_metadata.keyid,
        "did:web:a.example.com#key-1"
    );
}

#[test]
fn test_did_wba_auth_header_reads_files_and_generates_headers() {
    let bundle = create_did_wba_document("example.com", DidDocumentOptions::default())
        .expect("DID creation should succeed");
    let temp = tempdir().expect("temp dir should exist");
    let did_path = temp.path().join("did.json");
    let key_path = temp.path().join("key.pem");
    fs::write(&did_path, serde_json::to_vec(&bundle.did_document).unwrap()).unwrap();
    fs::write(&key_path, &bundle.keys["key-1"].private_key_pem).unwrap();

    let mut helper = DIDWbaAuthHeader::new(&did_path, &key_path, AuthMode::HttpSignatures);
    let headers = helper
        .get_auth_header("https://api.example.com/orders", false, "GET", None, None)
        .expect("header generation should succeed");
    assert!(headers.contains_key("Signature-Input"));
    assert!(headers.contains_key("Signature"));
}

#[test]
fn test_did_wba_auth_header_reuses_server_nonce_for_challenge() {
    let bundle = create_did_wba_document("example.com", DidDocumentOptions::default())
        .expect("DID creation should succeed");
    let temp = tempdir().expect("temp dir should exist");
    let did_path = temp.path().join("did.json");
    let key_path = temp.path().join("key.pem");
    fs::write(&did_path, serde_json::to_vec(&bundle.did_document).unwrap()).unwrap();
    fs::write(&key_path, &bundle.keys["key-1"].private_key_pem).unwrap();

    let mut helper = DIDWbaAuthHeader::new(&did_path, &key_path, AuthMode::HttpSignatures);
    let mut response_headers = BTreeMap::new();
    response_headers.insert(
        "WWW-Authenticate".to_string(),
        "DIDWba realm=\"api.example.com\", error=\"invalid_nonce\", error_description=\"Retry\", nonce=\"server-nonce-123\"".to_string(),
    );
    response_headers.insert(
        "Accept-Signature".to_string(),
        "sig1=(\"@method\" \"@target-uri\" \"@authority\" \"content-digest\" \"content-type\");created;expires;nonce;keyid".to_string(),
    );
    let mut request_headers = BTreeMap::new();
    request_headers.insert("Content-Type".to_string(), "application/json".to_string());

    let headers = helper
        .get_challenge_auth_header(
            "https://api.example.com/orders",
            &response_headers,
            "POST",
            Some(&request_headers),
            Some(br#"{"item":"book"}"#),
        )
        .expect("challenge auth headers should be generated");
    let metadata = extract_signature_metadata(&headers).expect("metadata should parse");
    assert_eq!(metadata.nonce.as_deref(), Some("server-nonce-123"));
    assert!(metadata
        .components
        .iter()
        .any(|value| value == "content-type"));
    assert!(headers.contains_key("Content-Digest"));
}

#[test]
fn test_did_wba_auth_header_should_not_retry_invalid_did() {
    let bundle = create_did_wba_document("example.com", DidDocumentOptions::default())
        .expect("DID creation should succeed");
    let temp = tempdir().expect("temp dir should exist");
    let did_path = temp.path().join("did.json");
    let key_path = temp.path().join("key.pem");
    fs::write(&did_path, serde_json::to_vec(&bundle.did_document).unwrap()).unwrap();
    fs::write(&key_path, &bundle.keys["key-1"].private_key_pem).unwrap();

    let helper = DIDWbaAuthHeader::new(&did_path, &key_path, AuthMode::HttpSignatures);
    let mut response_headers = BTreeMap::new();
    response_headers.insert(
        "WWW-Authenticate".to_string(),
        "DIDWba realm=\"api.example.com\", error=\"invalid_did\", error_description=\"Unknown DID\"".to_string(),
    );
    assert!(!helper.should_retry_after_401(&response_headers));
}

#[tokio::test]
async fn test_did_wba_verifier_accepts_http_signatures() {
    let bundle = create_did_wba_document("example.com", DidDocumentOptions::default())
        .expect("DID creation should succeed");
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/.well-known/did.json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(bundle.did_document.clone()))
        .mount(&server)
        .await;

    let private_key = anp::PrivateKeyMaterial::from_pem(&bundle.keys["key-1"].private_key_pem)
        .expect("private key should load");
    let request_url = format!("{}/orders", server.uri());
    let headers = generate_http_signature_headers(
        &bundle.did_document,
        &request_url,
        "GET",
        &private_key,
        None,
        None,
        Default::default(),
    )
    .expect("HTTP signature generation should succeed");

    let mut verifier = DidWbaVerifier::new(DidWbaVerifierConfig {
        jwt_private_key: Some("test-secret".to_string()),
        jwt_public_key: Some("test-secret".to_string()),
        jwt_algorithm: "HS256".to_string(),
        did_resolution_options: anp::authentication::DidResolutionOptions {
            base_url_override: Some(server.uri()),
            verify_ssl: false,
            timeout_seconds: 5.0,
        },
        ..DidWbaVerifierConfig::default()
    });

    let result = verifier
        .verify_request("GET", &request_url, &headers, None, Some("api.example.com"))
        .await
        .expect("verification should succeed");
    assert_eq!(result.auth_scheme, "http_signatures");
    assert!(result.access_token.is_some());
}