Skip to main content

bitwarden_policies/policies/
maximum_vault_timeout.rs

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