Implement 2FA code verification

This commit is contained in:
coolneng 2020-11-25 11:06:38 +01:00
parent 66ce3d6ff5
commit e6018bd753
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 21 additions and 3 deletions

View File

@ -3,6 +3,7 @@ from secrets import token_hex
from smtplib import SMTP_SSL from smtplib import SMTP_SSL
from constants import DOMAIN, PASSWORD, USERNAME from constants import DOMAIN, PASSWORD, USERNAME
from database.crud import save_attribute
def initialize_smtp(domain, username, password): def initialize_smtp(domain, username, password):
@ -12,17 +13,19 @@ def initialize_smtp(domain, username, password):
def format_message(sender, recipient): 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 = EmailMessage()
message["From"] = sender message["From"] = sender
message["To"] = recipient message["To"] = recipient
message["Subject"] = "Clave para verificar su identidad" message["Subject"] = "Clave para verificar su identidad"
message.set_content(body) message.set_content(body)
return message return message, code
def send_mail(recipient): def send_mail(recipient):
server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD) 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.send_message(msg=message)
server.close() server.close()

View File

@ -25,3 +25,18 @@ def create_database(data):
if existing_row: if existing_row:
return return
insert_data(data) 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