From 040cd909f6dfa448c6afa60b5c3095e120334468 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Mon, 13 May 2024 16:34:18 -0700 Subject: [PATCH] Lint and fix --- crates/credentials/src/vc.rs | 2 +- crates/dids/src/bearer.rs | 8 ++-- crates/dids/src/methods/jwk.rs | 2 +- crates/jws/src/lib.rs | 40 ++++++++-------- crates/jwt/src/jws.rs | 4 +- .../keys/src/key_manager/local_key_manager.rs | 46 +++++++++++++------ crates/keys/src/key_manager/mod.rs | 7 +-- 7 files changed, 62 insertions(+), 47 deletions(-) diff --git a/crates/credentials/src/vc.rs b/crates/credentials/src/vc.rs index a9171268..7e86562a 100644 --- a/crates/credentials/src/vc.rs +++ b/crates/credentials/src/vc.rs @@ -173,7 +173,7 @@ mod test { use uuid::Uuid; fn create_bearer_did() -> BearerDid { - let key_manager = Arc::new(LocalKeyManager::new_in_memory()); + let key_manager = Arc::new(LocalKeyManager::new()); let options = DidJwkCreateOptions { curve: Curve::Ed25519, }; diff --git a/crates/dids/src/bearer.rs b/crates/dids/src/bearer.rs index aad79450..3a562558 100644 --- a/crates/dids/src/bearer.rs +++ b/crates/dids/src/bearer.rs @@ -77,7 +77,7 @@ mod test { #[tokio::test] async fn test_from_key_manager() { - let key_manager = Arc::new(LocalKeyManager::new_in_memory()); + let key_manager = Arc::new(LocalKeyManager::new()); let options = DidJwkCreateOptions { curve: Curve::Ed25519, }; @@ -85,10 +85,10 @@ mod test { let private_keys = key_manager.export_private_keys().unwrap(); let bearer_did = - BearerDid::from_key_manager(&did_jwk_bearer_did.identifier.uri, key_manager) + BearerDid::from_key_manager(&did_jwk_bearer_did.identifier.uri, key_manager.clone()) .await .unwrap(); - let bearer_did_private_keys = bearer_did.key_manager.export_private_keys().unwrap(); + let bearer_did_private_keys = key_manager.export_private_keys().unwrap(); assert_eq!(bearer_did.identifier.uri, did_jwk_bearer_did.identifier.uri); assert_eq!(private_keys.len(), bearer_did_private_keys.len()); @@ -100,7 +100,7 @@ mod test { #[test] fn test_sign() { - let key_manager = Arc::new(LocalKeyManager::new_in_memory()); + let key_manager = Arc::new(LocalKeyManager::new()); let options = DidJwkCreateOptions { curve: Curve::Ed25519, }; diff --git a/crates/dids/src/methods/jwk.rs b/crates/dids/src/methods/jwk.rs index 4c09c0c2..86f7da04 100644 --- a/crates/dids/src/methods/jwk.rs +++ b/crates/dids/src/methods/jwk.rs @@ -105,7 +105,7 @@ mod tests { use keys::key_manager::local_key_manager::LocalKeyManager; fn create_did_jwk() -> BearerDid { - let key_manager = Arc::new(LocalKeyManager::new_in_memory()); + let key_manager = Arc::new(LocalKeyManager::new()); let options = DidJwkCreateOptions { curve: Curve::Ed25519, }; diff --git a/crates/jws/src/lib.rs b/crates/jws/src/lib.rs index ff24e9b5..a018230a 100644 --- a/crates/jws/src/lib.rs +++ b/crates/jws/src/lib.rs @@ -149,9 +149,9 @@ mod tests { #[tokio::test] async fn test_jws_sign_and_verify() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -191,9 +191,9 @@ mod tests { #[test] fn test_jws_decode() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -242,9 +242,9 @@ mod tests { #[tokio::test] async fn test_jws_verify_malformed_header_alg_error() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -283,9 +283,9 @@ mod tests { #[tokio::test] async fn test_jws_verify_malformed_header_kid_error() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -324,9 +324,9 @@ mod tests { #[tokio::test] async fn test_jws_verify_algorithm_not_supported_error() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -365,9 +365,9 @@ mod tests { #[tokio::test] async fn test_jws_verify_resolution_error() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -406,9 +406,9 @@ mod tests { #[tokio::test] async fn test_jws_verify_document_verification_method_error() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -447,9 +447,9 @@ mod tests { #[tokio::test] async fn test_jws_verify_signature_decode_error() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -489,9 +489,9 @@ mod tests { #[tokio::test] async fn test_jws_verify_signature_crypto_error() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, @@ -513,9 +513,9 @@ mod tests { .to_string() .into_bytes(); - let invalid_key_manager = LocalKeyManager::new_in_memory(); + let invalid_key_manager = Arc::new(LocalKeyManager::new()); let invalid_bearer_did = DidJwk::create( - Arc::new(invalid_key_manager), + invalid_key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, diff --git a/crates/jwt/src/jws.rs b/crates/jwt/src/jws.rs index f7203f6d..f9256c37 100644 --- a/crates/jwt/src/jws.rs +++ b/crates/jwt/src/jws.rs @@ -84,9 +84,9 @@ mod tests { #[tokio::test] async fn test_sign_and_verify() { - let key_manager = LocalKeyManager::new_in_memory(); + let key_manager = Arc::new(LocalKeyManager::new()); let bearer_did = DidJwk::create( - Arc::new(key_manager), + key_manager, DidJwkCreateOptions { curve: Curve::Ed25519, }, diff --git a/crates/keys/src/key_manager/local_key_manager.rs b/crates/keys/src/key_manager/local_key_manager.rs index e0b123cb..653ed21f 100644 --- a/crates/keys/src/key_manager/local_key_manager.rs +++ b/crates/keys/src/key_manager/local_key_manager.rs @@ -22,6 +22,14 @@ impl LocalKeyManager { map: RwLock::new(HashMap::new()), } } + + pub fn export_private_keys(&self) -> Result>, KeyManagerError> { + let map_lock = self.map.read().map_err(|e| { + KeyManagerError::InternalKeyStoreError(format!("Unable to acquire Mutex lock: {}", e)) + })?; + let private_keys = map_lock.values().cloned().collect(); + Ok(private_keys) + } } impl KeyManager for LocalKeyManager { @@ -57,9 +65,13 @@ impl KeyManager for LocalKeyManager { } fn sign(&self, key_alias: &str, payload: &[u8]) -> Result, KeyManagerError> { - let map_lock: std::sync::RwLockReadGuard>> = self.map.read().map_err(|e| { - KeyManagerError::InternalKeyStoreError(format!("Unable to acquire Mutex lock: {}", e)) - })?; + let map_lock: std::sync::RwLockReadGuard>> = + self.map.read().map_err(|e| { + KeyManagerError::InternalKeyStoreError(format!( + "Unable to acquire Mutex lock: {}", + e + )) + })?; let private_key = map_lock .get(key_alias) .ok_or(KeyManagerError::KeyNotFound(key_alias.to_string()))?; @@ -86,15 +98,12 @@ impl KeyImporter for LocalKeyManager { } } - - impl Default for LocalKeyManager { fn default() -> Self { Self::new() } } - #[cfg(test)] mod tests { use crate::key::Key; @@ -135,7 +144,7 @@ mod tests { #[test] fn test_sign() { - let key_manager: LocalKeyManager = LocalKeyManager::new(); + let key_manager = LocalKeyManager::new(); let key_alias = key_manager .generate_private_key(Curve::Ed25519, None) .unwrap(); @@ -154,15 +163,21 @@ mod tests { let key_manager = LocalKeyManager::new(); let private_key = Arc::new(Ed25519::generate().unwrap()); - let key_alias = key_manager.import(private_key.clone()).expect("Failed to import private key"); - let default_alias = private_key.alias().expect("Failed to generate private key alias"); + let key_alias = key_manager + .import(private_key.clone()) + .expect("Failed to import private key"); + let default_alias = private_key + .alias() + .expect("Failed to generate private key alias"); assert_eq!(key_alias, default_alias); - let key_manager_public_key = key_manager.get_public_key(&key_alias) + let key_manager_public_key = key_manager + .get_public_key(&key_alias) .expect("Failed to get public key") .jwk() .expect("Failed to get alias of public key"); - let public_key = private_key.to_public() + let public_key = private_key + .to_public() .expect("Failed to convert private key to public") .jwk() .expect("Failed to get alias of public key"); @@ -175,14 +190,17 @@ mod tests { let private_key = Arc::new(Ed25519::generate().unwrap()); let key_alias = "1234".to_string(); - key_manager.import_with_alias(private_key.clone(), &key_alias) + key_manager + .import_with_alias(private_key.clone(), &key_alias) .expect("Failed to import private key with alias"); - let key_manager_public_key = key_manager.get_public_key(&key_alias) + let key_manager_public_key = key_manager + .get_public_key(&key_alias) .expect("Failed to get public key") .jwk() .expect("Failed to get alias of public key"); - let public_key = private_key.to_public() + let public_key = private_key + .to_public() .expect("Failed to convert private key to public") .jwk() .expect("Failed to get alias of public key"); diff --git a/crates/keys/src/key_manager/mod.rs b/crates/keys/src/key_manager/mod.rs index fd07e1d8..b84b66c5 100644 --- a/crates/keys/src/key_manager/mod.rs +++ b/crates/keys/src/key_manager/mod.rs @@ -55,12 +55,9 @@ pub trait KeyImporter: KeyManager { /// Imports a private key into the key manager using private_key.alias() as the alias /// Returns the key alias - fn import( - &self, - private_key: Arc - ) -> Result { + fn import(&self, private_key: Arc) -> Result { let key_alias = private_key.alias()?; self.import_with_alias(private_key, &key_alias)?; Ok(key_alias) } -} \ No newline at end of file +}