# Path: webtools/auth/models.py
# class User(Base):
#     __tablename__ = 'users'
# 
#     id = Column(Integer, primary_key=True, autoincrement=True)
#     username = Column(Unicode(200), nullable=False, index=True)
#     first_name = Column(Unicode(200), nullable=False)
#     last_name = Column(Unicode(200), nullable=False)
#     email = Column(Unicode(200), nullable=False)
#     password = Column(String(200), nullable=False)
# 
#     def __repr__(self):
#         return "<User {0}>".format(self.id)
# 
#     def set_password(self, password):
#         self.password = make_password(password)
# 
#     def check_password(self, password):
#         return check_password(password, self.password)
# 
# Path: webtools/auth/hashers.py
# def check_password(password, encoded, setter=None, preferred='default'):
#     """
#     Returns a boolean of whether the raw password matches the three
#     part encoded digest.
# 
#     If setter is specified, it'll be called when you need to
#     regenerate the password.
#     """
#     if not password or not is_password_usable(encoded):
#         return False
# 
#     preferred = get_hasher(preferred)
#     hasher = identify_hasher(encoded)
# 
#     must_update = hasher.algorithm != preferred.algorithm
#     is_correct = hasher.verify(password, encoded)
#     if setter and is_correct and must_update:
#         setter(password)
#     return is_correct


from sqlalchemy.orm.exc import NoResultFound
from .models import User
from .hashers import check_password



class BaseAuthenticationBackend(object):
    def __init__(self, application):
        self.application = application

    def authenticate(self, username=None, password=None):
        raise NotImplementedError()


class DatabaseAuthenticationBackend(BaseAuthenticationBackend):
    def authenticate(self, username=None, password=None):
        try:

# Path: webtools/auth/models.py
# class User(Base):
#     __tablename__ = 'users'
# 
#     id = Column(Integer, primary_key=True, autoincrement=True)
#     username = Column(Unicode(200), nullable=False, index=True)
#     first_name = Column(Unicode(200), nullable=False)
#     last_name = Column(Unicode(200), nullable=False)
#     email = Column(Unicode(200), nullable=False)
#     password = Column(String(200), nullable=False)
# 
#     def __repr__(self):
#         return "<User {0}>".format(self.id)
# 
#     def set_password(self, password):
#         self.password = make_password(password)
# 
#     def check_password(self, password):
#         return check_password(password, self.password)
# 
# Path: webtools/auth/hashers.py
# def check_password(password, encoded, setter=None, preferred='default'):
#     """
#     Returns a boolean of whether the raw password matches the three
#     part encoded digest.
# 
#     If setter is specified, it'll be called when you need to
#     regenerate the password.
#     """
#     if not password or not is_password_usable(encoded):
#         return False
# 
#     preferred = get_hasher(preferred)
#     hasher = identify_hasher(encoded)
# 
#     must_update = hasher.algorithm != preferred.algorithm
#     is_correct = hasher.verify(password, encoded)
#     if setter and is_correct and must_update:
#         setter(password)
#     return is_correct


# Path: webtools/auth/backends.py
from sqlalchemy.orm.exc import NoResultFound
from .models import User
from .hashers import check_password




class BaseAuthenticationBackend(object):
    def __init__(self, application):
        self.application = application

    def authenticate(self, username=None, password=None):
        raise NotImplementedError()


class DatabaseAuthenticationBackend(BaseAuthenticationBackend):
    def authenticate(self, username=None, password=None):
        try:

            user = self.application.db.query(User).filter(User.username == username).one()