def create_account(username: str, password: str, email: str = None, full_name: str = None, agency: str = None): """ Creates a new user account with specified information. No permissions are granted upon account creation and an administrator must manually add permissions to an account before it is able to access most endpoints. :param username: Username of new account. Must be unique :param password: Password of new account. This is immediately hashed and never stored in plaintext :param email: (optional) Email address associated with new account. :param full_name:(optional) User's full name (First + Last) as a single string :param agency: (optional) Agency/Organization associated with the new user as a string :return: {'status': 'success'} if account creation successful, else {'status': 'failure'} """ u = User(username=username, password=get_password_hash(password), email=email, full_name=full_name, roles=[], agency=agency) result = add_user_db(u) if not result: return { 'status': 'failure', 'detail': 'Account with this username already exists' } return { 'status': 'success', 'detail': 'account with username [' + username + '] created.' }
def get_current_user_profile( current_user: User = Depends(get_current_active_user)): """ Export the data of the current user to the client. :param current_user: Currently logged in user to have data exported. This field is auto filled by the HTTP request :return: User profile details, excluding hashed password. """ user_export_data = current_user.dict(exclude={'password', 'id'}) return user_export_data
def get_user_by_name_db(username: str) -> Union[User, None]: """ Finds a user in the database by a given username. :param username: username of user :return: User object if user with given username exists, else None """ if not user_collection.find_one({"username": username}): return None database_result = user_collection.find_one({"username": username}) user_object = User(**database_result) return user_object
def add_user_db(user: User) -> bool: """ Adds a new user to the database. :param user: User object to add to database :return: True if added, else False if error. """ # If request didn't specify permissions, ensure that none are stored in the database. if user.roles is None: roles = [] if not user_collection.find_one({"username": user.username}): user_collection.insert_one(user.dict()) return True return False # This means there is already a user in the database with this name.
def create_testing_keys(): """ Creates API keys for usage in test cases. These keys should NEVER be used for actual microservices. """ # Only create an API key testing account if it doesn't exist already if not get_user_by_name_db('api_key_testing'): password = str(uuid.uuid4()) u = User( username='******', password=get_password_hash( password), # Random unguessable password email='*****@*****.**', roles=['admin'], ) add_user_db(u) else: u = get_user_by_name_db('api_key_testing') # If keys do not already exist, create them if not get_api_keys_by_user_db(u): prediction_key_value = str(uuid.uuid4()) training_key_value = str(uuid.uuid4()) prediction_key = APIKeyData( **{ 'key': prediction_key_value, 'type': ExternalServices.predict_microservice.name, 'user': '******', 'detail': 'Key For Testing ONLY', 'enabled': True }) training_key = APIKeyData( **{ 'key': training_key_value, 'type': ExternalServices.train_microservice.name, 'user': '******', 'detail': 'Key For Testing ONLY', 'enabled': True }) add_api_key_db(prediction_key) add_api_key_db(training_key)
def create_testing_account(): """ Creates an account for usage in tests. This account should never be logged into, and is only accessed by username in test cases """ # Only create a testing account if it doesn't exist already if not get_user_by_name_db('testing'): password = str(uuid.uuid4()) u = User( username='******', password=get_password_hash( password), # Random unguessable password email='*****@*****.**', roles=['admin'], agency=password) add_user_db(u)
def create_admin_account_testing(): if get_user_by_name_db('admin'): return { 'status': 'failure', 'detail': 'Admin account already exists.', 'account': 'Username: "******", Password: "******"' } u = User(username='******', password=get_password_hash('password'), email='*****@*****.**', roles=['admin']) add_user_db(u) return { 'status': 'success', 'detail': 'Admin account has been created.', 'account': 'Username: "******", Password: "******"' }