Skip to content

Commit

Permalink
Merge pull request #26 from Rajdip019/rajdeep/session
Browse files Browse the repository at this point in the history
Fixed clones
  • Loading branch information
Rajdip019 authored May 3, 2024
2 parents cea718e + e64fb16 commit c55e13f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
32 changes: 16 additions & 16 deletions src/core/dek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let db = mongo_client.database("test");
let collection_dek: Collection<Dek> = 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<Self> {
let db = mongo_client.database("test");
Expand Down Expand Up @@ -113,20 +129,4 @@ impl Dek {
}
}
}

pub async fn encrypt_and_add(&self, mongo_client: &Client) -> Result<Self> {
let db = mongo_client.database("test");
let collection_dek: Collection<Dek> = 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(),
}),
}
}
}
5 changes: 2 additions & 3 deletions src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ impl Session {
let db = mongo_client.database("test");
let collection_session: Collection<Session> = 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(),
}),
Expand Down
13 changes: 1 addition & 12 deletions src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,7 @@ impl User {
user.password = salt_and_hash_password(user.password.as_str());
let collection: Collection<User> = 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(),
Expand Down

0 comments on commit c55e13f

Please sign in to comment.