Skip to content

Commit

Permalink
Merge pull request #59 from Rajdip019/dashboard-setup
Browse files Browse the repository at this point in the history
UID fixed
  • Loading branch information
Rajdip019 authored Jul 23, 2024
2 parents 3283736 + 4b1fa96 commit cbc4548
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/core/dek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ impl Dek {
let email_regex =
regex::Regex::new(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$").unwrap();
let is_email = email_regex.is_match(identifier);
let encrypted_identifier = Encryption::encrypt_data(&identifier, &server_kek);
match is_email {
true => {
// encrypt the email using kek
let encrypted_identifier = Encryption::encrypt_data(&identifier, &server_kek);
let cursor_dek = collection_dek
.find_one(
Some(doc! {
Expand All @@ -109,7 +109,7 @@ impl Dek {
let cursor_dek = collection_dek
.find_one(
Some(doc! {
"uid": encrypted_identifier.clone(),
"uid": identifier,
}),
None,
)
Expand Down
31 changes: 5 additions & 26 deletions src/core/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,11 @@ impl Session {
}
};

let encrypted_uid_dek = Encryption::encrypt_data(&dek_data.uid, &dek_data.dek);

println!("{:?}", encrypted_uid_dek);

// find the session in the sessions collection using the encrypted email to iterate over the sessions
let cursor_session = collection_session
.find_one(
Some(doc! {
"uid": encrypted_uid_dek,
"uid": &dek_data.uid,
}),
None,
)
Expand Down Expand Up @@ -346,12 +342,10 @@ impl Session {
Err(e) => return Err(e),
};

let encrypted_uid = Encryption::encrypt_data(uid, &dek_data.dek);

let mut cursor = collection_session
.find(
doc! {
"uid": encrypted_uid,
"uid": uid,
},
None,
)
Expand Down Expand Up @@ -398,11 +392,10 @@ impl Session {
Err(e) => return Err(e),
};

let encrypted_uid = Encryption::encrypt_data(uid, &dek_data.dek);
let encrypted_session_id = Encryption::encrypt_data(session_id, &dek_data.dek);

let session = match collection_session
.find_one(doc! {"uid": encrypted_uid, "session_id": encrypted_session_id}, None)
.find_one(doc! {"uid": &uid, "session_id": encrypted_session_id}, None)
.await
{
Ok(session) => {
Expand Down Expand Up @@ -441,15 +434,8 @@ impl Session {
let db = mongo_client.database("auth");
let collection_session: Collection<Session> = db.collection("sessions");

let dek_data = match Dek::get(mongo_client, uid).await {
Ok(dek) => dek,
Err(e) => return Err(e),
};

let encrypted_uid = Encryption::encrypt_data(uid, &dek_data.dek);

match collection_session
.update_many(doc! {"uid": encrypted_uid}, doc! {"$set": {"is_revoked": true}}, None)
.update_many(doc! {"uid": &uid }, doc! {"$set": {"is_revoked": true}}, None)
.await
{
Ok(_) => Ok(()),
Expand Down Expand Up @@ -514,15 +500,8 @@ impl Session {
let db = mongo_client.database("auth");
let collection_session: Collection<Session> = db.collection("sessions");

let dek_data = match Dek::get(mongo_client, uid).await {
Ok(dek) => dek,
Err(e) => return Err(e),
};

let encrypted_uid = Encryption::encrypt_data(uid, &dek_data.dek);

match collection_session
.delete_many(doc! {"uid": encrypted_uid}, None)
.delete_many(doc! {"uid": &uid }, None)
.await
{
Ok(_) => Ok(()),
Expand Down
20 changes: 10 additions & 10 deletions src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl User {
match user_collection
.find_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": dek_data.uid,
},
None,
)
Expand Down Expand Up @@ -110,7 +110,7 @@ impl User {
match collection
.find_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &uid,
},
None,
)
Expand Down Expand Up @@ -281,7 +281,7 @@ impl User {
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$set": {
Expand Down Expand Up @@ -331,7 +331,7 @@ impl User {
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$set": {
Expand Down Expand Up @@ -380,7 +380,7 @@ impl User {
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$inc": {
Expand Down Expand Up @@ -436,7 +436,7 @@ impl User {
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$set": {
Expand Down Expand Up @@ -480,7 +480,7 @@ impl User {
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$set": {
Expand Down Expand Up @@ -524,7 +524,7 @@ impl User {
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$set": {
Expand Down Expand Up @@ -574,7 +574,7 @@ impl User {
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$set": {
Expand Down Expand Up @@ -829,7 +829,7 @@ impl User {
match collection
.delete_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
None,
)
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/user_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn update_user_handler(
match collection
.update_one(
doc! {
"uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek),
"uid": &dek_data.uid,
},
doc! {
"$set": {
Expand Down
39 changes: 14 additions & 25 deletions src/traits/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,33 @@ where
// Deserialize the JSON string into a serde_json::Value
let mut value: Value = serde_json::from_str(&json_str).unwrap();

// Encrypt the keys and values recursively
// Decrypt the keys and values recursively
decrypt_value(&mut value, key);

// Deserialize the serde_json::Value back to the original object
serde_json::from_value(value).unwrap()
}
}

// Recursive function to encrypt object keys and values
// Recursive function to decrypt object keys and values
fn decrypt_value(value: &mut Value, key: &str) {
match value {
Value::String(s) => {
// Encrypt string values
// Decrypt string values
*s = Encryption::decrypt_data(s, key);
}
Value::Object(map) => {
// check if this is a ObjectId if yes do nothing return the same value
if map.contains_key("$oid")
|| map.contains_key("$date")
|| map.contains_key("$numberLong")
|| map.contains_key("$binary")
|| map.contains_key("$timestamp")
|| map.contains_key("$regex")
|| map.contains_key("$symbol")
|| map.contains_key("$code")
|| map.contains_key("$codeWithScope")
|| map.contains_key("$minKey")
|| map.contains_key("$maxKey")
|| map.contains_key("$undefined")
|| map.contains_key("$null")
|| map.contains_key("$numberInt")
|| map.contains_key("$numberDouble")
|| map.contains_key("$numberDecimal")
{
return;
}
// Recursively encrypt keys and values of nested objects
for (_, v) in map.iter_mut() {
// Check if this is a special MongoDB type, or if the key is "uid"
let special_keys = [
"$oid", "$date", "$numberLong", "$binary", "$timestamp", "$regex",
"$symbol", "$code", "$codeWithScope", "$minKey", "$maxKey",
"$undefined", "$null", "$numberInt", "$numberDouble", "$numberDecimal"
];

for (k, v) in map.iter_mut() {
if special_keys.contains(&k.as_str()) || k == "uid" {
continue;
}
decrypt_value(v, key);
}
}
Expand Down
35 changes: 12 additions & 23 deletions src/traits/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where

// Encrypt the keys and values recursively
encrypt_value(&mut value, key);

// Deserialize the serde_json::Value back to the original object
serde_json::from_value(value).unwrap()
}
Expand All @@ -34,28 +34,17 @@ fn encrypt_value(value: &mut Value, key: &str) {
*s = Encryption::encrypt_data(s, key);
}
Value::Object(map) => {
// check if this is a ObjectId if yes do nothing return the same value
if map.contains_key("$oid")
|| map.contains_key("$date")
|| map.contains_key("$numberLong")
|| map.contains_key("$binary")
|| map.contains_key("$timestamp")
|| map.contains_key("$regex")
|| map.contains_key("$symbol")
|| map.contains_key("$code")
|| map.contains_key("$codeWithScope")
|| map.contains_key("$minKey")
|| map.contains_key("$maxKey")
|| map.contains_key("$undefined")
|| map.contains_key("$null")
|| map.contains_key("$numberInt")
|| map.contains_key("$numberDouble")
|| map.contains_key("$numberDecimal")
{
return;
}
// Recursively encrypt keys and values of nested objects
for (_, v) in map.iter_mut() {
// Check if this is a special MongoDB type, or if the key is "uid"
let special_keys = [
"$oid", "$date", "$numberLong", "$binary", "$timestamp", "$regex",
"$symbol", "$code", "$codeWithScope", "$minKey", "$maxKey",
"$undefined", "$null", "$numberInt", "$numberDouble", "$numberDecimal"
];

for (k, v) in map.iter_mut() {
if special_keys.contains(&k.as_str()) || k == "uid" {
continue;
}
encrypt_value(v, key);
}
}
Expand Down

0 comments on commit cbc4548

Please sign in to comment.