Skip to content

Commit

Permalink
- Make sure that the password is always bytes when passed into the pr…
Browse files Browse the repository at this point in the history
…incipal

  registry.

- Fix deprecation warnings.
  • Loading branch information
strichter committed Mar 3, 2013
1 parent de4bfbf commit b1546ce
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 34 deletions.
5 changes: 4 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ CHANGES
4.0.0a2 (unreleased)
--------------------

- Nothing changed yet.
- Make sure that the password is always bytes when passed into the principal
registry.

- Fix deprecation warnings.


4.0.0a1 (2013-02-22)
Expand Down
28 changes: 14 additions & 14 deletions src/zope/principalregistry/meta.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@

<meta:directives namespace="http://namespaces.zope.org/zope">

<meta:directive
name="principal"
<meta:directive
name="principal"
schema=".metadirectives.IDefinePrincipalDirective"
handler=".metaconfigure.principal"
/>
<meta:directive
name="unauthenticatedPrincipal"

<meta:directive
name="unauthenticatedPrincipal"
schema=".metadirectives.IDefineUnauthenticatedPrincipalDirective"
handler=".metaconfigure.unauthenticatedPrincipal"
/>
<meta:directive
name="unauthenticatedGroup"

<meta:directive
name="unauthenticatedGroup"
schema=".metadirectives.IDefineUnauthenticatedGroupDirective"
handler=".metaconfigure.unauthenticatedGroup"
/>
<meta:directive
name="authenticatedGroup"

<meta:directive
name="authenticatedGroup"
schema=".metadirectives.IDefineAuthenticatedGroupDirective"
handler=".metaconfigure.authenticatedGroup"
/>
<meta:directive
name="everybodyGroup"

<meta:directive
name="everybodyGroup"
schema=".metadirectives.IDefineEverybodyGroupDirective"
handler=".metaconfigure.everybodyGroup"
/>
Expand Down
3 changes: 3 additions & 0 deletions src/zope/principalregistry/metaconfigure.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def _principal():

def principal(_context, id, title, login,
password, description='', password_manager="Plain Text"):
# Make sure password is encoded to bytes, which is required by the
# principal registry.
password = password.encode('utf-8')
_context.action(
discriminator = ('principal', id),
callable = principalregistry.principalRegistry.definePrincipal,
Expand Down
4 changes: 3 additions & 1 deletion src/zope/principalregistry/principalregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def authenticate(self, request):
if a is not None:
login = a.getLogin()
if login is not None:
p = self.__principalsByLogin.get(login, None)
# The login will be in bytes, but the registry stores them
# using strings.
p = self.__principalsByLogin.get(login.decode(), None)
if p is not None:
password = a.getPassword()
if p.validate(password):
Expand Down
36 changes: 18 additions & 18 deletions src/zope/principalregistry/tests/test_principalregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def testDup(self):

def testSearch(self):
r = self.reg.getPrincipals('J')
self.assertEquals(len(r), 1)
self.failUnless(r[0] is self.reg.getPrincipal('2'))
self.assertEqual(len(r), 1)
self.assertTrue(r[0] is self.reg.getPrincipal('2'))

def testByLogin(self):
tim = self.reg.getPrincipalByLogin('tim')
self.assertEquals(tim.getLogin(), 'tim')
self.assertEqual(tim.getLogin(), 'tim')
jim = self.reg.getPrincipalByLogin('jim')
self.assertEquals(jim.getLogin(), 'jim')
self.assertEqual(jim.getLogin(), 'jim')
self.assertRaises(KeyError,
self.reg.getPrincipalByLogin, 'kim')

Expand All @@ -98,41 +98,41 @@ def testValidation(self):
self.assertFalse(tim.validate('12'))

def testAuthenticate(self):
req = Request(('tim', '123'))
req = Request((b'tim', b'123'))
pid = self.reg.authenticate(req).id
self.assertEquals(pid, '1')
req = Request(('tim', '1234'))
self.assertEqual(pid, '1')
req = Request((b'tim', b'1234'))
p = self.reg.authenticate(req)
self.assertEquals(p, None)
req = Request(('kim', '123'))
self.assertEqual(p, None)
req = Request((b'kim', b'123'))
p = self.reg.authenticate(req)
self.assertEquals(p, None)
self.assertEqual(p, None)

def testUnauthorized(self):
request = Request(None)
self.reg.unauthorized(self.reg.unauthenticatedPrincipal(), request)
self.assertEquals(request.challenge, 'basic realm="Zope"')
self.assertEqual(request.challenge, 'basic realm="Zope"')
request = Request(None)
self.reg.unauthorized(None, request)
self.assertEquals(request.challenge, 'basic realm="Zope"')
self.assertEqual(request.challenge, 'basic realm="Zope"')
request = Request(None)
self.reg.unauthorized("1", request)
self.assertEquals(request.challenge, None)
self.assertEqual(request.challenge, None)

def testDefaultPrincipal(self):
self.assertEquals(self.reg.unauthenticatedPrincipal(), None)
self.assertEqual(self.reg.unauthenticatedPrincipal(), None)
self.assertRaises(DuplicateId, self.reg.defineDefaultPrincipal,
"1", "tim")
self.reg.defineDefaultPrincipal("everybody", "Default Principal")
self.assertEquals(self.reg.unauthenticatedPrincipal().id, "everybody")
self.assertEqual(self.reg.unauthenticatedPrincipal().id, "everybody")
self.reg.defineDefaultPrincipal("anybody", "Default Principal",
"This is the default headmaster")
self.assertEquals(self.reg.unauthenticatedPrincipal().id, "anybody")
self.assertEqual(self.reg.unauthenticatedPrincipal().id, "anybody")
self.assertRaises(PrincipalLookupError,
self.reg.getPrincipal, "everybody")
p = self.reg.getPrincipal("anybody")
self.assertEquals(p.id, "anybody")
self.assertEquals(p.title, "Default Principal")
self.assertEqual(p.id, "anybody")
self.assertEqual(p.title, "Default Principal")
self.assertRaises(DuplicateId, self.reg.definePrincipal,
"anybody", "title")

Expand Down

0 comments on commit b1546ce

Please sign in to comment.