Skip to content

Commit

Permalink
add ability to set environment name
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyp committed Jul 5, 2023
1 parent cf38724 commit da134a5
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 150 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "localencrypt"
version = "0.0.3-pre"
version = "0.0.3"
authors = ["Anthony Potappel <[email protected]>"]
edition = "2021"

Expand Down
11 changes: 6 additions & 5 deletions src/api/local_encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod tests {
// Using builder to set up LocalEncrypt with localStorage backend
let username = "test_builder_local_storage_backend";
let password = "password";
let credentials = Credentials::new(username, password);
let credentials = Credentials::new(None, username, password);
let local_storage = LocalStorage::new(credentials)
.await
.expect("Failed to create local storage backend");
Expand Down Expand Up @@ -101,7 +101,7 @@ mod tests {
let local_encrypt2 = LocalEncrypt::new();
assert_eq!(local_encrypt1, local_encrypt2);

let credentials = Credentials::new("username", "password");
let credentials = Credentials::new(None, "username", "password");
let local_storage = LocalStorage::new(credentials)
.await
.expect("Failed to create local storage backend");
Expand All @@ -118,12 +118,13 @@ mod tests {
// Case with password
let username = "test_initiate_with_local_storage";
let password = "password";
let result = StorageBackend::initiate_with_local_storage(username, Some(password)).await;
let result =
StorageBackend::initiate_with_local_storage(None, username, Some(password)).await;
assert!(result.is_ok());

// Case without password
let username = "username";
let result = StorageBackend::initiate_with_local_storage(username, None).await;
let result = StorageBackend::initiate_with_local_storage(None, username, None).await;
assert!(result.is_ok());
}

Expand All @@ -132,7 +133,7 @@ mod tests {
// Case with Browser backend
let username = "test_hard_reset";
let password = "password";
let backend = StorageBackend::initiate_with_local_storage(username, Some(password))
let backend = StorageBackend::initiate_with_local_storage(None, username, Some(password))
.await
.expect("Failed to initiate local storage backend");
let result = backend.hard_reset().await;
Expand Down
3 changes: 2 additions & 1 deletion src/api/local_encrypt_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ impl LocalEncryptJs {
_ = console_log::init_with_level(log::Level::Debug);
let future = async move {
let storage_backend =
StorageBackend::initiate_with_local_storage(&username, Some(&password)).await?;
StorageBackend::initiate_with_local_storage(None, &username, Some(&password))
.await?;
let local_encrypt = LocalEncrypt::builder()
.with_backend(storage_backend)
.build();
Expand Down
15 changes: 12 additions & 3 deletions src/common/credentials.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const DEFAULT_ENVIRONMENT: &str = "LocalEncrypt";

#[derive(Clone, PartialEq)]
pub struct Credentials {
environment: String,
username: String,
password: String,
}

impl Credentials {
pub fn new(username: &str, password: &str) -> Self {
pub fn new(environment: Option<&str>, username: &str, password: &str) -> Self {
Self {
environment: environment.unwrap_or(DEFAULT_ENVIRONMENT).to_string(),
username: username.to_string(),
password: password.to_string(),
}
}

pub fn environment(&self) -> String {
self.environment.clone()
}

pub fn username(&self) -> String {
self.username.clone()
}
Expand All @@ -26,6 +34,7 @@ use std::fmt::{Debug, Formatter};
impl Debug for Credentials {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Credentials")
.field("environment", &self.environment)
.field("username", &self.username)
.field("password", &"********")
.finish()
Expand All @@ -44,7 +53,7 @@ mod tests {
async fn test_credentials_new() {
let username = "username";
let password = "password";
let credentials = Credentials::new(username, password);
let credentials = Credentials::new(None, username, password);
assert_eq!(credentials.username(), username);
assert_eq!(credentials.password(), password);
}
Expand All @@ -53,7 +62,7 @@ mod tests {
async fn test_credentials_debug() {
let username = "username";
let password = "secretpassword";
let credentials = Credentials::new(username, password);
let credentials = Credentials::new(None, username, password);
let debug_str = format!("{:?}", credentials);
assert!(debug_str.contains("username"));
assert!(debug_str.contains("********"));
Expand Down
51 changes: 11 additions & 40 deletions src/common/object_key.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
use crate::SecureStringError;

const DEFAULT_ENVIRONMENT: &str = "LocalEncrypt";

#[derive(Clone, PartialEq, Debug)]
pub struct ObjectKey {
environment: String,
tag: String,
id: String,
}

impl ObjectKey {
pub fn new(tag: &str, id: &str) -> Result<Self, SecureStringError> {
pub fn new(environment: &str, tag: &str, id: &str) -> Result<Self, SecureStringError> {
if id.is_empty() {
return Err(SecureStringError::InvalidArgument(String::from(
"Tag and ID must not be empty",
)));
}

Ok(Self {
environment: environment.to_string(),
tag: tag.to_string(),
id: id.to_string(),
})
}

pub fn new_with_form_tag(id: &str) -> Result<Self, SecureStringError> {
if id.is_empty() {
return Err(SecureStringError::InvalidArgument(String::from(
"ID must not be empty",
)));
}

Ok(Self {
tag: "".to_string(),
id: id.to_string(),
})
pub fn environment(&self) -> String {
self.environment.clone()
}

pub fn tag(&self) -> String {
Expand All @@ -50,22 +45,15 @@ mod tests {

#[wasm_bindgen_test]
fn test_object_key_new() {
let object_key = ObjectKey::new("test", "test_id").unwrap();
let object_key = ObjectKey::new("debug", "test", "test_id").unwrap();
assert_eq!(object_key.tag(), "test");
assert_eq!(object_key.id(), "test_id");
}

#[wasm_bindgen_test]
fn test_object_key_new_with_form_tag() {
let object_key = ObjectKey::new_with_form_tag("test_id").unwrap();
assert_eq!(object_key.tag(), "");
assert_eq!(object_key.id(), "test_id");
}

#[wasm_bindgen_test]
async fn test_invalid_object_key_creation() {
// Check that ObjectKey::new returns an error when given an empty id
let object_key_empty_id = ObjectKey::new("test_tag", "");
let object_key_empty_id = ObjectKey::new("debug", "test_tag", "");
assert!(
object_key_empty_id.is_err(),
"Successfully created ObjectKey with empty id"
Expand All @@ -74,7 +62,7 @@ mod tests {

#[wasm_bindgen_test]
fn test_object_key_new_empty_tag_and_id() {
let object_key = ObjectKey::new("", "");
let object_key = ObjectKey::new("debug", "", "");
assert!(
object_key.is_err(),
"Successfully created ObjectKey with empty tag and id"
Expand All @@ -89,26 +77,9 @@ mod tests {
}
}

#[wasm_bindgen_test]
fn test_object_key_new_with_form_tag_empty_id() {
let object_key = ObjectKey::new_with_form_tag("");
assert!(
object_key.is_err(),
"Successfully created ObjectKey with form tag and empty id"
);
match object_key {
Err(SecureStringError::InvalidArgument(msg)) => {
assert_eq!(msg, "ID must not be empty");
}
_ => {
panic!("Unexpected result from ObjectKey::new_with_form_tag with empty id");
}
}
}

#[wasm_bindgen_test]
fn test_object_key_new_error_message() {
let object_key = ObjectKey::new("test", "");
let object_key = ObjectKey::new("debug", "test", "");
assert!(
object_key.is_err(),
"Successfully created ObjectKey with empty id"
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod tests {

#[wasm_bindgen_test]
async fn test_derive_key_same_password() {
let object_key = ObjectKey::new("crypto_test1", "crypto_test_id1").unwrap();
let object_key = ObjectKey::new("debug", "crypto_test1", "crypto_test_id1").unwrap();
let password = "password";

let result = derive_key_from_password(&object_key, password).await;
Expand Down Expand Up @@ -100,7 +100,7 @@ mod tests {

#[wasm_bindgen_test]
async fn test_derive_key_empty_password() {
let object_key = ObjectKey::new("crypto_test2", "crypto_test_id2").unwrap();
let object_key = ObjectKey::new("debug", "crypto_test2", "crypto_test_id2").unwrap();
let password_empty = "";

let result_empty = derive_key_from_password(&object_key, password_empty).await;
Expand All @@ -109,7 +109,7 @@ mod tests {

#[wasm_bindgen_test]
async fn test_derive_key_different_salt() {
let object_key = ObjectKey::new("crypto_test3", "crypto_test_id3").unwrap();
let object_key = ObjectKey::new("debug", "crypto_test3", "crypto_test_id3").unwrap();
let password = "password";

// Get the storage_key for saving the salt
Expand Down Expand Up @@ -138,7 +138,7 @@ mod tests {

#[wasm_bindgen_test]
async fn test_derive_key_same_salt_different_password() {
let object_key = ObjectKey::new("crypto_test4", "crypto_test_id4").unwrap();
let object_key = ObjectKey::new("debug", "crypto_test4", "crypto_test_id4").unwrap();

// Derive key with password1
let password1 = "password1";
Expand Down
Loading

0 comments on commit da134a5

Please sign in to comment.