Skip to content

Commit 9947387

Browse files
authored
Expose ClientManagedTokens to mobile (#470)
Expose `ClientManagedTokens` trait to mobile and require it on constructor.
1 parent c975847 commit 9947387

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

crates/bitwarden-core/src/client/internal.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub(crate) enum Tokens {
6565
}
6666

6767
/// Access tokens managed by client applications, such as the web or mobile apps.
68+
#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
6869
#[async_trait::async_trait]
6970
pub trait ClientManagedTokens: std::fmt::Debug + Send + Sync {
7071
/// Returns the access token, if available.

crates/bitwarden-uniffi/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.compose.ui.Modifier
2222
import androidx.compose.ui.graphics.Color
2323
import androidx.compose.ui.unit.dp
2424
import androidx.fragment.app.FragmentActivity
25+
import com.bitwarden.core.ClientManagedTokens
2526
import com.bitwarden.core.DateTime
2627
import com.bitwarden.vault.Folder
2728
import com.bitwarden.core.InitOrgCryptoRequest
@@ -82,6 +83,12 @@ const val PIN = "1234"
8283
// We should separate keys for each user by appending the user_id
8384
const val BIOMETRIC_KEY = "biometric_key"
8485

86+
class Token: ClientManagedTokens {
87+
override suspend fun getAccessToken(): String? {
88+
return null
89+
}
90+
}
91+
8592
class MainActivity : FragmentActivity() {
8693
private lateinit var biometric: Biometric
8794
private lateinit var client: Client
@@ -98,7 +105,7 @@ class MainActivity : FragmentActivity() {
98105
override fun onCreate(savedInstanceState: Bundle?) {
99106
super.onCreate(savedInstanceState)
100107
biometric = Biometric(this)
101-
client = Client(null)
108+
client = Client(Token(), null)
102109
http = httpClient()
103110

104111
setContent {
@@ -172,7 +179,7 @@ class MainActivity : FragmentActivity() {
172179
Button({
173180
GlobalScope.launch {
174181
client.destroy()
175-
client = Client(null)
182+
client = Client(Token(), null)
176183
outputText.value = "OK"
177184
}
178185
}) {

crates/bitwarden-uniffi/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
uniffi::setup_scaffolding!();
44

5+
use std::sync::Arc;
6+
57
use auth::AuthClient;
6-
use bitwarden_core::ClientSettings;
8+
use bitwarden_core::{client::internal::ClientManagedTokens, ClientSettings};
79

810
#[allow(missing_docs)]
911
pub mod auth;
@@ -35,14 +37,20 @@ pub struct Client(pub(crate) bitwarden_pm::PasswordManagerClient);
3537
impl Client {
3638
/// Initialize a new instance of the SDK client
3739
#[uniffi::constructor]
38-
pub fn new(settings: Option<ClientSettings>) -> Self {
40+
pub fn new(
41+
token_provider: Arc<dyn ClientManagedTokens>,
42+
settings: Option<ClientSettings>,
43+
) -> Self {
3944
init_logger();
4045
setup_error_converter();
4146

4247
#[cfg(target_os = "android")]
4348
android_support::init();
4449

45-
Self(bitwarden_pm::PasswordManagerClient::new(settings))
50+
Self(bitwarden_pm::PasswordManagerClient::new_with_client_tokens(
51+
settings,
52+
token_provider,
53+
))
4654
}
4755

4856
/// Crypto operations

crates/bitwarden-uniffi/swift/iOS/App/ContentView.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ let PASSWORD = "asdfasdfasdf"
2525

2626
let PIN = "1234"
2727

28+
final class Token: ClientManagedTokens {
29+
func getAccessToken() async -> String? {
30+
return nil
31+
}
32+
}
33+
2834
struct ContentView: View {
2935
private var http: URLSession
3036

@@ -38,7 +44,7 @@ struct ContentView: View {
3844
configuration: URLSessionConfiguration.default, delegate: IgnoreHttpsDelegate(),
3945
delegateQueue: nil)
4046

41-
client = Client(settings: nil)
47+
client = Client(tokenProvider: Token(), settings: nil)
4248
}
4349

4450
@State var setupBiometrics: Bool = true
@@ -104,7 +110,7 @@ struct ContentView: View {
104110
})
105111

106112
Button(action: {
107-
client = Client(settings: nil)
113+
client = Client(tokenProvider: Token(), settings: nil)
108114
}, label: {
109115
Text("Lock & reset client")
110116
}).padding()
@@ -213,7 +219,7 @@ struct ContentView: View {
213219
}
214220

215221
if setupPin {
216-
let pinOptions = try await clientCrypto.derivePinKey(pin: PIN)
222+
let pinOptions = try clientCrypto.derivePinKey(pin: PIN)
217223

218224
let defaults = UserDefaults.standard
219225
defaults.set(loginData.PrivateKey, forKey: "privateKey")
@@ -329,7 +335,7 @@ struct ContentView: View {
329335
let dateFormatter = ISO8601DateFormatter()
330336
dateFormatter.formatOptions = [.withFractionalSeconds]
331337

332-
let decryptedFolders = try await clientVault.folders().decryptList(
338+
let decryptedFolders = try clientVault.folders().decryptList(
333339
folders: syncData.folders.map {
334340
Folder(
335341
id: $0.id, name: $0.name,

0 commit comments

Comments
 (0)