def bind_search(self, username, password):
        """
        Bind to BIND_DN/BIND_AUTH then search for user to perform lookup. 
        """

        log.debug("Performing bind/search")

        ctx = {'username':username, 'password':password}

        user = self.config['BIND_DN'] % ctx

        bind_auth = self.config['BIND_AUTH']
        try:
            log.debug("Binding with the BIND_DN %s" % user)
            self.conn.simple_bind_s(user, bind_auth)
        except ldap.INVALID_CREDENTIALS:
            log.debug("Could not connect bind with the BIND_DN=%s" % user)
            return None

        user_search = self.config.get('USER_SEARCH')

        results = None
        for search in user_search:
            base = search['base']
            filt = search['filter'] % ctx
            scope = search.get('scope', ldap.SCOPE_SUBTREE)
            log.debug("Search for base=%s filter=%s" % (base, filt))
            results = self.conn.search_s(base, scope, filt, attrlist=self.attrlist)
            if results:
                log.debug("User with DN=%s found" % results[0][0])
                try:
                    self.conn.simple_bind_s(results[0][0], password)
                except ldap.INVALID_CREDENTIALS:
                    self.conn.simple_bind_s(user, bind_auth)
                    log.debug("Username/password mismatch, continue search...")
                    results = None
                    continue
                else:
                    log.debug("Username/password OK")
                    break

        log.debug("Unbind")
        self.conn.unbind_s()

        return self.format_results(results)
dataset/ETHPy150Open ContinuumIO/flask-ldap-login/flask_ldap_login/__init__.py/LDAPLoginManager.bind_search/original