auths-policy 0.0.1-rc.10

Policy expression engine for Auths - composable authorization logic
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
//! Canonical types for policy expressions.
//!
//! Every string that crosses the policy boundary gets validated and canonicalised
//! at compile time. These types ensure that invalid data cannot reach the evaluator.

use std::fmt;

use serde::{Deserialize, Serialize};

// CanonicalDid and AssuranceLevel live in auths-verifier (Layer 1) so all shared types are co-located.
pub use auths_verifier::types::{AssuranceLevel, AssuranceLevelParseError, CanonicalDid};

/// Re-export DidParseError from auths-verifier for backwards compatibility.
pub type DidParseError = auths_verifier::DidParseError;

/// A validated capability identifier.
///
/// Enforces the same rules as `Capability::validate_custom`:
/// alphanumeric + colon/hyphen/underscore, max 64 chars. Stored in canonical
/// lowercase form.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct CanonicalCapability(String);

/// Error returned when parsing a capability fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapabilityParseError(pub String);

impl std::fmt::Display for CapabilityParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::error::Error for CapabilityParseError {}

impl CanonicalCapability {
    /// Parse and validate a capability string into canonical form.
    ///
    /// # Errors
    ///
    /// Returns an error if the capability:
    /// - Is empty or exceeds 64 characters
    /// - Contains characters other than alphanumeric, colon, hyphen, or underscore
    pub fn parse(raw: &str) -> Result<Self, CapabilityParseError> {
        let trimmed = raw.trim();
        if trimmed.is_empty() || trimmed.len() > 64 {
            return Err(CapabilityParseError(format!(
                "capability must be 1-64 chars, got {}",
                trimmed.len()
            )));
        }
        if !trimmed
            .chars()
            .all(|c| c.is_alphanumeric() || c == ':' || c == '-' || c == '_')
        {
            return Err(CapabilityParseError(format!(
                "invalid chars in capability: '{}'",
                trimmed
            )));
        }
        // Canonical: lowercase
        Ok(Self(trimmed.to_lowercase()))
    }

    /// Returns the canonical capability as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl TryFrom<String> for CanonicalCapability {
    type Error = CapabilityParseError;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        Self::parse(&s)
    }
}

impl From<CanonicalCapability> for String {
    fn from(c: CanonicalCapability) -> Self {
        c.0
    }
}

impl fmt::Display for CanonicalCapability {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// The type of entity that produced a signature.
///
/// Used to distinguish human, AI agent, and workload (CI/CD) signers
/// in policy evaluation.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SignerType {
    /// A human user.
    Human,
    /// An autonomous AI agent.
    Agent,
    /// A CI/CD workload or service identity.
    Workload,
}

/// A validated glob pattern for path/ref matching.
///
/// Restricted to:
/// - ASCII printable characters only
/// - Max 256 chars
/// - Wildcards: `*` (single segment), `**` (multi-segment)
/// - No `..` path traversal
/// - Segments separated by `/`
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct ValidatedGlob(String);

/// Error returned when parsing a glob pattern fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GlobParseError(pub String);

impl std::fmt::Display for GlobParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::error::Error for GlobParseError {}

impl ValidatedGlob {
    /// Parse and validate a glob pattern into canonical form.
    ///
    /// # Errors
    ///
    /// Returns an error if the glob:
    /// - Is empty or exceeds 256 characters
    /// - Contains non-ASCII or control characters
    /// - Contains path traversal (`..`)
    pub fn parse(raw: &str) -> Result<Self, GlobParseError> {
        let trimmed = raw.trim();
        if trimmed.is_empty() || trimmed.len() > 256 {
            return Err(GlobParseError(format!(
                "glob must be 1-256 chars, got {}",
                trimmed.len()
            )));
        }
        if !trimmed.chars().all(|c| c.is_ascii() && !c.is_control()) {
            return Err(GlobParseError(
                "glob contains non-ASCII or control chars".into(),
            ));
        }
        if trimmed.contains("..") {
            return Err(GlobParseError("glob contains path traversal (..)".into()));
        }
        // Normalise consecutive slashes
        let normalised: String = trimmed
            .split('/')
            .filter(|s| !s.is_empty())
            .collect::<Vec<_>>()
            .join("/");
        Ok(Self(normalised))
    }

