themis-auth 0.3.0

Extensible authorization engine: permission catalog, RBAC, ABAC and ReBAC in one decision, with governance and auditing of changes.
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
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::attr::{Attributes, Value};
use crate::graph;
use crate::resource::GLOBAL;

/// Definition of an atomic permission.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PermissionDef {
    pub key: String,
    /// Target kinds a grant of this permission can anchor to, besides the broad ones.
    pub targets: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// More general permissions that grant this one. A grant on any of them holds here.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub implied_by: Vec<String>,
    /// Application metadata (UI group, display level, sensitivity). The engine does not read it;
    /// catalog rules ([`CatalogRule`]) can require it.
    #[serde(default, skip_serializing_if = "Attributes::is_empty")]
    pub meta: Attributes,
}

impl PermissionDef {
    pub fn new(key: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            targets: Vec::new(),
            label: None,
            description: None,
            implied_by: Vec::new(),
            meta: Attributes::new(),
        }
    }

    pub fn targets<I, S>(mut self, targets: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.targets.extend(targets.into_iter().map(Into::into));
        self
    }

    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    pub fn implied_by<I, S>(mut self, parents: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.implied_by.extend(parents.into_iter().map(Into::into));
        self
    }

    pub fn meta(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.meta.insert(key.into(), value.into());
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum CatalogError {
    #[error("chave invĂ¡lida '{0}': use segmentos [a-z0-9_] separados por ponto")]
    InvalidKey(String),
    #[error("chave duplicada '{0}'")]
    DuplicateKey(String),
    #[error("'{key}' Ă© implicada por chave inexistente '{parent}'")]
    UnknownParent { key: String, parent: String },
    #[error("'{key}' mira tipo de alvo nĂ£o registrado '{target}'")]
    UnknownTarget { key: String, target: String },
    #[error("'{0}' nĂ£o declara nenhum alvo")]
    NoTargets(String),
    #[error("ciclo de implicaĂ§Ă£o: {}", .0.join(" -> "))]
    Cycle(Vec<String>),
    #[error("'{key}' viola a regra {rule}: {message}")]
    Rule { rule: String, key: String, message: String },
}

/// All the errors of a catalog, at once.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CatalogErrors(pub Vec<CatalogError>);

impl fmt::Display for CatalogErrors {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "catĂ¡logo invĂ¡lido ({} erro(s)):", self.0.len())?;
        for error in &self.0 {
            writeln!(f, "  - {error}")?;
        }
        Ok(())
    }
}

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

/// Application-specific validation over each permission, run in
/// [`CatalogBuilder::build`] alongside the structural validations.
pub trait CatalogRule {
    fn name(&self) -> &str;
    /// Returns the error message if `def` violates the rule.
    fn check(&self, def: &PermissionDef) -> Option<String>;
}

/// Requires the metadata key `meta` on permissions whose key ends with `suffix` (all of them, with
/// no suffix), optionally restricted to a list of values.
#[derive(Debug, Clone)]
pub struct RequireMeta {
    name: String,
    meta: String,
    suffix: Option<String>,
    allowed: Vec<Value>,
}

impl RequireMeta {
    pub fn new(meta: impl Into<String>) -> Self {
        let meta = meta.into();
        Self {
            name: format!("require_meta:{meta}"),
            meta,
            suffix: None,
            allowed: Vec::new(),
        }
    }

    pub fn for_suffix(mut self, suffix: impl Into<String>) -> Self {
        self.suffix = Some(suffix.into());
        self
    }

    pub fn one_of<I, V>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = V>,
        V: Into<Value>,
    {
        self.allowed = values.into_iter().map(Into::into).collect();
        self
    }
}

impl CatalogRule for RequireMeta {
    fn name(&self) -> &str {
        &self.name
    }

    fn check(&self, def: &PermissionDef) -> Option<String> {
        if self.suffix.as_ref().is_some_and(|s| !def.key.ends_with(s.as_str())) {
            return None;
        }
        match def.meta.get(&self.meta) {
            None => Some(format!("falta o metadado '{}'", self.meta)),
            Some(value) if !self.allowed.is_empty() && !self.allowed.contains(value) => {
                Some(format!("'{}' fora dos valores aceitos: {value:?}", self.meta))
            }
            Some(_) => None,
        }
    }
}

