LDAP anybody?

102 views
Skip to first unread message

mdipierro

unread,
Apr 30, 2009, 2:30:10 PM4/30/09
to web2py Web Framework
It should be easy to include LDAP support in web2py.
If you are familiar with Auth in tools, something like this should
work:

"""
based
on
http://www.packtpub.com/article/installing-and-configuring-the-python-ldap-library-and-binding-to-an-ldap-directory
"""
import ldap, ldap.sasl
import sys, getpass
from gluon.tools import Auth

class AuthLDAP(Auth):
def login(self):
form=SQLFORM('username:',INPUT(_name='username'),
'password:',INPUT
(_name='password',_type='password'),
INPUT(_type='submit',_value='login'))
if form.accepts(request.vars,session):
try:
con = ldap.initialize("ldap://localhost")
auth_tokens = ldap.sasl.digest_md5(form.vars.username,

reqest.vars.password)
con.sasl_interactive_bind_s( "", auth_tokens )
con.unbind()
user = self.setings.table_user
users = self.db
(user.username==form.vars.username).select():
if not users:
user_id = user.insert(username=form.vars.username,
password=form.vars.username)
group_id = self.add_group('user_%i' % user_id)
self.add_membership(group_id, user_id)
user = user.filter_fields(user[user_id],id=True)
else:
user = user.filter_fields(users[0],id=True)
self.user = user
session.auth = Storage(user=user,
last_visit=request.now,

expiration=self.settings.expiration)
# ... do onaccept and session.flash and redirect as in
Auth.login
except ldap.LDAPError, e::
pass
return dict(form=form)


Can you help me test it and fill the blanks?

Massimo

Fran

unread,
Apr 30, 2009, 5:12:26 PM4/30/09
to web2py Web Framework
On Apr 30, 7:30 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Can you help me test it and fill the blanks?

I can have a go at testing, yes :)
Can do it against Active Directory...& OpenLDAP too if I get time :)

I've copy/paste/cleaned-up formatting, installed python-ldap &
corrected a couple of typos, but I still get this:

from applications.sahana.modules.ldap import AuthLDAP
File "C:\Bin\web2py\applications\sahana\modules\ldap.py", line 1, in
<module>
import ldap, ldap.sasl
ImportError: No module named sasl

import ldap.sasl works fine in the Web2Py shell...

Anyway, I switched to simple binds & my form doesn't display with
{{=form}}, I just get:
{'form': <gluon.html.FORM object at 0x01FEBBB0>}

Again, this looks fine in the Web2Py shell.

My modification sent by email (copy/paste will get destroyed
formatting)

F

mdipierro

unread,
Apr 30, 2009, 5:30:32 PM4/30/09
to web2py Web Framework
{{=form}} does not work? Is this because of ldap?

Massimo

Fran

unread,
Apr 30, 2009, 5:43:05 PM4/30/09
to web2py Web Framework
On Apr 30, 10:30 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> {{=form}} does not work? Is this because of ldap?

I doubt because of LDAP but something in the construction of the
class..
Am trying to replicate it in a simple test controller & it works fine
there.

mdipierro

unread,
Apr 30, 2009, 6:38:10 PM4/30/09
to web2py Web Framework
yes I guess

form=SQLFORM('username:',INPUT(_name='username'),
'password:',INPUT
(_name='password',_type='password'),
INPUT(_type='submit',_value='login'))

should have been

form=FORM('username:',INPUT(_name='username'),
'password:',INPUT
(_name='password',_type='password'),
INPUT(_type='submit',_value='login'))

Reply all
Reply to author
Forward
0 new messages