Skip to main content

bitwarden_core/client/
persisted_state.rs

1//! Persisted state types and setting keys for the Bitwarden SDK.
2
3use bitwarden_crypto::{EncString, UnsignedSharedKey};
4use bitwarden_state::{register_repository_item, register_setting_key};
5use serde::{Deserialize, Serialize};
6#[cfg(feature = "wasm")]
7use tsify::Tsify;
8
9use super::{flags::Flags, login_method::UserLoginMethod};
10use crate::{
11    OrganizationId, UserId,
12    key_management::account_cryptographic_state::WrappedAccountCryptographicState,
13};
14
15/// A persisted organization encryption key.
16///
17/// Stored in a Repository keyed by [`OrganizationId`].
18/// The `org_id` is included in the struct so it can be recovered from `Repository::list()`.
19#[derive(Clone, Serialize, Deserialize)]
20#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
21#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
22pub struct OrganizationSharedKey {
23    /// The organization this key belongs to.
24    pub org_id: OrganizationId,
25    /// The organization's shared encryption key.
26    pub key: UnsignedSharedKey,
27}
28
29register_repository_item!(OrganizationId => OrganizationSharedKey, "OrganizationSharedKey");
30
31/// Base API URLs for identity and API services.
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct BaseUrls {
34    /// The identity service URL.
35    pub identity_url: String,
36    /// The API service URL.
37    pub api_url: String,
38}
39
40/// Authentication tokens for API access.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct AuthenticationTokens {
43    /// The access token.
44    pub access_token: String,
45    /// The refresh token.
46    pub refresh_token: Option<String>,
47    /// Unix timestamp when the access token expires.
48    pub expires_on: i64,
49}
50
51// Setting keys
52register_setting_key!(
53    /// Setting key for the user's login method.
54    pub const USER_LOGIN_METHOD: UserLoginMethod = "user_login_method"
55);
56register_setting_key!(
57    /// Setting key for the base API URLs.
58    pub const BASE_URLS: BaseUrls = "base_urls"
59);
60register_setting_key!(
61    /// Setting key for the user ID.
62    pub const USER_ID: UserId = "user_id"
63);
64register_setting_key!(
65    /// Setting key for feature flags.
66    pub const FLAGS: Flags = "flags"
67);
68register_setting_key!(
69    /// Setting key for authentication tokens.
70    pub const AUTHENTICATION_TOKENS: AuthenticationTokens = "authentication_tokens"
71);
72register_setting_key!(
73    /// Setting key for the account cryptographic state.
74    pub const ACCOUNT_CRYPTO_STATE: WrappedAccountCryptographicState = "account_crypto_state"
75);
76register_setting_key!(
77    /// Setting key for the session-protected user key.
78    pub const SESSION_PROTECTED_USER_KEY: EncString = "session_protected_user_key"
79);