Skip to main content

bitwarden_user_crypto_management/pin_settings/
pin_settings_client.rs

1use bitwarden_core::key_management::{PinLockSystem, PinLockType, PinUnlockStatus};
2use bitwarden_error::bitwarden_error;
3use thiserror::Error;
4
5use crate::UserCryptoManagementClient;
6
7#[derive(Clone)]
8#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
9#[bitwarden_ffi::wasm_object]
10/// Sub-client for configuring PIN unlock behavior.
11pub struct PinSettingsClient {
12    pub(crate) client: bitwarden_core::Client,
13}
14
15#[derive(Debug, Error)]
16#[bitwarden_error(flat)]
17/// Errors returned by PIN settings operations.
18pub enum PinSettingsError {
19    #[error("Failed to set PIN state")]
20    /// Failed while enrolling or storing the PIN-protected key envelope.
21    SetPinState,
22}
23
24impl PinSettingsClient {
25    pub(crate) fn new(client: bitwarden_core::Client) -> Self {
26        Self { client }
27    }
28}
29
30#[bitwarden_ffi::wasm_export]
31#[cfg_attr(feature = "uniffi", uniffi::export(async_runtime = "tokio"))]
32impl PinSettingsClient {
33    /// Sets or updates the account PIN and stores the corresponding unlock state.
34    ///
35    /// The `lock_type` determines how PIN unlock behaves for this account.
36    /// Returns an error when the PIN-protected state cannot be persisted.
37    pub async fn set_pin(
38        &self,
39        pin: String,
40        lock_type: bitwarden_core::key_management::PinLockType,
41    ) -> Result<(), PinSettingsError> {
42        PinLockSystem::with_client(&self.client)
43            .set_pin(pin, lock_type)
44            .await
45            .map_err(|_| PinSettingsError::SetPinState)
46    }
47
48    /// Unenrolls from PIN-based unlock
49    pub async fn unset_pin(&self) {
50        PinLockSystem::with_client(&self.client).unset_pin().await
51    }
52
53    /// Returns the current status of PIN unlock
54    pub async fn get_status(&self) -> PinUnlockStatus {
55        PinLockSystem::with_client(&self.client)
56            .get_pin_status()
57            .await
58    }
59
60    /// Returns the configured PIN lock type, if a PIN lock is set.
61    pub async fn get_lock_type(&self) -> Option<PinLockType> {
62        PinLockSystem::with_client(&self.client)
63            .get_pin_lock_type()
64            .await
65    }
66
67    /// Returns the number of characters in the PIN, readable while locked, e.g. so the lock screen
68    /// can try the PIN as soon as it is fully typed. `None` when no PIN is set, or for a PIN set
69    /// before its length was stored that hasn't unlocked since.
70    pub async fn get_pin_length(&self) -> Option<u32> {
71        PinLockSystem::with_client(&self.client)
72            .get_pin_length()
73            .await
74    }
75
76    /// Validates whether `pin` matches the currently configured unlock PIN.
77    pub async fn validate_pin(&self, pin: String) -> bool {
78        PinLockSystem::with_client(&self.client)
79            .validate_pin(pin)
80            .await
81    }
82
83    /// Returns the currently configured PIN, if available.
84    pub async fn get_pin(&self) -> Option<String> {
85        PinLockSystem::with_client(&self.client).get_pin().await
86    }
87}
88
89#[bitwarden_ffi::wasm_export]
90#[cfg_attr(feature = "uniffi", uniffi::export)]
91impl UserCryptoManagementClient {
92    /// Returns the PIN settings sub-client.
93    pub fn pin_settings(&self) -> PinSettingsClient {
94        PinSettingsClient::new(self.client.clone())
95    }
96}