auths-id 0.1.2

Multi-device identity and attestation crate for Auths
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
//! Identity-level witness configuration.
//!
//! Declares which witnesses an identity uses, the quorum threshold,
//! and the degradation policy when witnesses are unreachable.
//!
//! A witness is identified by its **AID** (a curve-tagged CESR verkey prefix),
//! not merely a URL: the AID is what an identity designates in `b[]`, what KAWA
//! dedupes quorum by, and what a collected receipt's signature is verified
//! against. The URL is only where to reach it. Resolving a URL to its AID
//! (`GET /health`) happens at the infra/CLI boundary (it needs the HTTP client);
//! this layer owns the typed config + the pin/dedup logic.

use std::path::Path;

use auths_keri::Prefix;
use auths_keri::witness::independence::{
    Independence, IndependencePolicy, OperatorAttributes, WitnessOperatorInfo, spans_distinct,
};
use serde::{Deserialize, Serialize};
use url::Url;

/// Bundled witness configuration for anchor operations.
///
/// Replaces the error-prone `(Option<&WitnessConfig>, Option<&Path>)` pair.
/// With two separate Options, a caller can pass `Some(config)` with `None` for
/// `repo_path`, silently degrading `Enforce` to a no-op. This enum makes that
/// invalid state unrepresentable.
#[derive(Debug, Clone, Copy)]
pub enum WitnessParams<'a> {
    /// Witness receipting is active with a validated config and storage path.
    Enabled {
        config: &'a WitnessConfig,
        repo_path: &'a Path,
    },
    /// Witness receipting is explicitly disabled for this operation.
    Disabled,
}

/// A configured witness: where to reach it (`url`) and who it is (`aid`).
///
/// `aid` is the witness's curve-tagged CESR verkey prefix (parseable via
/// `KeriPublicKey::parse`) — the value designated in `b[]` and matched against
/// receipt signatures. It is resolved once (from the witness's `/health`) and
/// pinned, so later trust decisions never depend on re-resolving a URL.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WitnessRef {
    /// Where to reach the witness server.
    pub url: Url,
    /// The witness's pinned AID (curve-tagged CESR verkey prefix).
    pub aid: Prefix,
    /// Operator-independence attributes for this witness ([`WitnessOperatorInfo`],
    /// shared with the CT trust root). Absent ⇒ this witness cannot contribute to
    /// proving quorum independence (fail closed, never "assume distinct").
    #[serde(default)]
    pub operator_info: Option<WitnessOperatorInfo>,
}

impl WitnessRef {
    /// Build the [`OperatorAttributes`] for this witness, keyed by its AID.
    ///
    /// Returns `None` when the witness has no pinned `operator_info` — the caller
    /// must treat that as "independence cannot be proven", not as a distinct
    /// operator.
    ///
    /// Args:
    /// * `self`: The pinned witness.
    ///
    /// Usage:
    /// ```ignore
    /// let attrs = witness_ref.operator_attributes();
    /// ```
    pub fn operator_attributes(&self) -> Option<OperatorAttributes> {
        self.operator_info
            .as_ref()
            .map(|info| info.to_attributes(self.aid.as_str()))
    }
}

/// Configuration for witness receipts on an identity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WitnessConfig {
    /// Schema version for forwards-compatible deserialization.
    #[serde(default = "default_version")]
    pub version: u8,
    /// Configured witnesses (pinned `(url, aid)` pairs).
    #[serde(default)]
    pub witnesses: Vec<WitnessRef>,
    /// Minimum receipts required (k-of-n threshold).
    pub threshold: usize,
    /// Per-witness timeout in milliseconds.
    pub timeout_ms: u64,
    /// Degradation policy when quorum is not met.
    pub policy: WitnessPolicy,
}

fn default_version() -> u8 {
    2
}

impl Default for WitnessConfig {
    fn default() -> Self {
        Self {
            version: 2,
            witnesses: vec![],
            threshold: 0,
            timeout_ms: 5000,
            policy: WitnessPolicy::Enforce,
        }
    }
}

impl WitnessConfig {
    /// Returns `true` when witness collection should actually run.
    pub fn is_enabled(&self) -> bool {
        !self.witnesses.is_empty() && self.threshold > 0 && self.policy != WitnessPolicy::Skip
    }