    /// Returns the normalised glob pattern as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl TryFrom<String> for ValidatedGlob {
    type Error = GlobParseError;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        Self::parse(&s)
    }
}

impl From<ValidatedGlob> for String {
    fn from(g: ValidatedGlob) -> Self {
        g.0
    }
}

impl fmt::Display for ValidatedGlob {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// Evaluates quorum requirements across multiple signers.
///
/// This operates at a higher level than `Expr` (which evaluates a single
/// `EvalContext`). A `QuorumPolicy` aggregates the results of per-signer
/// policy evaluations and enforces typed signer count thresholds.
///
/// Usage:
/// ```ignore
/// let quorum = QuorumPolicy {
///     required_humans: 1,
///     required_agents: 1,
///     required_total: 2,
///     base_expression: Expr::And(vec![Expr::NotRevoked, Expr::NotExpired]),
/// };
/// let approved = quorum.evaluate(&contexts, |expr, ctx| evaluator(expr, ctx));
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct QuorumPolicy {
    /// Minimum number of human signers required.
    pub required_humans: u32,
    /// Minimum number of agent signers required.
    pub required_agents: u32,
    /// Minimum total signers required (human + agent + workload).
    pub required_total: u32,
    /// Each signer must also pass this expression.
    pub base_expression: crate::expr::Expr,
}

impl QuorumPolicy {
    /// Evaluate the quorum against a set of signer contexts.
    ///
    /// Args:
    /// * `contexts`: The evaluation contexts for each signer.
    /// * `eval_fn`: A function that evaluates the base expression against a context.
    ///   Returns `true` if the signer passes the base policy.
    pub fn evaluate<F>(&self, contexts: &[crate::context::EvalContext], eval_fn: F) -> bool
    where
        F: Fn(&crate::expr::Expr, &crate::context::EvalContext) -> bool,
    {
        let mut human_count: u32 = 0;
        let mut agent_count: u32 = 0;
        let mut total_count: u32 = 0;

        for ctx in contexts {
            if eval_fn(&self.base_expression, ctx) {
                total_count += 1;
                match ctx.signer_type {
                    Some(SignerType::Human) => human_count += 1,
                    Some(SignerType::Agent) => agent_count += 1,
                    Some(SignerType::Workload) | None => {}
                }
            }
        }

        human_count >= self.required_humans
            && agent_count >= self.required_agents
            && total_count >= self.required_total
    }
}

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

    mod canonical_did {
        use super::*;

        #[test]
        fn parses_valid_did() {
            let did = CanonicalDid::parse("did:keri:EOrg123").unwrap();
            assert_eq!(did.as_str(), "did:keri:EOrg123");
        }

        #[test]
        fn lowercases_method() {
            let did = CanonicalDid::parse("did:KERI:EOrg123").unwrap();
            assert_eq!(did.as_str(), "did:keri:EOrg123");
        }

        #[test]
        fn preserves_id_case() {
            let did = CanonicalDid::parse("did:key:zABC123XYZ").unwrap();
            assert_eq!(did.as_str(), "did:key:zABC123XYZ");
        }

        #[test]
        fn trims_whitespace() {
            let did = CanonicalDid::parse("  did:keri:EOrg123  ").unwrap();
            assert_eq!(did.as_str(), "did:keri:EOrg123");
        }

        #[test]
        fn rejects_empty() {
            assert!(CanonicalDid::parse("").is_err());
            assert!(CanonicalDid::parse("   ").is_err());
        }

        #[test]
        fn rejects_missing_parts() {
            assert!(CanonicalDid::parse("did").is_err());
            assert!(CanonicalDid::parse("did:keri").is_err());
            assert!(CanonicalDid::parse("did::id").is_err());
            assert!(CanonicalDid::parse("did:keri:").is_err());
        }

        #[test]
        fn rejects_wrong_prefix() {
            assert!(CanonicalDid::parse("uri:keri:id").is_err());
        }

        #[test]
        fn rejects_control_chars() {
            assert!(CanonicalDid::parse("did:keri:id\x00").is_err());
            assert!(CanonicalDid::parse("did:keri:id\n").is_err());
        }

