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    /// Enables AutoType GA policy for members who have not explicitly set their AutoType GA
76    /// preference.
77    AutotypeGaPolicy = 23,
78}
79
80impl TryFrom<bitwarden_api_api::models::PolicyType> for PolicyType {
81    type Error = PolicyParseError;
82
83    fn try_from(value: bitwarden_api_api::models::PolicyType) -> Result<Self, Self::Error> {
84        use bitwarden_api_api::models::PolicyType as Api;
85        Ok(match value {
86            Api::TwoFactorAuthentication => PolicyType::TwoFactorAuthentication,
87            Api::MasterPassword => PolicyType::MasterPassword,
88            Api::PasswordGenerator => PolicyType::PasswordGenerator,
89            Api::SingleOrg => PolicyType::SingleOrg,
90            Api::RequireSso => PolicyType::RequireSso,
91            Api::OrganizationDataOwnership => PolicyType::OrganizationDataOwnership,
92            Api::DisableSend => PolicyType::DisableSend,
93            Api::SendOptions => PolicyType::SendOptions,
94            Api::ResetPassword => PolicyType::ResetPassword,
95            Api::MaximumVaultTimeout => PolicyType::MaximumVaultTimeout,
96            Api::DisablePersonalVaultExport => PolicyType::DisablePersonalVaultExport,
97            Api::ActivateAutofill => PolicyType::ActivateAutofill,
98            Api::AutomaticAppLogIn => PolicyType::AutomaticAppLogIn,
99            Api::FreeFamiliesSponsorshipPolicy => PolicyType::FreeFamiliesSponsorship,
100            Api::RemoveUnlockWithPin => PolicyType::RemoveUnlockWithPin,
101            Api::RestrictedItemTypesPolicy => PolicyType::RestrictedItemTypes,
102            Api::UriMatchDefaults => PolicyType::UriMatchDefaults,
103            Api::AutotypeDefaultSetting => PolicyType::AutotypeDefaultSetting,
104            Api::AutomaticUserConfirmation => PolicyType::AutomaticUserConfirmation,
105            Api::BlockClaimedDomainAccountCreation => PolicyType::BlockClaimedDomainAccountCreation,
106            Api::OrganizationUserNotification => PolicyType::OrganizationUserNotification,
107            Api::SendControls => PolicyType::SendControls,
108            Api::FillAssist => PolicyType::FillAssist,
109            Api::AutotypeGaPolicy => PolicyType::AutotypeGaPolicy,
110            // Catch-all so that the build doesn't break every time a new policy is added on the
111            // server. This happens fairly regularly so it's worth trading off exhaustiveness.
112            unknown => return Err(PolicyParseError::UnknownPolicyType(unknown.as_i64())),
113        })
114    }
115}
116
117impl PolicyType {
118    /// Dispatches this runtime policy type to its concrete (zero-sized)
119    /// [`crate::Policy`] implementation, erased behind [`ErasedPolicy`] so the
120    /// differing associated `Data` types can be handled uniformly.
121    pub(crate) fn resolve_policy(self) -> Box<dyn ErasedPolicy> {
122        match self {
123            PolicyType::TwoFactorAuthentication => Box::new(TwoFactorAuthenticationPolicy),
124            PolicyType::MasterPassword => Box::new(MasterPasswordPolicy),
125            PolicyType::PasswordGenerator => Box::new(PasswordGeneratorPolicy),
126            PolicyType::SingleOrg => Box::new(SingleOrgPolicy),
127            PolicyType::RequireSso => Box::new(RequireSsoPolicy),
128            PolicyType::OrganizationDataOwnership => Box::new(OrganizationDataOwnershipPolicy),
129            PolicyType::DisableSend => Box::new(DisableSendPolicy),
130            PolicyType::SendOptions => Box::new(SendOptionsPolicy),
131            PolicyType::ResetPassword => Box::new(ResetPasswordPolicy),
132            PolicyType::MaximumVaultTimeout => Box::new(MaximumVaultTimeoutPolicy),
133            PolicyType::DisablePersonalVaultExport => Box::new(DisablePersonalVaultExportPolicy),
134            PolicyType::ActivateAutofill => Box::new(ActivateAutofillPolicy),
135            PolicyType::AutomaticAppLogIn => Box::new(AutomaticAppLogInPolicy),
136            PolicyType::FreeFamiliesSponsorship => Box::new(FreeFamiliesSponsorshipPolicy),
137            PolicyType::RemoveUnlockWithPin => Box::new(RemoveUnlockWithPinPolicy),
138            PolicyType::RestrictedItemTypes => Box::new(RestrictedItemTypesPolicy),
139            PolicyType::UriMatchDefaults => Box::new(UriMatchDefaultsPolicy),
140            PolicyType::AutotypeDefaultSetting => Box::new(AutotypeDefaultSettingPolicy),
141            PolicyType::AutomaticUserConfirmation => Box::new(AutomaticUserConfirmationPolicy),
142            PolicyType::BlockClaimedDomainAccountCreation => {
143                Box::new(BlockClaimedDomainAccountCreationPolicy)
144            }
145            PolicyType::OrganizationUserNotification => {
146                Box::new(OrganizationUserNotificationPolicy)
147            }
148            PolicyType::SendControls => Box::new(SendControlsPolicy),
149            PolicyType::FillAssist => Box::new(FillAssistPolicy),
150            PolicyType::AutotypeGaPolicy => Box::new(AutotypeGaPolicy),
151        }
152    }
153}
154
155/// Type-erased policy type + data for crossing the FFI boundary.
156///
157/// Each variant carries the strongly-typed data for one policy, mirroring the
158/// generic `Policy::Data` used on the native side. Variants are
159/// named identically to (and documented by) the matching [`PolicyType`] variant.
160///
161/// The discriminator is serialized under `_policyType` rather than `type` so it
162/// cannot collide with a policy data field named `type` (e.g.
163/// [`MaximumVaultTimeoutPolicyData`]), which the internally-tagged
164/// representation flattens alongside the discriminator.
165// Variants mirror the already-documented `PolicyType`, so per-variant docs would
166// be redundant boilerplate.
167#[allow(missing_docs)]
168#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
169#[serde(rename_all = "camelCase", tag = "_policyType")]
170#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
171#[bitwarden_ffi::wasm_record]
172// TODO: `SendControls` and `UriMatchDefaults` are temporarily unit variants. They gain
173// their data models in follow-up PRs, once `SendType`/`UriMatchType` move into
174// lower-level crates that this crate can depend on without a cycle.
175pub enum PolicyDataType {
176    TwoFactorAuthentication,
177    MasterPassword(MasterPasswordPolicyData),
178    PasswordGenerator(PasswordGeneratorPolicyData),
179    SingleOrg,
180    RequireSso,
181    OrganizationDataOwnership(OrganizationDataOwnershipPolicyData),
182    DisableSend,
183    SendOptions(SendOptionsPolicyData),
184    ResetPassword(ResetPasswordPolicyData),
185    MaximumVaultTimeout(MaximumVaultTimeoutPolicyData),
186    DisablePersonalVaultExport,
187    ActivateAutofill,
188    AutomaticAppLogIn(AutomaticAppLogInPolicyData),
189    FreeFamiliesSponsorship,
190    RemoveUnlockWithPin,
191    RestrictedItemTypes,
192    UriMatchDefaults,
193    AutotypeDefaultSetting,
194    AutomaticUserConfirmation,
195    BlockClaimedDomainAccountCreation,
196    OrganizationUserNotification(OrganizationUserNotificationPolicyData),
197    SendControls,
198    FillAssist(FillAssistPolicyData),
199    AutotypeGaPolicy,
200}