Skip to content

Commit 203e538

Browse files
authored
[PM-16221] uniffi bindings for ssh agent and keygen (#89)
## 🎟️ Tracking https://bitwarden.atlassian.net/browse/PM-16221 ## 📔 Objective Adds bindings so we can add keygen and import to the mobile clients. ## ⏰ 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 73747b6 commit 203e538

File tree

10 files changed

+62
-1
lines changed

10 files changed

+62
-1
lines changed

Cargo.lock

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

crates/bitwarden-ssh/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ wasm = [
2020
"dep:tsify-next",
2121
"dep:wasm-bindgen"
2222
] # WASM support
23+
uniffi = ["dep:uniffi"] # Uniffi bindings
2324

2425
[dependencies]
2526
bitwarden-error = { workspace = true }
@@ -36,6 +37,7 @@ ssh-key = { version = ">=0.6.7, <0.7", features = [
3637
], default-features = false }
3738
thiserror = { workspace = true }
3839
tsify-next = { workspace = true, optional = true }
40+
uniffi = { workspace = true, optional = true }
3941
wasm-bindgen = { workspace = true, optional = true }
4042

4143
[dev-dependencies]

crates/bitwarden-ssh/src/generator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{error, error::KeyGenerationError, SshKey};
77

88
#[derive(Serialize, Deserialize)]
99
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
10+
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
1011
pub enum KeyAlgorithm {
1112
Ed25519,
1213
Rsa3072,

crates/bitwarden-ssh/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ use ssh_key::{HashAlg, PrivateKey};
99
#[cfg(feature = "wasm")]
1010
use tsify_next::Tsify;
1111

12+
#[cfg(feature = "uniffi")]
13+
uniffi::setup_scaffolding!();
14+
1215
#[derive(Serialize, Deserialize, Debug)]
1316
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
17+
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
1418
pub struct SshKey {
1519
/// The private key in OpenSSH format
1620
pub private_key: String,

crates/bitwarden-ssh/uniffi.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[bindings.kotlin]
2+
package_name = "com.bitwarden.ssh"
3+
generate_immutable_records = true
4+
android = true
5+
6+
[bindings.swift]
7+
ffi_module_name = "BitwardenSshFFI"
8+
module_name = "BitwardenSsh"
9+
generate_immutable_records = true

crates/bitwarden-uniffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ bitwarden-exporters = { workspace = true, features = ["uniffi"] }
2424
bitwarden-fido = { workspace = true, features = ["uniffi"] }
2525
bitwarden-generators = { workspace = true, features = ["uniffi"] }
2626
bitwarden-send = { workspace = true, features = ["uniffi"] }
27+
bitwarden-ssh = { workspace = true, features = ["uniffi"] }
2728
bitwarden-vault = { workspace = true, features = ["uniffi"] }
2829
chrono = { workspace = true, features = ["std"] }
2930
env_logger = "0.11.1"

crates/bitwarden-uniffi/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ pub enum Error {
7676
DecryptFido2AutofillCredentials(#[from] bitwarden_fido::DecryptFido2AutofillCredentialsError),
7777
#[error(transparent)]
7878
Fido2Client(#[from] bitwarden_fido::Fido2ClientError),
79+
80+
#[error(transparent)]
81+
SshGeneration(#[from] bitwarden_ssh::error::KeyGenerationError),
82+
#[error(transparent)]
83+
SshImport(#[from] bitwarden_ssh::error::SshKeyImportError),
7984
}

crates/bitwarden-uniffi/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod android_support;
1919
use crypto::CryptoClient;
2020
use error::Result;
2121
use platform::PlatformClient;
22-
use tool::{ExporterClient, GeneratorClients, SendClient};
22+
use tool::{ExporterClient, GeneratorClients, SendClient, SshClient};
2323
use vault::VaultClient;
2424

2525
#[derive(uniffi::Object)]
@@ -67,6 +67,11 @@ impl Client {
6767
Arc::new(SendClient(self))
6868
}
6969

70+
/// SSH operations
71+
pub fn ssh(self: Arc<Self>) -> Arc<SshClient> {
72+
Arc::new(SshClient(self))
73+
}
74+
7075
/// Auth operations
7176
pub fn auth(self: Arc<Self>) -> Arc<AuthClient> {
7277
Arc::new(AuthClient(self))

crates/bitwarden-uniffi/src/tool/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use crate::{
1515
mod sends;
1616
pub use sends::SendClient;
1717

18+
mod ssh;
19+
pub use ssh::SshClient;
20+
1821
#[derive(uniffi::Object)]
1922
pub struct GeneratorClients(pub(crate) Arc<Client>);
2023

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::sync::Arc;
2+
3+
use crate::{
4+
error::{BitwardenError, Error},
5+
Client, Result,
6+
};
7+
8+
#[derive(uniffi::Object)]
9+
pub struct SshClient(pub Arc<Client>);
10+
11+
#[uniffi::export]
12+
impl SshClient {
13+
pub fn generate_ssh_key(
14+
&self,
15+
key_algorithm: bitwarden_ssh::generator::KeyAlgorithm,
16+
) -> Result<bitwarden_ssh::SshKey> {
17+
bitwarden_ssh::generator::generate_sshkey(key_algorithm)
18+
.map_err(|e| BitwardenError::E(Error::SshGeneration(e)))
19+
}
20+
21+
pub fn import_ssh_key(
22+
&self,
23+
imported_key: String,
24+
password: Option<String>,
25+
) -> Result<bitwarden_ssh::SshKey> {
26+
bitwarden_ssh::import::import_key(imported_key, password)
27+
.map_err(|e| BitwardenError::E(Error::SshImport(e)))
28+
}
29+
}

0 commit comments

Comments
 (0)