~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/auth/openidrp.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
    @copyright: 2007 MoinMoin:JohannesBerg
6
6
    @license: GNU GPL, see COPYING for details.
7
7
"""
8
 
from MoinMoin import log
9
 
logging = log.getLogger(__name__)
10
 
 
11
8
from MoinMoin.util.moinoid import MoinOpenIDStore
12
9
from MoinMoin import user
13
10
from MoinMoin.auth import BaseAuth
18
15
from MoinMoin.auth import CancelLogin, ContinueLogin
19
16
from MoinMoin.auth import MultistageFormLogin, MultistageRedirectLogin
20
17
from MoinMoin.auth import get_multistage_continuation_url
21
 
from werkzeug import url_encode
 
18
 
22
19
 
23
20
class OpenIDAuth(BaseAuth):
24
21
    login_inputs = ['openid_identifier']
25
22
    name = 'openid'
26
23
    logout_possible = True
27
 
    auth_attribs = ()
28
24
 
29
25
    def __init__(self, modify_request=None,
30
26
                       update_user=None,
31
27
                       create_user=None,
32
 
                       forced_service=None,
33
 
                       idselector_com=None):
 
28
                       forced_service=None):
34
29
        BaseAuth.__init__(self)
35
 
        self._modify_request = modify_request or (lambda x, c: None)
36
 
        self._update_user = update_user or (lambda i, u, c: None)
37
 
        self._create_user = create_user or (lambda i, u, c: None)
 
30
        self._modify_request = modify_request or (lambda x: None)
 
31
        self._update_user = update_user or (lambda i, u: None)
 
32
        self._create_user = create_user or (lambda i, u: None)
38
33
        self._forced_service = forced_service
39
 
        self._idselector_com = idselector_com
40
34
        if forced_service:
41
35
            self.login_inputs = ['special_no_input']
42
36
 
45
39
        if create:
46
40
            # pass in a created but unsaved user object
47
41
            u = user.User(request, auth_method=self.name,
48
 
                          auth_username=request.session['openid.id'],
49
 
                          auth_attribs=self.auth_attribs)
 
42
                          auth_username=request.session['openid.id'])
50
43
            # invalid name
51
44
            u.name = ''
52
 
            u = self._create_user(request.session['openid.info'], u, request.cfg)
 
45
            u = self._create_user(request.session['openid.info'], u)
53
46
 
54
47
        if u:
55
 
            self._update_user(request.session['openid.info'], u, request.cfg)
 
48
            self._update_user(request.session['openid.info'], u)
56
49
 
57
50
            # just in case the wiki admin screwed up
58
51
            if (not user.isValidName(request, u.name) or
76
69
        # that they want to use on this wiki
77
70
        # XXX: request nickname from OP and suggest using it
78
71
        # (if it isn't in use yet)
79
 
        logging.debug("running _get_account_name")
80
72
        _ = request.getText
81
73
        form.append(html.INPUT(type='hidden', name='oidstage', value='2'))
82
74
        table = html.TABLE(border='0')
141
133
        oidconsumer = consumer.Consumer(request.session,
142
134
                                        MoinOpenIDStore(request))
143
135
        query = {}
144
 
        for key in request.values.keys():
145
 
            query[key] = request.values.get(key)
146
 
        current_url = get_multistage_continuation_url(request, self.name,
147
 
                                                      {'oidstage': '1'})
148
 
        info = oidconsumer.complete(query, current_url)
 
136
        for key in request.form:
 
137
            query[key] = request.form[key][0]
 
138
        return_to = get_multistage_continuation_url(request, self.name,
 
139
                                                    {'oidstage': '1'})
 
140
        info = oidconsumer.complete(query, return_to=return_to)
149
141
        if info.status == consumer.FAILURE:
150
 
            logging.debug(_("OpenID error: %s.") % info.message)
151
142
            return CancelLogin(_('OpenID error: %s.') % info.message)
152
143
        elif info.status == consumer.CANCEL:
153
 
            logging.debug(_("OpenID verification canceled."))
154
144
            return CancelLogin(_('Verification canceled.'))
155
145
        elif info.status == consumer.SUCCESS:
156
 
            logging.debug(_("OpenID success. id: %s") % info.identity_url)
157
146
            request.session['openid.id'] = info.identity_url
158
147
            request.session['openid.info'] = info
159
148
 
161
150
            uid = user.getUserIdByOpenId(request, info.identity_url)
162
151
            if uid:
163
152
                u = user.User(request, id=uid, auth_method=self.name,
164
 
                              auth_username=info.identity_url,
165
 
                              auth_attribs=self.auth_attribs)
 
153
                              auth_username=info.identity_url)
166
154
            else:
167
155
                u = None
168
156
 
173
161
 
174
162
            # if no user found, then we need to ask for a username,
175
163
            # possibly associating an existing account.
176
 
            logging.debug("OpenID: No user found, prompting for username")
177
 
            #request.session['openid.id'] = info.identity_url
 
164
            request.session['openid.id'] = info.identity_url
178
165
            return MultistageFormLogin(self._get_account_name)
179
166
        else:
180
 
            logging.debug(_("OpenID failure"))
181
167
            return CancelLogin(_('OpenID failure.'))
182
168
 
183
169
    def _handle_name_continuation(self, request):
184
 
        _ = request.getText
185
170
        if not 'openid.id' in request.session:
186
 
            return CancelLogin(_('No OpenID found in session.'))
 
171
            return CancelLogin(None)
187
172
 
188
 
        newname = request.form.get('username', '')
 
173
        _ = request.getText
 
174
        newname = request.form.get('username', [''])[0]
189
175
        if not newname:
190
176
            return MultistageFormLogin(self._get_account_name)
191
177
        if not user.isValidName(request, newname):
196
182
        if not uid:
197
183
            # we can create a new user with this name :)
198
184
            u = user.User(request, auth_method=self.name,
199
 
                          auth_username=request.session['openid.id'],
200
 
                          auth_attribs=self.auth_attribs)
 
185
                          auth_username=request.session['openid.id'])
201
186
            u.name = newname
202
187
            u = self._handle_user_data(request, u)
203
188
            return ContinueLogin(u)
208
193
 
209
194
    def _handle_associate_continuation(self, request):
210
195
        if not 'openid.id' in request.session:
211
 
            return CancelLogin(_('No OpenID found in session.'))
 
196
            return CancelLogin()
212
197
 
213
198
        _ = request.getText
214
 
        username = request.form.get('username', '')
215
 
        password = request.form.get('password', '')
 
199
        username = request.form.get('username', [''])[0]
 
200
        password = request.form.get('password', [''])[0]
216
201
        if not password:
217
202
            return self._handle_name_continuation(request)
218
203
        u = user.User(request, name=username, password=password,
219
204
                      auth_method=self.name,
220
 
                      auth_username=request.session['openid.id'],
221
 
                      auth_attribs=self.auth_attribs)
 
205
                      auth_username=request.session['openid.id'])
222
206
        if u.valid:
223
207
            self._handle_user_data(request, u)
224
208
            return ContinueLogin(u, _('Your account is now associated to your OpenID.'))
228
212
            return MultistageFormLogin(assoc)
229
213
 
230
214
    def _handle_continuation(self, request):
231
 
        _ = request.getText
232
 
        oidstage = request.values.get('oidstage')
 
215
        oidstage = request.form.get('oidstage', [0])[0]
233
216
        if oidstage == '1':
234
 
            logging.debug('OpenID: handle verify continuation')
235
217
            return self._handle_verify_continuation(request)
236
218
        elif oidstage == '2':
237
 
            logging.debug('OpenID: handle name continuation')
238
219
            return self._handle_name_continuation(request)
239
220
        elif oidstage == '3':
240
 
            logging.debug('OpenID: handle associate continuation')
241
221
            return self._handle_associate_continuation(request)
242
 
        logging.debug('OpenID error: unknown continuation stage')
243
 
        return CancelLogin(_('OpenID error: unknown continuation stage'))
 
222
        return CancelLogin()
244
223
 
245
224
    def _openid_form(self, request, form, oidhtml):
246
225
        _ = request.getText
273
252
        _ = request.getText
274
253
 
275
254
        # user entered something but the session can't be stored
276
 
        if not request.cfg.cookie_lifetime[0]:
 
255
        if not request.session.is_stored:
277
256
            return ContinueLogin(user_obj,
278
257
                                 _('Anonymous sessions need to be enabled for OpenID login.'))
279
258
 
297
276
            if oidreq is None:
298
277
                return ContinueLogin(None, _('No OpenID.'))
299
278
 
300
 
            self._modify_request(oidreq, request.cfg)
 
279
            self._modify_request(oidreq)
301
280
 
302
281
            return_to = get_multistage_continuation_url(request, self.name,
303
282
                                                        {'oidstage': '1'})
304
 
            trust_root = request.url_root
 
283
            trust_root = request.getBaseURL()
305
284
            if oidreq.shouldSendRedirect():
306
285
                redirect_url = oidreq.redirectURL(trust_root, return_to)
307
286
                return MultistageRedirectLogin(redirect_url)
315
294
 
316
295
    def login_hint(self, request):
317
296
        _ = request.getText
318
 
        msg = u''
319
 
        if self._idselector_com:
320
 
            msg = self._idselector_com
321
 
        msg += _("If you do not have an account yet, you can still log in "
 
297
        return _("If you do not have an account yet, you can still log in "
322
298
                 "with your OpenID and create one during login.")
323
 
        return msg