Skip to content

signup bug #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions servers/tenant/blueprints/simple/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
alchemyConverter
)
from flask import make_response, request, jsonify, Blueprint, abort
from helpers.identity_helpers import IdentityHelper

user_bp = Blueprint("user_bp", __name__, url_prefix="user")

Expand Down Expand Up @@ -44,6 +45,32 @@ def user_post():
return "success"


@user_bp.route("/login", methods=["POST"])
@auth_required()
def user_login():
user = IdentityHelper.get_logged_in_userId()

if user_controller._get_count(filters={'userId' : user}) > 0:
return "success"

existing_users = user_controller._get(None)
existing_users_lookup = {}
for eu in existing_users:
existing_users_lookup[eu.userId] = True

cognito_users = IdentityHelper.get_cognito_users()
sync_users = []
for cu in cognito_users:
if cu["userId"] in existing_users_lookup:
continue
sync_users.append(cu)

inserted = user_controller._create_bulk(sync_users)
print(f"Successfully Synchronised {len(inserted)} Users.")

return "success"


@user_bp.route("", methods=["PUT"])
@auth_required()
def user_modify():
Expand All @@ -54,8 +81,6 @@ def user_modify():
return "success"




@user_bp.route("/", methods=["DELETE"])
@auth_required()
def user_delete():
Expand Down
65 changes: 64 additions & 1 deletion servers/tenant/helpers/identity_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from flask import session
import os
import boto3
from botocore.config import Config

class IdentityHelper:

Expand All @@ -7,4 +10,64 @@ def get_logged_in_userId():
claims = session.get("claims")
if claims and "sub" in claims:
return claims["sub"]
return None
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