# Path: stack_configs/ldap_functions.py
# def getLDAPConnWithUser(dn,password):
#     #ldap3
#     #returns bound connection object if user authenticated
#     if settings.LDAP3['validate_certs']:
#         validate=ssl.CERT_REQUIRED
#     else:
#         validate=ssl.CERT_NONE
#         
#     tls = Tls(validate=validate, version=ssl.PROTOCOL_TLSv1, ca_certs_file=settings.LDAP3['path_to_ca_cert'])
#     server = Server(settings.LDAP3['host'],port=settings.LDAP3['port'], get_info=ALL,tls=tls)
#     logger.info('Connecting to ldap as %s at %s',dn,settings.LDAP3['host'] )
#    
#     con = Connection(server,dn,password,auto_bind=False,raise_exceptions=settings.DEBUG)
#     con.open()
#     
#     if settings.LDAP3['use_start_tls']:
#         con.start_tls()
#     con.bind()  
#         
#     return con


from django.conf import settings
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User,Group
from stack_configs.ldap_functions import getLDAPConnWithUser
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
import logging

logger = logging.getLogger(__name__)

class OpenLdapBackend(object):
    """
    Authenticate against openLdap using LDAP3 library
    
    The authenticate function and groupsync function is modified using LDAP3
    All other functions are copied as is from 
    https://github.com/django/django/blob/master/django/contrib/auth/backends.py#L1
    
    """

    def authenticate(self, username, password):
        
        #authenticate with cn=username
        #users who are introduced via phpLDAPadmin may get different cn and uid and would not
        #authenticate. 
        ldapAttributes=[]
        for userAttribute, ldapAttribute in settings.LDAP3_ATTR_MAP.items():
            ldapAttributes.append(ldapAttribute)
        
        dn= str("cn=")+str(username)+str(",")+str(settings.AUTH_LDAP_USERS_OU_DN)
        logger.debug('connecting to ldap as user %s ',dn)
        try:

# Path: stack_configs/ldap_functions.py
# def getLDAPConnWithUser(dn,password):
#     #ldap3
#     #returns bound connection object if user authenticated
#     if settings.LDAP3['validate_certs']:
#         validate=ssl.CERT_REQUIRED
#     else:
#         validate=ssl.CERT_NONE
#         
#     tls = Tls(validate=validate, version=ssl.PROTOCOL_TLSv1, ca_certs_file=settings.LDAP3['path_to_ca_cert'])
#     server = Server(settings.LDAP3['host'],port=settings.LDAP3['port'], get_info=ALL,tls=tls)
#     logger.info('Connecting to ldap as %s at %s',dn,settings.LDAP3['host'] )
#    
#     con = Connection(server,dn,password,auto_bind=False,raise_exceptions=settings.DEBUG)
#     con.open()
#     
#     if settings.LDAP3['use_start_tls']:
#         con.start_tls()
#     con.bind()  
#         
#     return con


# Path: front/backends.py
from django.conf import settings
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User,Group
from stack_configs.ldap_functions import getLDAPConnWithUser
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
import logging


logger = logging.getLogger(__name__)

class OpenLdapBackend(object):
    """
    Authenticate against openLdap using LDAP3 library
    
    The authenticate function and groupsync function is modified using LDAP3
    All other functions are copied as is from 
    https://github.com/django/django/blob/master/django/contrib/auth/backends.py#L1
    
    """

    def authenticate(self, username, password):
        
        #authenticate with cn=username
        #users who are introduced via phpLDAPadmin may get different cn and uid and would not
        #authenticate. 
        ldapAttributes=[]
        for userAttribute, ldapAttribute in settings.LDAP3_ATTR_MAP.items():
            ldapAttributes.append(ldapAttribute)
        
        dn= str("cn=")+str(username)+str(",")+str(settings.AUTH_LDAP_USERS_OU_DN)
        logger.debug('connecting to ldap as user %s ',dn)
        try:

            conn=getLDAPConnWithUser(dn,password)