annotate MoinMoin/userprefs/oid.py @ 2301:35617ed953c4

OpenID preferences (allows adding/removing OpenIDs)
author Johannes Berg <johannes AT sipsolutions DOT net>
date Tue, 10 Jul 2007 19:13:03 +0200
parents
children 4f73af30fd75
rev   line source
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
2 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
3 MoinMoin - OpenID preferences
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
4
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
5 @copyright: 2007 MoinMoin:JohannesBerg
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
7 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
8
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
9 from MoinMoin import wikiutil
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
10 from MoinMoin.widget import html
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
11 from MoinMoin.userprefs import UserPrefBase
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12 from MoinMoin.auth.openidrp import OpenIDAuth
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13 import sha
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14 from MoinMoin.util.moinoid import MoinOpenIDStore
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 from openid.consumer import consumer
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
16 from openid.yadis.discover import DiscoveryFailure
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
17 from openid.fetchers import HTTPFetchingError
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
18
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
19
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
20 class Settings(UserPrefBase):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
21 def __init__(self, request):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
22 """ Initialize OpenID settings form. """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23 UserPrefBase.__init__(self, request)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24 self.request = request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 self._ = request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 self.cfg = request.cfg
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 self.title = self._("OpenID settings")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
28
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29 def allowed(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 for authm in self.request.cfg.auth:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 if isinstance(authm, OpenIDAuth):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
32 return True
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33 return False
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35 def _handle_remove(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
37 openids = self.request.user.openids[:]
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
38 for oid in self.request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
39 name = "rm-%s" % sha.new(oid).hexdigest()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 if name in self.request.form:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41 openids.remove(oid)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42 if not openids and len(self.request.cfg.auth) == 1:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 return _("Cannot remove all OpenIDs.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 self.request.user.openids = openids
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 self.request.user.save()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46 return _("The selected OpenIDs have been removed.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 def _handle_add(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 openid_id = request.form.get('openid_identifier', [''])[0]
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 if openid_id in request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 return _("OpenID is already present.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 oidconsumer = consumer.Consumer(request.session,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 MoinOpenIDStore(self.request))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 try:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 oidreq = oidconsumer.begin(openid_id)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 except HTTPFetchingError:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
62 return _('Failed to resolve OpenID.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
63 except DiscoveryFailure:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64 return _('OpenID discovery failure, not a valid OpenID.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
66 if oidreq is None:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
67 return _("No OpenID.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
68
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69 qstr = wikiutil.makeQueryString({'action': 'userprefs',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70 'handler': 'oid',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71 'oid.return': '1'})
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 return_to = ''.join([request.getBaseURL(), '?', qstr])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
73 trust_root = request.getBaseURL()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
74 if oidreq.shouldSendRedirect():
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
75 redirect_url = oidreq.redirectURL(trust_root, return_to)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
76 request.http_redirect(redirect_url)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
77 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
78 form_html = oidreq.formMarkup(trust_root, return_to,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
79 form_tag_attrs={'id': 'openid_message'})
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
80 request.session['openid.prefs.form_html'] = form_html
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
81
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
82
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
83 def _handle_oidreturn(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
84 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
85 _ = request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
86
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
87 oidconsumer = consumer.Consumer(request.session,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
88 MoinOpenIDStore(request))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
89 query = {}
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
90 for key in request.form:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
91 query[key] = request.form[key][0]
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
92 qstr = wikiutil.makeQueryString({'action': 'userprefs',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
93 'handler': 'oid',
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
94 'oid.return': '1'})
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
95 return_to = ''.join([request.getBaseURL(), '?', qstr])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
96 info = oidconsumer.complete(query, return_to=return_to)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
97 if info.status == consumer.FAILURE:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
98 return _('OpenID error: %s.') % info.message
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
99 elif info.status == consumer.CANCEL:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
100 return _('Verification canceled.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
101 elif info.status == consumer.SUCCESS:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
102 if not info.identity_url in request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
103 request.user.openids.append(info.identity_url)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
104 request.user.save()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
105 return _("OpenID added successfully.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
106 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
107 return _("OpenID is already present.")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
108 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
109 return _('OpenID failure.')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
110
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
111
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
112 def handle_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
113 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
114 form = self.request.form
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
115
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
116 if form.has_key('oid.return'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
117 return self._handle_oidreturn()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
118
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
119 if form.has_key('cancel'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
120 return
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
121
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
122 if form.has_key('remove'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
123 return self._handle_remove()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
124
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
125 if form.has_key('add'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
126 return self._handle_add()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
127
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
128 return
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
129
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
130 def _make_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
131 sn = self.request.getScriptname()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
132 pi = self.request.getPathinfo()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
133 action = u"%s%s" % (sn, pi)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
134 _form = html.FORM(action=action)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
135 _form.append(html.INPUT(type="hidden", name="action", value="userprefs"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
136 _form.append(html.INPUT(type="hidden", name="handler", value="oid"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
137 return _form
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
138
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
139 def _make_row(self, label, cell, **kw):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
140 """ Create a row in the form table.
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
141 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
142 self._table.append(html.TR().extend([
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
143 html.TD(**kw).extend([html.B().append(label), ' ']),
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
144 html.TD().extend(cell),
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
145 ]))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
146
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
147 def _oidlist(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
148 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
149 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
150 for oid in self.request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
151 name = "rm-%s" % sha.new(oid).hexdigest()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
152 form.append(html.INPUT(type="checkbox", name=name, id=name))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
153 form.append(html.LABEL(for_=name).append(html.Text(oid)))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
154 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
155 self._make_row(_("Current OpenIDs"), [form], valign='top')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
156 label = _("Remove selected")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
157 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
158 form.append(html.INPUT(type="submit", name="remove", value=label))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
159
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
160 def _addoidform(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
161 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
162 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
163 # go back to this page
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
164 form.append(html.INPUT(type="hidden", name="sub", value="oid"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
165 label = _("Add OpenID")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
166 form.append(html.INPUT(type="text", size="32",
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
167 name="openid_identifier",
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
168 id="openididentifier"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
169 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
170 form.append(html.INPUT(type="submit", name="add", value=label))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
171 self._make_row(_('Add OpenID'), [form])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
172
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
173 def create_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
174 """ Create the complete HTML form code. """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
175 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
176
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
177 ret = html.P()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
178 # Use the user interface language and direction
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
179 lang_attr = self.request.theme.ui_lang_attr()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
180 ret.append(html.Raw('<div %s>' % lang_attr))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
181 self._table = html.TABLE(border="0")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
182 ret.append(self._table)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
183 ret.append(html.Raw("</div>"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
184
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
185 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
186
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
187 if 'openid.prefs.form_html' in request.session:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
188 txt = _('OpenID verification requires that you click this button:')
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
189 # create JS to automatically submit the form if possible
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
190 submitjs = """<script type="text/javascript">
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
191 <!--//
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
192 document.getElementById("openid_message").submit();
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
193 //-->
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
194 </script>
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
195 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
196 oidhtml = request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
197 del request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
198 return ''.join([txt, oidhtml, submitjs])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
199
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
200 if request.user.openids:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
201 self._oidlist()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
202 self._addoidform()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
203
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
204 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
205 label = _("Cancel")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
206 form.append(html.INPUT(type="submit", name='cancel', value=label))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
207 self._make_row('', [form])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
208 return unicode(ret)