6

Ci sono due modi per autenticare un utente che utilizza Django Auth LDAPDjango Auth LDAP - Bind diretta usando sAMAccountName

  1. Ricerca/Bind e
  2. diretto Bind.

Il primo implica la connessione al server LDAP in modo anonimo o con un account fisso e la ricerca del nome distinto dell'utente che esegue l'autenticazione. Quindi possiamo tentare di legare di nuovo con la password dell'utente.

Il secondo metodo consiste nel derivare il DN dell'utente dal suo nome utente e tentare di eseguire il binding direttamente dall'utente.

Desidero essere in grado di eseguire un binding diretto utilizzando l'id utente (sAMAccountName) e la password dell'utente che sta tentando di accedere all'applicazione. Per favore fatemi sapere se c'è un modo per raggiungere questo obiettivo? Al momento, non riesco a farlo funzionare a causa del problema spiegato di seguito.

Nel mio caso, il DN di utenti in LDAP è del seguente formato

**'CN=Steven Jones,OU=Users,OU=Central,OU=US,DC=client,DC=corp'** 

Questo si traduce in pratica a 'CN = Nome Cognome, OU = Utenti, UO = Centrale, OU = US, DC = cliente, DC = corp'

questo sono io impedire l'utilizzo diretto Bind come sAMAccountName dell'utente è sjones e questo è il parametro che corrisponde al nome utente (% utente) e non posso capire un modo per inquadrare una corretta AUTH_LDAP_USER_DN_TEMPLAT E per ricavare il DN dell'utente utilizzando.

A causa del problema di cui sopra spiegato, io sto usando Ricerca/Bind per ora, ma questo mi richiede di avere una credenziale utente fisso da specificare in auth_ldap_bind_dn e AUTH_LDAP_BIND_PASSWORD.

Ecco il mio attuale settings.py configurazione

