-
Notifications
You must be signed in to change notification settings - Fork 2
/
cognitoHandler.py
84 lines (75 loc) · 2.66 KB
/
cognitoHandler.py
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
import boto3
class Cognito():
client = None
def __init__(self, profile):
self.session = boto3.Session(profile_name = profile)
self.client = self.session.client('cognito-idp')
super().__init__()
def checkCognitoUser(self, username: str,
user_pool_id: str) -> None:
# initial sign up
try:
resp = self.client.admin_get_user(
UserPoolId=user_pool_id,
Username=username
)
except self.client.exceptions.UserNotFoundException as e:
print(e)
#if e == 'UserNotFoundException':
return False
if resp:
return True
def authenticateCognitoUser(self, username: str, password: str,
user_pool_id: str, app_client_id: str) -> None:
respUpdate = self.client.update_user_pool_client(
UserPoolId=user_pool_id,
ClientId=app_client_id,
ExplicitAuthFlows=['ADMIN_NO_SRP_AUTH','USER_PASSWORD_AUTH']
)
if respUpdate:
respInitiate = self.client.admin_initiate_auth(
UserPoolId=user_pool_id,
ClientId=app_client_id,
AuthFlow='ADMIN_NO_SRP_AUTH',
AuthParameters={
"USERNAME": username,
"PASSWORD": password
}
)
if respInitiate:
print("Log in success")
else:
exit()
return respInitiate['AuthenticationResult']['IdToken']
def createCognitoUser(self, username: str, password: str,
user_pool_id: str, app_client_id: str) -> None:
#modify user pool to allow user signup
respUpdate = self.client.update_user_pool(
UserPoolId=user_pool_id,
AdminCreateUserConfig=
{
'AllowAdminCreateUserOnly': False,
}
)
if respUpdate:
# initial sign up
respSignup = self.client.sign_up(
ClientId=app_client_id,
Username=username,
Password=password,
UserAttributes=[
{
'Name': 'email',
'Value': '[email protected]'
},
]
)
if respSignup:
# then confirm signup
respConfirm = self.client.admin_confirm_sign_up(
UserPoolId=user_pool_id,
Username=username
)
if respConfirm: return True
else:
return False