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#[cfg(feature = "wasm")]
5use wasm_bindgen::prelude::*;
6
7use crate::UserCryptoManagementClient;
8
9#[derive(Clone)]
10#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
11#[cfg_attr(feature = "wasm", wasm_bindgen)]
12/// Sub-client for configuring PIN unlock behavior.
13pub struct PinSettingsClient {
14    pub(crate) client: bitwarden_core::Client,
15}
16
17#[derive(Debug, Error)]
18#[bitwarden_error(flat)]
19/// Errors returned by PIN settings operations.
20pub enum PinSettingsError {
21    #[error("Failed to set PIN state")]
22    /// Failed while enrolling or storing the PIN-protected key envelope.
23    SetPinState,
24}
25
26impl PinSettingsClient {
27    pub(crate) fn new(client: bitwarden_core::Client) -> Self {
28        Self { client }
29    }
30}
31
32#[cfg_attr(feature = "wasm", wasm_bindgen)]
33impl PinSettingsClient {
34    /// Sets or updates the account PIN and stores the corresponding unlock state.
35    ///
36    /// The `lock_type` determines how PIN unlock behaves for this account.
37    /// Returns an error when the PIN-protected state cannot be persisted.
38    pub async fn set_pin(
39        &self,
40        pin: String,
41        lock_type: bitwarden_core::key_management::PinLockType,
42    ) -> Result<(), PinSettingsError> {
43        PinLockSystem::with_client(&self.client)
44            .set_pin(pin, lock_type)
45            .await
46            .map_err(|_| PinSettingsError::SetPinState)
47    }
48
49    /// Unenrolls from PIN-based unlock
50    pub async fn unset_pin(&self) {
51        PinLockSystem::with_client(&self.client).unset_pin().await
52    }
53
54    /// Returns the current status of PIN unlock
55    pub async fn get_status(&self) -> PinUnlockStatus {
56        PinLockSystem::with_client(&self.client)
57            .get_pin_status()
58            .await
59    }
60
61    /// Returns the configured PIN lock type, if a PIN lock is set.
62    pub async fn get_lock_type(&self) -> Option<PinLockType> {
63        PinLockSystem::with_client(&self.client)
64            .get_pin_lock_type()
65            .await
66    }
67
68    /// Validates whether `pin` matches the currently configured unlock PIN.
69    pub async fn validate_pin(&self, pin: String) -> bool {
70        PinLockSystem::with_client(&self.client)
71            .validate_pin(pin)
72            .await
73    }
74
75    /// Returns the currently configured PIN, if available.
76    pub async fn get_pin(&self) -> Option<String> {
77        PinLockSystem::with_client(&self.client).get_pin().await
78    }
79}
80
81#[cfg_attr(feature = "wasm", wasm_bindgen)]
82impl UserCryptoManagementClient {
83    /// Returns the PIN settings sub-client.
84    pub fn pin_settings(&self) -> PinSettingsClient {
85        PinSettingsClient::new(self.client.clone())
86    }
87}