auths-sdk 0.1.16

Application services layer for Auths identity operations
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
//! Epic E.3 — agent as a KERI `dip`-delegated identifier (SDK `agents::add`).

use std::ops::ControlFlow;
use std::sync::Arc;

use auths_core::PrefilledPassphraseProvider;
use auths_core::signing::{PassphraseProvider, StorageSigner};
use auths_core::storage::keychain::{KeyAlias, extract_public_key_bytes};
use auths_core::testing::IsolatedKeychainHandle;
use auths_crypto::CurveType;
use auths_id::keri::Event;
use auths_id::keri::delegation::mark_agent_scope;
use auths_id::keri::types::Prefix;
use auths_id::keri::validate_delegation;
use auths_id::storage::registry::backend::RegistryBackend;
use auths_keri::{AgentScope, Capability};
use auths_sdk::context::AuthsContext;
use auths_sdk::domains::agents::{AgentError, add, add_scoped, list, revoke, rotate};
use auths_sdk::domains::device::{add_device, list_delegated_devices, remove_device};
use auths_sdk::domains::identity::service::initialize;
use auths_sdk::domains::identity::types::{
    CreateDeveloperIdentityConfig, IdentityConfig, InitializeResult,
};
use auths_sdk::domains::signing::types::GitSigningScope;

use crate::cases::helpers::{build_test_context, build_test_context_with_provider};

const PASS: &str = "Test-passphrase1!";

fn setup_test_identity(registry_path: &std::path::Path) -> (KeyAlias, IsolatedKeychainHandle) {
    let keychain = IsolatedKeychainHandle::new();
    let signer = StorageSigner::new(keychain.clone());
    let provider = PrefilledPassphraseProvider::new(PASS);
    let config = CreateDeveloperIdentityConfig::builder(KeyAlias::new_unchecked("test-key"))
        .with_git_signing_scope(GitSigningScope::Skip)
        .build();
    let ctx = build_test_context(registry_path, Arc::new(keychain.clone()));
    let result = match initialize(
        IdentityConfig::Developer(config),
        &ctx,
        Arc::new(keychain.clone()),
        &signer,
        &provider,
        None,
    )
    .unwrap()
    {
        InitializeResult::Developer(r) => r,
        _ => unreachable!(),
    };
    (result.key_alias, keychain)
}

/// (ctx, root signing alias, root prefix) for a fresh delegating identity.
fn setup() -> (AuthsContext, KeyAlias, Prefix, tempfile::TempDir) {
    let tmp = tempfile::tempdir().unwrap();
    let (root_alias, keychain) = setup_test_identity(tmp.path());
    let provider: Arc<dyn PassphraseProvider + Send + Sync> =
        Arc::new(PrefilledPassphraseProvider::new(PASS));
    let ctx =
        build_test_context_with_provider(tmp.path(), Arc::new(keychain.clone()), Some(provider));
    let managed = ctx.identity_storage.load_identity().expect("root identity");
    let root_prefix = Prefix::new_unchecked(
        managed
            .controller_did
            .as_str()
            .strip_prefix("did:keri:")
            .unwrap()
            .to_string(),
    );
    (ctx, root_alias, root_prefix, tmp)
}

fn collect_kel(backend: &(dyn RegistryBackend + Send + Sync), prefix: &Prefix) -> Vec<Event> {
    let mut events = Vec::new();
    backend
        .visit_events(prefix, 0, &mut |e| {
            events.push(e.clone());
            ControlFlow::Continue(())
        })
        .expect("walk KEL");
    events
}

#[test]
fn agents_add_returns_anchored_dip() {
    let (ctx, root_alias, root_prefix, _tmp) = setup();
    let agent_alias = KeyAlias::new_unchecked("deploy-bot");

    let agent = add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519).expect("delegate agent");
    assert!(agent.agent_did.starts_with("did:keri:"));

    // The root anchored the agent's dip → validate_delegation confirms it bilaterally.
    let agent_prefix = Prefix::new_unchecked(agent.agent_prefix.clone());
    let dip = ctx
        .registry
        .get_event(&agent_prefix, 0)
        .expect("agent dip stored");
    let root_kel = collect_kel(ctx.registry.as_ref(), &root_prefix);
    validate_delegation(&dip, &root_kel).expect("root anchored the agent bilaterally");
}

#[test]
fn agent_did_derives_from_dip_said() {
    let (ctx, root_alias, _root_prefix, _tmp) = setup();
    let agent_alias = KeyAlias::new_unchecked("derive-bot");

    let agent = add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519).expect("delegate agent");
    let agent_prefix = Prefix::new_unchecked(agent.agent_prefix.clone());
    let dip = ctx
        .registry
        .get_event(&agent_prefix, 0)
        .expect("agent dip stored");

    // A dip is self-addressing: prefix == SAID, and the did:keri wraps that prefix.
    assert_eq!(agent.agent_prefix, dip.said().as_str());
    assert_eq!(agent.agent_did, format!("did:keri:{}", dip.said()));
}