        #[test]
        fn serde_roundtrip() {
            let did = CanonicalDid::parse("did:keri:EOrg123").unwrap();
            let json = serde_json::to_string(&did).unwrap();
            let parsed: CanonicalDid = serde_json::from_str(&json).unwrap();
            assert_eq!(did, parsed);
        }
    }

    mod canonical_capability {
        use super::*;

        #[test]
        fn parses_valid_capability() {
            let cap = CanonicalCapability::parse("sign_commit").unwrap();
            assert_eq!(cap.as_str(), "sign_commit");
        }

        #[test]
        fn lowercases() {
            let cap = CanonicalCapability::parse("Sign_Commit").unwrap();
            assert_eq!(cap.as_str(), "sign_commit");
        }

        #[test]
        fn allows_colons_and_hyphens() {
            let cap = CanonicalCapability::parse("repo:read-write").unwrap();
            assert_eq!(cap.as_str(), "repo:read-write");
        }

        #[test]
        fn trims_whitespace() {
            let cap = CanonicalCapability::parse("  sign_commit  ").unwrap();
            assert_eq!(cap.as_str(), "sign_commit");
        }

        #[test]
        fn rejects_empty() {
            assert!(CanonicalCapability::parse("").is_err());
        }

        #[test]
        fn rejects_too_long() {
            let long = "a".repeat(65);
            assert!(CanonicalCapability::parse(&long).is_err());
        }

        #[test]
        fn accepts_max_length() {
            let max = "a".repeat(64);
            assert!(CanonicalCapability::parse(&max).is_ok());
        }

        #[test]
        fn rejects_invalid_chars() {
            assert!(CanonicalCapability::parse("sign commit").is_err()); // space
            assert!(CanonicalCapability::parse("sign.commit").is_err()); // dot
            assert!(CanonicalCapability::parse("sign/commit").is_err()); // slash
        }

        #[test]
        fn serde_roundtrip() {
            let cap = CanonicalCapability::parse("sign_commit").unwrap();
            let json = serde_json::to_string(&cap).unwrap();
            let parsed: CanonicalCapability = serde_json::from_str(&json).unwrap();
            assert_eq!(cap, parsed);
        }
    }

    mod validated_glob {
        use super::*;

        #[test]
        fn parses_simple_path() {
            let glob = ValidatedGlob::parse("refs/heads/main").unwrap();
            assert_eq!(glob.as_str(), "refs/heads/main");
        }

        #[test]
        fn parses_wildcards() {
            let glob = ValidatedGlob::parse("refs/heads/*").unwrap();
            assert_eq!(glob.as_str(), "refs/heads/*");

            let glob = ValidatedGlob::parse("refs/**/main").unwrap();
            assert_eq!(glob.as_str(), "refs/**/main");
        }

        #[test]
        fn normalises_consecutive_slashes() {
            let glob = ValidatedGlob::parse("refs//heads///main").unwrap();
            assert_eq!(glob.as_str(), "refs/heads/main");
        }

        #[test]
        fn strips_leading_trailing_slashes() {
            let glob = ValidatedGlob::parse("/refs/heads/main/").unwrap();
            assert_eq!(glob.as_str(), "refs/heads/main");
        }

        #[test]
        fn trims_whitespace() {
            let glob = ValidatedGlob::parse("  refs/heads/main  ").unwrap();
            assert_eq!(glob.as_str(), "refs/heads/main");
        }

        #[test]
        fn rejects_empty() {
            assert!(ValidatedGlob::parse("").is_err());
        }

        #[test]
        fn rejects_too_long() {
            let long = "a/".repeat(129); // 258 chars
            assert!(ValidatedGlob::parse(&long).is_err());
        }

        #[test]
        fn rejects_path_traversal() {
            assert!(ValidatedGlob::parse("refs/../secrets").is_err());
            assert!(ValidatedGlob::parse("..").is_err());
            assert!(ValidatedGlob::parse("foo/..").is_err());
        }

        #[test]
        fn rejects_non_ascii() {
            assert!(ValidatedGlob::parse("refs/héads/main").is_err());
        }

