[4073] | 1 | """Authentication for WAeUP portals. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[5900] | 4 | from zope.component import getUtility, getUtilitiesFor |
---|
[5055] | 5 | try: |
---|
| 6 | from zope.pluggableauth.plugins.session import SessionCredentialsPlugin |
---|
| 7 | except ImportError: |
---|
| 8 | # BBB |
---|
| 9 | from zope.app.authentication.session import SessionCredentialsPlugin |
---|
| 10 | try: |
---|
| 11 | from zope.pluggableauth.interfaces import ( |
---|
| 12 | ICredentialsPlugin, IAuthenticatorPlugin, IPrincipalInfo) |
---|
| 13 | from zope.password.interfaces import IPasswordManager |
---|
| 14 | except ImportError: |
---|
| 15 | # BBB |
---|
| 16 | from zope.app.authentication.interfaces import ( |
---|
| 17 | ICredentialsPlugin, IAuthenticatorPlugin, IPrincipalInfo, |
---|
| 18 | IPasswordManager) |
---|
[4129] | 19 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
| 20 | from zope.securitypolicy.principalrole import principalRoleManager |
---|
[5900] | 21 | from waeup.sirp.interfaces import IUserAccount, IAuthPluginUtility |
---|
[4073] | 22 | |
---|
| 23 | def setup_authentication(pau): |
---|
| 24 | """Set up plugguble authentication utility. |
---|
| 25 | |
---|
| 26 | Sets up an IAuthenticatorPlugin and |
---|
| 27 | ICredentialsPlugin (for the authentication mechanism) |
---|
[5900] | 28 | |
---|
| 29 | Then looks for any external utilities that want to modify the PAU. |
---|
[4073] | 30 | """ |
---|
[4576] | 31 | pau.credentialsPlugins = ['No Challenge if Authenticated', 'credentials'] |
---|
[4073] | 32 | pau.authenticatorPlugins = ['users'] |
---|
| 33 | |
---|
[5900] | 34 | # Give any third-party code and subpackages a chance to modify the PAU |
---|
| 35 | auth_plugin_utilities = getUtilitiesFor(IAuthPluginUtility) |
---|
[5901] | 36 | for name, util in auth_plugin_utilities: |
---|
[5900] | 37 | util.register(pau) |
---|
| 38 | |
---|
[4073] | 39 | class WAeUPSessionCredentialsPlugin(grok.GlobalUtility, |
---|
| 40 | SessionCredentialsPlugin): |
---|
| 41 | grok.provides(ICredentialsPlugin) |
---|
| 42 | grok.name('credentials') |
---|
| 43 | |
---|
| 44 | loginpagename = 'login' |
---|
| 45 | loginfield = 'form.login' |
---|
| 46 | passwordfield = 'form.password' |
---|
| 47 | |
---|
| 48 | class PrincipalInfo(object): |
---|
| 49 | grok.implements(IPrincipalInfo) |
---|
| 50 | |
---|
| 51 | def __init__(self, id, title, description): |
---|
| 52 | self.id = id |
---|
| 53 | self.title = title |
---|
| 54 | self.description = description |
---|
| 55 | self.credentialsPlugin = None |
---|
| 56 | self.authenticatorPlugin = None |
---|
| 57 | |
---|
| 58 | class Account(grok.Model): |
---|
[4109] | 59 | grok.implements(IUserAccount) |
---|
[4129] | 60 | |
---|
[4125] | 61 | def __init__(self, name, password, title=None, description=None, |
---|
| 62 | roles = []): |
---|
[4073] | 63 | self.name = name |
---|
[4087] | 64 | if title is None: |
---|
| 65 | title = name |
---|
| 66 | if description is None: |
---|
| 67 | description = title |
---|
| 68 | self.title = title |
---|
| 69 | self.description = description |
---|
[4073] | 70 | self.setPassword(password) |
---|
[4129] | 71 | self.setRoles(roles) |
---|
[4073] | 72 | |
---|
| 73 | def setPassword(self, password): |
---|
| 74 | passwordmanager = getUtility(IPasswordManager, 'SHA1') |
---|
| 75 | self.password = passwordmanager.encodePassword(password) |
---|
| 76 | |
---|
| 77 | def checkPassword(self, password): |
---|
| 78 | passwordmanager = getUtility(IPasswordManager, 'SHA1') |
---|
| 79 | return passwordmanager.checkPassword(self.password, password) |
---|
| 80 | |
---|
[4129] | 81 | def getRoles(self): |
---|
| 82 | prm = self._getPrincipalRoleManager() |
---|
| 83 | roles = [x[0] for x in prm.getRolesForPrincipal(self.name) |
---|
| 84 | if x[0].startswith('waeup.')] |
---|
| 85 | return roles |
---|
[5055] | 86 | |
---|
[4129] | 87 | def setRoles(self, roles): |
---|
| 88 | prm = self._getPrincipalRoleManager() |
---|
[5055] | 89 | |
---|
[4129] | 90 | old_roles = self.getRoles() |
---|
| 91 | for role in old_roles: |
---|
| 92 | # Remove old roles, not to be set now... |
---|
| 93 | if role.startswith('waeup.') and role not in roles: |
---|
| 94 | prm.unsetRoleForPrincipal(role, self.name) |
---|
| 95 | |
---|
| 96 | for role in roles: |
---|
| 97 | prm.assignRoleToPrincipal(role, self.name) |
---|
| 98 | |
---|
| 99 | roles = property(getRoles, setRoles) |
---|
| 100 | |
---|
| 101 | def _getPrincipalRoleManager(self): |
---|
| 102 | portal = grok.getSite() |
---|
| 103 | if portal is not None: |
---|
| 104 | return IPrincipalRoleManager(portal) |
---|
| 105 | return principalRoleManager |
---|
[5055] | 106 | |
---|
[4073] | 107 | class UserAuthenticatorPlugin(grok.GlobalUtility): |
---|
| 108 | grok.provides(IAuthenticatorPlugin) |
---|
| 109 | grok.name('users') |
---|
| 110 | |
---|
| 111 | def authenticateCredentials(self, credentials): |
---|
| 112 | if not isinstance(credentials, dict): |
---|
| 113 | return None |
---|
| 114 | if not ('login' in credentials and 'password' in credentials): |
---|
| 115 | return None |
---|
| 116 | account = self.getAccount(credentials['login']) |
---|
| 117 | |
---|
| 118 | if account is None: |
---|
| 119 | return None |
---|
| 120 | if not account.checkPassword(credentials['password']): |
---|
| 121 | return None |
---|
| 122 | return PrincipalInfo(id=account.name, |
---|
[4612] | 123 | title=account.title, |
---|
| 124 | description=account.description) |
---|
[4073] | 125 | |
---|
| 126 | def principalInfo(self, id): |
---|
| 127 | account = self.getAccount(id) |
---|
| 128 | if account is None: |
---|
| 129 | return None |
---|
| 130 | return PrincipalInfo(id=account.name, |
---|
[4612] | 131 | title=account.title, |
---|
| 132 | description=account.description) |
---|
[4073] | 133 | |
---|
| 134 | def getAccount(self, login): |
---|
[4087] | 135 | # ... look up the account object and return it ... |
---|
| 136 | usercontainer = self.getUserContainer() |
---|
| 137 | if usercontainer is None: |
---|
| 138 | return |
---|
| 139 | return usercontainer.get(login, None) |
---|
| 140 | |
---|
| 141 | def addAccount(self, account): |
---|
| 142 | usercontainer = self.getUserContainer() |
---|
| 143 | if usercontainer is None: |
---|
| 144 | return |
---|
| 145 | # XXX: complain if name already exists... |
---|
| 146 | usercontainer.addAccount(account) |
---|
| 147 | |
---|
[4091] | 148 | def addUser(self, name, password, title=None, description=None): |
---|
| 149 | usercontainer = self.getUserContainer() |
---|
| 150 | if usercontainer is None: |
---|
| 151 | return |
---|
| 152 | usercontainer.addUser(name, password, title, description) |
---|
[5055] | 153 | |
---|
[4087] | 154 | def getUserContainer(self): |
---|
| 155 | site = grok.getSite() |
---|
[4743] | 156 | return site['users'] |
---|