bitwarden_user_crypto_management/pin_settings/
pin_settings_client.rs1use 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)]
12pub struct PinSettingsClient {
14 pub(crate) client: bitwarden_core::Client,
15}
16
17#[derive(Debug, Error)]
18#[bitwarden_error(flat)]
19pub enum PinSettingsError {
21 #[error("Failed to set PIN state")]
22 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 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 pub async fn unset_pin(&self) {
51 PinLockSystem::with_client(&self.client).unset_pin().await
52 }
53
54 pub async fn get_status(&self) -> PinUnlockStatus {
56 PinLockSystem::with_client(&self.client)
57 .get_pin_status()
58 .await
59 }
60
61 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 pub async fn validate_pin(&self, pin: String) -> bool {
70 PinLockSystem::with_client(&self.client)
71 .validate_pin(pin)
72 .await
73 }
74
75 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 pub fn pin_settings(&self) -> PinSettingsClient {
85 PinSettingsClient::new(self.client.clone())
86 }
87}