def register_provider(provider: RegisterProvider, database: BlockchainDb = Depends()): aws_cognito = Cognito(os.environ['USER_POOL_ID'], os.environ['USER_POOL_WEB_CLIENT_ID']) aws_cognito.username = provider.username aws_cognito.set_base_attributes(email=provider.username, name=f'{provider.name.firstName}') aws_cognito.add_custom_attributes(usertype='provider') response = aws_cognito.register(provider.username, provider.password) for providable_treatment in provider.providableTreatments: providable_treatment.providableTreatmentId = uuid.uuid4() for address in provider.addresses: address.addressId = uuid.uuid4() try: database.commit_transaction( Provider( providerId=response['UserSub'], name=provider.name, phoneNumbers=provider.phoneNumbers, addresses=provider.addresses, dateOfBirth=provider.dateOfBirth, email=provider.email, providableTreatments=provider.providableTreatments).dict(), 'CREATE', 'Provider', 'providerId', response['UserSub']) return response['UserSub'] except: aws_cognito.delete_user() return status.HTTP_400_BAD_REQUEST
def register(user_name, password, email, registration_frame): try: u = Cognito(user_pool_id, client_id) u.set_base_attributes(email=email) u.register(user_name, password) print("New user created successfully!") Label(registration_frame, text="New user created successfully!").grid(row=5, column=0, columnspan=2) except Exception as e: print(e) return False
def register_client(client: RegisterClient, database: BlockchainDb = Depends()): aws_cognito = Cognito(os.environ['USER_POOL_ID'], os.environ['USER_POOL_WEB_CLIENT_ID']) aws_cognito.username = client.username aws_cognito.set_base_attributes(email=client.username, name=f'{client.name.firstName}') aws_cognito.add_custom_attributes(usertype='client') response = aws_cognito.register(client.username, client.password) client.address.addressId = uuid.uuid4() database.commit_transaction( Client(clientId=response['UserSub'], name=client.name, phoneNumbers=client.phoneNumbers, address=client.address, dateOfBirth=client.dateOfBirth, email=client.email).dict(), 'CREATE', 'Client', 'clientId', response['UserSub']) return response['UserSub']
class CognitoUser: def __init__(self, user_pool_id, client_id, client_secret, username): self.user_pool_id = user_pool_id self.client_id = client_id self.client_secret = client_secret self.username = username self.cognito = Cognito(user_pool_id, client_id, client_secret=client_secret, username=username) self.log = Helper.get_logger() # this method allows the registration of a user in AWS cognito def register_user(self, password, email, first_name, middle_name, last_name, phone_number): is_registered = False self.log.info("registering user with username " + self.username) try: self.cognito.set_base_attributes(email=email, given_name=first_name, middle_name=middle_name, family_name=last_name, phone_number=phone_number) self.cognito.register(username=self.username, password=password) is_registered = True self.log.info("username " + self.username + " registered successfully") except Exception as e: if "UsernameExistException" in str(e): self.log.error( "Username already exist, please user another username") else: self.log.error(str(e)) return is_registered # This method allows for the user to confirm registration def confirm_signup(self, confirmation_code): signup_confirmed = False self.log.info("confirming user="******" with registration code " + confirmation_code) try: self.cognito.confirm_sign_up(confirmation_code, username=self.username) signup_confirmed = True self.log.info("user = "******" has been confirmed ") except Exception as e: self.log.error(str(e)) return signup_confirmed # This method allows a user to be authenticated with AWS cognito authentication def authenticate_user(self, password): is_authenticated = False self.log.info("Authenticating user = "******"user = "******" has been authenticated") except Exception as e: self.log.error(str(e)) return is_authenticated # this method returns all the personal information of a given username def get_user_info(self, password): self.log.info("Getting user info for username = "******"given_name": "first_name", "middle_name": "middle_name", "family_name": "last_name", "email": "email", "phone_number": "phone_number" }) except Exception as e: print(str(e.with_traceback())) return user