Skip to content

Commit

Permalink
Removed mut for encrypt decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajdip019 committed May 3, 2024
1 parent 1758dc1 commit 9c34acc
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/core/dek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Dek {
.unwrap();

match cursor_dek {
Some(mut data) => return Ok(data.decrypt(&server_kek)),
Some(data) => return Ok(data.decrypt(&server_kek)),
None => {
return Err(Error::KeyNotFound {
message: "DEK not found".to_string(),
Expand All @@ -103,7 +103,7 @@ impl Dek {
.unwrap();

match cursor_dek {
Some(mut data) => return Ok(data.decrypt(&server_kek)),
Some(data) => return Ok(data.decrypt(&server_kek)),
None => {
return Err(Error::KeyNotFound {
message: "DEK not found".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Session {
let db = mongo_client.database("test");
let collection_session: Collection<Session> = db.collection("sessions");

let mut session = self.clone();
let session = self.clone();
let encrypted_session = session.encrypt(key);

match collection_session.insert_one(encrypted_session, None).await {
Expand Down
12 changes: 6 additions & 6 deletions src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl User {
)
.await
{
Ok(Some(mut user)) => {
Ok(Some(user)) => {
let decrypted_user = user.decrypt(&dek_data.dek);
return Ok(decrypted_user);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ impl User {
)
.await
{
Ok(Some(mut user)) => {
Ok(Some(user)) => {
let decrypted_user = user.decrypt(&dek_data.dek);
return Ok(decrypted_user);
}
Expand All @@ -150,7 +150,7 @@ impl User {
let collection: Collection<User> = db.collection("users");
let collection_dek: Collection<Dek> = db.collection("deks");

// let mut cursor = collection.find(None, None).await.unwrap();
// let cursor = collection.find(None, None).await.unwrap();
let mut cursor_dek = collection_dek.find(None, None).await.unwrap();

let mut users = Vec::new();
Expand All @@ -159,7 +159,7 @@ impl User {
// iterate over the users and decrypt the data
while let Some(dek) = cursor_dek.next().await {
let dek_data: Dek = match dek {
Ok(mut data) => data.decrypt(&kek),
Ok(data) => data.decrypt(&kek),
Err(_) => {
return Err(Error::ServerError {
message: "Failed to get DEK".to_string(),
Expand All @@ -181,7 +181,7 @@ impl User {
.unwrap();

match cursor_user {
Some(mut user) => {
Some(user) => {
let user_data = user.decrypt(&dek_data.dek);

users.push(UserResponse {
Expand Down Expand Up @@ -317,7 +317,7 @@ impl User {
}
};
// get user
let mut user = match collection
let user = match collection
.find_one(
doc! { "email": Encryption::encrypt_data(&email, &dek_data.dek) },
None,
Expand Down
4 changes: 2 additions & 2 deletions src/traits/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use serde_json::Value;
use crate::utils::encryption_utils::Encryption;

pub trait Decrypt {
fn decrypt(&mut self, key: &str) -> Self;
fn decrypt(&self, key: &str) -> Self;
}

impl<T> Decrypt for T
where
T: Serialize + DeserializeOwned,
{
fn decrypt(&mut self, key: &str) -> Self {
fn decrypt(&self, key: &str) -> Self {
// Serialize the object to JSON
let json_str = serde_json::to_string(self).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions src/traits/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use serde_json::Value;
use crate::utils::encryption_utils::Encryption;

pub trait Encrypt {
fn encrypt(&mut self, key: &str) -> Self;
fn encrypt(&self, key: &str) -> Self;
}

impl<T> Encrypt for T
where
T: Serialize + DeserializeOwned,
{
fn encrypt(&mut self, key: &str) -> Self {
fn encrypt(&self, key: &str) -> Self {
// Serialize the object to JSON
let json_str = serde_json::to_string(self).unwrap();

Expand Down

0 comments on commit 9c34acc

Please sign in to comment.