#[test]
fn agents_add_rejects_duplicate_key() {
    let (ctx, root_alias, _root_prefix, _tmp) = setup();
    let agent_alias = KeyAlias::new_unchecked("dup-bot");

    add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519).expect("first delegation");
    let err = add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519)
        .expect_err("re-delegating an existing alias must be rejected");
    assert!(
        matches!(err, AgentError::AlreadyDelegated { .. }),
        "expected AlreadyDelegated, got {err:?}"
    );
}

#[test]
fn agents_rotate_advances_kel() {
    let (ctx, root_alias, _root, _tmp) = setup();
    let agent_alias = KeyAlias::new_unchecked("rot-bot");
    let agent = add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519).expect("delegate agent");
    let agent_prefix = Prefix::new_unchecked(agent.agent_prefix.clone());

    rotate(&ctx, &root_alias, &agent.agent_did).expect("rotate agent");

    let drt = ctx
        .registry
        .get_event(&agent_prefix, 1)
        .expect("drt at sequence 1");
    assert!(matches!(drt, Event::Drt(_)), "rotation authors a drt");
    assert_eq!(drt.sequence().value(), 1);
}

#[test]
fn old_key_stops_verifying_after_rotate() {
    let (ctx, root_alias, _root, _tmp) = setup();
    let agent_alias = KeyAlias::new_unchecked("oldkey-bot");
    let agent = add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519).expect("delegate agent");
    let agent_prefix = Prefix::new_unchecked(agent.agent_prefix.clone());

    let dip = ctx.registry.get_event(&agent_prefix, 0).expect("dip");
    let old_key = dip.keys().expect("dip keys")[0].clone();

    rotate(&ctx, &root_alias, &agent.agent_did).expect("rotate agent");

    let drt = ctx.registry.get_event(&agent_prefix, 1).expect("drt");
    let new_key = drt.keys().expect("drt keys")[0].clone();
    assert_ne!(
        old_key, new_key,
        "rotation must replace the agent's current key (the old key no longer verifies)"
    );
}

#[test]
fn rotate_revoked_agent_rejected() {
    let (ctx, root_alias, _root, _tmp) = setup();
    let agent_alias = KeyAlias::new_unchecked("rev-bot");
    let agent = add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519).expect("delegate agent");

    // Revoke via the shared delegation engine (revoking a delegated AID is curve-
    // and role-agnostic — agents and devices share it).
    remove_device(&ctx, &root_alias, &agent.agent_did).expect("revoke agent");

    let err =
        rotate(&ctx, &root_alias, &agent.agent_did).expect_err("a revoked agent must not rotate");
    assert!(
        matches!(err, AgentError::Revoked { .. }),
        "expected Revoked, got {err:?}"
    );
}

#[test]
fn drt_chain_validates_after_rotate() {
    let (ctx, root_alias, root_prefix, _tmp) = setup();
    let agent_alias = KeyAlias::new_unchecked("chain-bot");
    let agent = add(&ctx, &root_alias, &agent_alias, CurveType::Ed25519).expect("delegate agent");
    let agent_prefix = Prefix::new_unchecked(agent.agent_prefix.clone());

    rotate(&ctx, &root_alias, &agent.agent_did).expect("rotate agent");

    let dip = ctx.registry.get_event(&agent_prefix, 0).expect("dip");
    let drt = ctx.registry.get_event(&agent_prefix, 1).expect("drt");
    // The drt chains onto the dip, and is bilaterally anchored by the delegator.
    assert_eq!(
        drt.previous().expect("drt has prior").as_str(),
        dip.said().as_str()
    );
    let root_kel = collect_kel(ctx.registry.as_ref(), &root_prefix);
    validate_delegation(&dip, &root_kel).expect("dip bilateral binding holds");
    validate_delegation(&drt, &root_kel)
        .expect("drt bilateral binding holds against the delegator");
}

#[test]
fn agents_revoke_marks_revoked() {
    let (ctx, root_alias, _root, _tmp) = setup();
    let agent = add(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("revoke-bot"),
        CurveType::Ed25519,
    )
    .expect("delegate agent");

    revoke(&ctx, &root_alias, &agent.agent_did).expect("revoke agent");

    let agents = list(&ctx).expect("list agents");
    let entry = agents
        .iter()
        .find(|a| a.agent_did == agent.agent_did)
        .expect("agent still in the full set");
    assert!(entry.revoked, "revoked agent must be flagged revoked");
}

#[test]
fn revoked_agent_excluded_from_list() {
    let (ctx, root_alias, _root, _tmp) = setup();
    let agent = add(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("excluded-bot"),
        CurveType::Ed25519,
    )
    .expect("delegate agent");

    revoke(&ctx, &root_alias, &agent.agent_did).expect("revoke agent");

    // The live set (non-revoked) excludes it.
    let live = list(&ctx)
        .expect("list agents")
        .into_iter()
        .filter(|a| !a.revoked)
        .count();
    assert_eq!(live, 0, "revoked agent must drop out of the live set");
}

