[Checkins] SVN: Sandbox/luciano/kirbi/src/kirbi/ implemented
IAuthenticatorPlugin in UserFolder class
Luciano Ramalho
luciano at ramalho.org
Wed Aug 15 23:07:33 EDT 2007
Log message for revision 78864:
implemented IAuthenticatorPlugin in UserFolder class
Changed:
U Sandbox/luciano/kirbi/src/kirbi/ftests/user.txt
U Sandbox/luciano/kirbi/src/kirbi/user.py
-=-
Modified: Sandbox/luciano/kirbi/src/kirbi/ftests/user.txt
===================================================================
--- Sandbox/luciano/kirbi/src/kirbi/ftests/user.txt 2007-08-16 03:06:33 UTC (rev 78863)
+++ Sandbox/luciano/kirbi/src/kirbi/ftests/user.txt 2007-08-16 03:07:32 UTC (rev 78864)
@@ -2,11 +2,11 @@
Testing user funcionality
==========================
-Users can be adapted to IPrincipalInfo::
+Users can be adapted to ``IPrincipalInfo``::
>>> from kirbi.user import IUser, User
>>> from zope.app.authentication.interfaces import IPrincipalInfo
- >>> alice = User('alice', u'Vincent Damon Furnier', u'headless-chicken')
+ >>> alice = User(u'alice', u'Vincent Damon Furnier', u'1234')
>>> IUser.providedBy(alice)
True
>>> IPrincipalInfo.providedBy(alice)
@@ -19,6 +19,41 @@
>>> principal.title = u'Alice Cooper'
>>> alice.name
u'Alice Cooper'
+
+Each ``Kirbi`` instance contains a ``UserFolder`` which provides
+``IAuthenticatorPlugin`` so it can be used to authenticate users.
+To test this, we need to setup a Kirbi app::
+ >>> from kirbi.app import Kirbi
+ >>> root = getRootFolder()
+ >>> kirbi_app = root['kirbi'] = Kirbi()
+ >>> kirbi_app.user_folder
+ <kirbi.user.UserFolder object ...>
+
+ >>> from zope.app.authentication.interfaces import IAuthenticatorPlugin
+ >>> IAuthenticatorPlugin.providedBy(kirbi_app.user_folder)
+ True
+
+Now we put the ``alice`` user created before in the ``user_folder``, and
+exercise the ``IAuthenticatorPlugin`` methods::
+
+ >>> kirbi_app.user_folder[u'alice'] = alice
+ >>> kirbi_app.user_folder.principalInfo(u'alice')
+ {'login': u'alice'}
+ >>> kirbi_app.user_folder.principalInfo('nonexistent-user') is None
+ True
+
+ >>> good_credentials = {'login':u'alice','password':u'1234'}
+ >>> bad_login_cred = {'login':u'bob','password':u'1234'}
+ >>> bad_passwd_cred = {'login':u'alice','password':u'9999'}
+ >>> kirbi_app.user_folder.authenticateCredentials(good_credentials)
+ {'login': u'alice'}
+ >>> kirbi_app.user_folder.authenticateCredentials(bad_login_cred) is None
+ True
+ >>> kirbi_app.user_folder.authenticateCredentials(bad_passwd_cred) is None
+ True
+
+
+
Modified: Sandbox/luciano/kirbi/src/kirbi/user.py
===================================================================
--- Sandbox/luciano/kirbi/src/kirbi/user.py 2007-08-16 03:06:33 UTC (rev 78863)
+++ Sandbox/luciano/kirbi/src/kirbi/user.py 2007-08-16 03:07:32 UTC (rev 78864)
@@ -5,10 +5,26 @@
from zope.interface import Interface, implements, invariant, Invalid
from zope import schema
import sha
+import app
class UserFolder(grok.Container):
- pass
+ implements(IAuthenticatorPlugin)
+ def principalInfo(self, id):
+ """Find a principal given an id"""
+ if id in self:
+ # in Kirbi, the login and the id are the same
+ return {'login' : id}
+
+ def authenticateCredentials(self, credentials):
+ """Authenticate a principal"""
+ login = credentials['login']
+ user = self.get(login)
+ if user is not None:
+ given_hash = sha.new(credentials['password']).hexdigest()
+ if user.password == given_hash:
+ return {'login':login}
+
class User(grok.Container):
"""A Kirbi user implementation.
@@ -18,7 +34,7 @@
>>> alice = User('alice', u'Alice Cooper', u'headless-chicken')
>>> IUser.providedBy(alice)
True
- >>> alice.passwordHash()
+ >>> alice.password
'f030ff587c602e0e9a68aba75f41c51a0dc22c62'
>>> alice.name_and_login()
u'Alice Cooper (alice)'
@@ -28,17 +44,14 @@
login = u''
name = u''
- password = u''
+ password = ''
def __init__(self, login, name, password):
super(User, self).__init__()
self.login = login
self.name = name
- self.password = password
+ self.password = sha.new(password).hexdigest()
- def passwordHash(self):
- return sha.new(self.password).hexdigest()
-
def name_and_login(self):
if self.name:
return '%s (%s)' % (self.name, self.login)
@@ -104,28 +117,3 @@
self.context[login] = User(**data)
self.redirect(self.url(login))
-
-class UserAuthenticationPlugin(object):
- """Simple authentication and search plugin"""
- implements(IAuthenticatorPlugin)
-
- principals = (
- {'id':'alice', 'login':'alice', 'password':'123'},
- {'id':'bob', 'login':'bob', 'password':'123'}
- )
-
- prefix = "" # principal id prefix
-
- def principalInfo(self, id):
- """Find a principal given an id"""
- for principal in self.principals:
- if self.prefix + "." + principal['id'] == id:
- return {'login' : principal['login']}
-
- def authenticateCredentials(self, credentials):
- """Authenticate a principal"""
- for principal in self.principals:
- if credentials['login']==principal['login'] and \
- credentials['password']==principal['password']:
- return (self.prefix + "." + principal['id'],
- {'login' : principal['login']})
More information about the Checkins
mailing list