/// Rule built from a closure.
pub struct FnRule<F> {
    name: String,
    f: F,
}

impl<F: Fn(&PermissionDef) -> Option<String>> FnRule<F> {
    pub fn new(name: impl Into<String>, f: F) -> Self {
        Self { name: name.into(), f }
    }
}

impl<F: Fn(&PermissionDef) -> Option<String>> CatalogRule for FnRule<F> {
    fn name(&self) -> &str {
        &self.name
    }

    fn check(&self, def: &PermissionDef) -> Option<String> {
        (self.f)(def)
    }
}

/// Serializable form of the catalog, to generate frontend types or load from configuration.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CatalogExport {
    pub target_kinds: Vec<String>,
    #[serde(default)]
    pub broad_targets: Vec<String>,
    pub permissions: Vec<PermissionDef>,
}

#[derive(Default)]
pub struct CatalogBuilder {
    target_kinds: Vec<String>,
    broad_targets: Vec<String>,
    rules: Vec<Box<dyn CatalogRule>>,
    defs: Vec<PermissionDef>,
}

impl fmt::Debug for CatalogBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CatalogBuilder")
            .field("target_kinds", &self.target_kinds)
            .field("broad_targets", &self.broad_targets)
            .field("rules", &self.rules.iter().map(|r| r.name()).collect::<Vec<_>>())
            .field("defs", &self.defs.len())
            .finish()
    }
}

impl CatalogBuilder {
    pub fn target_kind(mut self, kind: impl Into<String>) -> Self {
        self.target_kinds.push(kind.into());
        self
    }

    pub fn target_kinds<I, S>(mut self, kinds: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.target_kinds.extend(kinds.into_iter().map(Into::into));
        self
    }

    /// Target kind accepted by any permission, such as the organization or the tenant. `global`
    /// is already broad.
    pub fn broad_target(mut self, kind: impl Into<String>) -> Self {
        self.broad_targets.push(kind.into());
        self
    }

    pub fn rule(mut self, rule: impl CatalogRule + 'static) -> Self {
        self.rules.push(Box::new(rule));
        self
    }

    pub fn permission(mut self, def: PermissionDef) -> Self {
        self.defs.push(def);
        self
    }

    /// Adds the permissions of a module at once.
    pub fn permissions(mut self, defs: impl IntoIterator<Item = PermissionDef>) -> Self {
        self.defs.extend(defs);
        self
    }

    pub fn build(self) -> Result<Catalog, CatalogErrors> {
        let mut broad_targets: BTreeSet<String> = self.broad_targets.into_iter().collect();
        broad_targets.insert(GLOBAL.to_string());
        let mut target_kinds: BTreeSet<String> = self.target_kinds.into_iter().collect();
        target_kinds.extend(broad_targets.iter().cloned());

        let mut errors = Vec::new();
        let mut index = HashMap::new();
        for (i, def) in self.defs.iter().enumerate() {
            if !valid_key(&def.key) {
                errors.push(CatalogError::InvalidKey(def.key.clone()));
            }
            if index.insert(def.key.clone(), i).is_some() {
                errors.push(CatalogError::DuplicateKey(def.key.clone()));
            }
            if def.targets.is_empty() {
                errors.push(CatalogError::NoTargets(def.key.clone()));
            }
            for target in def.targets.iter().filter(|t| !target_kinds.contains(*t)) {
                errors.push(CatalogError::UnknownTarget {
                    key: def.key.clone(),
                    target: target.clone(),
                });
            }
            for rule in &self.rules {
                if let Some(message) = rule.check(def) {
                    errors.push(CatalogError::Rule {
                        rule: rule.name().to_string(),
                        key: def.key.clone(),
                        message,
                    });
                }
            }
        }
        for def in &self.defs {
            for parent in def.implied_by.iter().filter(|p| !index.contains_key(*p)) {
                errors.push(CatalogError::UnknownParent {
                    key: def.key.clone(),
                    parent: parent.clone(),
                });
            }
        }
        if !errors.is_empty() {
            return Err(CatalogErrors(errors));
        }

        let parents: BTreeMap<String, Vec<String>> = self
            .defs
            .iter()
            .map(|d| (d.key.clone(), d.implied_by.clone()))
            .collect();
        if let Some(cycle) = graph::find_cycle(&parents) {
            return Err(CatalogErrors(vec![CatalogError::Cycle(cycle)]));
        }
        let effective = parents
            .keys()
            .map(|k| (k.clone(), graph::reachable(k, &parents)))
            .collect();

        let mut children: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for def in &self.defs {
            for parent in &def.implied_by {
                children.entry(parent.clone()).or_default().push(def.key.clone());
            }
        }

        Ok(Catalog {
            defs: self.defs,
            index,
            effective,
            children,
            target_kinds,
            broad_targets,
        })
    }
}