        #[test]
        fn rejects_control_chars() {
            assert!(ValidatedGlob::parse("refs/heads/main\x00").is_err());
        }

        #[test]
        fn serde_roundtrip() {
            let glob = ValidatedGlob::parse("refs/heads/*").unwrap();
            let json = serde_json::to_string(&glob).unwrap();
            let parsed: ValidatedGlob = serde_json::from_str(&json).unwrap();
            assert_eq!(glob, parsed);
        }
    }

    mod signer_type {
        use super::*;

        #[test]
        fn serde_roundtrip() {
            for st in [SignerType::Human, SignerType::Agent, SignerType::Workload] {
                let json = serde_json::to_string(&st).unwrap();
                let parsed: SignerType = serde_json::from_str(&json).unwrap();
                assert_eq!(st, parsed);
            }
        }

        #[test]
        fn equality() {
            assert_eq!(SignerType::Human, SignerType::Human);
            assert_ne!(SignerType::Human, SignerType::Agent);
            assert_ne!(SignerType::Agent, SignerType::Workload);
        }
    }

    mod quorum_policy {
        use super::*;
        use crate::context::EvalContext;
        use crate::expr::Expr;
        use chrono::Utc;

        fn did(s: &str) -> CanonicalDid {
            CanonicalDid::parse(s).unwrap()
        }

        fn make_ctx(signer_type: SignerType) -> EvalContext {
            EvalContext::new(Utc::now(), did("did:keri:issuer"), did("did:keri:subject"))
                .signer_type(signer_type)
        }

        fn always_pass(_expr: &Expr, _ctx: &EvalContext) -> bool {
            true
        }

        fn always_fail(_expr: &Expr, _ctx: &EvalContext) -> bool {
            false
        }

        #[test]
        fn quorum_met_with_mixed_signers() {
            let quorum = QuorumPolicy {
                required_humans: 1,
                required_agents: 1,
                required_total: 2,
                base_expression: Expr::True,
            };
            let contexts = vec![make_ctx(SignerType::Human), make_ctx(SignerType::Agent)];
            assert!(quorum.evaluate(&contexts, always_pass));
        }

        #[test]
        fn quorum_not_met_missing_human() {
            let quorum = QuorumPolicy {
                required_humans: 1,
                required_agents: 1,
                required_total: 2,
                base_expression: Expr::True,
            };
            let contexts = vec![make_ctx(SignerType::Agent), make_ctx(SignerType::Agent)];
            assert!(!quorum.evaluate(&contexts, always_pass));
        }

        #[test]
        fn quorum_not_met_base_expression_fails() {
            let quorum = QuorumPolicy {
                required_humans: 1,
                required_agents: 0,
                required_total: 1,
                base_expression: Expr::True,
            };
            let contexts = vec![make_ctx(SignerType::Human)];
            assert!(!quorum.evaluate(&contexts, always_fail));
        }

        #[test]
        fn quorum_empty_contexts() {
            let quorum = QuorumPolicy {
                required_humans: 0,
                required_agents: 0,
                required_total: 0,
                base_expression: Expr::True,
            };
            assert!(quorum.evaluate(&[], always_pass));
        }

        #[test]
        fn quorum_total_threshold() {
            let quorum = QuorumPolicy {
                required_humans: 0,
                required_agents: 0,
                required_total: 3,
                base_expression: Expr::True,
            };
            let contexts = vec![make_ctx(SignerType::Human), make_ctx(SignerType::Agent)];
            assert!(!quorum.evaluate(&contexts, always_pass));

            let contexts = vec![
                make_ctx(SignerType::Human),
                make_ctx(SignerType::Agent),
                make_ctx(SignerType::Workload),
            ];
            assert!(quorum.evaluate(&contexts, always_pass));
        }

        #[test]
        fn serde_roundtrip() {
            let quorum = QuorumPolicy {
                required_humans: 1,
                required_agents: 1,
                required_total: 2,
                base_expression: Expr::And(vec![Expr::NotRevoked, Expr::NotExpired]),
            };
            let json = serde_json::to_string(&quorum).unwrap();
            let parsed: QuorumPolicy = serde_json::from_str(&json).unwrap();
            assert_eq!(quorum, parsed);
        }
    }
}