Skip to main content

bitwarden_policies/
policy_type.rs

1//! The [`PolicyType`] enum.
2
3use serde::{Deserialize, Serialize};
4use serde_repr::{Deserialize_repr, Serialize_repr};
5
6use crate::{PolicyParseError, policies::*, policy_definition::ErasedPolicy};
7
8/// The type of an organization policy.
9///
10/// The integer value matches the server's wire format.
11#[derive(PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr, Debug, Copy, Clone)]
12#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
13#[bitwarden_ffi::wasm_object]
14#[repr(u8)]
15pub enum PolicyType {
16    /// Requires members to have two-step login enabled on their account.
17    TwoFactorAuthentication = 0,
18    /// Sets minimum requirements for members' master passwords.
19    MasterPassword = 1,
20    /// Sets minimum requirements for the password generator.
21    PasswordGenerator = 2,
22    /// Restricts members to being part of a single organization.
23    SingleOrg = 3,
24    /// Requires members to authenticate with single sign-on.
25    RequireSso = 4,
26    /// Forces newly added or cloned items to be owned by the organization rather than the
27    /// member's personal vault. Also enables My Items functionality.
28    OrganizationDataOwnership = 5,
29    /// Disables the ability to create and edit Bitwarden Sends.
30    ///
31    /// Superseded by [`SendControls`](Self::SendControls) when the
32    /// `pm-31885-send-controls` feature flag is active.
33    DisableSend = 6,
34    /// Sets restrictions or defaults for Bitwarden Sends.
35    ///
36    /// Superseded by [`SendControls`](Self::SendControls) when the
37    /// `pm-31885-send-controls` feature flag is active.
38    SendOptions = 7,
39    /// Allows administrators to recover member accounts.
40    ResetPassword = 8,
41    /// Sets the maximum allowed vault timeout for members.
42    MaximumVaultTimeout = 9,
43    /// Disables members' ability to export their personal vault.
44    DisablePersonalVaultExport = 10,
45    /// Activates autofill on page load in the browser extension.
46    ActivateAutofill = 11,
47    /// Automatically logs members into apps using single sign-on.
48    AutomaticAppLogIn = 12,
49    /// Removes members' access to the free Bitwarden Families sponsorship benefit.
50    FreeFamiliesSponsorship = 13,
51    /// Prevents members from unlocking the app with a PIN.
52    RemoveUnlockWithPin = 14,
53    /// Restricts the item types that members can create.
54    RestrictedItemTypes = 15,
55    /// Sets the default URI match detection strategy for autofill.
56    UriMatchDefaults = 16,
57    /// Sets the default behavior for the autotype feature.
58    AutotypeDefaultSetting = 17,
59    /// Automatically confirms invited users into the organization.
60    AutomaticUserConfirmation = 18,
61    /// Blocks account creation for users with email addresses on claimed domains.
62    BlockClaimedDomainAccountCreation = 19,
63    /// Displays an organization-configured banner message to members in their vault.
64    OrganizationUserNotification = 20,
65    /// Configures Send-related behavior: disabling Sends, email visibility, access controls,
66    /// Send types, and deletion.
67    ///
68    /// Supersedes [`DisableSend`](Self::DisableSend) and [`SendOptions`](Self::SendOptions) when
69    /// the `pm-31885-send-controls` feature flag is active on the server.
70    SendControls = 21,
71    /// Enables the Fill Assist targeting-rules autofill engine as the default for members who
72    /// have not explicitly set their Fill Assist preference, and optionally overrides the default
73    /// rules feed URL.
74    FillAssist = 22,
75}
76
77impl TryFrom<bitwarden_api_api::models::PolicyType> for PolicyType {
78    type Error = PolicyParseError;
79
80    fn try_from(value: bitwarden_api_api::models::PolicyType) -> Result<Self, Self::Error> {
81        use bitwarden_api_api::models::PolicyType as Api;
82        Ok(match value {
83            Api::TwoFactorAuthentication => PolicyType::TwoFactorAuthentication,
84            Api::MasterPassword => PolicyType::MasterPassword,
85            Api::PasswordGenerator => PolicyType::PasswordGenerator,
86            Api::SingleOrg => PolicyType::SingleOrg,
87            Api::RequireSso => PolicyType::RequireSso,
88            Api::OrganizationDataOwnership => PolicyType::OrganizationDataOwnership,
89            Api::DisableSend => PolicyType::DisableSend,
90            Api::SendOptions => PolicyType::SendOptions,
91            Api::ResetPassword => PolicyType::ResetPassword,
92            Api::MaximumVaultTimeout => PolicyType::MaximumVaultTimeout,
93            Api::DisablePersonalVaultExport => PolicyType::DisablePersonalVaultExport,
94            Api::ActivateAutofill => PolicyType::ActivateAutofill,
95            Api::AutomaticAppLogIn => PolicyType::AutomaticAppLogIn,
96            Api::FreeFamiliesSponsorshipPolicy => PolicyType::FreeFamiliesSponsorship,
97            Api::RemoveUnlockWithPin => PolicyType::RemoveUnlockWithPin,
98            Api::RestrictedItemTypesPolicy => PolicyType::RestrictedItemTypes,
99            Api::UriMatchDefaults => PolicyType::UriMatchDefaults,
100            Api::AutotypeDefaultSetting => PolicyType::AutotypeDefaultSetting,
101            Api::AutomaticUserConfirmation => PolicyType::AutomaticUserConfirmation,
102            Api::BlockClaimedDomainAccountCreation => PolicyType::BlockClaimedDomainAccountCreation,
103            Api::OrganizationUserNotification => PolicyType::OrganizationUserNotification,
104            Api::SendControls => PolicyType::SendControls,
105            Api::FillAssist => PolicyType::FillAssist,
106            Api::__Unknown(value) => return Err(PolicyParseError::UnknownPolicyType(value)),
107        })
108    }
109}
110
111impl PolicyType {
112    /// Dispatches this runtime policy type to its concrete (zero-sized)
113    /// [`crate::Policy`] implementation, erased behind [`ErasedPolicy`] so the
114    /// differing associated `Data` types can be handled uniformly.
115    pub(crate) fn resolve_policy(self) -> Box<dyn ErasedPolicy> {
116        match self {
117            PolicyType::TwoFactorAuthentication => Box::new(TwoFactorAuthenticationPolicy),
118            PolicyType::MasterPassword => Box::new(MasterPasswordPolicy),
119            PolicyType::PasswordGenerator => Box::new(PasswordGeneratorPolicy),
120            PolicyType::SingleOrg => Box::new(SingleOrgPolicy),
121            PolicyType::RequireSso => Box::new(RequireSsoPolicy),
122            PolicyType::OrganizationDataOwnership => Box::new(OrganizationDataOwnershipPolicy),
123            PolicyType::DisableSend => Box::new(DisableSendPolicy),
124            PolicyType::SendOptions => Box::new(SendOptionsPolicy),
125            PolicyType::ResetPassword => Box::new(ResetPasswordPolicy),
126            PolicyType::MaximumVaultTimeout => Box::new(MaximumVaultTimeoutPolicy),
127            PolicyType::DisablePersonalVaultExport => Box::new(DisablePersonalVaultExportPolicy),
128            PolicyType::ActivateAutofill => Box::new(ActivateAutofillPolicy),
129            PolicyType::AutomaticAppLogIn => Box::new(AutomaticAppLogInPolicy),
130            PolicyType::FreeFamiliesSponsorship => Box::new(FreeFamiliesSponsorshipPolicy),
131            PolicyType::RemoveUnlockWithPin => Box::new(RemoveUnlockWithPinPolicy),
132            PolicyType::RestrictedItemTypes => Box::new(RestrictedItemTypesPolicy),
133            PolicyType::UriMatchDefaults => Box::new(UriMatchDefaultsPolicy),
134            PolicyType::AutotypeDefaultSetting => Box::new(AutotypeDefaultSettingPolicy),
135            PolicyType::AutomaticUserConfirmation => Box::new(AutomaticUserConfirmationPolicy),
136            PolicyType::BlockClaimedDomainAccountCreation => {
137                Box::new(BlockClaimedDomainAccountCreationPolicy)
138            }
139            PolicyType::OrganizationUserNotification => {
140                Box::new(OrganizationUserNotificationPolicy)
141            }
142            PolicyType::SendControls => Box::new(SendControlsPolicy),
143            PolicyType::FillAssist => Box::new(FillAssistPolicy),
144        }
145    }
146}
147
148/// Type-erased policy type + data for crossing the FFI boundary.
149///
150/// Each variant carries the strongly-typed data for one policy, mirroring the
151/// generic `Policy::Data` used on the native side. Variants are
152/// named identically to (and documented by) the matching [`PolicyType`] variant.
153///
154/// The discriminator is serialized under `_policyType` rather than `type` so it
155/// cannot collide with a policy data field named `type` (e.g.
156/// [`MaximumVaultTimeoutPolicyData`]), which the internally-tagged
157/// representation flattens alongside the discriminator.
158// Variants mirror the already-documented `PolicyType`, so per-variant docs would
159// be redundant boilerplate.
160#[allow(missing_docs)]
161#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
162#[serde(rename_all = "camelCase", tag = "_policyType")]
163#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
164#[bitwarden_ffi::wasm_record]
165// TODO: `SendControls` and `UriMatchDefaults` are temporarily unit variants. They gain
166// their data models in follow-up PRs, once `SendType`/`UriMatchType` move into
167// lower-level crates that this crate can depend on without a cycle.
168pub enum PolicyDataType {
169    TwoFactorAuthentication,
170    MasterPassword(MasterPasswordPolicyData),
171    PasswordGenerator(PasswordGeneratorPolicyData),
172    SingleOrg,
173    RequireSso,
174    OrganizationDataOwnership(OrganizationDataOwnershipPolicyData),
175    DisableSend,
176    SendOptions(SendOptionsPolicyData),
177    ResetPassword(ResetPasswordPolicyData),
178    MaximumVaultTimeout(MaximumVaultTimeoutPolicyData),
179    DisablePersonalVaultExport,
180    ActivateAutofill,
181    AutomaticAppLogIn(AutomaticAppLogInPolicyData),
182    FreeFamiliesSponsorship,
183    RemoveUnlockWithPin,
184    RestrictedItemTypes,
185    UriMatchDefaults,
186    AutotypeDefaultSetting,
187    AutomaticUserConfirmation,
188    BlockClaimedDomainAccountCreation,
189    OrganizationUserNotification(OrganizationUserNotificationPolicyData),
190    SendControls,
191    FillAssist(FillAssistPolicyData),
192}