Skip to content

Commit

Permalink
Fix clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
quexten committed Nov 21, 2024
1 parent c9172d8 commit 9419971
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions crates/bitwarden-ssh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ keywords.workspace = true

[features]
uniffi = ["dep:uniffi", "bitwarden-core/uniffi", "bitwarden-vault/uniffi"]
wasm = [
"bitwarden-error/wasm",
"dep:wasm-bindgen",
"dep:js-sys",
"dep:tsify-next",
] # WASM support

[dependencies]
async-trait = ">=0.1.80, <0.2"
base64 = ">=0.22.1, <0.23"
bitwarden-core = { workspace = true }
bitwarden-crypto = { workspace = true }
bitwarden-error = { workspace = true }
bitwarden-vault = { workspace = true }
chrono = { workspace = true }
coset = { version = "0.3.7" }
Expand All @@ -38,6 +45,9 @@ ssh-key = { version = "0.6.7", features = ["ed25519", "encryption", "rsa", "getr
thiserror = { workspace = true }
uniffi = { workspace = true, optional = true }
uuid = { workspace = true }
wasm-bindgen = { workspace = true, optional = true }
tsify-next = { workspace = true, optional = true }
js-sys = { workspace = true, optional = true }

[lints]
workspace = true
2 changes: 2 additions & 0 deletions crates/bitwarden-ssh/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use thiserror::Error;
use bitwarden_error::prelude::*;

#[bitwarden_error(flat)]
#[derive(Error, Debug)]
pub enum KeyGenerationError {
#[error("Failed to generate key: {0}")]
Expand Down
12 changes: 6 additions & 6 deletions crates/bitwarden-ssh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
use ssh_key::{Algorithm, HashAlg, LineEnding};

mod error;
pub mod error;
pub mod models;

pub enum KeyAlgorithm {
Expand All @@ -11,7 +11,7 @@ pub enum KeyAlgorithm {
Rsa4096,
}

pub async fn generate_keypair(
pub fn generate_keypair(
key_algorithm: KeyAlgorithm,
) -> Result<models::SshKey, error::KeyGenerationError> {
// sourced from cryptographically secure entropy source, with sources for all targets: https://docs.rs/getrandom
Expand All @@ -28,21 +28,21 @@ pub async fn generate_keypair(
};

let rsa_keypair = ssh_key::private::RsaKeypair::random(&mut rng, bits)
.or_else(|e| Err(error::KeyGenerationError::KeyGenerationError(e.to_string())))?;
.map_err(|e| error::KeyGenerationError::KeyGenerationError(e.to_string()))?;

let private_key = ssh_key::PrivateKey::new(
ssh_key::private::KeypairData::from(rsa_keypair),
"".to_string(),
)
.or_else(|e| Err(error::KeyGenerationError::KeyGenerationError(e.to_string())))?;
.map_err(|e| error::KeyGenerationError::KeyGenerationError(e.to_string()))?;
Ok(private_key)
}
}
.or_else(|e| Err(error::KeyGenerationError::KeyGenerationError(e.to_string())))?;
.map_err(|e| error::KeyGenerationError::KeyGenerationError(e.to_string()))?;

let private_key_openssh = key
.to_openssh(LineEnding::LF)
.or_else(|e| Err(error::KeyGenerationError::KeyConversionError(e.to_string())))?;
.map_err(|e| error::KeyGenerationError::KeyConversionError(e.to_string()))?;
Ok(models::SshKey {
private_key: private_key_openssh.to_string(),
public_key: key.public_key().to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-wasm-internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bitwarden-core = { workspace = true, features = ["wasm", "internal"] }
bitwarden-crypto = { workspace = true, features = ["wasm"] }
bitwarden-error = { version = "1.0.0", path = "../bitwarden-error" }
bitwarden-vault = { workspace = true, features = ["wasm"] }
bitwarden-ssh = { workspace = true }
bitwarden-ssh = { workspace = true, features = ["wasm"] }
console_error_panic_hook = "0.1.7"
console_log = { version = "1.0.0", features = ["color"] }
js-sys = "0.3.68"
Expand Down
6 changes: 1 addition & 5 deletions crates/bitwarden-wasm-internal/src/ssh.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use bitwarden_ssh;
use wasm_bindgen::prelude::*;

use crate::error::{GenericError, Result};

#[wasm_bindgen]
pub enum KeyAlgorithm {
Ed25519,
Expand Down Expand Up @@ -53,9 +51,7 @@ impl SshKey {
}

#[wasm_bindgen]
pub async fn generate_ssh_key(key_algorithm: KeyAlgorithm) -> Result<SshKey> {
pub fn generate_ssh_key(key_algorithm: KeyAlgorithm) -> Result<SshKey, bitwarden_ssh::error::KeyGenerationError> {
bitwarden_ssh::generate_keypair(key_algorithm.into())
.await
.map(|key| SshKey::from(key))
.map_err(|e| GenericError(e.to_string()))
}

0 comments on commit 9419971

Please sign in to comment.