fn valid_key(key: &str) -> bool {
    !key.is_empty()
        && key.split('.').all(|segment| {
            !segment.is_empty()
                && segment
                    .chars()
                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
        })
}

/// Validated, immutable catalog of permissions.
#[derive(Debug, Clone)]
pub struct Catalog {
    defs: Vec<PermissionDef>,
    index: HashMap<String, usize>,
    effective: HashMap<String, BTreeSet<String>>,
    children: BTreeMap<String, Vec<String>>,
    target_kinds: BTreeSet<String>,
    broad_targets: BTreeSet<String>,
}

impl Catalog {
    pub fn builder() -> CatalogBuilder {
        CatalogBuilder::default()
    }

    /// Builder preloaded with an export, to add rules before validating.
    pub fn builder_from(export: CatalogExport) -> CatalogBuilder {
        let mut builder = Self::builder().target_kinds(export.target_kinds);
        for kind in export.broad_targets {
            builder = builder.broad_target(kind);
        }
        builder.permissions(export.permissions)
    }

    pub fn from_export(export: CatalogExport) -> Result<Self, CatalogErrors> {
        Self::builder_from(export).build()
    }

    pub fn export(&self) -> CatalogExport {
        CatalogExport {
            target_kinds: self.target_kinds.iter().cloned().collect(),
            broad_targets: self.broad_targets.iter().cloned().collect(),
            permissions: self.defs.clone(),
        }
    }

    pub fn get(&self, key: &str) -> Option<&PermissionDef> {
        self.index.get(key).map(|i| &self.defs[*i])
    }

    pub fn contains(&self, key: &str) -> bool {
        self.index.contains_key(key)
    }

    pub fn all(&self) -> &[PermissionDef] {
        &self.defs
    }

    pub fn len(&self) -> usize {
        self.defs.len()
    }

    pub fn is_empty(&self) -> bool {
        self.defs.is_empty()
    }

    /// The key and every permission that grants it, transitively.
    pub fn effective(&self, key: &str) -> Option<&BTreeSet<String>> {
        self.effective.get(key)
    }

    /// `general` grants `specific` (or is the same key).
    pub fn implies(&self, general: &str, specific: &str) -> bool {
        self.effective(specific).is_some_and(|set| set.contains(general))
    }

    /// Permissions granted directly by `key`.
    pub fn children(&self, key: &str) -> &[String] {
        self.children.get(key).map(Vec::as_slice).unwrap_or(&[])
    }

    /// All permissions granted by `key`, transitively, not including itself.
    pub fn descendants(&self, key: &str) -> BTreeSet<String> {
        let mut set = graph::reachable(key, &self.children);
        set.remove(key);
        set
    }

    pub fn is_target_kind(&self, kind: &str) -> bool {
        self.target_kinds.contains(kind)
    }

    pub fn is_broad_target(&self, kind: &str) -> bool {
        self.broad_targets.contains(kind)
    }

