From e64fb1642568f5ee8c079637616e4e688b0234fd Mon Sep 17 00:00:00 2001 From: Rajdeep Sengupta Date: Fri, 3 May 2024 16:02:29 +0530 Subject: [PATCH] Fixed clones --- src/core/dek.rs | 32 ++++++++++++++++---------------- src/core/session.rs | 5 ++--- src/core/user.rs | 13 +------------ 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/core/dek.rs b/src/core/dek.rs index aca03cd..6afe0b4 100644 --- a/src/core/dek.rs +++ b/src/core/dek.rs @@ -55,6 +55,22 @@ impl Dek { let key_iv = format!("{}.{}", hex_key, hex_iv); return key_iv; } + + pub async fn encrypt_and_add(&self, mongo_client: &Client) -> Result { + let db = mongo_client.database("test"); + let collection_dek: Collection = db.collection("deks"); + + let server_kek = env::var("SERVER_KEK").expect("Server Kek must be set."); + + let encrypted_dek = self.encrypt(&server_kek); + + match collection_dek.insert_one(&encrypted_dek, None).await { + Ok(_) => return Ok(self.clone()), + Err(e) => return Err(Error::ServerError { + message: e.to_string(), + }), + } + } pub async fn get(mongo_client: &Client, identifier: &str) -> Result { let db = mongo_client.database("test"); @@ -113,20 +129,4 @@ impl Dek { } } } - - pub async fn encrypt_and_add(&self, mongo_client: &Client) -> Result { - let db = mongo_client.database("test"); - let collection_dek: Collection = db.collection("deks"); - - let server_kek = env::var("SERVER_KEK").expect("Server Kek must be set."); - - let encrypted_dek = self.clone().encrypt(&server_kek); - - match collection_dek.insert_one(&encrypted_dek, None).await { - Ok(_) => return Ok(self.clone()), - Err(e) => return Err(Error::ServerError { - message: e.to_string(), - }), - } - } } diff --git a/src/core/session.rs b/src/core/session.rs index ef63772..f890f32 100644 --- a/src/core/session.rs +++ b/src/core/session.rs @@ -31,11 +31,10 @@ impl Session { let db = mongo_client.database("test"); let collection_session: Collection = db.collection("sessions"); - let session = self.clone(); - let encrypted_session = session.encrypt(key); + let encrypted_session = self.encrypt(key); match collection_session.insert_one(encrypted_session, None).await { - Ok(_) => Ok(session), + Ok(_) => Ok(self.clone()), Err(e) => Err(Error::ServerError { message: e.to_string(), }), diff --git a/src/core/user.rs b/src/core/user.rs index 825dadd..f86d4a2 100644 --- a/src/core/user.rs +++ b/src/core/user.rs @@ -51,18 +51,7 @@ impl User { user.password = salt_and_hash_password(user.password.as_str()); let collection: Collection = db.collection("users"); match collection.insert_one(user.encrypt(&dek), None).await { - Ok(_) => return Ok(Self { - _id: self._id, - uid: self.uid.clone(), - name: self.name.clone(), - email: self.email.clone(), - role: self.role.clone(), - password: self.password.clone(), - created_at: self.created_at, - updated_at: self.updated_at, - email_verified: self.email_verified, - is_active: self.is_active, - }), + Ok(_) => return Ok(user), Err(_) => { return Err(Error::ServerError { message: "Failed to Insert User".to_string(),