    /// The configured witness URLs (where to reach each witness).
    pub fn urls(&self) -> impl Iterator<Item = &Url> {
        self.witnesses.iter().map(|w| &w.url)
    }

    /// The configured witness AIDs (the values designated in `b[]`).
    pub fn aids(&self) -> impl Iterator<Item = &Prefix> {
        self.witnesses.iter().map(|w| &w.aid)
    }

    /// Whether `aid` is one of the configured witnesses (for receipt provenance
    /// checks and KAWA membership).
    pub fn contains_aid(&self, aid: &Prefix) -> bool {
        self.witnesses.iter().any(|w| &w.aid == aid)
    }

    /// Pin a witness, deduped by AID. Returns `true` if newly added, `false` if
    /// an entry with the same AID was already present.
    pub fn pin(&mut self, witness: WitnessRef) -> bool {
        if self.witnesses.iter().any(|w| w.aid == witness.aid) {
            return false;
        }
        self.witnesses.push(witness);
        true
    }

    /// Remove the witness with the given URL. Returns `true` if one was removed.
    pub fn remove_url(&mut self, url: &Url) -> bool {
        let before = self.witnesses.len();
        self.witnesses.retain(|w| &w.url != url);
        self.witnesses.len() != before
    }

    /// Whether the configured roster *could* form an independent quorum.
    ///
    /// A startup fail-fast check over the whole pinned set: if any witness lacks
    /// `operator_info`, independence cannot be proven (fail closed). This is the
    /// roster-capability check — NOT the security-decisive per-attestation check,
    /// which must run [`spans_distinct`] over the ACTUAL cosigning quorum.
    ///
    /// Args:
    /// * `policy`: The minimum-diversity thresholds to require.
    ///
    /// Usage:
    /// ```ignore
    /// let verdict = config.roster_independence(&IndependencePolicy::default());
    /// if !verdict.independent { /* refuse to start, or warn */ }
    /// ```
    pub fn roster_independence(&self, policy: &IndependencePolicy) -> Independence {
        let mut attesters = Vec::with_capacity(self.witnesses.len());
        for w in &self.witnesses {
            match w.operator_attributes() {
                Some(a) => attesters.push(a),
                None => {
                    return Independence::cannot_prove(format!(
                        "witness {} is missing operator/organization/jurisdiction/infrastructure attributes",
                        w.aid.as_str()
                    ));
                }
            }
        }
        spans_distinct(&attesters, policy)
    }
}

/// What to do when the witness quorum cannot be met.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub enum WitnessPolicy {
    /// Fail the operation if quorum is not met.
    #[default]
    Enforce,
    /// Log a warning but continue.
    Warn,
    /// Skip witness collection entirely.
    Skip,
}

#[cfg(test)]
mod tests {
    use super::*;
    use auths_keri::witness::independence::{
        Infrastructure, Jurisdiction, OperatorId, Organization,
    };

    fn ref_for(url: &str, aid: &str) -> WitnessRef {
        WitnessRef {
            url: url.parse().unwrap(),
            aid: Prefix::new_unchecked(aid.to_string()),
            operator_info: None,
        }
    }

    fn tagged_ref(aid: &str, operator: &str, org: &str, jur: &str, infra: &str) -> WitnessRef {
        WitnessRef {
            url: "http://w:3333".parse().unwrap(),
            aid: Prefix::new_unchecked(aid.to_string()),
            operator_info: Some(WitnessOperatorInfo {
                operator: OperatorId::new(operator).unwrap(),
                organization: Organization::new(org).unwrap(),
                jurisdiction: Jurisdiction::new(jur).unwrap(),
                infrastructure: Infrastructure::new(infra).unwrap(),
            }),
        }
    }

    fn config_with(witnesses: Vec<WitnessRef>) -> WitnessConfig {
        WitnessConfig {
            witnesses,
            threshold: 2,
            ..Default::default()
        }
    }

    #[test]
    fn roster_independent_with_three_diverse_operators() {
        let config = config_with(vec![
            tagged_ref(
                "BW_A0000000000000000000000000000000000000000",
                "op-a",
                "org-a",
                "US",
                "aws/us-east-1",
            ),
            tagged_ref(
                "BW_B0000000000000000000000000000000000000000",
                "op-b",
                "org-b",
                "DE",
                "gcp/eu-west-1",
            ),
            tagged_ref(
                "BW_C0000000000000000000000000000000000000000",
                "op-c",
                "org-c",
                "JP",
                "azure/jp-east",
            ),
        ]);
        assert!(
            config
                .roster_independence(&IndependencePolicy::default())
                .independent
        );
    }

