Skip to content

Commit 9f58e91

Browse files
authored
Create bitwarden-auth crate (#354)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective Created a new auth crate for the work that auth is moving to the SDK, and defined the WASM API for the send access functionality as a basic skeleton. Some things left open: - We already have an `AuthClient` in `bitwarden-core`, it's not used for WASM but it is for UniFFI. This PR leaves both clients in place but the goal is to migrate the older clients functionality over to the auth crate or somewhere else, and keep it out of core. - Because we already have the `AuthClient` in core, I've made the extension trait for auth use a function name like `auth_new`, this is to avoid conflicts with the other function and should be renamed once the older `AuthClient` is deleted. - Currently `AuthClient` is wrapping a bitwarden-core `Client`, so it can reuse the `ApiConfigurations` defined in core, this seems like it would complicate the usage of the auth crate, so I'm thinking we should extract the `ApiConfigurations` and maybe part of the token handling code to a separate bitwarden-api crate that both core and auth can use without depending on eachother? ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 1ba460c commit 9f58e91

File tree

11 files changed

+146
-0
lines changed

11 files changed

+146
-0
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# Platform is the default owner for all files
88
* @bitwarden/team-platform-dev
99

10+
crates/bitwarden-auth/** @bitwarden/team-auth-dev @bitwarden/team-platform-dev
11+
1012
crates/bitwarden-vault/** @bitwarden/team-vault-dev @bitwarden/team-platform-dev
1113

1214
# Temporarily owned by multiple teams

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ keywords = ["bitwarden"]
2121
bitwarden = { path = "crates/bitwarden", version = "=1.0.0" }
2222
bitwarden-api-api = { path = "crates/bitwarden-api-api", version = "=1.0.0" }
2323
bitwarden-api-identity = { path = "crates/bitwarden-api-identity", version = "=1.0.0" }
24+
bitwarden-auth = { path = "crates/bitwarden-auth", version = "=1.0.0" }
2425
bitwarden-cli = { path = "crates/bitwarden-cli", version = "=1.0.0" }
2526
bitwarden-core = { path = "crates/bitwarden-core", version = "=1.0.0" }
2627
bitwarden-crypto = { path = "crates/bitwarden-crypto", version = "=1.0.0" }

crates/bitwarden-auth/Cargo.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[package]
2+
name = "bitwarden-auth"
3+
description = """
4+
Internal crate for the bitwarden crate. Do not use.
5+
"""
6+
7+
version.workspace = true
8+
authors.workspace = true
9+
edition.workspace = true
10+
rust-version.workspace = true
11+
readme.workspace = true
12+
homepage.workspace = true
13+
repository.workspace = true
14+
license-file.workspace = true
15+
keywords.workspace = true
16+
17+
[features]
18+
uniffi = ["bitwarden-core/uniffi", "dep:uniffi"] # Uniffi bindings
19+
wasm = [
20+
"bitwarden-core/wasm",
21+
"dep:tsify",
22+
"dep:wasm-bindgen",
23+
"dep:wasm-bindgen-futures"
24+
] # WASM support
25+
26+
[dependencies]
27+
bitwarden-core = { workspace = true, features = ["internal"] }
28+
bitwarden-error = { workspace = true }
29+
serde = { workspace = true }
30+
serde_json = { workspace = true }
31+
serde_qs = { workspace = true }
32+
thiserror = { workspace = true }
33+
tsify = { workspace = true, optional = true }
34+
uniffi = { workspace = true, optional = true }
35+
wasm-bindgen = { workspace = true, optional = true }
36+
wasm-bindgen-futures = { workspace = true, optional = true }
37+
38+
[lints]
39+
workspace = true

crates/bitwarden-auth/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Bitwarden Auth
2+
3+
Contains the implementation of the auth functionality for the Bitwarden Password Manager.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use bitwarden_core::Client;
2+
#[cfg(feature = "wasm")]
3+
use wasm_bindgen::prelude::*;
4+
5+
use crate::send_access::SendAccessClient;
6+
7+
/// Subclient containing auth functionality.
8+
#[derive(Clone)]
9+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
10+
pub struct AuthClient {
11+
// TODO: The AuthClient should probably not contain the whole bitwarden-core client.
12+
// Instead, it should contain the ApiConfigurations and Tokens struct to do API requests and
13+
// handle token renewals, and those structs should be shared between core and auth.
14+
pub(crate) client: Client,
15+
}
16+
17+
impl AuthClient {
18+
/// Constructs a new `AuthClient` with the given `Client`.
19+
pub fn new(client: Client) -> Self {
20+
Self { client }
21+
}
22+
}
23+
24+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
25+
impl AuthClient {
26+
/// Client for send access functionality
27+
pub fn send_access(&self) -> SendAccessClient {
28+
SendAccessClient::new(self.client.clone())
29+
}
30+
}
31+
32+
/// Extension trait for `Client` to provide access to the `AuthClient`.
33+
pub trait AuthClientExt {
34+
/// Creates a new `AuthClient` instance.
35+
fn auth_new(&self) -> AuthClient;
36+
}
37+
38+
impl AuthClientExt for Client {
39+
fn auth_new(&self) -> AuthClient {
40+
AuthClient {
41+
client: self.clone(),
42+
}
43+
}
44+
}

crates/bitwarden-auth/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
mod auth_client;
4+
mod send_access;
5+
6+
pub use auth_client::{AuthClient, AuthClientExt};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use bitwarden_core::Client;
2+
#[cfg(feature = "wasm")]
3+
use wasm_bindgen::prelude::*;
4+
5+
#[derive(Clone)]
6+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
7+
pub struct SendAccessClient {
8+
pub(crate) client: Client,
9+
}
10+
11+
impl SendAccessClient {
12+
pub(crate) fn new(client: Client) -> Self {
13+
Self { client }
14+
}
15+
}
16+
17+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
18+
impl SendAccessClient {
19+
/// Request an access token for the provided send
20+
pub async fn request_send_access_token(&self, request: String) -> String {
21+
// TODO: This is just here to silence some warnings
22+
let _config = self.client.internal.get_api_configurations().await;
23+
request
24+
}
25+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod client;
2+
pub use client::SendAccessClient;

crates/bitwarden-wasm-internal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ crate-type = ["cdylib"]
1818
[dependencies]
1919
async-trait = { workspace = true }
2020
base64 = ">=0.22.1, <0.23.0"
21+
bitwarden-auth = { workspace = true, features = ["wasm"] }
2122
bitwarden-core = { workspace = true, features = ["wasm", "internal"] }
2223
bitwarden-crypto = { workspace = true, features = ["wasm"] }
2324
bitwarden-error = { workspace = true }

0 commit comments

Comments
 (0)