annotate MoinMoin/auth/openidrp.py @ 3123:5cb80bd604b4

refactored _() getText calls to match new api
author Radomir Dopieralski <moindev@sheep.art.pl>
date Wed, 27 Feb 2008 00:28:28 +0100
parents 059054049961
children f272fc54b68d
rev   line source
2299
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
2 """
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
3 MoinMoin - OpenID authorization
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
4
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
5 @copyright: 2007 MoinMoin:JohannesBerg
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
7 """
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
8 from MoinMoin.util.moinoid import MoinOpenIDStore
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
9 from MoinMoin import user
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
10 from MoinMoin.auth import BaseAuth
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
11 from openid.consumer import consumer
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12 from openid.yadis.discover import DiscoveryFailure
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13 from openid.fetchers import HTTPFetchingError
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14 from MoinMoin import wikiutil
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 from MoinMoin.widget import html
2421
059054049961 don't use parenthesised imports in openid code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2302
diff changeset
16 from MoinMoin.auth import CancelLogin, ContinueLogin
059054049961 don't use parenthesised imports in openid code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2302
diff changeset
17 from MoinMoin.auth import MultistageFormLogin, MultistageRedirectLogin
059054049961 don't use parenthesised imports in openid code
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2302
diff changeset
18 from MoinMoin.auth import get_multistage_continuation_url
2299
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
19
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
20 class OpenIDAuth(BaseAuth):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
21 login_inputs = ['openid_identifier']
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
22 name = 'openid'
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23 logout_possible = True
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 def _get_account_name(self, request, form, msg=None):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 # now we need to ask the user for a new username
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 # that they want to use on this wiki
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
28 # XXX: request nickname from OP and suggest using it
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29 # (if it isn't in use yet)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 _ = request.getText
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 form.append(html.INPUT(type='hidden', name='oidstage', value='2'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
32 table = html.TABLE(border='0')
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33 form.append(table)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34 td = html.TD(colspan=2)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35 td.append(html.Raw(_("""Please choose an account name now.
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 If you choose an existing account name you will be asked for the
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
37 password and be able to associate the account with your OpenID.""")))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
38 table.append(html.TR().append(td))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
39 if msg:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 td = html.TD(colspan='2')
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41 td.append(html.P().append(html.STRONG().append(html.Raw(msg))))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42 table.append(html.TR().append(td))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 td1 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 td1.append(html.STRONG().append(html.Raw(_('Name'))))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 td2 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46 td2.append(html.INPUT(type='text', name='username'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 table.append(html.TR().append(td1).append(td2))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 td1 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49 td2 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50 td2.append(html.INPUT(type='submit', name='submit',
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 value=_('Choose this name')))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 table.append(html.TR().append(td1).append(td2))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 def _associate_account(self, request, form, accountname, msg=None):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 _ = request.getText
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 form.append(html.INPUT(type='hidden', name='oidstage', value='3'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 table = html.TABLE(border='0')
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 form.append(table)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 td = html.TD(colspan=2)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 td.append(html.Raw(_("""The username you have chosen is already
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
62 taken. If it is your username, enter your password below to associate
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
63 the username with your OpenID. Otherwise, please choose a different
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64 username and leave the password field blank.""")))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65 table.append(html.TR().append(td))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
66 if msg:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
67 td.append(html.P().append(html.STRONG().append(html.Raw(msg))))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
68 td1 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69 td1.append(html.STRONG().append(html.Raw(_('Name'))))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70 td2 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71 td2.append(html.INPUT(type='text', name='username', value=accountname))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 table.append(html.TR().append(td1).append(td2))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
73 td1 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
74 td1.append(html.STRONG().append(html.Raw(_('Password'))))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
75 td2 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
76 td2.append(html.INPUT(type='password', name='password'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
77 table.append(html.TR().append(td1).append(td2))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
78 td1 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
79 td2 = html.TD()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
80 td2.append(html.INPUT(type='submit', name='submit',
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
81 value=_('Associate this name')))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
82 table.append(html.TR().append(td1).append(td2))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
83
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
84 def _handle_verify_continuation(self, request):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
85 _ = request.getText
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
86 oidconsumer = consumer.Consumer(request.session,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
87 MoinOpenIDStore(request))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
88 query = {}
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
89 for key in request.form:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
90 query[key] = request.form[key][0]
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
91 return_to = get_multistage_continuation_url(request, self.name,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
92 {'oidstage': '1'})
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
93 info = oidconsumer.complete(query, return_to=return_to)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
94 if info.status == consumer.FAILURE:
3123
5cb80bd604b4 refactored _() getText calls to match new api
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 2421
diff changeset
95 return CancelLogin(_('OpenID error: %s.') % info.message, percent=True)
2299
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
96 elif info.status == consumer.CANCEL:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
97 return CancelLogin(_('Verification canceled.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
98 elif info.status == consumer.SUCCESS:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
99 # try to find user object
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
100 uid = user.getUserIdByOpenId(request, info.identity_url)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
101 if uid:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
102 u = user.User(request, id=uid, auth_method=self.name,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
103 auth_username=info.identity_url)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
104 return ContinueLogin(u)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
105 # if no user found, then we need to ask for a username,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
106 # possibly associating an existing account.
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
107 request.session['openid.id'] = info.identity_url
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
108 return MultistageFormLogin(self._get_account_name)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
109 else:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
110 return CancelLogin(_('OpenID failure.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
111
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
112 def _handle_name_continuation(self, request):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
113 if not 'openid.id' in request.session:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
114 return CancelLogin(None)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
115
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
116 _ = request.getText
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
117 newname = request.form.get('username', [''])[0]
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
118 if not newname:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
119 return MultistageFormLogin(self._get_account_name)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
120 if not user.isValidName(request, newname):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
121 return MultistageFormLogin(self._get_account_name,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
122 _('This is not a valid username, choose a different one.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
123 uid = None
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
124 if newname:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
125 uid = user.getUserId(request, newname)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
126 if not uid:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
127 # we can create a new user with this name :)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
128 u = user.User(request, id=uid, auth_method=self.name,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
129 auth_username=request.session['openid.id'])
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
130 u.name = newname
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
131 u.openids = [request.session['openid.id']]
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
132 u.aliasname = request.session['openid.id']
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
133 del request.session['openid.id']
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
134 u.save()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
135 return ContinueLogin(u)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
136 # requested username already exists. if they know the password,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
137 # they can associate that account with the openid.
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
138 assoc = lambda req, form: self._associate_account(req, form, newname)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
139 return MultistageFormLogin(assoc)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
140
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
141 def _handle_associate_continuation(self, request):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
142 if not 'openid.id' in request.session:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
143 return CancelLogin()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
144
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
145 _ = request.getText
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
146 username = request.form.get('username', [''])[0]
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
147 password = request.form.get('password', [''])[0]
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
148 if not password:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
149 return self._handle_name_continuation(request)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
150 u = user.User(request, name=username, password=password,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
151 auth_method=self.name,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
152 auth_username=request.session['openid.id'])
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
153 if u.valid:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
154 if not hasattr(u, 'openids'):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
155 u.openids = []
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
156 u.openids.append(request.session['openid.id'])
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
157 if not u.aliasname:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
158 u.aliasname = request.session['openid.id']
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
159 u.save()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
160 del request.session['openid.id']
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
161 return ContinueLogin(u, _('Your account is now associated to your OpenID.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
162 else:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
163 msg = _('The password you entered is not valid.')
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
164 assoc = lambda req, form: self._associate_account(req, form, username, msg=msg)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
165 return MultistageFormLogin(assoc)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
166
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
167 def _handle_continuation(self, request):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
168 oidstage = request.form.get('oidstage', [0])[0]
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
169 if oidstage == '1':
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
170 return self._handle_verify_continuation(request)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
171 elif oidstage == '2':
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
172 return self._handle_name_continuation(request)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
173 elif oidstage == '3':
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
174 return self._handle_associate_continuation(request)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
175 return CancelLogin()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
176
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
177 def _openid_form(self, request, form, oidhtml):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
178 _ = request.getText
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
179 txt = _('OpenID verification requires that you click this button:')
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
180 # create JS to automatically submit the form if possible
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
181 submitjs = """<script type="text/javascript">
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
182 <!--//
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
183 document.getElementById("openid_message").submit();
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
184 //-->
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
185 </script>
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
186 """
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
187 return ''.join([txt, oidhtml, submitjs])
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
188
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
189 def login(self, request, user_obj, **kw):
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
190 continuation = kw.get('multistage')
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
191
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
192 if continuation:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
193 return self._handle_continuation(request)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
194
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
195 # openid is designed to work together with other auths
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
196 if user_obj and user_obj.valid:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
197 return ContinueLogin(user_obj)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
198
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
199 openid_id = kw.get('openid_identifier')
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
200 # nothing entered? continue...
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
201 if not openid_id:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
202 return ContinueLogin(user_obj)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
203
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
204 _ = request.getText
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
205
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
206 # user entered something but the session can't be stored
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
207 if not request.session.is_stored:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
208 return ContinueLogin(user_obj,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
209 _('Anonymous sessions need to be enabled for OpenID login.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
210
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
211 oidconsumer = consumer.Consumer(request.session,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
212 MoinOpenIDStore(request))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
213
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
214 try:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
215 oidreq = oidconsumer.begin(openid_id)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
216 except HTTPFetchingError:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
217 return ContinueLogin(None, _('Failed to resolve OpenID.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
218 except DiscoveryFailure:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
219 return ContinueLogin(None, _('OpenID discovery failure, not a valid OpenID.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
220 else:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
221 if oidreq is None:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
222 return ContinueLogin(None, _('No OpenID.'))
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
223
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
224 return_to = get_multistage_continuation_url(request, self.name,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
225 {'oidstage': '1'})
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
226 trust_root = request.getBaseURL()
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
227 if oidreq.shouldSendRedirect():
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
228 redirect_url = oidreq.redirectURL(trust_root, return_to)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
229 return MultistageRedirectLogin(redirect_url)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
230 else:
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
231 form_html = oidreq.formMarkup(trust_root, return_to,
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
232 form_tag_attrs={'id': 'openid_message'})
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
233 mcall = lambda request, form:\
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
234 self._openid_form(request, form, form_html)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
235 ret = MultistageFormLogin(mcall)
39d11cf4af6c OpenID relying party (client) support
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
236 return ret
2302
1f449e482bcc allow auth methods to set the login hint below the input fields
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2299
diff changeset
237
1f449e482bcc allow auth methods to set the login hint below the input fields
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2299
diff changeset
238 def login_hint(self, request):
1f449e482bcc allow auth methods to set the login hint below the input fields
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2299
diff changeset
239 _ = request.getText
1f449e482bcc allow auth methods to set the login hint below the input fields
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2299
diff changeset
240 return _("If you do not have an account yet, you can still log in "
1f449e482bcc allow auth methods to set the login hint below the input fields
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2299
diff changeset
241 "with your OpenID and create one during login.")