    #[test]
    fn roster_fails_closed_when_attributes_missing() {
        // One tagged, one untagged → cannot prove independence.
        let config = config_with(vec![
            tagged_ref(
                "BW_A0000000000000000000000000000000000000000",
                "op-a",
                "org-a",
                "US",
                "aws/us-east-1",
            ),
            ref_for(
                "http://w2:3333",
                "BW_B0000000000000000000000000000000000000000",
            ),
        ]);
        let verdict = config.roster_independence(&IndependencePolicy::default());
        assert!(!verdict.independent);
    }

    #[test]
    fn roster_same_org_is_not_independent() {
        let config = config_with(vec![
            tagged_ref(
                "BW_A0000000000000000000000000000000000000000",
                "op-a",
                "acme",
                "US",
                "aws/us-east-1",
            ),
            tagged_ref(
                "BW_B0000000000000000000000000000000000000000",
                "op-b",
                "acme",
                "DE",
                "gcp/eu-west-1",
            ),
            tagged_ref(
                "BW_C0000000000000000000000000000000000000000",
                "op-c",
                "acme",
                "JP",
                "azure/jp-east",
            ),
        ]);
        assert!(
            !config
                .roster_independence(&IndependencePolicy::default())
                .independent
        );
    }

    #[test]
    fn default_is_disabled() {
        let config = WitnessConfig::default();
        assert!(!config.is_enabled());
    }

    #[test]
    fn enabled_with_witnesses_and_threshold() {
        let config = WitnessConfig {
            witnesses: vec![ref_for(
                "http://w1:3333",
                "BWitnessOne00000000000000000000000000000000",
            )],
            threshold: 1,
            timeout_ms: 5000,
            ..Default::default()
        };
        assert!(config.is_enabled());
        assert_eq!(config.urls().count(), 1);
        assert!(config.contains_aid(&Prefix::new_unchecked(
            "BWitnessOne00000000000000000000000000000000".to_string()
        )));
    }

    #[test]
    fn skip_policy_disables() {
        let config = WitnessConfig {
            witnesses: vec![ref_for(
                "http://w1:3333",
                "BWitnessOne00000000000000000000000000000000",
            )],
            threshold: 1,
            timeout_ms: 5000,
            policy: WitnessPolicy::Skip,
            ..Default::default()
        };
        assert!(!config.is_enabled());
    }

    #[test]
    fn zero_threshold_disables() {
        let config = WitnessConfig {
            witnesses: vec![ref_for(
                "http://w1:3333",
                "BWitnessOne00000000000000000000000000000000",
            )],
            threshold: 0,
            timeout_ms: 5000,
            ..Default::default()
        };
        assert!(!config.is_enabled());
    }

    #[test]
    fn pin_dedupes_by_aid() {
        let mut config = WitnessConfig::default();
        assert!(config.pin(ref_for(
            "http://w1:3333",
            "BWitnessOne00000000000000000000000000000000"
        )));
        // Same AID, different URL → not added again.
        assert!(!config.pin(ref_for(
            "http://other:3333",
            "BWitnessOne00000000000000000000000000000000"
        )));
        assert_eq!(config.witnesses.len(), 1);
    }

    #[test]
    fn remove_url_drops_entry() {
        let mut config = WitnessConfig::default();
        config.pin(ref_for(
            "http://w1:3333",
            "BWitnessOne00000000000000000000000000000000",
        ));
        assert!(config.remove_url(&"http://w1:3333".parse().unwrap()));
        assert!(config.witnesses.is_empty());
        assert!(!config.remove_url(&"http://w1:3333".parse().unwrap()));
    }

    #[test]
    fn deserializes_v2_shape() {
        let json = serde_json::json!({
            "version": 2,
            "witnesses": [],
            "threshold": 0,
            "timeout_ms": 5000,
            "policy": "Enforce"
        });
        let config: WitnessConfig = serde_json::from_value(json).unwrap();
        assert!(!config.is_enabled());
    }
}