# Path: project/user/finders/user_finder.py
# class UserFinder:
# 
#     @classmethod
#     def all(cls):
#         return User.query.filter().all()
# 
#     @classmethod
#     def by_id(cls, user_id):
#         return User.query.filter(User.id == user_id).first()
# 
#     @classmethod
#     def by_username(cls, username):
#         return User.query.filter(User.username == username).first()
# 
#     @classmethod
#     def by_api_key(cls, api_key):
#         return User.query.filter(User.api_key == api_key).first()


import base64
import binascii
from flask import current_app as app
from flask_bcrypt import check_password_hash
from project.user.finders.user_finder import UserFinder
        app.logger.error("User id \"{}\" does not exist".format(user_id))
        return False

    return user


def load_user_from_request(request):
    # first, try to login using the api_key url arg
    api_key = request.args.get('api_key')
    if api_key:
        user = UserFinder.by_api_key(api_key)
        if user:
            return user

    # next, try to login using Basic Auth first, try apikey and then user:pass
    auth = request.headers.get('Authorization')
    if not auth:
        return None

    # get bytes string instead of str representation
    auth = auth.replace('Basic ', '', 1)
    try:
        auth = bytes(auth, 'utf8')
        auth = base64.b64decode(auth)
        auth = auth.decode()
    except (UnicodeDecodeError, TypeError, binascii.Error):
        # make byte string "Str" again
        auth = auth.decode()

    # try if auth is api_key

# Path: project/user/finders/user_finder.py
# class UserFinder:
# 
#     @classmethod
#     def all(cls):
#         return User.query.filter().all()
# 
#     @classmethod
#     def by_id(cls, user_id):
#         return User.query.filter(User.id == user_id).first()
# 
#     @classmethod
#     def by_username(cls, username):
#         return User.query.filter(User.username == username).first()
# 
#     @classmethod
#     def by_api_key(cls, api_key):
#         return User.query.filter(User.api_key == api_key).first()


# Path: project/user/login_manager.py
import base64
import binascii
from flask import current_app as app
from flask_bcrypt import check_password_hash
from project.user.finders.user_finder import UserFinder

        app.logger.error("User id \"{}\" does not exist".format(user_id))
        return False

    return user


def load_user_from_request(request):
    # first, try to login using the api_key url arg
    api_key = request.args.get('api_key')
    if api_key:
        user = UserFinder.by_api_key(api_key)
        if user:
            return user

    # next, try to login using Basic Auth first, try apikey and then user:pass
    auth = request.headers.get('Authorization')
    if not auth:
        return None

    # get bytes string instead of str representation
    auth = auth.replace('Basic ', '', 1)
    try:
        auth = bytes(auth, 'utf8')
        auth = base64.b64decode(auth)
        auth = auth.decode()
    except (UnicodeDecodeError, TypeError, binascii.Error):
        # make byte string "Str" again
        auth = auth.decode()

    # try if auth is api_key

    user = UserFinder.by_api_key(auth)