bitwarden_auth/
auth_client.rs1use bitwarden_core::Client;
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::{login::LoginClient, registration::RegistrationClient, send_access::SendAccessClient};
6
7#[derive(Clone)]
9#[cfg_attr(feature = "wasm", wasm_bindgen)]
10pub struct AuthClient {
11 pub(crate) client: Client,
15}
16
17impl AuthClient {
18 pub fn new(client: Client) -> Self {
20 Self { client }
21 }
22}
23
24#[cfg_attr(feature = "wasm", wasm_bindgen)]
25impl AuthClient {
26 pub fn login(&self) -> LoginClient {
28 LoginClient::new(self.client.clone())
29 }
30
31 pub fn send_access(&self) -> SendAccessClient {
33 SendAccessClient::new(self.client.clone())
34 }
35
36 pub fn registration(&self) -> RegistrationClient {
38 RegistrationClient::new(self.client.clone())
39 }
40}
41
42pub trait AuthClientExt {
44 fn auth_new(&self) -> AuthClient;
46}
47
48impl AuthClientExt for Client {
49 fn auth_new(&self) -> AuthClient {
50 AuthClient {
51 client: self.clone(),
52 }
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use std::sync::Arc;
59
60 use super::*;
61
62 #[test]
63 fn login_client_shares_the_auth_clients_backing_client() {
64 let client = Client::new(None);
65 let auth_client = AuthClient::new(client.clone());
66
67 let login_client = auth_client.login();
68
69 assert!(
70 Arc::ptr_eq(&client.internal, &login_client.client.internal),
71 "LoginClient must reuse the same Client instance backing the AuthClient it came \
72 from, otherwise login requests can target a different server than the rest of the \
73 SDK"
74 );
75 }
76}