mid-signin 0.2.1

The drop-in Relying-Party kit for Sign in with MATA (mID). Wraps mid-verify's pure cryptography with the RP duties the verifier leaves open — single-use nonce enforcement, genesis-anchor + rollback persistence, session production — plus an embedded zero-schema store and ready-made axum routes.
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
//! The roster lifecycle of one derived identity, seen from a relying party
//! (DID-logins M4, ADR 0011 D5).
//!
//! One person, one vault key, one DID. Every device derives the same root and
//! the same canonical genesis (`device-0`, the frozen epoch), so a relying
//! party anchors every device on one hash. Devices then enter the roster as
//! chain entries signed by a rostered key, sign their own tokens under
//! `device-<id>`, can be revoked by another rostered device, and once every
//! device is rostered the root retires — after which a vault alone signs
//! nothing a relying party accepts.
//!
//! These tests mint real JWTs and drive the real [`Authenticator`] with its
//! anchor store, so each expectation is what a deployed RP would do.

use kms_client::InMemoryDeviceSigner;

use crate::test_fixtures::{
    make_signing_key, mint_jwt, sign_chain_entry, sign_genesis, DeviceKey, IdentityFixture,
};
use crate::{Authenticator, RpError, VerifyError, VerifyParams};
use mid_issuer::{
    build_mid_jwt, AttestedBy, ClaimRequest, ClaimValue, EmbeddedGenesisRoster,
    EmbeddedRosterChainEntry, IdentitySnapshot, MidIssuerError, MidJwtPayload, RpRequest,
    VerificationMethod,
};
use std::collections::BTreeMap;

const RP: &str = "https://acme.com";
/// `mata_identity::MID_DERIVED_GENESIS_EPOCH` (2025-09-30T00:00Z; the value is arbitrary and frozen), the frozen
/// `signed_at` of every canonical genesis. Spelled out here because this crate
/// is open-core and depends on nothing proprietary; the vector of record pins
/// the same value.
const EPOCH: u64 = 1_759_190_400;
const IAT: u64 = EPOCH + 60;
const NOW: u64 = IAT + 10;
const ROOT_KID: &str = "device-0";

/// The person: a root as `device-0` (stands in for the derived `#root`) and
/// two devices with their own M5 keys.
struct Person {
    identity: IdentityFixture,
    alienware: DeviceKey,
    macbook: DeviceKey,
}

impl Person {
    fn new() -> Self {
        Self {
            identity: IdentityFixture::new("vault-derived-root", ROOT_KID),
            alienware: DeviceKey::new("m5-alienware", "device-alienware"),
            macbook: DeviceKey::new("m5-macbook", "device-macbook"),
        }
    }

    fn did(&self) -> String {
        self.identity.did()
    }

    fn root_vm(&self) -> VerificationMethod {
        self.identity.vm_for(&self.identity.genesis)
    }

    /// The canonical genesis, self-signed by the root. Each call signs anew,
    /// which is exactly what two devices deriving it independently do.
    fn canonical_genesis(&self) -> EmbeddedGenesisRoster {
        let mut genesis = EmbeddedGenesisRoster {
            version: 1,
            did: self.did(),
            verification_methods: vec![self.root_vm()],
            signed_at: EPOCH,
            self_signed_by_genesis_key: String::new(),
        };
        genesis.self_signed_by_genesis_key =
            sign_genesis(&genesis, &self.identity.genesis.signing_key);
        genesis
    }

    /// One chain link: `new_vms` becomes the head at `version`, signed by
    /// `signer` under `signer_kid`.
    fn link(
        &self,
        version: u64,
        new_vms: Vec<VerificationMethod>,
        signer_kid: &str,
        signer: &DeviceKey,
    ) -> EmbeddedRosterChainEntry {
        let mut entry = EmbeddedRosterChainEntry {
            version,
            did: self.did(),
            verification_methods: new_vms,
            signed_at: EPOCH + 100 * version,
            signed_by_signer_in_prior_roster: String::new(),
            signer_kid: signer_kid.to_string(),
        };
        entry.signed_by_signer_in_prior_roster = sign_chain_entry(&entry, &signer.signing_key);
        entry
    }

    /// v2: the root adds both devices — what `bootstrap_roster` appends on the
    /// first boot where every paired device has announced under the DID.
    fn v2_root_adds_both(&self) -> EmbeddedRosterChainEntry {
        self.link(
            2,
            vec![
                self.root_vm(),
                self.identity.vm_for(&self.alienware),
                self.identity.vm_for(&self.macbook),
            ],
            ROOT_KID,
            &self.identity.genesis,
        )
    }

