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 strict cipher field decryption (propagates errors instead of nulling fields).
13    #[serde(alias = "pm-34500-strict-cipher-decryption")]
14    pub strict_cipher_decryption: bool,
15
16    /// Enable FedRAMP government region behavior.
17    #[serde(alias = "fedrampGovRegion")]
18    pub fedramp_gov_region: bool,
19
20    /// Finish SSO JIT provisioning through the `sso-jit-provision-finish` endpoints. These
21    /// endpoints let the client choose whether the organization membership stays Invited.
22    #[serde(alias = "pm-34429-invite-link-auto-confirm")]
23    pub invite_link_auto_confirm: bool,
24}
25
26impl Flags {
27    /// Create a new `Flags` instance from a map of flag names and values.
28    pub fn load_from_map(map: std::collections::HashMap<String, bool>) -> Self {
29        let map = map
30            .into_iter()
31            .map(|(k, v)| (k, serde_json::Value::Bool(v)))
32            .collect();
33        serde_json::from_value(serde_json::Value::Object(map)).expect("Valid map")
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_load_empty_map() {
43        let map = std::collections::HashMap::new();
44        let flags = Flags::load_from_map(map);
45        assert!(!flags.strict_cipher_decryption);
46    }
47
48    #[test]
49    fn test_load_valid_map() {
50        let mut map = std::collections::HashMap::new();
51        map.insert("strict-cipher-decryption".into(), true);
52        let flags = Flags::load_from_map(map);
53        assert!(flags.strict_cipher_decryption);
54    }
55
56    #[test]
57    fn test_load_valid_map_alias() {
58        let mut map = std::collections::HashMap::new();
59        map.insert("pm-34500-strict-cipher-decryption".into(), true);
60        let flags = Flags::load_from_map(map);
61        assert!(flags.strict_cipher_decryption);
62    }
63
64    #[test]
65    fn test_load_invite_link_auto_confirm_alias() {
66        let mut map = std::collections::HashMap::new();
67        map.insert("pm-34429-invite-link-auto-confirm".into(), true);
68        let flags = Flags::load_from_map(map);
69        assert!(flags.invite_link_auto_confirm);
70    }
71
72    #[test]
73    fn test_load_invalid_map() {
74        let mut map = std::collections::HashMap::new();
75        map.insert("thisIsNotAFlag".into(), true);
76        let flags = Flags::load_from_map(map);
77        assert!(!flags.strict_cipher_decryption);
78    }
79}