bitwarden_auth/
auth_client.rs

1use bitwarden_core::Client;
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::{
6    identity::IdentityClient, registration::RegistrationClient, send_access::SendAccessClient,
7};
8
9/// Subclient containing auth functionality.
10#[derive(Clone)]
11#[cfg_attr(feature = "wasm", wasm_bindgen)]
12pub struct AuthClient {
13    // TODO: The AuthClient should probably not contain the whole bitwarden-core client.
14    // Instead, it should contain the ApiConfigurations and Tokens struct to do API requests and
15    // handle token renewals, and those structs should be shared between core and auth.
16    pub(crate) client: Client,
17}
18
19impl AuthClient {
20    /// Constructs a new `AuthClient` with the given `Client`.
21    pub fn new(client: Client) -> Self {
22        Self { client }
23    }
24}
25
26#[cfg_attr(feature = "wasm", wasm_bindgen)]
27impl AuthClient {
28    /// Client for identity functionality
29    pub fn identity(&self) -> IdentityClient {
30        IdentityClient::new(self.client.clone())
31    }
32
33    /// Client for send access functionality
34    pub fn send_access(&self) -> SendAccessClient {
35        SendAccessClient::new(self.client.clone())
36    }
37
38    /// Client for initializing user account cryptography and unlock methods after JIT provisioning
39    pub fn registration(&self) -> RegistrationClient {
40        RegistrationClient::new(self.client.clone())
41    }
42}
43
44/// Extension trait for `Client` to provide access to the `AuthClient`.
45pub trait AuthClientExt {
46    /// Creates a new `AuthClient` instance.
47    fn auth_new(&self) -> AuthClient;
48}
49
50impl AuthClientExt for Client {
51    fn auth_new(&self) -> AuthClient {
52        AuthClient {
53            client: self.clone(),
54        }
55    }
56}