Skip to main content

bitwarden_policies/policies/
maximum_vault_timeout.rs

1use bitwarden_organizations::OrganizationUserType;
2use serde::{Deserialize, Serialize};
3#[cfg(feature = "wasm")]
4use tsify::Tsify;
5
6use crate::{Policy, PolicyType, policy_type::PolicyDataType};
7
8/// Maximum Vault Timeout policy.
9pub struct MaximumVaultTimeoutPolicy;
10
11impl Policy for MaximumVaultTimeoutPolicy {
12    type Data = MaximumVaultTimeoutPolicyData;
13
14    fn policy_type(&self) -> PolicyType {
15        PolicyType::MaximumVaultTimeout
16    }
17
18    fn to_erased(&self, data: Self::Data) -> PolicyDataType {
19        PolicyDataType::MaximumVaultTimeout(data)
20    }
21
22    fn exempt_roles(&self) -> &[OrganizationUserType] {
23        &[OrganizationUserType::Owner]
24    }
25}
26
27/// The kind of vault timeout the policy enforces.
28#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
29#[serde(rename_all = "camelCase")]
30#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
31#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
32pub enum VaultTimeoutType {
33    /// The vault never times out.
34    Never,
35    /// The vault times out when the app restarts.
36    OnAppRestart,
37    /// The vault times out when the system locks.
38    OnSystemLock,
39    /// The vault times out immediately.
40    Immediately,
41    /// The vault times out after a custom duration (see
42    /// [`MaximumVaultTimeoutPolicyData::minutes`]).
43    Custom,
44}
45
46/// The action taken when the vault times out.
47#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
48#[serde(rename_all = "camelCase")]
49#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
50#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
51pub enum VaultTimeoutAction {
52    /// Lock the vault, requiring the member to unlock it again.
53    Lock,
54    /// Log the member out entirely.
55    LogOut,
56}
57
58/// Configuration data for the Maximum Vault Timeout policy.
59#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
60#[serde(rename_all = "camelCase")]
61#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
62#[cfg_attr(
63    feature = "wasm",
64    derive(tsify::Tsify),
65    tsify(into_wasm_abi, from_wasm_abi)
66)]
67pub struct MaximumVaultTimeoutPolicyData {
68    /// The kind of vault timeout enforced. Serialized as `type` on the wire.
69    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
70    pub timeout_type: Option<VaultTimeoutType>,
71
72    /// The maximum allowed vault timeout, in minutes.
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub minutes: Option<i32>,
75
76    /// The action taken when the vault times out.
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub action: Option<VaultTimeoutAction>,
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn round_trips() {
87        let data = MaximumVaultTimeoutPolicyData {
88            timeout_type: Some(VaultTimeoutType::Custom),
89            minutes: Some(480),
90            action: Some(VaultTimeoutAction::LogOut),
91        };
92        let json = serde_json::to_string(&data).unwrap();
93        assert!(json.contains(r#""type":"custom""#));
94        assert!(json.contains(r#""action":"logOut""#));
95        assert!(json.contains(r#""minutes":480"#));
96        assert_eq!(
97            serde_json::from_str::<MaximumVaultTimeoutPolicyData>(&json).unwrap(),
98            data
99        );
100    }
101
102    #[test]
103    fn parses_string_union_values() {
104        let data: MaximumVaultTimeoutPolicyData =
105            serde_json::from_str(r#"{"type":"onAppRestart","action":"lock"}"#).unwrap();
106        assert_eq!(data.timeout_type, Some(VaultTimeoutType::OnAppRestart));
107        assert_eq!(data.action, Some(VaultTimeoutAction::Lock));
108    }
109
110    #[test]
111    fn empty_serializes_to_empty_object() {
112        assert_eq!(
113            serde_json::to_string(&MaximumVaultTimeoutPolicyData::default()).unwrap(),
114            "{}"
115        );
116    }
117}