    /// v3: the Alienware removes the MacBook, signed with its own rostered key
    /// — `revoke_device` riding `paired_devices_store::remove`.
    fn v3_alienware_revokes_macbook(&self) -> EmbeddedRosterChainEntry {
        self.link(
            3,
            vec![self.root_vm(), self.identity.vm_for(&self.alienware)],
            "device-alienware",
            &self.alienware,
        )
    }

    /// v4: the Alienware retires the root — `maybe_retire_root` once every
    /// admitted device is in the head and this device signs with its own key.
    fn v4_alienware_retires_root(&self) -> EmbeddedRosterChainEntry {
        self.link(
            4,
            vec![self.identity.vm_for(&self.alienware)],
            "device-alienware",
            &self.alienware,
        )
    }

    /// The wallet state a device presents: the canonical genesis (signed by
    /// this device's own derivation), the chain it has synced, and its own
    /// verification method as the signer.
    fn snapshot(&self, chain: Vec<EmbeddedRosterChainEntry>, device: &DeviceKey) -> IdentitySnapshot {
        let mut approved = BTreeMap::new();
        approved.insert(
            "did".into(),
            ClaimValue {
                value: serde_json::Value::String(self.did()),
                attested_by: AttestedBy::SelfAttested,
                verified_at_signup: None,
                computed_at: None,
                formula_version: None,
            },
        );
        IdentitySnapshot {
            did: self.did(),
            genesis_roster: self.canonical_genesis(),
            roster_chain: chain,
            current_verification_method: self.identity.embedded_vm_for(device),
            approved_claims: approved,
        }
    }

    fn signer_for(&self, device: &DeviceKey, seed: &str) -> InMemoryDeviceSigner {
        InMemoryDeviceSigner::new(device.device_id.clone(), make_signing_key(seed))
    }

    /// What an honest wallet does: ask the issuer for a token. The issuer's
    /// pre-flight refuses when this device's key is not in the head roster —
    /// a vault without a rostered device key cannot sign, before any RP is
    /// involved.
    fn mint_attempt(
        &self,
        chain: Vec<EmbeddedRosterChainEntry>,
        device: &DeviceKey,
        seed: &str,
    ) -> Result<String, MidIssuerError> {
        let snapshot = self.snapshot(chain, device);
        let signer = self.signer_for(device, seed);
        let req = RpRequest {
            rp_origin: RP.into(),
            nonce: "n".into(),
            claims: ClaimRequest { required: vec!["did".into()], optional: vec![], custom: BTreeMap::new() },
        };
        build_mid_jwt(&req, &snapshot, &signer, IAT)
    }

    /// What a dishonest wallet does: skip the issuer's pre-flight and sign the
    /// payload anyway. This is the token a relying party must still reject.
    fn forge_jwt(
        &self,
        chain: Vec<EmbeddedRosterChainEntry>,
        device: &DeviceKey,
        seed: &str,
        nonce: &str,
    ) -> String {
        let snapshot = self.snapshot(chain, device);
        let signer = self.signer_for(device, seed);
        let payload = MidJwtPayload {
            iss: snapshot.did.clone(),
            sub: snapshot.did.clone(),
            aud: RP.into(),
            iat: IAT,
            exp: IAT + 300,
            nonce: nonce.into(),
            claims: snapshot.approved_claims.clone(),
            embedded_genesis_roster: snapshot.genesis_roster.clone(),
            embedded_roster_chain: snapshot.roster_chain.clone(),
            embedded_verification_method: snapshot.current_verification_method.clone(),
        };
        let payload_json = serde_json::to_vec(&payload).unwrap();
        mid_issuer::jws::build_jws_compact(&payload_json, &signer).expect("raw JWS")
    }

    /// Sign in as `device` presenting `chain` at `auth`.
    fn sign_in(
        &self,
        auth: &Authenticator,
        chain: Vec<EmbeddedRosterChainEntry>,
        device: &DeviceKey,
        seed: &str,
    ) -> Result<crate::RpSession, RpError> {
        let snapshot = self.snapshot(chain, device);
        let signer = self.signer_for(device, seed);
        let nonce = auth.issue_nonce(NOW).unwrap();
        let jwt = mint_jwt(&snapshot, &signer, RP, &nonce, IAT);
        auth.authenticate(&jwt, &VerifyParams::new(RP, &nonce, NOW))
    }
}

