Skip to main content

agentmux_srv_core/backend/vault/
kinds.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What the Vault can hold: each kind of entry, the section it is listed
5//! in, its secret parts (sealed, never returned to an agent) and its plain
6//! fields (stored as they are; the pane shows them).
7//! docs/specs/SPEC_VAULT_PANE_AGENTS_USE_SECRETS_UNSEEN_2026_10_19.md ยง5.2.
8//!
9//! The pane's forms (`frontend/app/view/vault/vault-kinds.ts`) list the same
10//! kinds; srv refuses a part or field a kind doesn't have.
11
12use serde::Serialize;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ts_rs::TS)]
15#[serde(rename_all = "kebab-case")]
16#[ts(export, export_to = "../../../frontend/types/rpc/")]
17pub enum VaultSection {
18    Personal,
19    Cards,
20    ApiKeys,
21    Logins,
22    Notes,
23}
24
25pub struct Kind {
26    pub name: &'static str,
27    pub section: VaultSection,
28    /// Sealed. The first one is the entry's main value: its tail is shown.
29    pub parts: &'static [&'static str],
30    /// Plain fields, besides the label every entry has.
31    pub fields: &'static [&'static str],
32    /// Whether the pane shows the main value's last characters.
33    pub shows_tail: bool,
34}
35
36const ID_FIELDS: &[&str] = &["owner", "issuer", "expires", "notes"];
37
38pub const KINDS: &[Kind] = &[
39    Kind { name: "ssn", section: VaultSection::Personal, parts: &["value"], fields: &["owner", "notes"], shows_tail: true },
40    Kind { name: "itin", section: VaultSection::Personal, parts: &["value"], fields: &["owner", "notes"], shows_tail: true },
41    Kind { name: "tin", section: VaultSection::Personal, parts: &["value"], fields: &["owner", "notes"], shows_tail: true },
42    Kind { name: "ein", section: VaultSection::Personal, parts: &["value"], fields: &["owner", "notes"], shows_tail: true },
43    Kind { name: "date-of-birth", section: VaultSection::Personal, parts: &["value"], fields: &["owner", "notes"], shows_tail: false },
44    Kind { name: "passport", section: VaultSection::Personal, parts: &["value"], fields: ID_FIELDS, shows_tail: true },
45    Kind { name: "drivers-licence", section: VaultSection::Personal, parts: &["value"], fields: ID_FIELDS, shows_tail: true },
46    Kind { name: "other-id", section: VaultSection::Personal, parts: &["value"], fields: ID_FIELDS, shows_tail: true },
47    // `expiry` is the older single-part expiry, still read (card.rs). `expires`, `brand` and `default` are kept by card::settle.
48    Kind {
49        name: "card",
50        section: VaultSection::Cards,
51        parts: &["number", "exp_month", "exp_year", "expiry", "cvc", "name", "zip"],
52        fields: &["owner", "brand", "default", "expires", "notes"],
53        shows_tail: true,
54    },
55    Kind { name: "bank-account", section: VaultSection::Cards, parts: &["account", "routing"], fields: &["owner", "bank", "notes"], shows_tail: true },
56    Kind {
57        name: "api-key",
58        section: VaultSection::ApiKeys,
59        parts: &["value"],
60        fields: &["service", "env_name", "expires", "notes"],
61        shows_tail: true,
62    },
63    Kind { name: "login", section: VaultSection::Logins, parts: &["password", "totp"], fields: &["site", "username", "profile", "realm", "proxy", "expires", "notes"], shows_tail: false },
64    Kind { name: "note", section: VaultSection::Notes, parts: &["text"], fields: &["expires"], shows_tail: false },
65];
66
67pub fn kind(name: &str) -> Option<&'static Kind> {
68    KINDS.iter().find(|k| k.name == name)
69}
70
71/// The main value's last characters, for the pane only: four of a long
72/// value, two of a short one, none of one too short to hide anything.
73pub fn tail(kind: &Kind, main: &str) -> String {
74    if !kind.shows_tail {
75        return String::new();
76    }
77    let chars: Vec<char> = main.chars().filter(|c| c.is_alphanumeric()).collect();
78    let n = match chars.len() {
79        0..=4 => 0,
80        5..=7 => 2,
81        _ => 4,
82    };
83    chars[chars.len() - n..].iter().collect()
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn every_kind_has_a_main_value_and_a_unique_name() {
92        let mut names = std::collections::HashSet::new();
93        for k in KINDS {
94            assert!(!k.parts.is_empty(), "{} has no secret part", k.name);
95            assert!(names.insert(k.name), "{} is listed twice", k.name);
96            for f in k.fields {
97                assert!(!k.parts.contains(f), "{}: {f} is both a part and a field", k.name);
98            }
99        }
100    }
101
102    #[test]
103    fn a_tail_shows_little_and_nothing_of_a_short_value() {
104        let ssn = kind("ssn").unwrap();
105        assert_eq!(tail(ssn, "123-45-6789"), "6789");
106        assert_eq!(tail(ssn, "12345"), "45");
107        assert_eq!(tail(ssn, "1234"), "");
108        assert_eq!(tail(kind("login").unwrap(), "hunter2hunter2"), "", "a password shows no tail");
109        assert_eq!(tail(kind("date-of-birth").unwrap(), "1990-01-01"), "");
110    }
111}