title: Django LDAP authn backend: user authenticated but unable to login
body: <p>I have a strange issue with django ldap backend.</p>
  
  <p>this is my configuration:</p>
  
  <pre><code>AUTHENTICATION_BACKENDS = (
      'django_auth_ldap.backend.LDAPBackend',
      'django.contrib.auth.backends.ModelBackend'
  )
  
  AUTH_LDAP_SERVER_URI = "ldap://XXXXXXx"
  AUTH_LDAP_BIND_DN = "CN=Pti Release,OU=Service-Accounts,OU=DD,DC=ad,DC=como,DC=com"
  AUTH_LDAP_BIND_PASSWORD = "XXXXXXXX"
  AUTH_LDAP_USER_SEARCH = LDAPSearch("OU=DD,DC=ad,DC=como,DC=com", ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
  AUTH_LDAP_GLOBAL_OPTIONS = {
      ldap.OPT_X_TLS_REQUIRE_CERT: False,
  }
  
  AUTH_LDAP_GROUP_SEARCH = LDAPSearch("OU=DD,DC=ad,DC=como,DC=com", ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
  
  AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    "is_superuser" : "CN=airflow-super-users_GLOBAL,OU=Global,OU=Security-Groups,OU=DD,DC=ad,DC=como,DC=com",
       "is_active" : "CN=airflow-data-profilers_GLOBAL,OU=Global,OU=Security-Groups,OU=DD,DC=ad,DC=como,DC=com",
  }
  
  AUTH_LDAP_GROUP_TYPE = GroupOfNamesType()
  
  AUTH_PROFILE_MODULE = 'main.UserProfile'
  
  AUTH_LDAP_USER_ATTR_MAP = {
      "first_name": "givenName",
      "last_name": "sn",
      "email": "mail",
      "country": "c"
  }
  AUTH_LDAP_ALWAYS_UPDATE_USER = True
  
  AUTH_LDAP_CACHE_GROUPS = True
  AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
  </code></pre>
  
  <p>When I attempt to login, the django auth_user is populated correctly but django shows me this form:</p>
  
  <p>Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive.</p>
  
  <p>And no way to login. </p>
  
  <p>Any ideas? </p>
  
  <p>I'm adding here more information which can help to debug this. I know yo will notice <code>VariableDoesNotExist: Failed lookup for key [is_popup]</code> but it seems not be an error <a href="https://stackoverflow.com/questions/34797884/getting-error-with-is-popup-variable-in-django-1-9">Getting error with is_popup variable in django 1.9</a>. I'm using these packages have been installed into the django virtualenv:</p>
  
  <ul>
  <li>appdirs==1.4.3 </li>
  <li>coreapi==2.3.0 </li>
  <li>coreschema==0.0.4 </li>
  <li>Django==1.11</li>
  <li>django-auth-ldap==1.2.11 </li>
  <li>django-crispy-forms==1.6.1</li>
  <li>django-enumchoicefield==0.8.0</li>
  <li>django-enumfields==0.9.0</li>
  <li>django-filter==1.0.2</li>
  <li>django-filters==0.2.1</li>
  <li>django-mysql==1.1.1</li>
  <li>djangorestframework==3.6.2</li>
  <li>drf-enum-field==0.9.1</li>
  <li>enum==0.4.6</li>
  <li>enum34==1.1.6 </li>
  <li>itypes==1.1.0 </li>
  <li>Jinja2==2.9.6 </li>
  <li>MarkupSafe==1.0</li>
  <li>MySQL-python==1.2.5 </li>
  <li>packaging==16.8 </li>
  <li>pyparsing==2.2.0</li>
  <li>python-ldap==2.4.38 </li>
  <li>pytz==2017.2</li>
  <li>requests==2.13.0 </li>
  <li>six==1.10.0</li>
  <li>uritemplate==3.0.0</li>
  </ul>
  
  <p>Database before the login attempt</p>
  
  <p>mysql> select *  from auth_user\G</p>
  
  <pre><code>*************************** 1. row ***************************
            id: 1
      password: pbkdf2_sha256$36000$giZoAk1hhuFG$aSJ1w6nOs05VjecVnFVFVYTK2h84mqHqDTKg5St6BBE=
    last_login: 2017-05-04 08:28:05.580156
  is_superuser: 1
      username: root
    first_name: 
     last_name: 
         email: 
      is_staff: 1
     is_active: 1
   date_joined: 2017-04-10 13:27:46.202097
  </code></pre>
  
  <p>django logs ( DEBUG=True, output to console )</p>
  
  <pre><code>DEBUG search_s('OU=DD,DC=ad,DC=como,DC=com', 2, '(sAMAccountName=%(user)s)') returned 1 objects: cn=alessio palma,ou=hr,ou=florence,ou=italy,ou=dd,dc=ad,dc=como,dc=com
  DEBUG (0.000) SELECT @@SQL_AUTO_IS_NULL; args=None
  DEBUG (0.000) SELECT VERSION(); args=None
  DEBUG (0.002) SELECT `auth_user`.`id`, `auth_user`.`password`, `auth_user`....
  DEBUG (0.001) INSERT INTO `auth_user` (`password`, `last_login`, `is_superuser`... 
  DEBUG (0.001) INSERT INTO `authtoken_token` (`key`, `user_id`, `created`) VALUES ('91567e9aece75cd7fc97809b8685267fc53bc983', 7, '2017-05-05 06:21:15.359687'); args=(u'91567e9aece75cd7fc97809b8685267fc53bc983', 7, u'2017-05-05 06:21:15.359687')
  DEBUG Created Django user alessio.palma
  DEBUG Populating Django user alessio.palma
  DEBUG cn=alessio palma,ou=hr,ou=florence,ou=italy,ou=dd,dc=ad,dc=como,dc=com is a member of cn=airflow-super-users_global,ou=global,ou=security-groups,ou=dd,dc=ad,dc=como,dc=com
  DEBUG cn=alessio palma,ou=hr,ou=florence,ou=italy,ou=dd,dc=ad,dc=como,dc=com is a member of cn=airflow-data-profilers_global,ou=global,ou=security-groups,ou=dd,dc=ad,dc=como,dc=com
  DEBUG (0.001) UPDATE `auth_user` SET `password` = '!QBPYrozr5C3wMRneLcBJeiWmaE4YiTizNgJURGTQ', `last_login` = NULL, `is_superuser` = 1, `username` = 'alessio.palma', `first_name` = 'Alessio', `last_name` = 'Palma', `email` = 'xxxxx@como.com', `is_staff` = 0, `is_active` = 1, `date_joined` = '2017-05-05 06:21:15.357377' WHERE `auth_user`.`id` = 7; args=(u'!QBPYrozr5C3wMRneLcBJeiWmaE4YiTizNgJURGTQ', True, u'alessio.palma', u'Alessio', u'Palma', u'alessio.palma@docomodigital.com', False, True, u'2017-05-05 06:21:15.357377', 7)
  DEBUG Exception while resolving variable 'is_popup' in template 'admin/login.html'.
  Traceback (most recent call last):
    File "/home/room/.virtualenvs/django/lib/python2.7/site-packages/django/template/base.py", line 903, in _resolve_lookup
      (bit, current))  # missing attribute
  VariableDoesNotExist: Failed lookup for key [is_popup] in u"[{'False': False, 'None': None, 'True': True}, {u'csrf_token': &lt;SimpleLazyObject: &lt;function _get_val at 0x7f5af0051d70&gt;&gt;, 'user': &lt;SimpleLazyObject: &lt;django.contrib.auth.models.AnonymousUser object at 0x7f5af83a7cd0&gt;&gt;, 'perms': &lt;django.contrib.auth.context_processors.PermWrapper object at 0x7f5af005e690&gt;, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'WARNING': 30, 'SUCCESS': 25, 'ERROR': 40}, 'messages': &lt;django.contrib.messages.storage.fallback.FallbackStorage object at 0x7f5af8484c10&gt;, u'request': &lt;WSGIRequest: POST '/admin/login/?next=/admin/'&gt;}, {}, {'username': u'', 'app_path': u'/admin/login/?next=/admin/', 'available_apps': [], 'site_name': '172.17.0.2:8000', 'form': &lt;AdminAuthenticationForm bound=True, valid=False, fields=(username;password)&gt;, 'title': u'Log in', 'site_header': u'Django administration', 'site': &lt;django.contrib.sites.requests.RequestSite object at 0x7f5af84b6d50&gt;, 'next': u'/admin/', 'site_title': u'Django site admin', u'LANGUAGE_CODE': 'en-us', 'has_permission': False, 'site_url': '/', u'LANGUAGE_BIDI': False, u'view': &lt;django.contrib.auth.views.LoginView object at 0x7f5af83a7e10&gt;}]"
  DEBUG Exception while resolving variable 'is_popup' in template 'admin/login.html'.
  Traceback (most recent call last):
    File "/home/room/.virtualenvs/django/lib/python2.7/site-packages/django/template/base.py", line 903, in _resolve_lookup
      (bit, current))  # missing attribute
  VariableDoesNotExist: Failed lookup for key [is_popup] in u"[{'False': False, 'None': None, 'True': True}, {u'csrf_token': &lt;SimpleLazyObject: &lt;function _get_val at 0x7f5af0051d70&gt;&gt;, 'user': &lt;SimpleLazyObject: &lt;django.contrib.auth.models.AnonymousUser object at 0x7f5af83a7cd0&gt;&gt;, 'perms': &lt;django.contrib.auth.context_processors.PermWrapper object at 0x7f5af005e690&gt;, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'WARNING': 30, 'SUCCESS': 25, 'ERROR': 40}, 'messages': &lt;django.contrib.messages.storage.fallback.FallbackStorage object at 0x7f5af8484c10&gt;, u'request': &lt;WSGIRequest: POST '/admin/login/?next=/admin/'&gt;}, {}, {'username': u'', 'app_path': u'/admin/login/?next=/admin/', 'available_apps': [], 'site_name': '172.17.0.2:8000', 'form': &lt;AdminAuthenticationForm bound=True, valid=False, fields=(username;password)&gt;, 'title': u'Log in', 'site_header': u'Django administration', 'site': &lt;django.contrib.sites.requests.RequestSite object at 0x7f5af84b6d50&gt;, 'next': u'/admin/', 'site_title': u'Django site admin', u'LANGUAGE_CODE': 'en-us', 'has_permission': False, 'site_url': '/', u'LANGUAGE_BIDI': False, u'view': &lt;django.contrib.auth.views.LoginView object at 0x7f5af83a7e10&gt;}]"
  INFO "POST /admin/login/?next=/admin/ HTTP/1.1" 200 1819
  </code></pre>
  
  <p>Database after the login attempt</p>
  
  <pre><code>*************************** 1. row ***************************
            id: 1
      password: pbkdf2_sha256$36000$giZoAk1hhuFG$aSJ1w6nOs05VjecVnFVFVYTK2h84mqHqDTKg5St6BBE=
    last_login: 2017-05-04 08:28:05.580156
  is_superuser: 1
      username: root
    first_name: 
     last_name: 
         email: 
      is_staff: 1
     is_active: 1
   date_joined: 2017-04-10 13:27:46.202097
  *************************** 2. row ***************************
            id: 7
      password: !QBPYrozr5C3wMRneLcBJhiWmaE4YiTizNgJURGTQ
    last_login: NULL
  is_superuser: 1
      username: alessio.palma
    first_name: Alessio
     last_name: Palma
         email: xxxxxx@como.com
      is_staff: 0
     is_active: 1
   date_joined: 2017-05-05 06:21:15.357377
  2 rows in set (0,00 sec)
  </code></pre>
  
  <p>HELP! I think all this problem is the usual comma out of place.</p>