/// Two devices of one person, each deriving the genesis on its own, sign in
/// at one relying party under their own keys, on one anchor, at roster v2.
#[test]
fn two_devices_authenticate_at_one_relying_party_on_one_anchor() {
    let p = Person::new();
    let auth = Authenticator::in_memory();
    let chain = vec![p.v2_root_adds_both()];

    let a = p
        .sign_in(&auth, chain.clone(), &p.alienware, "m5-alienware")
        .expect("the Alienware signs in under device-alienware");
    let b = p
        .sign_in(&auth, chain.clone(), &p.macbook, "m5-macbook")
        .expect("the MacBook signs in under device-macbook, on the anchor the Alienware set");
    assert_eq!(a.did, p.did());
    assert_eq!(b.did, p.did());
    assert_eq!(a.current_version, 2);
    assert_eq!(b.current_version, 2);

    // The root itself still signs while `device-0` is in the head.
    let r = p
        .sign_in(&auth, chain, &p.identity.genesis, "vault-derived-root")
        .expect("the root signs as device-0 until retired");
    assert_eq!(r.current_version, 2);
}

/// A device that arrives with only the genesis (roster v1) after another
/// device has already presented v2 is a rollback, exactly as a stolen device
/// replaying an old state would be — the gate in `bootstrap_roster` exists so
/// this never happens to a legitimate device.
#[test]
fn a_device_behind_the_roster_head_reads_as_a_rollback() {
    let p = Person::new();
    let auth = Authenticator::in_memory();
    p.sign_in(&auth, vec![p.v2_root_adds_both()], &p.alienware, "m5-alienware")
        .expect("v2 seen");
    let err = p
        .sign_in(&auth, vec![], &p.identity.genesis, "vault-derived-root")
        .unwrap_err();
    assert!(
        matches!(err, RpError::Rollback { presented: 1, last_seen: 2, .. }),
        "got {err:?}"
    );
}

/// Revocation: the Alienware removes the MacBook with its own rostered key.
/// The MacBook's wallet then refuses to mint at all (its key is not in the
/// head); a forged token is refused by every relying party that sees the new
/// chain (`CurrentVmNotInHeadRoster`); and presenting the old head it was
/// still in is a rollback at any that saw v3.
#[test]
fn a_revoked_device_is_refused_and_its_old_head_is_a_rollback() {
    let p = Person::new();
    let auth = Authenticator::in_memory();
    let v2 = p.v2_root_adds_both();
    let v3 = p.v3_alienware_revokes_macbook();

    // Both in, then the revocation lands and the Alienware presents it.
    p.sign_in(&auth, vec![v2.clone()], &p.macbook, "m5-macbook")
        .expect("the MacBook was rostered at v2");
    let a = p
        .sign_in(&auth, vec![v2.clone(), v3.clone()], &p.alienware, "m5-alienware")
        .expect("a device-signed removal verifies");
    assert_eq!(a.current_version, 3);

    // The honest MacBook wallet with the honest chain: its issuer refuses.
    let err = p
        .mint_attempt(vec![v2.clone(), v3.clone()], &p.macbook, "m5-macbook")
        .unwrap_err();
    assert!(matches!(err, MidIssuerError::SignerNotInHeadRoster(_)), "got {err:?}");

    // A forged MacBook token over the honest chain: the RP refuses too.
    let nonce = auth.issue_nonce(NOW).unwrap();
    let jwt = p.forge_jwt(vec![v2.clone(), v3.clone()], &p.macbook, "m5-macbook", &nonce);
    let err = auth
        .authenticate(&jwt, &VerifyParams::new(RP, &nonce, NOW))
        .unwrap_err();
    assert!(
        matches!(err, RpError::Verify(VerifyError::CurrentVmNotInHeadRoster { .. })),
        "got {err:?}"
    );

    // The MacBook replaying the head it was still in: a rollback.
    let err = p
        .sign_in(&auth, vec![v2.clone()], &p.macbook, "m5-macbook")
        .unwrap_err();
    assert!(
        matches!(err, RpError::Rollback { presented: 2, last_seen: 3, .. }),
        "got {err:?}"
    );

    // And the revoked key cannot sign a link that re-admits itself: the
    // issuer lets it through (its own forged head lists it), the RP does not.
    let forged = p.link(
        4,
        vec![p.root_vm(), p.identity.vm_for(&p.alienware), p.identity.vm_for(&p.macbook)],
        "device-macbook",
        &p.macbook,
    );
    let err = p
        .sign_in(&auth, vec![v2, v3, forged], &p.macbook, "m5-macbook")
        .unwrap_err();
    assert!(
        matches!(err, RpError::Verify(VerifyError::ChainSignerNotInPriorRoster { .. })),
        "got {err:?}"
    );
}

