From e6018bd753078ebdc16d42e42f11d4cf6e005eb2 Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 25 Nov 2020 11:06:38 +0100 Subject: [PATCH] Implement 2FA code verification --- app/notifier.py | 9 ++++++--- database/crud.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/notifier.py b/app/notifier.py index 1e47ef3..848e27e 100644 --- a/app/notifier.py +++ b/app/notifier.py @@ -3,6 +3,7 @@ from secrets import token_hex from smtplib import SMTP_SSL from constants import DOMAIN, PASSWORD, USERNAME +from database.crud import save_attribute def initialize_smtp(domain, username, password): @@ -12,17 +13,19 @@ def initialize_smtp(domain, username, password): def format_message(sender, recipient): - body = "La clave es {}. Envíele esta clave al bot.".format(token_hex(4)) + code = token_hex(4) + body = "La clave es {}. Envíele esta clave al bot.".format() message = EmailMessage() message["From"] = sender message["To"] = recipient message["Subject"] = "Clave para verificar su identidad" message.set_content(body) - return message + return message, code def send_mail(recipient): server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD) - message = format_message(sender=USERNAME, recipient=recipient) + message, code = format_message(sender=USERNAME, recipient=recipient) + save_attribute(attribute="codigo", data=code) server.send_message(msg=message) server.close() diff --git a/database/crud.py b/database/crud.py index 4458f55..7ce31c1 100644 --- a/database/crud.py +++ b/database/crud.py @@ -25,3 +25,18 @@ def create_database(data): if existing_row: return insert_data(data) + + +def save_attribute(attribute, data): + key = eval("Users." + attribute) + db.query(Users).filter( + or_(Users.correo_institucional == id, Users.numero_de_documento == id) + ).update({key: data}) + + +def verify_code(id, code): + db_record = search_database(id=id) + valid_code = code == db_record + if valid_code: + return True + return False