#[test]
fn agent_list_excludes_devices() {
    let (ctx, root_alias, _root, _tmp) = setup();
    let agent = add(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("only-agent"),
        CurveType::Ed25519,
    )
    .expect("delegate agent");
    let device = add_device(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("only-device"),
        CurveType::Ed25519,
    )
    .expect("delegate device");

    // `agent list` shows the agent, never the device.
    let agents = list(&ctx).expect("list agents");
    assert_eq!(agents.len(), 1, "exactly one agent");
    assert_eq!(agents[0].agent_did, agent.agent_did);

    // `device list` shows delegated devices (including the init-created device #0), never
    // the agent.
    let devices = list_delegated_devices(&ctx).expect("list devices");
    assert!(
        devices.iter().any(|d| d.device_did == device.device_did),
        "the delegated device appears in the device list"
    );
    assert!(
        !devices.iter().any(|d| d.device_did == agent.agent_did),
        "the agent never appears in the device list"
    );
}

#[test]
fn revoke_already_revoked_idempotent() {
    let (ctx, root_alias, _root, _tmp) = setup();
    let agent = add(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("idem-bot"),
        CurveType::Ed25519,
    )
    .expect("delegate agent");

    revoke(&ctx, &root_alias, &agent.agent_did).expect("first revoke");
    revoke(&ctx, &root_alias, &agent.agent_did).expect("re-revoking is idempotent (Ok)");
}

#[test]
fn scope_cannot_exceed_delegator() {
    let (ctx, root_alias, root_prefix, _tmp) = setup();

    // Give the delegator (root) a scope of [read, write] (using its actual curve).
    let (_pk, root_curve) = extract_public_key_bytes(
        ctx.key_storage.as_ref(),
        &root_alias,
        ctx.passphrase_provider.as_ref(),
    )
    .expect("root curve");
    mark_agent_scope(
        ctx.registry.as_ref(),
        &root_prefix,
        &root_alias,
        root_curve,
        &root_prefix,
        &AgentScope {
            capabilities: vec![
                Capability::parse("read").unwrap(),
                Capability::parse("write").unwrap(),
            ],
            expires_at: None,
        },
        ctx.passphrase_provider.as_ref(),
        ctx.key_storage.as_ref(),
    )
    .expect("anchor delegator scope");

    // Delegating an agent with [read, admin] must be rejected — admin exceeds the
    // delegator's own scope (a delegate can only narrow, never widen).
    let err = add_scoped(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("scoped-bot"),
        CurveType::Ed25519,
        &[
            auths_keri::Capability::parse("read").unwrap(),
            auths_keri::Capability::parse("admin").unwrap(),
        ],
        None,
    )
    .expect_err("scope exceeding the delegator must be rejected");
    assert!(
        matches!(err, AgentError::OutsideDelegatorScope { ref capability } if capability == "admin"),
        "got {err:?}"
    );
}

#[test]
fn scoped_agent_may_narrow_the_delegator_scope() {
    let (ctx, root_alias, root_prefix, _tmp) = setup();

    // Give the delegator [read, write], so the subset check actually runs (not the
    // unconstrained-root path).
    let (_pk, root_curve) = extract_public_key_bytes(
        ctx.key_storage.as_ref(),
        &root_alias,
        ctx.passphrase_provider.as_ref(),
    )
    .expect("root curve");
    mark_agent_scope(
        ctx.registry.as_ref(),
        &root_prefix,
        &root_alias,
        root_curve,
        &root_prefix,
        &AgentScope {
            capabilities: vec![
                Capability::parse("read").unwrap(),
                Capability::parse("write").unwrap(),
            ],
            expires_at: None,
        },
        ctx.passphrase_provider.as_ref(),
        ctx.key_storage.as_ref(),
    )
    .expect("anchor delegator scope");

    // Delegating [read] — a strict subset of [read, write] — must be allowed. The subset
    // rule narrows; it must not reject a legitimate narrowing (a false-reject would be a
    // usability regression and push callers toward over-broad grants).
    let agent = add_scoped(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("narrowed-bot"),
        CurveType::Ed25519,
        &[Capability::parse("read").unwrap()],
        None,
    )
    .expect("narrowing the delegator's scope to a subset must be allowed");
    assert!(agent.agent_did.starts_with("did:keri:"));
}

#[test]
fn scoped_agent_with_empty_scope_is_allowed() {
    let (ctx, root_alias, _root_prefix, _tmp) = setup();

    // An empty scope is a subset of any delegator scope — a capability-less, anchor-only
    // agent (e.g. an identity placeholder) is valid, not an error.
    let agent = add_scoped(
        &ctx,
        &root_alias,
        &KeyAlias::new_unchecked("no-scope-bot"),
        CurveType::Ed25519,
        &[],
        None,
    )
    .expect("an empty scope is a valid (capability-less) delegation");
    assert!(agent.agent_did.starts_with("did:keri:"));
}