# Path: doorman/extensions.py
# class LogTee(object):
# class RuleManager(object):
#     class ContextTask(TaskBase):
#     def __init__(self, app=None):
#     def init_app(self, app):
#     def handle_status(self, data, **kwargs):
#     def handle_result(self, data, **kwargs):
#     def __init__(self, app=None):
#     def init_app(self, app):
#     def load_alerters(self):
#     def should_reload_rules(self):
#     def load_rules(self):
#     def handle_log_entry(self, entry, node):
# def make_celery(app, celery):
#         def __call__(self, *args, **kwargs):
# 
# Path: doorman/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     username = Column(db.String(80), unique=True, nullable=False)
#     email = Column(db.String)
# 
#     password = Column(db.String, nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
# 
#     # oauth related stuff
#     social_id = Column(db.String)
#     first_name = Column(db.String)
#     last_name = Column(db.String)
# 
#     def __init__(self, username, password=None, email=None, social_id=None,
#                  first_name=None, last_name=None):
#         self.username = username
#         self.email = email
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#         self.social_id = social_id
#         self.first_name = first_name
#         self.last_name = last_name
# 
#     def set_password(self, password):
#         self.update(password=bcrypt.generate_password_hash(password))
#         return
# 
#     def check_password(self, value):
#         if not self.password:
#             # still do the computation
#             return bcrypt.generate_password_hash(value) and False
#         return bcrypt.check_password_hash(self.password, value)


from flask import current_app
from flask_ldap3_login import AuthenticationResponseStatus
from flask_wtf import FlaskForm as Form
from wtforms import BooleanField, PasswordField, StringField
from wtforms.validators import DataRequired, Optional
from doorman.extensions import bcrypt, ldap_manager
from doorman.models import User
                from doorman.extensions import bcrypt
        if not initial_validation:
            return False

        error_message = u'Invalid username or password.'

        if current_app.config['DOORMAN_AUTH_METHOD'] == 'doorman':
            self.user = User.query.filter_by(username=self.username.data).first()

            if not self.user:
                # avoid timing leaks
                bcrypt.generate_password_hash(self.password.data)
                self.username.errors.append(error_message)
                return False

            if not self.user.check_password(self.password.data):
                self.username.errors.append(error_message)
                return False

            return True

        elif current_app.config['DOORMAN_AUTH_METHOD'] == 'ldap':
            result = ldap_manager.authenticate(
                self.username.data,
                self.password.data
            )

            if result.status == AuthenticationResponseStatus.fail:
                self.username.errors.append(error_message)
                return False


# Path: doorman/extensions.py
# class LogTee(object):
# class RuleManager(object):
#     class ContextTask(TaskBase):
#     def __init__(self, app=None):
#     def init_app(self, app):
#     def handle_status(self, data, **kwargs):
#     def handle_result(self, data, **kwargs):
#     def __init__(self, app=None):
#     def init_app(self, app):
#     def load_alerters(self):
#     def should_reload_rules(self):
#     def load_rules(self):
#     def handle_log_entry(self, entry, node):
# def make_celery(app, celery):
#         def __call__(self, *args, **kwargs):
# 
# Path: doorman/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     username = Column(db.String(80), unique=True, nullable=False)
#     email = Column(db.String)
# 
#     password = Column(db.String, nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
# 
#     # oauth related stuff
#     social_id = Column(db.String)
#     first_name = Column(db.String)
#     last_name = Column(db.String)
# 
#     def __init__(self, username, password=None, email=None, social_id=None,
#                  first_name=None, last_name=None):
#         self.username = username
#         self.email = email
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#         self.social_id = social_id
#         self.first_name = first_name
#         self.last_name = last_name
# 
#     def set_password(self, password):
#         self.update(password=bcrypt.generate_password_hash(password))
#         return
# 
#     def check_password(self, value):
#         if not self.password:
#             # still do the computation
#             return bcrypt.generate_password_hash(value) and False
#         return bcrypt.check_password_hash(self.password, value)


# Path: doorman/users/forms.py
from flask import current_app
from flask_ldap3_login import AuthenticationResponseStatus
from flask_wtf import FlaskForm as Form
from wtforms import BooleanField, PasswordField, StringField
from wtforms.validators import DataRequired, Optional
from doorman.extensions import bcrypt, ldap_manager
from doorman.models import User
                from doorman.extensions import bcrypt

        if not initial_validation:
            return False

        error_message = u'Invalid username or password.'

        if current_app.config['DOORMAN_AUTH_METHOD'] == 'doorman':
            self.user = User.query.filter_by(username=self.username.data).first()

            if not self.user:
                # avoid timing leaks
                bcrypt.generate_password_hash(self.password.data)
                self.username.errors.append(error_message)
                return False

            if not self.user.check_password(self.password.data):
                self.username.errors.append(error_message)
                return False

            return True

        elif current_app.config['DOORMAN_AUTH_METHOD'] == 'ldap':
            result = ldap_manager.authenticate(
                self.username.data,
                self.password.data
            )

            if result.status == AuthenticationResponseStatus.fail:
                self.username.errors.append(error_message)
                return False


            self.user = ldap_manager._save_user(