-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
82 lines (70 loc) · 2.65 KB
/
api.py
File metadata and controls
82 lines (70 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import re
from fastapi import FastAPI
from os import getenv
from re import match
import datetime
from database_setup import Temp_tokens
# import the database handler
import db_handler as db_handler
# import the email handler
import email_handler as email_handler
app: FastAPI = FastAPI()
# Get the environment variable from docker-compose
ALLOW_REGISTRATION: str = getenv("ALLOW_REGISTRATION")
def message_handler(message: str) -> dict:
"""
This function is used to handle the messages send to the user.
It will return a message to the user.
"""
return {"message": message}
@app.get("/register")
def register(email: str):
"""
This function is used to register the user.
It will send an email to the user with a temporary token.
"""
if ALLOW_REGISTRATION == "false":
return message_handler("Registration is not allowed")
if not match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", email):
return message_handler("Invalid email")
if db_handler.email_exists(email):
return message_handler("Email already exists")
# If all the conditions are met, create a temp token
token: str = db_handler.create_temp_token(email)
# Reading the email template
f = open("templates/confirm_template.html", "r")
# Template is return by readlines() as a list
template: list = f.readlines()
# We transform the list into a string
template: str = " ".join(map(str, template))
# We replace the token in the template
template: str = template.replace("{url}", f"http://localhost/confirm?token={token}")
# Send the email with the token
email_handler.send_html_email(
email=email, subject="Confirm your email", html=template
)
return message_handler("A token has been sent to your email")
@app.get("/confirm")
def confirm(token: str):
"""
This function is used to confirm the email of the user.
It will create a user in the database if the token is valid.
"""
# Check if the token exists
temp_token: Temp_tokens = db_handler.get_temp_token(token)
if temp_token is None:
return message_handler("Invalid token")
# Check if the token has been used
if temp_token.used:
return message_handler("Token has already been used")
# Check if the token is expired
if (
temp_token.generated_at + datetime.timedelta(minutes=5)
< datetime.datetime.now()
):
return message_handler("Token expired")
# If all the conditions are met, create a user
db_handler.create_user(temp_token.email)
# Set the token as used
db_handler.set_temp_token_used(token)
return message_handler("Your email has been confirmed")