    /// A grant of `key` can anchor to `kind`.
    pub fn allows_target(&self, key: &str, kind: &str) -> bool {
        self.is_broad_target(kind) || self.get(key).is_some_and(|d| d.targets.iter().any(|t| t == kind))
    }
}

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

    fn base() -> CatalogBuilder {
        Catalog::builder()
            .target_kinds(["project", "document"])
            .broad_target("org")
    }

    #[test]
    fn effective_contains_key_and_ancestors() {
        let cat = base()
            .permission(PermissionDef::new("project.view").targets(["project"]))
            .permission(
                PermissionDef::new("document.view")
                    .targets(["project"])
                    .implied_by(["project.view"]),
            )
            .permission(
                PermissionDef::new("document.report.view")
                    .targets(["document"])
                    .implied_by(["document.view"]),
            )
            .build()
            .unwrap();
        let eff = cat.effective("document.report.view").unwrap();
        assert_eq!(eff.len(), 3);
        assert!(cat.implies("project.view", "document.report.view"));
        assert!(!cat.implies("document.report.view", "project.view"));
        assert_eq!(cat.descendants("project.view").len(), 2);
        assert_eq!(cat.children("project.view"), ["document.view"]);
    }

    #[test]
    fn collects_every_error() {
        let errors = base()
            .rule(
                RequireMeta::new("visibility")
                    .for_suffix(".view")
                    .one_of(["hide", "redact"]),
            )
            .permission(PermissionDef::new("Bad Key").targets(["project"]))
            .permission(
                PermissionDef::new("a.edit")
                    .targets(["nowhere"])
                    .implied_by(["missing"]),
            )
            .permission(PermissionDef::new("a.edit").targets(["project"]))
            .permission(PermissionDef::new("a.view").targets(["project"]))
            .permission(
                PermissionDef::new("b.view")
                    .targets(["project"])
                    .meta("visibility", "blur"),
            )
            .permission(PermissionDef::new("a.run"))
            .build()
            .unwrap_err();
        let has = |pred: fn(&CatalogError) -> bool| errors.0.iter().any(pred);
        assert!(has(|e| matches!(e, CatalogError::InvalidKey(_))));
        assert!(has(|e| matches!(e, CatalogError::DuplicateKey(_))));
        assert!(has(|e| matches!(e, CatalogError::UnknownTarget { .. })));
        assert!(has(|e| matches!(e, CatalogError::UnknownParent { .. })));
        assert!(has(|e| matches!(e, CatalogError::NoTargets(_))));
        let rule_errors: Vec<_> = errors
            .0
            .iter()
            .filter_map(|e| match e {
                CatalogError::Rule { key, .. } => Some(key.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(rule_errors, ["a.view", "b.view"]);
    }

    #[test]
    fn closure_rule() {
        let errors = base()
            .rule(FnRule::new("has_label", |def: &PermissionDef| {
                def.label.is_none().then(|| "sem rĂ³tulo".to_string())
            }))
            .permission(PermissionDef::new("a.edit").targets(["project"]))
            .build()
            .unwrap_err();
        assert!(matches!(&errors.0[..], [CatalogError::Rule { rule, .. }] if rule == "has_label"));
    }

    #[test]
    fn rejects_implication_cycle() {
        let errors = base()
            .permission(PermissionDef::new("a.x").targets(["project"]).implied_by(["a.y"]))
            .permission(PermissionDef::new("a.y").targets(["project"]).implied_by(["a.x"]))
            .build()
            .unwrap_err();
        assert!(matches!(errors.0.as_slice(), [CatalogError::Cycle(_)]));
    }

    #[test]
    fn broad_targets_accept_any_permission() {
        let cat = base()
            .permission(PermissionDef::new("a.edit").targets(["project"]))
            .build()
            .unwrap();
        assert!(cat.allows_target("a.edit", "org"));
        assert!(cat.allows_target("a.edit", GLOBAL));
        assert!(!cat.allows_target("a.edit", "document"));
    }

    #[test]
    fn export_round_trip() {
        let cat = base()
            .permission(
                PermissionDef::new("a.edit")
                    .targets(["project"])
                    .label("Editar")
                    .meta("group", "a"),
            )
            .build()
            .unwrap();
        let json = serde_json::to_string(&cat.export()).unwrap();
        let back = Catalog::from_export(serde_json::from_str(&json).unwrap()).unwrap();
        assert_eq!(back.export(), cat.export());
    }
}