[Zope-CVS] SVN: ldapauth/trunk/ Rename principalsource.py to
source.py
Roger Ineichen
roger at projekt01.ch
Wed Jul 7 08:22:59 EDT 2004
Log message for revision 26152:
Rename principalsource.py to source.py
Fixed tests, thanks to Nicolas for create the patch
Correct some line endings
Now you can run tests like: python test.py -s ldapauth -puv ldapauth
-=-
Modified: ldapauth/trunk/__init__.py
===================================================================
--- ldapauth/trunk/__init__.py 2004-07-07 12:16:27 UTC (rev 26151)
+++ ldapauth/trunk/__init__.py 2004-07-07 12:22:58 UTC (rev 26152)
@@ -14,6 +14,4 @@
"""A plugable authentication module for LDAP.
$Id:
-"""
-
-from principalsource import LDAPPrincipalSource
+"""
Modified: ldapauth/trunk/interfaces.py
===================================================================
--- ldapauth/trunk/interfaces.py 2004-07-07 12:16:27 UTC (rev 26151)
+++ ldapauth/trunk/interfaces.py 2004-07-07 12:22:58 UTC (rev 26152)
@@ -14,37 +14,37 @@
"""A plugable authentication module for LDAP.
$Id:
-"""
-
-from zope.schema import TextLine, Int, List, Password
-from zope.app.i18n import ZopeMessageIDFactory as _
-from zope.app.pluggableauth.interfaces import IPrincipalSource
-
-class ILDAPBasedPrincipalSource(IPrincipalSource):
- """Describe LDAP-based authentication sources."""
- host = TextLine(
- title = _(u'Hostname'),
- description = _(u'LDAP Server location'),
- default = u'localhost')
-
- port = Int(
- title = _(u'Port'),
- description = _(u'LDAP Server Port'),
- default = 389)
-
- basedn = TextLine(
- title = _(u'Base DN'),
- description = _(u'Base of the distinguished name'))
-
- login_attribute = TextLine(
- title = _(u'Login attribut name'),
- description = _(u'LDAP attribute used as login name'))
-
- manager_dn = TextLine(
- title = _(u'Manager DN'),
- description = _(u'Manager DN used to bind to the server'))
-
- manager_passwd = Password(
- title = _(u'Manager password'),
- description = _(u"Manager's password"))
-
+"""
+
+from zope.schema import TextLine, Int, List, Password
+from zope.app.i18n import ZopeMessageIDFactory as _
+from zope.app.pluggableauth.interfaces import IPrincipalSource
+
+class ILDAPBasedPrincipalSource(IPrincipalSource):
+ """Describe LDAP-based authentication sources."""
+ host = TextLine(
+ title = _(u'Hostname'),
+ description = _(u'LDAP Server location'),
+ default = u'localhost')
+
+ port = Int(
+ title = _(u'Port'),
+ description = _(u'LDAP Server Port'),
+ default = 389)
+
+ basedn = TextLine(
+ title = _(u'Base DN'),
+ description = _(u'Base of the distinguished name'))
+
+ login_attribute = TextLine(
+ title = _(u'Login attribut name'),
+ description = _(u'LDAP attribute used as login name'))
+
+ manager_dn = TextLine(
+ title = _(u'Manager DN'),
+ description = _(u'Manager DN used to bind to the server'))
+
+ manager_passwd = Password(
+ title = _(u'Manager password'),
+ description = _(u"Manager's password"))
+
Deleted: ldapauth/trunk/principalsource.py
===================================================================
--- ldapauth/trunk/principalsource.py 2004-07-07 12:16:27 UTC (rev 26151)
+++ ldapauth/trunk/principalsource.py 2004-07-07 12:22:58 UTC (rev 26152)
@@ -1,144 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""A plugable authentication module for LDAP.
-
-$Id:
-"""
-
-import ldap
-from persistent import Persistent
-from zope.app.container.contained import Contained
-from zope.app.pluggableauth.interfaces import \
- ILoginPasswordPrincipalSource, IContainerPrincipalSource
-from zope.app.location import locate
-from zope.app.pluggableauth import SimplePrincipal
-from zope.exceptions import NotFoundError
-from zope.interface import implements
-
-from interfaces import ILDAPBasedPrincipalSource
-
-class LDAPPrincipalSource(Contained, Persistent):
- """A Principal source using LDAP"""
- implements(ILoginPasswordPrincipalSource, ILDAPBasedPrincipalSource,
- IContainerPrincipalSource)
-
- def __init__(self, server=u'', port=389, basedn=u'',
- login_attribute=u'',
- manager_dn=u'', manager_passwd=u''):
- self.host = server
- self.port = port
- self.basedn = basedn
- self.login_attribute = login_attribute
- self.manager_dn = manager_dn
- self.manager_passwd = manager_passwd
-
- ### IContainer-related methods
-
- def __delitem__(self, login):
- pass
-
- # XXX We should use setitem from zope.app.container.contained
- # Would allow events and so on. This is just a test.
- # This way of registering the principal is somehow stupid since we must
- # use it each time a new principal is created. This is UGLY.
- def __setitem__(self, login, obj):
- obj.id = login
- obj.__parent__ = self
-
- def keys(self):
- pass
-
- def __iter__(self):
- pass
-
- def __getitem__(self):
- pass
-
- def get(self, key, default=None):
- pass
-
- def values(self):
- pass
-
- def __len__(self):
- pass
-
- def items(self):
- pass
-
- def __contains__(self):
- pass
-
- ### IPrincipalSource methods
-
- def getPrincipal(self, id):
- uid = id.split('\t')[2]
- l = self.__connect()
- l.simple_bind_s(self.manager_dn, self.manager_passwd)
- lsearch = l.search_s(self.basedn, ldap.SCOPE_ONELEVEL,
- '(%s=%s)' % (self.login_attribute, uid))
- if lsearch:
- uid_dn, uid_dict = lsearch[0]
- principal = SimplePrincipal(
- login = uid_dict[self.login_attribute][0],
- password = uid_dict['userPassword'][0])
- self.__setitem__(principal.login, principal)
- return principal
- else:
- raise NotFoundError, id
-
- def getPrincipals(self, name):
- if name == '' :
- search = '(%s=*)' % self.login_attribute
- else:
- search = '(%s=*%s*)' % (self.login_attribute, name)
- l = self.__connect()
- l.simple_bind_s(self.manager_dn, self.manager_passwd)
- lsearch = l.search_s(self.basedn, ldap.SCOPE_ONELEVEL, search)
-
- principals = []
- for userinfo in lsearch:
- uid_dn, uid_dict = userinfo
- principal = SimplePrincipal(
- login = uid_dict[self.login_attribute][0],
- password = uid_dict['userPassword'][0])
- self.__setitem__(principal.login, principal)
- principals.append(principal)
-
- return principals
-
- def authenticate(self, uid, password):
- if password:
- l = self.__connect()
- dn = '%s=%s,' % (self.login_attribute, uid) + self.basedn
- try:
- l.simple_bind_s(dn, password)
- principal = SimplePrincipal(login = uid, password = password)
- self.__setitem__(uid, principal)
- return principal
- except ldap.INVALID_CREDENTIALS:
- return None
- else:
- return None
-
- def __connect(self):
- conn = getattr(self, '_v_conn', None)
- if not conn:
- connectstring = 'ldap://%s:%s' % (self.host, self.port)
- connection = ldap.initialize(connectstring)
- self._v_conn = connection
- return connection
- else:
- return conn
-
Copied: ldapauth/trunk/source.py (from rev 26150, ldapauth/trunk/principalsource.py)
Added: ldapauth/trunk/test_ldapsource.py
===================================================================
Property changes on: ldapauth/trunk/test_ldapsource.py
___________________________________________________________________
Name: svn:eol-style
+ native
Added: ldapauth/trunk/tests/interfaces.py
===================================================================
Property changes on: ldapauth/trunk/tests/interfaces.py
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: ldapauth/trunk/tests/test_ldapsource.py
===================================================================
--- ldapauth/trunk/tests/test_ldapsource.py 2004-07-07 12:16:27 UTC (rev 26151)
+++ ldapauth/trunk/tests/test_ldapsource.py 2004-07-07 12:22:58 UTC (rev 26152)
@@ -14,63 +14,68 @@
"""A plugable authentication module for LDAP.
$Id:
-"""
-
-import sys
-from unittest import TestCase, TestSuite, makeSuite, main
-
-# FakeLDAP taken from LDAPUserFolder of Jens Vagelpohl
-import FakeLDAP
-if sys.modules.has_key('_ldap'):
- del sys.modules['_ldap']
-sys.modules['ldap'] = FakeLDAP
-
-import ldapauth
-from zope.exceptions import NotFoundError
-
-class LDAPPrincipalSourceTest(TestCase):
-
- def setUp(self):
- self.source = ldapauth.LDAPPrincipalSource(
- 'localhost', 389, 'ou=people,dc=fake',
- 'uid', 'cn=Manager,dc=fake', 'root')
-
- def test_getPrincipal(self):
- toto = self.source.getPrincipal('\t\ttoto_l')
- self.assertEqual(toto.password, 'toto_p')
- self.assertEqual(toto.login, 'toto_l')
- self.assertRaises(NotFoundError, self.source.getPrincipal, '\t\tmoo')
-
- def test_getPrincipals(self):
- users = self.source.getPrincipals('t')
- self.assertEquals(len(users), 3)
- for user in users:
- self.assert_('t' in user.login)
- self.assertEquals(len(self.source.getPrincipals('ta')), 1)
-
- def test_authenticate(self):
- self.assertEquals(self.source.authenticate('toto_l', 'toto_p').login,
- 'toto_l')
- self.assertEquals(self.source.authenticate('toto_l', 'toto_p').password,
- 'toto_p')
- self.assertEquals(self.source.authenticate('toto_l', 'toto'), None)
- self.assertEquals(self.source.authenticate('toto', 'toto'), None)
-
-def test_suite():
- return TestSuite((
- makeSuite(LDAPPrincipalSourceTest),
- ))
-if __name__=='__main__':
- import ldap
- l = ldap.initialize('ldap://localhost:389')
- l.simple_bind_s('cn=Manager,dc=fake', 'root')
- l.add_s('uid=toto_l,ou=people,dc=fake',
- (('uid', 'toto_l'),
- ('userPassword', 'toto_p')))
- l.add_s('uid=tata_l,ou=people,dc=fake',
- (('uid', 'tata_l'),
- ('userPassword', 'tata_p')))
- l.add_s('uid=titi_l,ou=people,dc=fake',
- (('uid', 'titi_l'),
- ('userPassword', 'titi_p')))
- main(defaultTest='test_suite')
+"""
+
+import sys
+from unittest import TestCase, TestSuite, makeSuite, main
+
+# FakeLDAP taken from LDAPUserFolder of Jens Vagelpohl
+import FakeLDAP
+if sys.modules.has_key('_ldap'):
+ del sys.modules['_ldap']
+sys.modules['ldap'] = FakeLDAP
+
+import ldap
+from ldapauth.source import LDAPPrincipalSource
+from zope.exceptions import NotFoundError
+
+class LDAPPrincipalSourceTest(TestCase):
+
+ def setUp(self):
+ l = ldap.initialize('ldap://localhost:389')
+ l.simple_bind_s('cn=Manager,dc=fake', 'root')
+ try:
+ l.add_s('uid=toto_l,ou=people,dc=fake',
+ (('uid', 'toto_l'),
+ ('userPassword', 'toto_p')))
+ l.add_s('uid=tata_l,ou=people,dc=fake',
+ (('uid', 'tata_l'),
+ ('userPassword', 'tata_p')))
+ l.add_s('uid=titi_l,ou=people,dc=fake',
+ (('uid', 'titi_l'),
+ ('userPassword', 'titi_p')))
+ except ldap.ALREADY_EXISTS:
+ pass
+
+ self.source = LDAPPrincipalSource(
+ 'localhost', 389, 'ou=people,dc=fake',
+ 'uid', 'cn=Manager,dc=fake', 'root')
+
+ def test_getPrincipal(self):
+ toto = self.source.getPrincipal('\t\ttoto_l')
+ self.assertEqual(toto.password, 'toto_p')
+ self.assertEqual(toto.login, 'toto_l')
+ self.assertRaises(NotFoundError, self.source.getPrincipal, '\t\tmoo')
+
+ def test_getPrincipals(self):
+ users = self.source.getPrincipals('t')
+ self.assertEquals(len(users), 3)
+ for user in users:
+ self.assert_('t' in user.login)
+ self.assertEquals(len(self.source.getPrincipals('ta')), 1)
+
+ def test_authenticate(self):
+ self.assertEquals(self.source.authenticate('toto_l', 'toto_p').login,
+ 'toto_l')
+ self.assertEquals(self.source.authenticate('toto_l', 'toto_p').password,
+ 'toto_p')
+ self.assertEquals(self.source.authenticate('toto_l', 'toto'), None)
+ self.assertEquals(self.source.authenticate('toto', 'toto'), None)
+
+def test_suite():
+ return TestSuite((
+ makeSuite(LDAPPrincipalSourceTest),
+ ))
+
+if __name__=='__main__':
+ main(defaultTest='test_suite')
More information about the Zope-CVS
mailing list