agentmux_srv_core/backend/vault/
kinds.rs1use 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 pub parts: &'static [&'static str],
30 pub fields: &'static [&'static str],
32 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 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
71pub 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}