[Grok-dev] Fwd: Authentication and IAuthenticatorPlugin problem
Souheil CHELFOUH
trollfot at gmail.com
Fri Nov 5 09:06:27 EDT 2010
For the record....
---------- Forwarded message ----------
From: Lumir Jasiok <lumir.jasiok at vsb.cz>
Date: 2010/11/5
Subject: Re: [Grok-dev] Authentication and IAuthenticatorPlugin problem
To: Souheil CHELFOUH <trollfot at gmail.com>
On 11/5/10 11:08 AM, Souheil CHELFOUH wrote:
>
> Hello Lumir,
>
> Before we spend more time on this bug, a simple thing :
> Did you create a new application, after your set up the local utilities ?
> They are created on the creation of the application.
>
> If you did that, we'll look deeper into it !
>
It works.
I didn't realized that I need to create new instance, thanks a lot for
your advice.
I appreciate that a lot.
Lumir
> 2010/11/5 Lumir Jasiok<lumir.jasiok at vsb.cz>:
>>
>> Hi,
>>
>> I am learning Grog using Grok 1.0 Web Development (GWD) and writing
>> small application. Now I reached to the point when I need authenticate
>> users. Bud when I follow code from GWD I have problem with getting
>> IAuthenticatorPlugin - I guess. In app.py I have:
>>
>> from zope.app.authentication.authentication import PluggableAuthentication
>> from zope.app.security.interfaces import IAuthentication
>> from zope.app.authentication.interfaces import IAuthenticatorPlugin
>> from auth import setup_authentication
>> from auth import UserAuthenticatorPlugin
>>
>> class SpamView(grok.Application, grok.Container):
>> grok.local_utility(
>> PluggableAuthentication, provides=IAuthentication,
>> setup=setup_authentication,
>> )
>> grok.local_utility(
>> UserAuthenticatorPlugin, provides=IAuthenticatorPlugin,
>> name='users',
>> )
>> ...
>>
>> In auth.py I have:
>>
>> import grok
>>
>> from zope.app.authentication.session import SessionCredentialsPlugin
>> from zope.app.authentication.interfaces import ICredentialsPlugin
>> from zope.app.authentication.interfaces import IAuthenticatorPlugin
>> from zope.app.authentication.interfaces import IPrincipalInfo
>> from zope.app.authentication.interfaces import IPasswordManager
>> from zope.app.security.interfaces import IAuthentication,
>> IUnauthenticatedPrincipal, ILogout
>> from zope.securitypolicy.interfaces import IPrincipalPermissionManager
>> from zope.securitypolicy.interfaces import IPrincipalRoleManager
>> from zope.app.authentication.principalfolder import InternalPrincipal
>> from zope.interface import Interface
>> from zope import schema
>> from zope import component
>>
>> def setup_authentication(pau):
>> pau.credentialsPlugins = ['credentials']
>> pau.authenticatorPlugins = ['users']
>>
>> class MySessionCredentialsPlugin(grok.GlobalUtility,
>> SessionCredentialsPlugin):
>> grok.provides(ICredentialsPlugin)
>> grok.name('credentials')
>>
>> loginpagename = 'login'
>> loginfield = 'login'
>> passwordfield = 'password'
>>
>>
>> class ILoginForm(Interface):
>> login = schema.BytesLine(title=u'Username', required=True)
>> camefrom = schema.BytesLine(title=u'', required=False)
>> password = schema.Password(title=u'Password', required=True)
>>
>>
>> class Login(grok.Form):
>> grok.context(Interface)
>> grok.require('zope.Public')
>> label = "Login"
>> prefix = ''
>> form_fields = grok.Fields(ILoginForm)
>>
>> def setUpWidgets(self, ignore_request=False):
>> super(Login,self).setUpWidgets(ignore_request)
>> self.widgets['camefrom'].type = 'hidden'
>>
>> @grok.action('login')
>> def handle_login(self, **data):
>> print data
>> self.redirect(self.request.form.get('camefrom',
>> self.url(grok.getSite())))
>>
>> class Logout(grok.View):
>> grok.context(Interface)
>> grok.require('zope.Public')
>>
>> def update(self):
>> if not
>> IUnauthenticatedPrincipal.providedBy(self.request.principal):
>> auth = component.getUtility(IAuthentication)
>> ILogout(auth).logout(self.request)
>>
>> class UserAuthenticatorPlugin(grok.LocalUtility):
>> grok.implements(IAuthenticatorPlugin)
>> grok.name('users')
>>
>> def __init__(self):
>> print "UAP"
>> self.user_folder = UserFolder()
>>
>> def authenticateCredentials(self, credentials):
>> print credentials
>> if not isinstance(credentials, dict):
>> return None
>> if not ('login' in credentials and 'password' in credentials):
>> return None
>> account = self.getAccount(credentials['login'])
>> print "Account " + str(account)
>>
>> if account is None:
>> print "No account that name"
>> return None
>> if not account.checkPassword(credentials['password']):
>> return None
>> return PrincipalInfo(id=account.name,
>> title=account.real_name,
>> description=account.real_name)
>>
>> def principalInfo(self, id):
>> account = self.getAccount(id)
>> if account is None:
>> return None
>> return PrincipalInfo(id=account.name,
>> title=account.real_name,
>> description=account.real_name)
>>
>> def getAccount(self, login):
>> return login in self.user_folder and self.user_folder[login] or
>> None
>>
>> def addUser(self, username, password, real_name, role):
>> if username not in self.user_folder:
>> user = Account(username, password, real_name, role)
>> self.user_folder[username] = user
>> role_manager = IPrincipalRoleManager(grok.getSite())
>> if role==u'Project Manager':
>>
>> role_manager.assignRoleToPrincipal('spam.ProjectManager',username)
>> else:
>>
>> role_manager.assignRoleToPrincipal('spam.ProjectMember',username)
>>
>> def listUsers(self):
>> return self.user_folder.values()
>>
>> class Account(grok.Model):
>> def __init__(self, name, password, real_name, role):
>> self.name = name
>> self.real_name = real_name
>> self.role = role
>> self.setPassword(password)
>>
>> def setPassword(self, password):
>> passwordmanager = component.getUtility(IPasswordManager, 'SHA1')
>> self.password = passwordmanager.encodePassword(password)
>>
>> def checkPassword(self, password):
>> passwordmanager = component.getUtility(IPasswordManager, 'SHA1')
>> return passwordmanager.checkPassword(self.password, password)
>>
>> class IAddUserForm(Interface):
>> login = schema.BytesLine(title=u'Username', required=True)
>> password = schema.Password(title=u'Password', required=True)
>> confirm_password = schema.Password(title=u'Confirm password',
>> required=True)
>> real_name = schema.BytesLine(title=u'Real name', required=True)
>> role = schema.Choice(title=u'User role',
>> values=[u'Project Member', u'Project Manager'],
>> required=True)
>>
>> class AddUser(grok.Form):
>> grok.context(Interface)
>> grok.require('zope.ManageApplication')
>> label = "Add user"
>>
>> form_fields = grok.Fields(IAddUserForm)
>>
>> @grok.action('add')
>> def handle_add(self, **data):
>>
>> print data
>> users = component.getUtility(IAuthenticatorPlugin,'users')
>>
>> users.addUser(data['login'],data['password'],data['real_name'],data['role'])
>> self.redirect(self.url(grok.getSite(),'userlist'))
>>
>> class UserFolder(grok.Container):
>> pass
>>
>> class UserList(grok.View):
>> grok.context(Interface)
>> grok.require('zope.ManageApplication')
>>
>> def update(self):
>> users = component.getUtility(IAuthenticatorPlugin,'users')
>> self.users = users.listUsers()
>>
>> class ViewSpams(grok.Permission):
>> grok.name('spam.view')
>>
>> class ProjectMemberRole(grok.Role):
>> grok.name('spam.ProjectMember')
>> grok.permissions('spam.view')
>>
>> class ProjectManagerRole(grok.Role):
>> grok.name('spam.ProjectManager')
>> grok.permissions('spam.view')
>>
>>
>>
>> When I tried as site admin add or list users, I got error message:
>>
>> Traceback (most recent call last):
>> File
>> "/home/jas02/test/spam_view/eggs/zope.publisher-3.12.3-py2.6.egg/zope/publisher/publish.py",
>> line 134, in publish
>> result = publication.callObject(request, obj)
>> File
>> "/home/jas02/test/spam_view/eggs/grok-1.1.1-py2.6.egg/grok/publication.py",
>> line 89, in callObject
>> return super(ZopePublicationSansProxy, self).callObject(request, ob)
>> File
>> "/home/jas02/test/spam_view/eggs/zope.app.publication-3.10.2-py2.6.egg/zope/app/publication/zopepublication.py",
>> line 205, in callObject
>> return mapply(ob, request.getPositionalArguments(), request)
>> File
>> "/home/jas02/test/spam_view/eggs/zope.publisher-3.12.3-py2.6.egg/zope/publisher/publish.py",
>> line 109, in mapply
>> return debug_call(obj, args)
>> File
>> "/home/jas02/test/spam_view/eggs/zope.publisher-3.12.3-py2.6.egg/zope/publisher/publish.py",
>> line 115, in debug_call
>> return obj(*args)
>> File
>> "/home/jas02/test/spam_view/eggs/grokcore.view-1.13.2-py2.6.egg/grokcore/view/components.py",
>> line 92, in __call__
>> mapply(self.update, (), self.request)
>> File
>> "/home/jas02/test/spam_view/eggs/zope.publisher-3.12.3-py2.6.egg/zope/publisher/publish.py",
>> line 109, in mapply
>> return debug_call(obj, args)
>> File
>> "/home/jas02/test/spam_view/eggs/zope.publisher-3.12.3-py2.6.egg/zope/publisher/publish.py",
>> line 115, in debug_call
>> return obj(*args)
>> File "/home/jas02/test/spam_view/src/spam_view/auth.py", line 162, in
>> update
>> users = component.getUtility(IAuthenticatorPlugin,'users')
>> File
>> "/home/jas02/test/spam_view/eggs/zope.component-3.9.1-py2.6.egg/zope/component/_api.py",
>> line 171, in getUtility
>> raise ComponentLookupError(interface, name)
>> ComponentLookupError: (<InterfaceClass
>> zope.pluggableauth.interfaces.IAuthenticatorPlugin>, 'users')
>>
>> I don't now what is problem. It seems that I can't get utility
>> IAuthenticatorPlugin. What did I do wrong?
>>
>> Thanks for any help.
>>
>> Lumir
>>
>> --
>> Lumír Jasiok
>> VSB-TU Ostrava - Computer centre
>> Tel: +420 59 732 3189
>> E-mail: lumir.jasiok at vsb.cz
>> http://www.vsb.cz
>>
>> _______________________________________________
>> Grok-dev mailing list
>> Grok-dev at zope.org
>> https://mail.zope.org/mailman/listinfo/grok-dev
>>
--
Lumír Jasiok
VSB-TU Ostrava - Computer centre
Tel: +420 59 732 3189
E-mail: lumir.jasiok at vsb.cz
http://www.vsb.cz
More information about the Grok-dev
mailing list