Skip to main content

bitwarden_core/client/
flags.rs

1/// Internal Feature flag representation for the Bitwarden SDK client.
2///
3/// **Note:** The struct should be deserialized directly from the `api/config` endpoint. Take care
4/// to ensure any appropriate aliases are used. By default we use `kebab-schema` but there may be
5/// value in having shorter names.
6///
7/// **Note:** This struct while public, is intended for internal use and may change in future
8/// releases.
9#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
10#[serde(default, rename_all = "kebab-case")]
11pub struct Flags {
12    /// Enable cipher key encryption.
13    #[serde(alias = "enableCipherKeyEncryption", alias = "cipher-key-encryption")]
14    pub enable_cipher_key_encryption: bool,
15
16    /// Enable strict cipher field decryption (propagates errors instead of nulling fields).
17    #[serde(alias = "pm-34500-strict-cipher-decryption")]
18    pub strict_cipher_decryption: bool,
19
20    /// Enable FedRAMP government region behavior.
21    #[serde(alias = "fedrampGovRegion")]
22    pub fedramp_gov_region: bool,
23}
24
25impl Flags {
26    /// Create a new `Flags` instance from a map of flag names and values.
27    pub fn load_from_map(map: std::collections::HashMap<String, bool>) -> Self {
28        let map = map
29            .into_iter()
30            .map(|(k, v)| (k, serde_json::Value::Bool(v)))
31            .collect();
32        serde_json::from_value(serde_json::Value::Object(map)).expect("Valid map")
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_load_empty_map() {
42        let map = std::collections::HashMap::new();
43        let flags = Flags::load_from_map(map);
44        assert!(!flags.enable_cipher_key_encryption);
45    }
46
47    #[test]
48    fn test_load_valid_map() {
49        let mut map = std::collections::HashMap::new();
50        map.insert("enableCipherKeyEncryption".into(), true);
51        let flags = Flags::load_from_map(map);
52        assert!(flags.enable_cipher_key_encryption);
53    }
54
55    #[test]
56    fn test_load_valid_map_alias() {
57        let mut map = std::collections::HashMap::new();
58        map.insert("cipher-key-encryption".into(), true);
59        let flags = Flags::load_from_map(map);
60        assert!(flags.enable_cipher_key_encryption);
61    }
62
63    #[test]
64    fn test_load_invalid_map() {
65        let mut map = std::collections::HashMap::new();
66        map.insert("thisIsNotAFlag".into(), true);
67        let flags = Flags::load_from_map(map);
68        assert!(!flags.enable_cipher_key_encryption);
69    }
70}