/// Root retirement: once every admitted device is rostered, the Alienware
/// drops `device-0`. From then on the vault alone — the root key — signs
/// nothing: its wallet refuses to mint, a forged token is refused by the RP,
/// and it cannot sign a link to get back in. The rostered device keeps
/// signing in and can still admit devices.
#[test]
fn after_root_retirement_the_vault_alone_cannot_sign() {
    let p = Person::new();
    let auth = Authenticator::in_memory();
    let v2 = p.v2_root_adds_both();
    let v3 = p.v3_alienware_revokes_macbook();
    let v4 = p.v4_alienware_retires_root();
    let chain = vec![v2, v3, v4];

    let a = p
        .sign_in(&auth, chain.clone(), &p.alienware, "m5-alienware")
        .expect("the rostered device signs in after retirement");
    assert_eq!(a.current_version, 4);

    // The honest root wallet with the honest chain: its issuer refuses.
    let err = p
        .mint_attempt(chain.clone(), &p.identity.genesis, "vault-derived-root")
        .unwrap_err();
    assert!(matches!(err, MidIssuerError::SignerNotInHeadRoster(_)), "got {err:?}");

    // A forged root token over the honest chain: `device-0` is not in the head.
    let nonce = auth.issue_nonce(NOW).unwrap();
    let jwt = p.forge_jwt(chain.clone(), &p.identity.genesis, "vault-derived-root", &nonce);
    let err = auth
        .authenticate(&jwt, &VerifyParams::new(RP, &nonce, NOW))
        .unwrap_err();
    assert!(
        matches!(err, RpError::Verify(VerifyError::CurrentVmNotInHeadRoster { .. })),
        "a vault without a rostered device key cannot sign in: got {err:?}"
    );

    // The root presenting the chain from before its retirement: a rollback.
    let err = p
        .sign_in(&auth, chain[..2].to_vec(), &p.identity.genesis, "vault-derived-root")
        .unwrap_err();
    assert!(
        matches!(err, RpError::Rollback { presented: 3, last_seen: 4, .. }),
        "got {err:?}"
    );

    // The root signing a link that re-adds itself: not a signer in the prior roster.
    let mut readd = chain.clone();
    readd.push(p.link(
        5,
        vec![p.identity.vm_for(&p.alienware), p.root_vm()],
        ROOT_KID,
        &p.identity.genesis,
    ));
    let err = p
        .sign_in(&auth, readd, &p.identity.genesis, "vault-derived-root")
        .unwrap_err();
    assert!(
        matches!(err, RpError::Verify(VerifyError::ChainSignerNotInPriorRoster { .. })),
        "got {err:?}"
    );

    // A rostered device can still add a device after retirement.
    let mut readmit = chain;
    readmit.push(p.link(
        5,
        vec![p.identity.vm_for(&p.alienware), p.identity.vm_for(&p.macbook)],
        "device-alienware",
        &p.alienware,
    ));
    let b = p
        .sign_in(&auth, readmit, &p.macbook, "m5-macbook")
        .expect("re-admitted by a rostered device key, no root needed");
    assert_eq!(b.current_version, 5);
}

/// The anchor never moves: a genesis that is not the canonical one — the
/// pre-ADR-0011 profile-stamped anchor a stale cache could still hold — is a
/// different identity to a relying party that anchored the canonical hash.
#[test]
fn a_legacy_anchor_is_a_different_identity_to_the_relying_party() {
    let p = Person::new();
    let auth = Authenticator::in_memory();
    p.sign_in(&auth, vec![], &p.identity.genesis, "vault-derived-root")
        .expect("canonical genesis anchored");

    let mut stale = p.snapshot(vec![], &p.identity.genesis);
    stale.genesis_roster.signed_at = EPOCH - 12_345; // a profile stamp
    stale.genesis_roster.self_signed_by_genesis_key =
        sign_genesis(&stale.genesis_roster, &p.identity.genesis.signing_key);
    let signer = p.signer_for(&p.identity.genesis, "vault-derived-root");
    let nonce = auth.issue_nonce(NOW).unwrap();
    let jwt = mint_jwt(&stale, &signer, RP, &nonce, IAT);
    let err = auth
        .authenticate(&jwt, &VerifyParams::new(RP, &nonce, NOW))
        .unwrap_err();
    assert!(matches!(err, RpError::GenesisMismatch { .. }), "got {err:?}");
}