· 4 years ago · Apr 14, 2021, 03:36 AM
1from itsdangerous import URLSafeTimedSerializer
2import os
3
4
5SECRET_KEY = os.environ['MALGAZER_WEB_SECRET_KEY']
6SECURITY_PASSWORD_SALT = os.environ['MALGAZER_WEB_SECURITY_PASSWORD_SALT']
7
8
9def generate_confirmation_token(email):
10 """
11 Generates a confirmation token for an email address.
12 :param email: The email to embed in the token.
13 :return: The token.
14 """
15 serializer = URLSafeTimedSerializer(SECRET_KEY)
16 return serializer.dumps(email, salt=SECURITY_PASSWORD_SALT)
17
18
19def confirm_token(token, expiration=3600):
20 """
21 This determines if the token is valid.
22 :param token: The token.
23 :param expiration: The expiration, in seconds.
24 :return: False if there is an issue, the email for the token if it is confirmed.
25 """
26 serializer = URLSafeTimedSerializer(SECRET_KEY)
27 try:
28 email = serializer.loads(token, salt=SECURITY_PASSWORD_SALT, max_age=expiration)
29 except:
30 return False
31 return email
32