AUTH_LDAP_SERVER_URI = "ldap://10.5.120.161:389" 
AUTH_LDAP_BIND_DN='CN=Steven Jones,OU=Users,OU=Central,OU=US,DC=client,DC=corp' 
AUTH_LDAP_BIND_PASSWORD='fga.1234' 
#AUTH_LDAP_USER_DN_TEMPLATE = 'CN=%(user)s,OU=Appl Groups,OU=Central,OU=US,DC=client,DC=corp' 
AUTH_LDAP_USER_SEARCH = LDAPSearchUnion(
    LDAPSearch("OU=Users, OU=Central,OU=US,DC=client,DC=corp",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"), 
    LDAPSearch("OU=Users,OU=Regional,OU=Locales,OU=US,DC=client,DC=corp",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"), 
    ) 
AUTH_LDAP_USER_ATTR_MAP = {"first_name": "givenName", "last_name": "sn","email":"mail"} 
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("CN=GG_BusinessApp_US,OU=Appl Groups,OU=Central,OU=US,DC=client,DC=corp",ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)") 
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType() 
AUTH_LDAP_REQUIRE_GROUP = 'CN=GG_BusinessApp_US,OU=Appl Groups,OU=Central,OU=US,DC=client,DC=corp' 

In attesa di qualche indicazione dalle persone meravigliose qui.

risposta

8

Ho avuto lo stesso problema.

I corse dall'altra parte: https://bitbucket.org/psagers/django-auth-ldap/issue/21/cant-bind-and-search-on-activedirectory

L'autore ha portato un modo per cambiare i file di libreria per Django-auth-LDAP in modo che potesse fare una legatura diretta.

è venuto giù per cambiare /django_auth_ldap/backend.py per includere due linee attorno alla riga 364:

if sticky and ldap_settings.AUTH_LDAP_USER_SEARCH: 
    self._search_for_user_dn()[/code] 

ero in grado di farlo funzionare sul mio macheine locale che era in esecuzione Arch Linux 3.9.8 -1-ARCH, ma non sono riuscito a replicarlo sul server di sviluppo con Ubuntu 13.04.

Speriamo che questo possa essere d'aiuto.

+0

Grazie @amethystdragon Sembra che questo risolva il mio problema. Darò una prova a breve. – Guddu

4

(Questo è in realtà un commento alla risposta di @ amethystdragon, ma è un mucchio di codice, quindi postare come risposta separata.) Il problema sembra ancora esistere con django_auth_ldap 1.2.5. Ecco una patch aggiornata. Se non vuoi o non puoi modificare il codice sorgente, è possibile eseguire il patch delle scimmie. Basta inserire questo codice per es. fine di settings.py. (E sì, lo so scimmia-patching è brutto.)

import ldap 
from django_auth_ldap import backend 

def monkey(self, password): 
    """ 
    Binds to the LDAP server with the user's DN and password. Raises 
    AuthenticationFailed on failure. 
    """ 
    if self.dn is None: 
    raise self.AuthenticationFailed("failed to map the username to a DN.") 

    try: 
    sticky = self.settings.BIND_AS_AUTHENTICATING_USER 

    self._bind_as(self.dn, password, sticky=sticky) 

    #### The fix --> 
    if sticky and self.settings.USER_SEARCH: 
     self._search_for_user_dn() 
    #### <-- The fix 

    except ldap.INVALID_CREDENTIALS: 
    raise self.AuthenticationFailed("user DN/password rejected by LDAP server.") 

backend._LDAPUser._authenticate_user_dn = monkey 
+0

Grazie mille. Lo farò anche io – Guddu

0

Ho anche avuto questo problema in cui il vecchio server LDAP ha avuto un dn che è iniziato con UID, ma il DN del nuovo inizia con CN (' Steven Jones '). Ho usato questa configurazione (che ha risolto per me) in setting.py:

AUTH_LDAP_BIND_DN = 'CN=adreader,CN=Users,DC=xxx, DC=yyy' 

from django_auth_ldap.config import LDAPSearch 
import ldap 
AUTH_LDAP_USER_SEARCH = LDAPSearch(base_dn='ou=People, ou=xxx, dc=yyy, dc=zzz, 
    scope=ldap.SCOPE_SUBTREE, filterstr='(sAMAccountName=%(user)s)') 
0

Ho anche avuto questo problema, ma non ho voglia di modificare il file settings.py. La soluzione per me era di commentare la riga "AUTH_LDAP_USER_DN_TEMPLATE =" uid =% (utente) s, ou = percorso, dc = a, dc = dominio "". Ho anche aggiunto NestedActiveDirectoryGroupType come parte della mia risoluzione dei problemi. Non sono sicuro che sia necessario, ma funziona ora, quindi me ne sto andando. Ecco il mio file ldap_config.py.

import ldap 

# Server URI 
AUTH_LDAP_SERVER_URI = "ldap://urlForLdap" 

# The following may be needed if you are binding to Active Directory. 
AUTH_LDAP_CONNECTION_OPTIONS = { 
     # ldap.OPT_DEBUG_LEVEL: 1, 
    ldap.OPT_REFERRALS: 0 
} 

# Set the DN and password for the NetBox service account. 
AUTH_LDAP_BIND_DN = "CN=Netbox,OU=xxx,DC=xxx,DC=xxx" 
AUTH_LDAP_BIND_PASSWORD = "password" 

# Include this setting if you want to ignore certificate errors. This might be needed to accept a self-signed cert. 
# Note that this is a NetBox-specific setting which sets: 
#  ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) 
LDAP_IGNORE_CERT_ERRORS = True 

from django_auth_ldap.config import LDAPSearch, NestedActiveDirectoryGroupType 

# This search matches users with the sAMAccountName equal to the provided username. This is required if the user's 
# username is not in their DN (Active Directory). 
AUTH_LDAP_USER_SEARCH = LDAPSearch("OU=xxx,DC=xxx,DC=xxx", 
            ldap.SCOPE_SUBTREE, 
            "(sAMAccountName=%(user)s)") 

# If a user's DN is producible from their username, we don't need to search. 
# AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=users,dc=corp,dc=loc" 

# You can map user attributes to Django attributes as so. 
AUTH_LDAP_USER_ATTR_MAP = { 
    "first_name": "givenName", 
    "last_name": "sn", 
    "email": "mail" 
} 

from django_auth_ldap.config import LDAPSearch, GroupOfNamesType, NestedActiveDirectoryGroupType 

# This search ought to return all groups to which the user belongs. django_auth_ldap uses this to determine group 
# heirarchy. 
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("dc=xxx,dc=xxx", ldap.SCOPE_SUBTREE, 
            "(objectClass=group)") 
AUTH_LDAP_GROUP_TYPE = NestedActiveDirectoryGroupType() 

# Define a group required to login. 
AUTH_LDAP_REQUIRE_GROUP = "CN=NetBox_Users,OU=NetBox,OU=xxx,DC=xxx,DC=xxx" 

# Define special user types using groups. Exercise great caution when assigning superuser status. 
AUTH_LDAP_USER_FLAGS_BY_GROUP = { 
    "is_active": "CN=NetBox_Active,OU=NetBox,OU=xxx,DC=xxx,DC=xxx", 
    "is_staff": "CN=NetBox_Staff,OU=NetBox,OU=xxx,DC=xxx,DC=xxx", 
    "is_superuser": "CN=NetBox_Superuser,OU=NetBox,OU=xxx,DC=xxx,DC=xxx" 
} 

# For more granular permissions, we can map LDAP groups to Django groups. 
AUTH_LDAP_FIND_GROUP_PERMS = True 

# Cache groups for one hour to reduce LDAP traffic 
AUTH_LDAP_CACHE_GROUPS = True 
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600