[7192] | 1 | ## $Id: authentication.py 7192 2011-11-25 07:15:50Z henrik $ |
---|
[5431] | 2 | ## |
---|
[7192] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5431] | 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
[7192] | 8 | ## |
---|
[5431] | 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
[7192] | 13 | ## |
---|
[5431] | 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """Special authentication for applicants. |
---|
| 19 | |
---|
| 20 | XXX: This is work in progress, experimental code! Don't do that at home! |
---|
| 21 | """ |
---|
| 22 | import grok |
---|
| 23 | from zope.event import notify |
---|
| 24 | from zope.pluggableauth.factories import Principal |
---|
| 25 | from zope.pluggableauth.interfaces import ( |
---|
[5818] | 26 | ICredentialsPlugin, IAuthenticatorPlugin, |
---|
[5431] | 27 | IAuthenticatedPrincipalFactory, AuthenticatedPrincipalCreated) |
---|
| 28 | from zope.pluggableauth.plugins.session import SessionCredentialsPlugin |
---|
| 29 | from zope.publisher.interfaces import IRequest |
---|
| 30 | from zope.publisher.interfaces.http import IHTTPRequest |
---|
| 31 | from zope.session.interfaces import ISession |
---|
[5818] | 32 | from waeup.sirp.accesscodes import get_access_code |
---|
[5762] | 33 | from waeup.sirp.applicants.interfaces import ( |
---|
[5440] | 34 | IApplicantPrincipalInfo, IApplicantPrincipal, IApplicantSessionCredentials, |
---|
[5907] | 35 | ) |
---|
[5818] | 36 | from waeup.sirp.applicants import get_applicant_data |
---|
[5902] | 37 | from waeup.sirp.interfaces import IAuthPluginUtility |
---|
[5431] | 38 | |
---|
[5818] | 39 | |
---|
[5431] | 40 | class ApplicantPrincipalInfo(object): |
---|
[5441] | 41 | """Infos about an applicant principal. |
---|
| 42 | """ |
---|
[5431] | 43 | grok.implements(IApplicantPrincipalInfo) |
---|
| 44 | |
---|
[5818] | 45 | def __init__(self, access_code): |
---|
| 46 | self.id = principal_id(access_code) |
---|
[6410] | 47 | self.title = u'Applicant' |
---|
| 48 | self.description = u'An Applicant' |
---|
[5431] | 49 | self.credentialsPlugin = None |
---|
| 50 | self.authenticatorPlugin = None |
---|
| 51 | self.access_code = access_code |
---|
| 52 | |
---|
| 53 | class ApplicantPrincipal(Principal): |
---|
[5441] | 54 | """An applicant principal. |
---|
[5431] | 55 | |
---|
[5441] | 56 | Applicant principals provide an extra `access_code` and `reg_no` |
---|
| 57 | attribute extending ordinary principals. |
---|
| 58 | """ |
---|
| 59 | |
---|
[5431] | 60 | grok.implements(IApplicantPrincipal) |
---|
| 61 | |
---|
[5818] | 62 | def __init__(self, access_code, prefix=None): |
---|
| 63 | self.id = principal_id(access_code) |
---|
[5464] | 64 | if prefix is not None: |
---|
| 65 | self.id = '%s.%s' % (prefix, self.id) |
---|
[6465] | 66 | self.title = u'Applicant' |
---|
[6410] | 67 | self.description = u'An applicant' |
---|
[5431] | 68 | self.groups = [] |
---|
| 69 | self.access_code = access_code |
---|
| 70 | |
---|
| 71 | def __repr__(self): |
---|
| 72 | return 'ApplicantPrincipal(%r)' % self.id |
---|
| 73 | |
---|
| 74 | class AuthenticatedApplicantPrincipalFactory(grok.MultiAdapter): |
---|
| 75 | """Creates 'authenticated' applicant principals. |
---|
[5441] | 76 | |
---|
| 77 | Adapts (principal info, request) to an ApplicantPrincipal instance. |
---|
| 78 | |
---|
| 79 | This adapter is used by the standard PAU to transform |
---|
| 80 | PrincipalInfos into Principal instances. |
---|
[5431] | 81 | """ |
---|
| 82 | grok.adapts(IApplicantPrincipalInfo, IRequest) |
---|
| 83 | grok.implements(IAuthenticatedPrincipalFactory) |
---|
| 84 | |
---|
| 85 | def __init__(self, info, request): |
---|
| 86 | self.info = info |
---|
| 87 | self.request = request |
---|
| 88 | |
---|
| 89 | def __call__(self, authentication): |
---|
| 90 | principal = ApplicantPrincipal( |
---|
[5464] | 91 | self.info.access_code, |
---|
| 92 | authentication.prefix, |
---|
[5431] | 93 | ) |
---|
| 94 | notify( |
---|
| 95 | AuthenticatedPrincipalCreated( |
---|
| 96 | authentication, principal, self.info, self.request)) |
---|
| 97 | return principal |
---|
| 98 | |
---|
[5440] | 99 | |
---|
| 100 | # |
---|
| 101 | # Credentials plugins and related.... |
---|
| 102 | # |
---|
| 103 | |
---|
| 104 | class ApplicantCredentials(object): |
---|
| 105 | """Credentials class for ordinary applicants. |
---|
| 106 | """ |
---|
| 107 | grok.implements(IApplicantSessionCredentials) |
---|
| 108 | |
---|
| 109 | def __init__(self, access_code): |
---|
| 110 | self.access_code = access_code |
---|
| 111 | |
---|
| 112 | def getAccessCode(self): |
---|
| 113 | """Get the access code. |
---|
| 114 | """ |
---|
| 115 | return self.access_code |
---|
| 116 | |
---|
[5444] | 117 | def getLogin(self): |
---|
| 118 | """Stay compatible with non-applicant authenticators. |
---|
| 119 | """ |
---|
| 120 | return None |
---|
| 121 | |
---|
| 122 | def getPassword(self): |
---|
| 123 | """Stay compatible with non-applicant authenticators. |
---|
| 124 | """ |
---|
| 125 | return None |
---|
[6122] | 126 | |
---|
[5431] | 127 | class WAeUPApplicantCredentialsPlugin(grok.GlobalUtility, |
---|
| 128 | SessionCredentialsPlugin): |
---|
[5440] | 129 | """A credentials plugin that scans requests for applicant credentials. |
---|
| 130 | """ |
---|
[5431] | 131 | grok.provides(ICredentialsPlugin) |
---|
| 132 | grok.name('applicant_credentials') |
---|
| 133 | |
---|
| 134 | loginpagename = 'login' |
---|
[6110] | 135 | accesscode_prefix_field = 'form.ac_prefix' |
---|
[5444] | 136 | accesscode_series_field = 'form.ac_series' |
---|
| 137 | accesscode_number_field = 'form.ac_number' |
---|
[5431] | 138 | |
---|
| 139 | def extractCredentials(self, request): |
---|
| 140 | """Extracts credentials from a session if they exist. |
---|
| 141 | """ |
---|
| 142 | if not IHTTPRequest.providedBy(request): |
---|
| 143 | return None |
---|
| 144 | session = ISession(request) |
---|
| 145 | sessionData = session.get( |
---|
| 146 | 'zope.pluggableauth.browserplugins') |
---|
[5444] | 147 | access_code_prefix = request.get(self.accesscode_prefix_field, None) |
---|
| 148 | access_code_series = request.get(self.accesscode_series_field, None) |
---|
| 149 | access_code_no = request.get(self.accesscode_number_field, None) |
---|
| 150 | access_code = '%s-%s-%s' % ( |
---|
| 151 | access_code_prefix, access_code_series, access_code_no) |
---|
| 152 | if None in [access_code_prefix, access_code_series, access_code_no]: |
---|
| 153 | access_code = None |
---|
[5431] | 154 | credentials = None |
---|
| 155 | |
---|
[5818] | 156 | if access_code: |
---|
[5440] | 157 | credentials = ApplicantCredentials(access_code) |
---|
[5431] | 158 | elif not sessionData: |
---|
| 159 | return None |
---|
| 160 | sessionData = session[ |
---|
| 161 | 'zope.pluggableauth.browserplugins'] |
---|
| 162 | if credentials: |
---|
| 163 | sessionData['credentials'] = credentials |
---|
| 164 | else: |
---|
| 165 | credentials = sessionData.get('credentials', None) |
---|
| 166 | if not credentials: |
---|
| 167 | return None |
---|
[5497] | 168 | if not IApplicantSessionCredentials.providedBy(credentials): |
---|
| 169 | # If credentials were stored in session from another |
---|
| 170 | # credentials plugin then we cannot make assumptions about |
---|
| 171 | # its structure. |
---|
| 172 | return None |
---|
[5818] | 173 | return {'accesscode': credentials.getAccessCode()} |
---|
[5431] | 174 | |
---|
[5452] | 175 | |
---|
[5818] | 176 | |
---|
[5431] | 177 | class ApplicantsAuthenticatorPlugin(grok.GlobalUtility): |
---|
| 178 | """Authenticate applicants. |
---|
| 179 | """ |
---|
| 180 | grok.provides(IAuthenticatorPlugin) |
---|
| 181 | grok.name('applicants') |
---|
| 182 | |
---|
| 183 | def authenticateCredentials(self, credentials): |
---|
[5909] | 184 | """Validate the given `credentials` |
---|
| 185 | |
---|
| 186 | Credentials for applicants have to be passed as a regular |
---|
| 187 | dictionary with a key ``accesscode``. This access code is the |
---|
| 188 | password and username of an applicant. |
---|
| 189 | |
---|
| 190 | Returns a :class:`ApplicantPrincipalInfo` in case of |
---|
| 191 | successful validation, ``None`` else. |
---|
| 192 | |
---|
| 193 | Credentials are not valid if: |
---|
| 194 | |
---|
| 195 | - The passed accesscode does not exist (i.e. was not generated |
---|
| 196 | by the :mod:`waeup.sirp.accesscode` module). |
---|
| 197 | |
---|
| 198 | or |
---|
| 199 | |
---|
| 200 | - the accesscode was disabled |
---|
| 201 | |
---|
| 202 | or |
---|
[6122] | 203 | |
---|
[5909] | 204 | - the accesscode was already used and a dataset for this |
---|
| 205 | applicant was already generated with a different accesscode |
---|
| 206 | (currently impossible, as applicant datasets are indexed by |
---|
| 207 | accesscode) |
---|
| 208 | |
---|
| 209 | or |
---|
| 210 | |
---|
| 211 | - a dataset for the applicant already exists with an |
---|
| 212 | accesscode set and this accesscode does not match the given |
---|
| 213 | one. |
---|
[6122] | 214 | |
---|
[5909] | 215 | """ |
---|
[5431] | 216 | if not isinstance(credentials, dict): |
---|
| 217 | return None |
---|
[5460] | 218 | accesscode = credentials.get('accesscode', None) |
---|
| 219 | if accesscode is None: |
---|
[5431] | 220 | return None |
---|
[5818] | 221 | applicant_data = get_applicant_data(accesscode) |
---|
| 222 | ac = get_access_code(accesscode) # Get the real access code object |
---|
[5460] | 223 | appl_ac = getattr(applicant_data, 'access_code', None) |
---|
[5446] | 224 | if ac is None: |
---|
| 225 | return None |
---|
[6470] | 226 | if ac.state == 'disabled': |
---|
[6409] | 227 | return None |
---|
[6470] | 228 | if ac.state == 'used' and appl_ac != ac.representation: |
---|
[6409] | 229 | return None |
---|
[7063] | 230 | # If the following fails we have a catalog error. Bad enough |
---|
| 231 | # to pull emergency break. |
---|
| 232 | assert appl_ac is None or appl_ac == ac.representation |
---|
[5818] | 233 | return ApplicantPrincipalInfo(accesscode) |
---|
[5431] | 234 | |
---|
| 235 | def principalInfo(self, id): |
---|
| 236 | """Returns an IPrincipalInfo object for the specified principal id. |
---|
| 237 | |
---|
[5441] | 238 | This method is used by the stadard PAU to lookup for instance |
---|
| 239 | groups. If a principal belongs to a group, the group is looked |
---|
| 240 | up by the id. Currently we always return ``None``, |
---|
| 241 | indicating, that the principal could not be found. This also |
---|
| 242 | means, that is has no effect if applicant users belong to a |
---|
| 243 | certain group. They can not gain extra-permissions this way. |
---|
[5431] | 244 | """ |
---|
| 245 | return None |
---|
[5435] | 246 | |
---|
[5902] | 247 | class ApplicantsAuthUtility(grok.GlobalUtility): |
---|
| 248 | """A global utility that sets up any PAU passed. |
---|
[5904] | 249 | |
---|
| 250 | The methods of this utility are called during setup of a new site |
---|
| 251 | (`University`) instance and after the regular authentication |
---|
| 252 | systems (regular users, officers, etc.) were set up. |
---|
[5902] | 253 | """ |
---|
[7063] | 254 | grok.implements(IAuthPluginUtility) |
---|
[5902] | 255 | grok.name('applicants_auth_setup') |
---|
| 256 | |
---|
| 257 | def register(self, pau): |
---|
[5903] | 258 | """Register our local applicants specific PAU components. |
---|
[5902] | 259 | |
---|
[5904] | 260 | Applicants provide their own authentication system resulting |
---|
[5903] | 261 | in a specialized credentials plugin and a specialized |
---|
| 262 | authenticator plugin. |
---|
| 263 | |
---|
| 264 | Here we tell a given PAU that these plugins exist and should |
---|
| 265 | be consulted when trying to authenticate a user. |
---|
| 266 | |
---|
[5904] | 267 | We stack our local plugins at end of the plugin list, so that |
---|
| 268 | other authentication mechanisms (the normal user |
---|
| 269 | authentication for instance) have precedence and to avoid |
---|
| 270 | "account-shadowing". |
---|
[5903] | 271 | """ |
---|
| 272 | # The local credentials plugin is registered under the name |
---|
| 273 | # 'applicant_credentials' (see above). |
---|
[6661] | 274 | plugins = list(pau.credentialsPlugins) + ['applicant_credentials'] |
---|
| 275 | pau.credentialsPlugins = tuple(plugins) |
---|
[5903] | 276 | # The local authenticator plugin is registered under the name |
---|
| 277 | # 'applicants' (subject to change?) |
---|
[6661] | 278 | plugins = list(pau.authenticatorPlugins) + ['applicants'] |
---|
| 279 | pau.authenticatorPlugins = tuple(plugins) |
---|
[5903] | 280 | return pau |
---|
| 281 | |
---|
[5902] | 282 | def unregister(self, pau): |
---|
[7063] | 283 | """Unregister applicant specific authentication components from PAU. |
---|
| 284 | """ |
---|
| 285 | pau.credentialsPlugins = tuple( |
---|
| 286 | [x for x in list(pau.credentialsPlugins) |
---|
| 287 | if x != 'applicant_credentials']) |
---|
| 288 | pau.authenticatorPlugins = tuple( |
---|
| 289 | [x for x in list(pau.authenticatorPlugins) |
---|
| 290 | if x != 'applicants']) |
---|
[5903] | 291 | return pau |
---|
[5904] | 292 | |
---|
| 293 | |
---|
[5818] | 294 | def principal_id(access_code): |
---|
[5435] | 295 | """Get a principal ID for applicants. |
---|
| 296 | |
---|
| 297 | We need unique principal ids for appliants. As access codes must |
---|
| 298 | be unique we simply return them. |
---|
| 299 | """ |
---|
| 300 | return access_code |
---|