Skip to content

Commit

Permalink
Added dn as keyword argument to authenticate(), search_params as keyw…
Browse files Browse the repository at this point in the history
…ord argument to __init__. netid is now a keyword argument in authenticate(). Addresses #2, #3
  • Loading branch information
jgr68 committed Oct 27, 2014
1 parent f05831d commit e08e165
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
72 changes: 46 additions & 26 deletions ScarletLDAP3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,61 @@
class ScarletLDAP:

def __init__(self, server, search_base, port,
user_srv = ['eden', 'pegasus', 'clam'], admin_srv = ['rci']):
user_srv = ['eden', 'pegasus', 'clam'],
admin_srv = ['rci'], search_params=None):

self.server = Server(host=server, port=port, use_ssl=True)
self.base = search_base
self.user_srv = user_srv
self.admin_srv = admin_srv

def authenticate(self, netid, passwd, use_enigma=False):
# set default search parameters
self.search_params = {
'filter' : '(uid=%s)',
'scope' : SEARCH_SCOPE_WHOLE_SUBTREE,
'base' : search_base
}

if search_params is not None:

# update self.search_params from valid keys search_params.keys()
self.search_params.update(
{ key:val for key,val in search_params.items()
if key in self.search_params.keys() }
)

# establish initial connection to ldap server and bind anonymously
conn = Connection(self.server, user=netid, auto_bind=True)
def authenticate(self, passwd, netid=None, use_enigma=False, dn=None):

if dn is None:

if netid is None:
raise Exception("Must specify either netid or dn as keyword arg")

# establish initial connection to ldap server and bind anonymously
conn = Connection(self.server, user=netid, auto_bind=True)

# search for dn to authenticate against
conn.search(
search_base = self.base,
search_scope = SEARCH_SCOPE_WHOLE_SUBTREE,
search_filter = '(uid='+netid+')',
attributes = ['dn']
)

# the user may have an account in any of the servers in server_names
server_names = self.admin_srv if use_enigma else self.user_srv

dn = None
for resp in conn.response:

# check if dn is associated with a valid account in server_names
if len( [ s for s in server_names if s in resp['dn'] ] ):
dn = resp['dn']
break

if dn == None:
return False
# search for dn to authenticate against
conn.search(
search_base = self.search_params['base'],
search_scope = self.search_params['scope'],
search_filter = self.search_params['filter'] % ( netid ),
attributes = ['dn']
)

conn.unbind()
# the user may have an account in any of the servers in server_names
server_names = self.admin_srv if use_enigma else self.user_srv

for resp in conn.response:

# check if dn is associated with a valid account in server_names
if len( [ s for s in server_names if s in resp['dn'] ] ):
dn = resp['dn']
break

if dn is None:
return False

conn.unbind()

# attempt to authenticate against dn using passwd
try:
Expand Down
1 change: 1 addition & 0 deletions test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!env/bin/python3.3

from ScarletLDAP3 import ScarletLDAP
from getpass import getpass

Expand Down

0 comments on commit e08e165

Please sign in to comment.