Skip to content

Commit

Permalink
Add uniffi bindings for ssh keygen and import
Browse files Browse the repository at this point in the history
  • Loading branch information
quexten committed Dec 19, 2024
1 parent 8d70e02 commit 56dcd48
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/bitwarden-ssh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ keywords.workspace = true
wasm = [
"bitwarden-error/wasm",
"dep:tsify-next",
"dep:wasm-bindgen"
"dep:wasm-bindgen",
] # WASM support
uniffi = ["dep:uniffi"] # Uniffi bindings

[dependencies]
bitwarden-error = { workspace = true }
Expand All @@ -36,6 +37,7 @@ ssh-key = { version = ">=0.6.7, <0.7", features = [
], default-features = false }
thiserror = { workspace = true }
tsify-next = { workspace = true, optional = true }
uniffi = { workspace = true, optional = true }
wasm-bindgen = { workspace = true, optional = true }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-ssh/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{error, error::KeyGenerationError, SshKey};

#[derive(Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum KeyAlgorithm {
Ed25519,
Rsa3072,
Expand Down
4 changes: 4 additions & 0 deletions crates/bitwarden-ssh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ use ssh_key::{HashAlg, PrivateKey};
#[cfg(feature = "wasm")]
use tsify_next::Tsify;

#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();

#[derive(Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct SshKey {
/// The private key in OpenSSH format
pub private_key: String,
Expand Down
9 changes: 9 additions & 0 deletions crates/bitwarden-ssh/uniffi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[bindings.kotlin]
package_name = "com.bitwarden.ssh"
generate_immutable_records = true
android = true

[bindings.swift]
ffi_module_name = "BitwardenSshFFI"
module_name = "BitwardenSsh"
generate_immutable_records = true
1 change: 1 addition & 0 deletions crates/bitwarden-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bitwarden-exporters = { workspace = true, features = ["uniffi"] }
bitwarden-fido = { workspace = true, features = ["uniffi"] }
bitwarden-generators = { workspace = true, features = ["uniffi"] }
bitwarden-send = { workspace = true, features = ["uniffi"] }
bitwarden-ssh = { workspace = true, features = ["uniffi"] }
bitwarden-vault = { workspace = true, features = ["uniffi"] }
chrono = { workspace = true, features = ["std"] }
env_logger = "0.11.1"
Expand Down
7 changes: 7 additions & 0 deletions crates/bitwarden-uniffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ pub enum Error {
DecryptFido2AutofillCredentials(#[from] bitwarden_fido::DecryptFido2AutofillCredentialsError),
#[error(transparent)]
Fido2Client(#[from] bitwarden_fido::Fido2ClientError),

#[error(transparent)]
SshGenerationError(#[from] bitwarden_ssh::error::KeyGenerationError),
#[error(transparent)]
SshImportError(#[from] bitwarden_ssh::error::SshKeyImportError),
#[error(transparent)]
SshExportError(#[from] bitwarden_ssh::error::SshKeyExportError),
}
18 changes: 18 additions & 0 deletions crates/bitwarden-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ impl Client {

Ok(res.text().await.map_err(bitwarden_core::Error::Reqwest)?)
}

pub fn generate_ssh_key(
&self,
key_algorithm: bitwarden_ssh::generator::KeyAlgorithm,
) -> Result<bitwarden_ssh::SshKey> {
let test = bitwarden_ssh::generator::generate_sshkey(key_algorithm);
let a = test.map_err(|e| error::BitwardenError::E(error::Error::SshGenerationError(e)));
a
}

pub fn import_ssh_key(
&self,
imported_key: String,
password: Option<String>,
) -> Result<bitwarden_ssh::SshKey> {
bitwarden_ssh::import::import_key(imported_key, password)
.map_err(|e| error::BitwardenError::E(error::Error::SshImportError(e)))
}
}

fn init_logger() {
Expand Down

0 comments on commit 56dcd48

Please sign in to comment.