django-auth-ldap failed authentication
Asked Answered
P

2

5

I'm trying to use Django-Auth-Ldap in my project (Django 1.6, Python 2.7) but it is not working.

My active Directory shema is:

enter image description here

  1. I've tested the connection on the cmd line by installing the ldap-utils package

    sudo apt-get install ldap-utils
    
    ldapsearch -H ldap://domain.com -D "ou=Resources,ou=Company, dc=domain,dc=com" -U "user_name" -w "user_password" -v -d 1
    

The connection test works fine.

  1. I am using below code to test python-ldap connection from the shell:

    import ldap
    
    con = ldap.initialize('ldap://domain.com')
    
    con.simple_bind_s('User_mail', 'User_password')
    
    results = con.search_s('ou=Users,ou=Resources,ou=Company,dc=domain,dc=com', ldap.SCOPE_SUBTREE, "(cn=User_name)")
    

python-ldap connection works fine.

  1. My problem is how to authenticate AD users from my django login interface?

settings.py:

import ldap 
from django_auth_ldap.config import LDAPSearch

# The URL of the LDAP server.
AUTH_LDAP_SERVER_URI = "ldap://domain.com"
AUTH_LDAP_BIND_DN = "cn='User_name',ou=Resources,ou=Company,dc=domain,dc=com"   
AUTH_LDAP_BIND_PASSWORD = "User_password"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Users,ou=Resources,ou=Company,dc=domain,dc=com",ldap.SCOPE_SUBTREE, "(cn=%(user)s)")    
AUTH_LDAP_GLOBAL_OPTIONS = { ldap.OPT_REFERRALS : False }

AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)

views.py:

from django_auth_ldap.backend import LDAPBackend
auth = LDAPBackend()
user = auth.authenticate(username="User_name", password="User_password")

In the file django-ldap-debug.log I have this error:

Caught LDAPError while authenticating User_name: INVALID_CREDENTIALS({'info': '80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1', 'desc': 'Invalid credentials'},)
Picker answered 14/10, 2016 at 17:35 Comment(0)
P
10

I found the answer.

I changed the AUTH_LDAP_BIND_DN by adding (OU=Users)

I must use samAccountName instead of CN in AUTH_LDAP_USER_SEARCH

My new settings.py :

import ldap, logging
from django_auth_ldap.config import LDAPSearch

logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

AUTH_LDAP_SERVER_URI = "ldap://domain.com"
AUTH_LDAP_BIND_DN = "CN=User_name,OU=Users,OU=Resources,OU=Company,DC=domain,DC=com"
AUTH_LDAP_BIND_PASSWORD = "User_password"
AUTH_LDAP_USER_SEARCH = LDAPSearch("OU=Users,OU=Resources,OU=Company,DC=domain,DC=com",ldap.SCOPE_SUBTREE, "(samAccountName=%(user)s)")

AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)

My views.py

from django_auth_ldap.backend import LDAPBackend

def login(request):  
    if request.method == 'POST':
        form = MyLoginForm(data=request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']            
            auth = LDAPBackend()
            user = auth.authenticate(username=username, password=password)
            if user is not None:
                ....
    else:
        form = MyLoginForm()

    ....

Hope this help all :)

Picker answered 17/10, 2016 at 16:44 Comment(0)
C
0

I had similar problem but I have solved it other way around. Under AUTH_LDAP_BIND_DN have just put the user and domain and password. After that this was working like charm...

While I was investigating my ldaps issues I found above solution somehow useful, maybe also someone will benefit with my solution

LDAP_IGNORE_CERT_ERRORS = True
AUTH_LDAP_START_TLS = False

AUTH_LDAP_SERVER_URI = "ldaps://domain.com:636" 
AUTH_LDAP_BIND_DN = "[email protected]"
AUTH_LDAP_BIND_PASSWORD = "superPass"
AUTH_LDAP_USER_SEARCH = LDAPSearch(
        "OU=Company,DC=domain,DC=com",ldap.SCOPE_SUBTREE,"(sAMAccountName=%(user)s)"
        )

AUTH_LDAP_USER_ATTR_MAP = {
       "first_name": "givenName",
       "last_name": "sn",
       "email": "mail"
       }
Coen answered 22/9, 2021 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.