Skip to content

Commit

Permalink
refactor: fixed clippy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Todd committed Mar 25, 2024
1 parent 6b31592 commit e09436d
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 28 deletions.
12 changes: 9 additions & 3 deletions at_chops/src/default_crypto_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ impl DefaultCryptoFunctions {
}
}

impl Default for DefaultCryptoFunctions {
fn default() -> Self {
Self::new()
}
}

impl CryptoFunctions for DefaultCryptoFunctions {
// ----- Base64 -----
fn base64_encode(&self, data: &[u8]) -> String {
Expand All @@ -33,13 +39,13 @@ impl CryptoFunctions for DefaultCryptoFunctions {

// ----- RSA -----
fn construct_rsa_private_key(&self, key: &[u8]) -> Result<RsaPrivateKey> {
let rsa_private_key = RsaPrivateKey::from_pkcs8_der(key.as_ref())?;
let rsa_private_key = RsaPrivateKey::from_pkcs8_der(key)?;
rsa_private_key.validate()?;
Ok(rsa_private_key)
}

fn construct_rsa_public_key(&self, key: &[u8]) -> Result<RsaPublicKey> {
let rsa_public_key = RsaPublicKey::from_public_key_der(key.as_ref())?;
let rsa_public_key = RsaPublicKey::from_public_key_der(key)?;
Ok(rsa_public_key)
}

Expand Down Expand Up @@ -80,7 +86,7 @@ impl CryptoFunctions for DefaultCryptoFunctions {

// ----- AES -----
fn construct_aes_cipher(&self, key: &[u8], iv: &[u8; 16]) -> Result<Box<dyn StreamCipher>> {
let key = GenericArray::from_slice(key.as_ref());
let key = GenericArray::from_slice(key);
let nonce = GenericArray::from_slice(iv);
let cipher = Ctr128BE::<Aes256>::new(key, nonce);
Ok(Box::new(cipher))
Expand Down
12 changes: 4 additions & 8 deletions at_chops/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub struct AtChops {
rsa_public_key: RsaPublicKey,
/// Used for authenticating with AtServer by signing challenges.
pkam_private_key: RsaPrivateKey,
/// Pretty sure this is never used.
pkam_public_key: RsaPublicKey,
//? Need to check that some of the functions should return String::from_utf8() instead of String created from base64encode
//? In the future it probably makes sense to get rid of public keys entirely as the private keys are enough to derive them
}
Expand All @@ -29,7 +27,7 @@ impl AtChops {
encoded_and_encrypted_pkam_private_key: &str,
) -> Result<Self> {
let decrypted_private_key = Self::decrypt_private_key(
&crypto_service,
crypto_service.as_ref(),
encoded_and_encrypted_private_key,
encoded_self_encryption_key,
)?;
Expand All @@ -40,7 +38,7 @@ impl AtChops {
let rsa_private_key = crypto_service.construct_rsa_private_key(&decrypted_private_key)?;
let rsa_public_key = rsa_private_key.to_public_key();
let decrypted_pkam_private_key = Self::decrypt_private_key(
&crypto_service,
crypto_service.as_ref(),
encoded_and_encrypted_pkam_private_key,
encoded_self_encryption_key,
)?;
Expand All @@ -50,22 +48,20 @@ impl AtChops {
);
let pkam_private_key =
crypto_service.construct_rsa_private_key(&decrypted_pkam_private_key)?;
let pkam_public_key = pkam_private_key.to_public_key();
debug!("AtChops initialized");
Ok(Self {
crypto_service,
rsa_private_key,
rsa_public_key,
pkam_private_key,
pkam_public_key,
})
}

// Note: Anything that is a `String` or `&str` is base64 encoded. Anything that is a `Vec<u8>` is byte data.

/// Helper method to decrypt the private key using the self encryption key.
fn decrypt_private_key(
crypto_service: &Box<dyn CryptoFunctions>,
crypto_service: &dyn CryptoFunctions,
encoded_and_encrypted_private_key: &str,
encoded_self_encryption_key: &str,
) -> Result<Vec<u8>> {
Expand All @@ -83,7 +79,7 @@ impl AtChops {
};
//? The key was originally a string?
let string_result = String::from_utf8(unpadded_data)?;
let result = crypto_service.base64_decode(&string_result.as_bytes())?;
let result = crypto_service.base64_decode(string_result.as_bytes())?;
Ok(result)
}

Expand Down
2 changes: 1 addition & 1 deletion at_records/src/at_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Display for AtKey {
buffer.push_str("private:");
}
Visibility::Internal => {
buffer.push_str("_");
buffer.push('_');
}
Visibility::Shared(shared_with) => {
buffer.push_str(&format!("{}:", shared_with.get_at_sign_with_prefix()));
Expand Down
3 changes: 3 additions & 0 deletions at_records/src/at_record.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(unused_variables)]
#![allow(dead_code)]

use at_chops::AtChops;

use crate::{at_key::AtKey, record_metadata::RecordMetadata};
Expand Down
3 changes: 3 additions & 0 deletions at_records/src/record_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(unused_variables)]
#![allow(dead_code)]

#[derive(Debug)]
pub struct RecordMetadata {
/// A Date and Time derived from the ttb (now + ttb). A Key should be only available after availableFrom.
Expand Down
10 changes: 5 additions & 5 deletions at_secrets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ impl AtSecrets {
// Get the keys
let aes_pkam_public_key = v["aesPkamPublicKey"]
.as_str()
.ok_or_else(|| "Unable to find aesPkamPublicKey")?
.ok_or("Unable to find aesPkamPublicKey")?
.to_owned();
let aes_pkam_private_key = v["aesPkamPrivateKey"]
.as_str()
.ok_or_else(|| "Unable to find aesPkamPrivateKey")?
.ok_or("Unable to find aesPkamPrivateKey")?
.to_owned();
let aes_encrypt_public_key = v["aesEncryptPublicKey"]
.as_str()
.ok_or_else(|| "Unable to find aesEncryptPublicKey")?
.ok_or("Unable to find aesEncryptPublicKey")?
.to_owned();
let aes_encrypt_private_key = v["aesEncryptPrivateKey"]
.as_str()
.ok_or_else(|| "Unable to find aesEncryptPrivateKey")?
.ok_or("Unable to find aesEncryptPrivateKey")?
.to_owned();
let aes_self_encrypt_key = v["selfEncryptionKey"]
.as_str()
.ok_or_else(|| "Unable to find selfEncryptionKey")?
.ok_or("Unable to find selfEncryptionKey")?
.to_owned();

Ok(AtSecrets::new(
Expand Down
2 changes: 1 addition & 1 deletion at_verbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ mod prelude {
pub use crate::verb_trait::Verb;
pub use at_errors::{AtError, Result};
pub use at_tls::TlsClient;
pub use log::{debug, error, info};
pub use log::{debug, error};
}
6 changes: 1 addition & 5 deletions at_verbs/src/lookup_verb.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use at_records::{
at_key::AtKey,
at_record::{AtRecord, AtValue},
record_metadata::RecordMetadata,
};
use at_records::at_key::AtKey;

use super::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion at_verbs/src/update_verb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'a> Verb<'a> for UpdateVerb {

let value = match input.value {
AtValue::Text(text) => text,
AtValue::Binary(data) => todo!(),
AtValue::Binary(_) => todo!(),
};

string_buf.push_str(format!(" {}", value).as_str());
Expand Down
2 changes: 1 addition & 1 deletion at_verbs/src/verb_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub trait Verb<'a> {
// Check that it doesn't contain error codes
if response.starts_with("error") {
let code = response
.split_once(":")
.split_once(':')
.ok_or(AtError::UnknownAtClientException(String::from(
"Unexpected formatting of error message from server",
)))?
Expand Down
6 changes: 3 additions & 3 deletions src/at_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl AtClient {
AtValue::Text(text) => {
let encrypted_data = self
.at_chops
.encrypt_data_with_shared_symmetric_key(&new_symm_key, &text)?;
.encrypt_data_with_shared_symmetric_key(&new_symm_key, text)?;
let encrypted_data = AtValue::Text(encrypted_data);
let update_verb_args = UpdateVerbInputs::new(at_key, &encrypted_data);
let result = UpdateVerb::execute(&mut self.tls_client, update_verb_args)?;
Expand All @@ -260,7 +260,7 @@ impl AtClient {
AtValue::Text(text) => {
let encrypted_data = self
.at_chops
.encrypt_data_with_shared_symmetric_key(&symm_key, &text)?;
.encrypt_data_with_shared_symmetric_key(&symm_key, text)?;
let encrypted_data = AtValue::Text(encrypted_data);
let update_verb_args = UpdateVerbInputs::new(at_key, &encrypted_data);
let result = UpdateVerb::execute(&mut self.tls_client, update_verb_args)?;
Expand All @@ -276,7 +276,7 @@ impl AtClient {
}
}

pub fn put_metadata(&mut self, at_key: &AtKey, metadata: &RecordMetadata) -> Result<String> {
pub fn put_metadata(&mut self, _at_key: &AtKey, _metadata: &RecordMetadata) -> Result<String> {
// let update_verb_args = UpdateVerbInputs::new(at_key, &AtValue::Metadata(metadata.clone()));
// let result = UpdateVerb::execute(&mut self.tls_client, update_verb_args)?;
// Ok(result)
Expand Down

0 comments on commit e09436d

Please sign in to comment.