'NoneType' object is not subscriptable when authenticating user
See original GitHub issueI have a Mezzanine CMS using Django’s LDAP authentication backend. Authenticating a user causes a crash in django-auth-ldap code. I’m just setting up the LDAP authentication so there may be some error in the setup, but username and password should be correct and anyway correct behavior would be to throw a proper exception.
Quickly looking seems that self.attrs[attr] is None and there is no check for it, causing the error.
`Environment:
Request Method: POST Request URL: http://staging.tampere.hacklab.fi/admin/login/?next=/admin/
Django Version: 1.11.15 Python Version: 3.6.6 Installed Applications: (‘mezzanine.boot’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘django.contrib.redirects’, ‘django.contrib.sessions’, ‘django.contrib.sites’, ‘django.contrib.sitemaps’, ‘mezzanine.conf’, ‘mezzanine.core’, ‘mezzanine.generic’, ‘mezzanine.pages’, ‘mezzanine.blog’, ‘mezzanine.forms’, ‘mezzanine.galleries’, ‘mezzanine.twitter’, ‘filebrowser_safe’, ‘grappelli_safe’, ‘django.contrib.admin’, ‘django.contrib.staticfiles’, ‘django_comments’) Installed Middleware: (‘django.contrib.sessions.middleware.SessionMiddleware’, ‘django.middleware.common.CommonMiddleware’, ‘django.middleware.csrf.CsrfViewMiddleware’, ‘django.contrib.auth.middleware.AuthenticationMiddleware’, ‘django.contrib.auth.middleware.SessionAuthenticationMiddleware’, ‘django.contrib.messages.middleware.MessageMiddleware’, ‘django.middleware.clickjacking.XFrameOptionsMiddleware’, ‘mezzanine.core.request.CurrentRequestMiddleware’, ‘mezzanine.core.middleware.RedirectFallbackMiddleware’, ‘mezzanine.core.middleware.TemplateForDeviceMiddleware’, ‘mezzanine.core.middleware.TemplateForHostMiddleware’, ‘mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware’, ‘mezzanine.core.middleware.SitePermissionMiddleware’, ‘mezzanine.pages.middleware.PageMiddleware’)
Traceback:
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/core/handlers/exception.py” in inner 41. response = get_response(request)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/core/handlers/base.py” in _legacy_get_response 249. response = self._get_response(request)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/core/handlers/base.py” in _get_response 178. response = middleware_method(request, callback, callback_args, callback_kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/mezzanine/core/middleware.py” in process_view 40. response = view_func(request, *view_args, **view_kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/views/decorators/cache.py” in _wrapped_view_func 57. response = view_func(request, *args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/contrib/admin/sites.py” in login 393. return LoginView.as_view(**defaults)(request)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/views/generic/base.py” in view 68. return self.dispatch(request, *args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/utils/decorators.py” in _wrapper 67. return bound_func(*args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/views/decorators/debug.py” in sensitive_post_parameters_wrapper 76. return view(request, *args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/utils/decorators.py” in bound_func 63. return func.get(self, type(self))(*args2, **kwargs2)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/utils/decorators.py” in _wrapper 67. return bound_func(*args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/utils/decorators.py” in _wrapped_view 149. response = view_func(request, *args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/utils/decorators.py” in bound_func 63. return func.get(self, type(self))(*args2, **kwargs2)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/utils/decorators.py” in _wrapper 67. return bound_func(*args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/views/decorators/cache.py” in _wrapped_view_func 57. response = view_func(request, *args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/utils/decorators.py” in bound_func 63. return func.get(self, type(self))(*args2, **kwargs2)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/contrib/auth/views.py” in dispatch 90. return super(LoginView, self).dispatch(request, *args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/views/generic/base.py” in dispatch 88. return handler(request, *args, **kwargs)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/views/generic/edit.py” in post 182. if form.is_valid():
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/forms/forms.py” in is_valid 183. return self.is_bound and not self.errors
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/forms/forms.py” in errors 175. self.full_clean()
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/forms/forms.py” in full_clean 385. self._clean_form()
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/forms/forms.py” in _clean_form 412. cleaned_data = self.clean()
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/contrib/auth/forms.py” in clean 187. self.user_cache = authenticate(self.request, username=username, password=password)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/contrib/auth/init.py” in authenticate 70. user = _authenticate_with_backend(backend, backend_path, request, credentials)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django/contrib/auth/init.py” in _authenticate_with_backend 116. return backend.authenticate(*args, **credentials)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django_auth_ldap/backend.py” in authenticate 150. user = self.authenticate_ldap_user(ldap_user, password)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django_auth_ldap/backend.py” in authenticate_ldap_user 210. return ldap_user.authenticate(password)
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django_auth_ldap/backend.py” in authenticate 350. self._get_or_create_user()
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django_auth_ldap/backend.py” in _get_or_create_user 604. self._populate_user()
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django_auth_ldap/backend.py” in _populate_user 623. self._populate_user_from_attributes()
File “/mezzanine/virtualenv/lib/python3.6/site-packages/django_auth_ldap/backend.py” in _populate_user_from_attributes 629. value = self.attrs[attr][0]
Exception Type: TypeError at /admin/login/ Exception Value: ‘NoneType’ object is not subscriptable `
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
I did the same change and it seemed to work. AUTH_LDAP_USER_DN_TEMPLATE seems to be broken at the moment. My configuration worked with older version so this is a regression.
Could someone in this thread test #102 and report back please? I’m unable to craft a real world scenario but believe it will fix the case for the provided stack trace. If it passes for you, I’ll merge and release. Thanks!