-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_signup.py
More file actions
99 lines (88 loc) · 2.58 KB
/
Copy pathlambda_signup.py
File metadata and controls
99 lines (88 loc) · 2.58 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import boto3
import hmac
import hashlib
import base64
import json
USER_POOL_ID = ""
COGNITO_CLIENT_ID = ""
COGNITO_CLIENT_SECRET= ""
def get_secret_hash(username):
msg = username + COGNITO_CLIENT_ID
dig = hmac.new(str(COGNITO_CLIENT_SECRET).encode('utf-8'),
msg = str(msg).encode('utf-8'), digestmod=hashlib.sha256).digest()
d2 = base64.b64encode(dig).decode()
return d2
def lambda_handler(event, context):
body = json.loads(event['body'])
for field in ["username", "email", "password","tag"]:
if field not in body:
return {
'statusCode': 400,
'body': json.dumps({"error": True,
"message": f"{field} is not present",
})}
username = body['username']
email = body["email"]
password = body['password']
tags = ";".join(body['tag'])
if len(password)<8:
return {
'statusCode': 400,
'body': json.dumps({"error": True,
"message": "Username not confirmed",
})}
client = boto3.client('cognito-idp')
try:
resp = client.sign_up(
ClientId=COGNITO_CLIENT_ID,
SecretHash=get_secret_hash(username),
Username=username,
Password=password,
UserAttributes=[
{
'Name': "email",
'Value': email
},
{
'Name': "custom:tag",
'Value': tags
}
],
ValidationData=[
{
'Name': "email",
'Value': email
},
{
'Name': "custom:username",
'Value': username
}
])
except client.exceptions.UsernameExistsException as e:
return {
'statusCode': 409,
'body': json.dumps({"error": True,
"message": "Username already exists",
})}
except client.exceptions.UserLambdaValidationException as e:
return {
'statusCode': 409,
'body': json.dumps({"error": True,
"message": "Email already exists",
})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({"error": False,
"success": True,
"message": str(e), })
}
return {
'statusCode': 201,
'body': json.dumps({
"success": True,
"message": "Sign up sucessful",
"data": None
})
}