# Buggy code
def auth_user_ldap(self, username, password):
    """
    Method for authenticating user, auth LDAP style.
    depends on ldap module that is not mandatory requirement
    for F.A.B.

    :param username:
        The username
    :param password:
        The password
    """
    if username is None or username == "":
        return None
    user = self.find_user(username=username)
    if user is not None and (not user.is_active()):
        return None
    else:
        try:
            import ldap
        except:
            raise Exception("No ldap library for python.")
        try:
            if self.auth_ldap_allow_self_signed:
                ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
            con = ldap.initialize(self.auth_ldap_server)
            con.set_option(ldap.OPT_REFERRALS, 0)
            if self.auth_ldap_use_tls:
                try:
                    con.start_tls_s()
                except Exception:
                    log.info(LOGMSG_ERR_SEC_AUTH_LDAP_TLS.format(self.auth_ldap_server))
                    return None
            # Authenticate user
            if not self._bind_ldap(ldap, con, username, password):
                if user:
                    self.update_user_auth_stat(user, False)
                log.info(LOGMSG_WAR_SEC_LOGIN_FAILED.format(username))
                return None
            # If user does not exist on the DB and not self user registration, go away
            if not user and not self.auth_user_registration:
                return None
            # User does not exist, create one if self registration.
            elif not user and self.auth_user_registration:
                new_user = self._search_ldap(ldap, con, username)
                if not new_user:
                    log.warning(LOGMSG_WAR_SEC_NOLDAP_OBJ.format(username))
                    return None
                ldap_user_info = new_user[0][1]
                if self.auth_user_registration and user is None:
                    user = self.add_user(
                        username=username,
                        first_name=ldap_user_info.get(
                            self.auth_ldap_firstname_field, [username]
                        )[0],
                        last_name=ldap_user_info.get(
                            self.auth_ldap_lastname_field, [username]
                        )[0],
                        email=ldap_user_info.get(
                            self.auth_ldap_email_field, [username + "@email.notfound"]
                        )[0],
                        role=self.find_role(self.auth_user_registration_role),
                    )

            self.update_user_auth_stat(user)
            return user

        except ldap.LDAPError as e:
            if type(e.message) == dict and "desc" in e.message:
                log.error(LOGMSG_ERR_SEC_AUTH_LDAP.format(e.message["desc"]))
                return None
            else:
                log.error(e)
                return None


# Traceback (TypeError)
Sorry, something went wrong

500 - Internal Server Error

Stacktrace

Traceback (most recent call last):
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/security/decorators.py", line 26, in wraps
return f(self, *args, **kwargs)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/views.py", line 450, in list
widgets = self._list()
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/baseviews.py", line 870, in _list
page_size=page_size)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/baseviews.py", line 786, in _get_list_widget
count, lst = self.datamodel.query(joined_filters, order_column, order_direction, page=page, page_size=page_size)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/models/sqla/interface.py", line 103, in query
order_direction=order_direction)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/models/sqla/interface.py", line 67, in _get_base_query
query = query.order_by(order_column + ' ' + order_direction)
TypeError: coercing to Unicode: need string or buffer, NoneType found

# Fixed code
def auth_user_ldap(self, username, password):
    """
    Method for authenticating user, auth LDAP style.
    depends on ldap module that is not mandatory requirement
    for F.A.B.

    :param username:
        The username
    :param password:
        The password
    """
    if username is None or username == "":
        return None
    user = self.find_user(username=username)
    if user is not None and (not user.is_active()):
        return None
    else:
        try:
            import ldap
        except:
            raise Exception("No ldap library for python.")
        try:
            if self.auth_ldap_allow_self_signed:
                ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
            con = ldap.initialize(self.auth_ldap_server)
            con.set_option(ldap.OPT_REFERRALS, 0)
            if self.auth_ldap_use_tls:
                try:
                    con.start_tls_s()
                except Exception:
                    log.info(LOGMSG_ERR_SEC_AUTH_LDAP_TLS.format(self.auth_ldap_server))
                    return None
            # Authenticate user
            if not self._bind_ldap(ldap, con, username, password):
                if user:
                    self.update_user_auth_stat(user, False)
                log.info(LOGMSG_WAR_SEC_LOGIN_FAILED.format(username))
                return None
            # If user does not exist on the DB and not self user registration, go away
            if not user and not self.auth_user_registration:
                return None
            # User does not exist, create one if self registration.
            elif not user and self.auth_user_registration:
                new_user = self._search_ldap(ldap, con, username)
                if not new_user:
                    log.warning(LOGMSG_WAR_SEC_NOLDAP_OBJ.format(username))
                    return None
                ldap_user_info = new_user[0][1]
                if self.auth_user_registration and user is None:
                    user = self.add_user(
                        username=username,
                        first_name=self.ldap_extract(
                            ldap_user_info, self.auth_ldap_firstname_field, username
                        ),
                        last_name=self.ldap_extract(
                            ldap_user_info, self.auth_ldap_lastname_field, username
                        ),
                        email=self.ldap_extract(
                            ldap_user_info,
                            self.auth_ldap_email_field,
                            username + "@email.notfound",
                        ),
                        role=self.find_role(self.auth_user_registration_role),
                    )

            self.update_user_auth_stat(user)
            return user

        except ldap.LDAPError as e:
            if type(e.message) == dict and "desc" in e.message:
                log.error(LOGMSG_ERR_SEC_AUTH_LDAP.format(e.message["desc"]))
                return None
            else:
                log.error(e)
                return None