-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentity_helpers.py
73 lines (64 loc) · 2.33 KB
/
identity_helpers.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
from flask import session
import os
import boto3
from botocore.config import Config
class IdentityHelper:
@staticmethod
def get_logged_in_userId():
claims = session.get("claims")
if claims and "sub" in claims:
return claims["sub"]
return None
@staticmethod
def get_cognito_users():
cognito_users = []
my_config = Config(
region_name = os.getenv("AWS_REGION"),
signature_version = 'v4',
retries = {
'max_attempts': 10,
'mode': 'standard'
}
)
client = boto3.client('cognito-idp', config=my_config,
aws_access_key_id = os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key = os.getenv("AWS_SECRET_ACCESS_KEY")
)
list_users_args = {
'UserPoolId': os.getenv("AWS_COGNITO_USER_POOL_ID"),
'AttributesToGet': [
'email',
'name',
'phone_number',
'sub',
'custom:UserType',
],
'Limit': 2
}
# Get users from cognito
pagination = True
while pagination:
response = client.list_users(**list_users_args)
if "PaginationToken" in response:
list_users_args["PaginationToken"] = response["PaginationToken"]
else:
pagination = False
for user_res in response['Users']:
usr = {}
for att in user_res["Attributes"]:
usr[att['Name']] = att["Value"]
first_name = usr["name"].split(" ")[0]
last_name = "" if len(usr["name"].split(" ")) < 2 else usr["name"].split(" ")[1]
cognito_users.append(
{
"userId": usr["sub"],
"userType": usr["custom:UserType"],
"username": usr["email"],
"firstName": first_name,
"lastName": last_name,
"email": usr["email"],
"createdAt": user_res["UserCreateDate"].strftime('%s'),
"modifiedAt": user_res["UserLastModifiedDate"].strftime('%s')
}
)
return cognito_users