Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Apr 5, 2024
1 parent 44b5d15 commit ef70a37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
9 changes: 6 additions & 3 deletions crates/bitwarden-crypto/src/keys/shareable_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use std::pin::Pin;

use aes::cipher::typenum::U64;
use generic_array::GenericArray;
use hmac::{Hmac, Mac};
use hmac::Mac;

use crate::{keys::SymmetricCryptoKey, util::hkdf_expand};
use crate::{
keys::SymmetricCryptoKey,
util::{hkdf_expand, PbkdfSha256Hmac},
};

/// Derive a shareable key using hkdf from secret and name.
///
Expand All @@ -19,7 +22,7 @@ pub fn derive_shareable_key(

// TODO: Are these the final `key` and `info` parameters or should we change them? I followed
// the pattern used for sends
let res = Hmac::<sha2::Sha256>::new_from_slice(format!("bitwarden-{}", name).as_bytes())
let res = PbkdfSha256Hmac::new_from_slice(format!("bitwarden-{}", name).as_bytes())
.expect("hmac new_from_slice should not fail")
.chain_update(secret)
.finalize()
Expand Down
23 changes: 14 additions & 9 deletions crates/bws/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,28 @@ impl ProfileKey {
pub(crate) const FILENAME: &str = "config";
pub(crate) const DIRECTORY: &str = ".bws";

pub(crate) fn get_config_path(config_file: Option<&Path>, ensure_folder_exists: bool) -> PathBuf {
let config_file = config_file.map(ToOwned::to_owned).unwrap_or_else(|| {
let base_dirs = BaseDirs::new().expect("A valid home directory should exist");
base_dirs.home_dir().join(DIRECTORY).join(FILENAME)
});
fn get_config_path(config_file: Option<&Path>, ensure_folder_exists: bool) -> Result<PathBuf> {
let config_file = match config_file {
Some(path) => path.to_owned(),
None => {
let Some(base_dirs) = BaseDirs::new() else {
bail!("A valid home directory doesn't exist");

Check warning on line 54 in crates/bws/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/config.rs#L54

Added line #L54 was not covered by tests
};
base_dirs.home_dir().join(DIRECTORY).join(FILENAME)
}
};

if ensure_folder_exists {
if let Some(parent_folder) = config_file.parent() {
std::fs::create_dir_all(parent_folder).expect("Parent directory should be writable");
std::fs::create_dir_all(parent_folder)?;

Check warning on line 62 in crates/bws/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/config.rs#L62

Added line #L62 was not covered by tests
}
}

config_file
Ok(config_file)
}

pub(crate) fn load_config(config_file: Option<&Path>, must_exist: bool) -> Result<Config> {
let file = get_config_path(config_file, false);
let file = get_config_path(config_file, false)?;

let content = match file.exists() {
true => read_to_string(file),
Expand All @@ -75,7 +80,7 @@ pub(crate) fn load_config(config_file: Option<&Path>, must_exist: bool) -> Resul
}

fn write_config(config: Config, config_file: Option<&Path>) -> Result<()> {
let file = get_config_path(config_file, true);
let file = get_config_path(config_file, true)?;

Check warning on line 83 in crates/bws/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/config.rs#L83

Added line #L83 was not covered by tests

let content = toml::to_string_pretty(&config)?;

Expand Down

0 comments on commit ef70a37

Please sign in to comment.