Skip to content

Commit

Permalink
Merge pull request #34 from asetalias/encryption
Browse files Browse the repository at this point in the history
Encryption and Decryption support added
  • Loading branch information
Vyogami authored May 31, 2023
2 parents 95e8dc3 + 4e54e5f commit bc75977
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
6 changes: 5 additions & 1 deletion controllers/db.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from util.db_client import profile
from util.encryption import encrypt, decrypt


async def create_profile(telegram_id: int, username, password) -> str:
data = await profile.find_one({"_id": telegram_id})
if data != None:
resp = await update_profile(telegram_id, username, password)
encrypted_password = encrypt(password)
resp = await update_profile(telegram_id, username, encrypted_password)
return resp

encrypted_password = encrypt(password)
data = {"_id": telegram_id, "username": username, "password": password}

try:
Expand All @@ -19,6 +22,7 @@ async def create_profile(telegram_id: int, username, password) -> str:
async def get_profile(telegram_id: int) -> dict:
try:
data = await profile.find_one({"_id": telegram_id})
data["password"] = decrypt(data["password"])
return data
except Exception as e:
print(e)
Expand Down
9 changes: 9 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from util.encryption import encrypt, decrypt

password = "Achintya"

encrypted_password = encrypt(password)
print(encrypted_password)

decrypted_password = decrypt(encrypted_password)
print(decrypted_password)
10 changes: 0 additions & 10 deletions util/db_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
# import pymongo
# from util.env import MONGO_URI, MONGO_DATABASE

# client = pymongo.MongoClient(MONGO_URI)

# db = client[MONGO_DATABASE]

# # Collection, use profile to create and query the db
# profile = db["profile"]

import asyncio
import motor.motor_asyncio
from util.env import MONGO_URI, MONGO_DATABASE, MONGO_COLLECTION
Expand Down
12 changes: 12 additions & 0 deletions util/encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from cryptography.fernet import Fernet
from util.env import KEY

def encrypt(password):
cipher_suite = Fernet(KEY)
enccrypted_password = cipher_suite.encrypt(password.encode())
return enccrypted_password.decode()

def decrypt(encrypted_password):
cipher_suite = Fernet(KEY)
decrypted_password = cipher_suite.decrypt(encrypted_password).decode()
return decrypted_password
2 changes: 2 additions & 0 deletions util/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

MONGO_URI = os.environ.get("MONGO_URI", "mongodb://localhost:27017/")
MONGO_DATABASE = os.environ.get("MONGO_DATABASE", "users")
KEY = os.environ.get("KEY")
TOKEN = os.environ.get("TOKEN", "token")
URL = "amizone.fly.dev:443"
MONGO_COLLECTION = "profile"

0 comments on commit bc75977

Please sign in to comment.