-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverification.py
More file actions
71 lines (61 loc) · 2.09 KB
/
verification.py
File metadata and controls
71 lines (61 loc) · 2.09 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
from datetime import datetime, timedelta
import logging
from user import setUserVerified
import os
import jwt
import boto3
# Create an SES client
ses_client = boto3.client('ses',region_name='us-east-2')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def sendVerificationEmail(receiver,token):
try:
verification_link = f"https://tf0mj1svb3.execute-api.us-east-2.amazonaws.com/prod/verify?token={token}"
email_body = f"""<pre>
Bienvenido! Tu cuenta ha sido creada.
<a href="{verification_link}">Haz click aquí para verificar tu cuenta</a>
Gracias,
Equipo BAMX Rewards.
</pre>"""
response = ses_client.send_email(
Source=sender,
Destination={'ToAddresses': [receiver]},
Message={
'Subject': {'Charset': 'UTF-8','Data': 'Verificacion de tu cuenta BAMX.'},
'Body': {'Html': {'Data': email_body}}
}
)
except Exception as e:
logger.error(f"Error al enviar el correo electrónico de verificación: {str(e)}")
def createVerifyToken(email,sourceIp):
if sourceIp is None:
return None
try:
token_data = {'email':email,'sourceIp':sourceIp,'exp': datetime.utcnow() + timedelta(hours=6)}
token = jwt.encode(token_data, os.environ['JWT_SECRET'], algorithm='HS256')
except Exception as e:
return None
return token
def verifyEmail(token,sourceIp):
try:
decoded = jwt.decode(token, os.environ['JWT_SECRET'], algorithms=['HS256'])
email = decoded.get('email')
tokenIp = decoded.get('sourceIp')
if sourceIp == tokenIp:
return setUserVerified(email)
else:
return {
'verified' : False,
'message' : 'Ip Not Valid',
}
except jwt.ExpiredSignatureError:
return {
'verified': False,
'message': 'Token Expired',
}
except jwt.DecodeError:
return {
'verified': False,
'message': 'Invalid Token',
}