nickw444 / flask-ldap3-login

LDAP3 Logins for Flask/Flask-Login

Home Page:http://flask-ldap3-login.readthedocs.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: 'NoneType' object is not callable for result.user_groups

vladciobancai opened this issue · comments

Hi, I have a flask project configured with multiple blueprints and we are working to integrate the project with this module. We are receiving the below error

DEBUG:root:Validating LDAPLoginForm against LDAP
DEBUG:flask_ldap3_login:Opening connection with bind user '...'
DEBUG:flask_ldap3_login:Authentication was successful for user '...'
DEBUG:flask_ldap3_login:Destroying connection at <0x10f48ff98>
INFO:werkzeug:127.0.0.1 - - [15/Mar/2019 17:12:12] "POST /login HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/ciobancaiv2/PycharmProjects/ThePortal/app/auth/views.py", line 98, in login
    if form.validate_on_submit():
  File "/usr/local/lib/python3.7/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
    return self.is_submitted() and self.validate()
  File "/usr/local/lib/python3.7/site-packages/flask_ldap3_login/forms.py", line 69, in validate
    return self.validate_ldap()
  File "/usr/local/lib/python3.7/site-packages/flask_ldap3_login/forms.py", line 45, in validate_ldap
    result.user_groups
TypeError: 'NoneType' object is not callable

Below is the configuration for ldap
# Ldap
LDAP_HOST = 'ldap-host'
LDAP_BASE_DN = 'dc=com'
LDAP_USER_DN = 'ou=People'
LDAP_GROUP_DN = 'ou=Groups'
LDAP_BIND_USER_DN = 'secret'
LDAP_BIND_USER_PASSWORD = 'secret'
LDAP_USE_SSL = True
LDAP_PORT = 636
LDAP_GROUP_OBJECT_FILTER = '(objectclass=groupOfUniqueNames)'
LDAP_GROUP_SEARCH_SCOPE = 'SUBTREE'

From the logs we can see the user is authenticated but for some reason the group is empty, is there any way to ignore this ?

Ah looks like the form is making some assumptions about the return type, which could potentially have user_groups omitted:

if self.config.get('LDAP_SEARCH_FOR_GROUPS'):
response.user_groups = self.get_user_groups(
dn=bind_user, _connection=connection)

Should be a relatively simply fix to the forms code, will try raise a fix later today.

The problem above also occurs for me. I have isolated the problem to the _save_user decorator not registering. This means the _save_user function stays None and this causes the NoneType error.

Any suggestion on how to make sure the _save_user decorator gets loaded?

EDIT:
@ciobancai
In order to fix this problem I had to import my User model into my _init_ file. This automagically sets the _save_user callback.

I have isolated the problem to the _save_user decorator not registering.

Looks like this is the exact problem - the stack trace above looks like a red-herring to me on second glance.

@ciobancai does the above solution solve your problem? I will assume so and close this issue, but if not please re-open.

Also, just a side note: after a long internal debate, I have made the difficult decision that I will no longer be actively maintaining this repo. I am now in the process of finding a new maintainer - if this is something that may interest you, you can find more details in the README