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