Skip to content

Commit

Permalink
Replace ldap lib with ldap3
Browse files Browse the repository at this point in the history
Replace ldap with ldap3 because ldap lib is not py3 compatible.
Functionality is the same.
  • Loading branch information
dosomder committed Sep 9, 2014
1 parent 2d9acbe commit d7fa1bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ install:
- wget -O - https://spritelink.github.io/NIPAP/nipap.gpg.key | sudo apt-key add -
- sudo apt-get update -qq
# install dependencies for installing & running nipap
- sudo apt-get install -qq -y --force-yes python-pysqlite2 python-psycopg2 python-ipy python-ldap python-docutils postgresql postgresql-9.1-ip4r python-tornado python-flask python-flask-xml-rpc python-flask-compress
- sudo apt-get install -qq -y --force-yes python-pysqlite2 python-psycopg2 python-ipy python-docutils postgresql postgresql-9.1-ip4r python-tornado python-flask python-flask-xml-rpc python-flask-compress
- sudo pip install python3-ldap
# install dependencies for building packages and build NIPAP debian packages
- sudo apt-get install -qq -y --force-yes devscripts python-docutils
# if we are testing the upgrade, first install NIPAP packages from official repo
Expand Down
24 changes: 11 additions & 13 deletions nipap/nipap/authlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

# Used by auth modules
import sqlite3
import ldap
import ldap3
import string
import random

Expand Down Expand Up @@ -298,7 +298,7 @@ def __init__(self, name, username, password, authoritative_source, auth_options=
self._logger.debug('Creating LdapAuth instance')

self._logger.debug('LDAP URI: ' + self._ldap_uri)
self._ldap_conn = ldap.initialize(self._ldap_uri)
self._ldap_conn = ldap3.Server(self._ldap_uri)



Expand All @@ -314,29 +314,27 @@ def authenticate(self):
return self._authenticated

try:
self._ldap_conn.simple_bind_s('uid=' + self.username + ',' + self._ldap_basedn, self.password)
except ldap.SERVER_DOWN as exc:
with ldap3.Connection(self._ldap_conn, 'uid=' + self.username + ',' + self._ldap_basedn, self.password, raise_exceptions = True) as con:
res = con.search(self._ldap_basedn, '(uid=' + self.username + ')', ldap3.SEARCH_SCOPE_WHOLE_SUBTREE, attributes = ['cn'], size_limit = 1)
if (not res) or (not con.response):
self.full_name = ''
else:
self.full_name = con.response[0]['attributes']['cn'][0]
except ldap3.LDAPSocketOpenError as exc:
raise AuthError('Could not connect to LDAP server')
except (ldap.INVALID_CREDENTIALS, ldap.INVALID_DN_SYNTAX,
ldap.UNWILLING_TO_PERFORM) as exc:
except (ldap3.LDAPInvalidCredentialsResult, ldap3.LDAPInvalidDNSyntaxResult,
ldap3.LDAPUnwillingToPerformResult) as exc:
# Auth failed
self._logger.debug('erroneous password for user %s' % self.username)
self._authenticated = False
return self._authenticated


# auth succeeded
self.authenticated_as = self.username
self._authenticated = True
self.trusted = False
self.readonly = False

try:
res = self._ldap_conn.search_s(self._ldap_basedn, ldap.SCOPE_SUBTREE, 'uid=' + self.username, ['cn'])
self.full_name = res[0][1]['cn'][0]
except:
self.full_name = ''

self._logger.debug('successfully authenticated as %s, username %s' % (self.authenticated_as, self.username))
return self._authenticated

Expand Down
2 changes: 1 addition & 1 deletion nipap/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_data_files():
url = nipap.__url__,
packages = ['nipap'],
keywords = ['nipap'],
requires = ['ldap', 'sqlite3', 'IPy', 'psycopg2'],
requires = ['ldap3', 'sqlite3', 'IPy', 'psycopg2'],
data_files = get_data_files(),
classifiers = [
'Development Status :: 4 - Beta',
Expand Down

0 comments on commit d7fa1bb

Please sign in to comment.