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
5use crate::UserCryptoManagementClient;
6
7#[derive(Clone)]
8#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
9#[bitwarden_ffi::wasm_object]
10pub struct PinSettingsClient {
12 pub(crate) client: bitwarden_core::Client,
13}
14
15#[derive(Debug, Error)]
16#[bitwarden_error(flat)]
17pub enum PinSettingsError {
19 #[error("Failed to set PIN state")]
20 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 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 pub async fn unset_pin(&self) {
50 PinLockSystem::with_client(&self.client).unset_pin().await
51 }
52
53 pub async fn get_status(&self) -> PinUnlockStatus {
55 PinLockSystem::with_client(&self.client)
56 .get_pin_status()
57 .await
58 }
59
60 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 pub async fn validate_pin(&self, pin: String) -> bool {
69 PinLockSystem::with_client(&self.client)
70 .validate_pin(pin)
71 .await
72 }
73
74 pub async fn get_pin(&self) -> Option<String> {
76 PinLockSystem::with_client(&self.client).get_pin().await
77 }
78}
79
80#[bitwarden_ffi::wasm_export]
81#[cfg_attr(feature = "uniffi", uniffi::export)]
82impl UserCryptoManagementClient {
83 pub fn pin_settings(&self) -> PinSettingsClient {
85 PinSettingsClient::new(self.client.clone())
86 }
87}