bitwarden_server_communication_config/platform_api.rs
1use bitwarden_error::bitwarden_error;
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4
5/// A cookie acquired from the platform
6///
7/// Represents a single cookie name/value pair as received from the browser or HTTP client.
8/// For sharded cookies (AWS ALB pattern), each shard is a separate `AcquiredCookie` with
9/// its own name including the `-{N}` suffix (e.g., `AWSELBAuthSessionCookie-0`).
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11#[bitwarden_ffi::wasm_record]
12#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
13pub struct AcquiredCookie {
14 /// Cookie name
15 ///
16 /// For sharded cookies, this includes the shard suffix (e.g., `AWSELBAuthSessionCookie-0`)
17 /// For unsharded cookies, this is the cookie name without any suffix.
18 pub name: String,
19 /// Cookie value
20 pub value: String,
21}
22
23/// Errors that can occur during cookie acquisition
24#[derive(Debug, Error, Clone, PartialEq, Eq)]
25#[bitwarden_error(flat)]
26pub enum AcquireCookieError {
27 /// Cookie acquisition was cancelled by the user
28 #[error("Must sync to connect to server")]
29 Cancelled,
30
31 /// The server configuration does not support cookie acquisition
32 #[error("Server configuration does not support SSO cookie acquisition (Direct bootstrap)")]
33 UnsupportedConfiguration,
34
35 /// The acquired cookie name does not match the expected cookie name
36 #[error("Cookie name mismatch: expected {expected}, got {actual}")]
37 CookieNameMismatch {
38 /// Expected cookie name from server configuration
39 expected: String,
40 /// Actual cookie name returned by platform
41 actual: String,
42 },
43
44 /// Failed to retrieve server configuration from repository
45 #[error("Failed to get server configuration: {0}")]
46 RepositoryGet(String),
47
48 /// Failed to save updated configuration to repository
49 #[error("Failed to save server configuration: {0}")]
50 RepositorySave(String),
51}
52
53/// Platform API for acquiring cookies from the platform client
54///
55/// This trait abstracts the platform-specific logic for acquiring SSO cookies
56/// from load balancers. Platform clients (web, mobile, desktop) implement this
57/// trait to provide cookie acquisition through browser interactions or native
58/// HTTP client capabilities.
59#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
60#[async_trait::async_trait]
61pub trait ServerCommunicationConfigPlatformApi: Send + Sync {
62 /// Acquires cookies using the provided vault URL
63 ///
64 /// The platform client should trigger any necessary user interaction
65 /// (e.g., browser redirect to IdP) to acquire cookies from the
66 /// load balancer.
67 ///
68 /// # Parameters
69 /// - `vault_url`: The full vault URL (scheme + host + port, e.g., `"https://vault.bitwarden.com"`
70 /// or `"https://localhost:8000"`). This URL is used for constructing the redirect URL.
71 ///
72 /// # Returns
73 /// Returns `Some(Vec<AcquiredCookie>)` if cookies were successfully acquired,
74 /// or `None` if the operation was cancelled or failed.
75 async fn acquire_cookies(&self, vault_url: String) -> Option<Vec<AcquiredCookie>>;
76}