Skip to content

Commit

Permalink
added user existance check
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Apr 16, 2024
1 parent 1b8550e commit 329e9a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
30 changes: 19 additions & 11 deletions backend/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
import os
import base64


def get_secret_key_path(email: str):
sender_email = email.replace("@", "").replace(".", "")
filename = f"{sender_email}.pem"
secret_key_path = os.path.join("keys", "private", filename)
if not os.path.exists(secret_key_path):
os.makedirs(os.path.dirname(secret_key_path), exist_ok=True)
return secret_key_path


def verify_by_base64_key(base64_key: str, ps_message: str, ps_signature: str) -> bool:
public_key = serialization.load_pem_public_key(
base64.b64decode(base64_key),
Expand All @@ -37,8 +27,26 @@ def verify_by_base64_key(base64_key: str, ps_message: str, ps_signature: str) ->


class RSA:

@staticmethod
def email_to_filename(email: str):
return email.replace("@", "").replace(".", "")

@staticmethod
def get_secret_key_path(email: str):
filename = RSA.email_to_filename(email)
secret_key_path = os.path.join("keys", "private", f"{filename}.pem")
if not os.path.exists(secret_key_path):
os.makedirs(os.path.dirname(secret_key_path), exist_ok=True)
return secret_key_path

@staticmethod
def is_user_key_present(email: str):
private_key_path = RSA.get_secret_key_path(email)
return os.path.exists(private_key_path)

def __init__(self, sender_email: str):
private_key_path = get_secret_key_path(sender_email)
private_key_path = RSA.get_secret_key_path(sender_email)
if os.path.exists(private_key_path):
print("Loading private key from file")
with open(private_key_path, "rb") as f:
Expand Down
7 changes: 5 additions & 2 deletions routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,14 @@ def read_root():
@router.get("/key")
def get_public_key(user_email: str):
try:
if not RSA.is_user_key_present(user_email):
return {"status": "notfound", "error": "No key found for user"}

rsa = RSA(user_email)
key_str = rsa.get_public_key()
return {"public_key": key_str}
return {"status": "ok", "public_key": key_str}
except Exception as e:
return {"error": str(e)}
return {"status": "error", "error": str(e)}


@router.post("/verify/email")
Expand Down

0 comments on commit 329e9a8

Please sign in to comment.