annotate MoinMoin/userprefs/oid.py @ 4566:2eaf4b42b400

merged moin/1.8
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 10 Feb 2009 13:21:59 +0100
parents 173c67aff3d6 68d29223dfa9
children 2c992293ece4
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
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
9 from MoinMoin import wikiutil, user
2301
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
4363
817d99d715fe remove direct usage of deprecated sha module - use hashlib, if possible
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
12 from MoinMoin.support.python_compatibility import hash_new
817d99d715fe remove direct usage of deprecated sha module - use hashlib, if possible
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
13
2370
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
14 try:
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
15 from MoinMoin.auth.openidrp import OpenIDAuth
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
16 from MoinMoin.util.moinoid import MoinOpenIDStore
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
17 from openid.consumer import consumer
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
18 from openid.yadis.discover import DiscoveryFailure
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
19 from openid.fetchers import HTTPFetchingError
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
20 _openid_disabled = False
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
21 except ImportError:
6d9970321d79 disable openid preferences if openid libs are not installed
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2364
diff changeset
22 _openid_disabled = True
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 class Settings(UserPrefBase):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 def __init__(self, request):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 """ Initialize OpenID settings form. """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
28 UserPrefBase.__init__(self, request)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29 self.request = request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 self._ = request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 self.cfg = request.cfg
2423
76bf8b6b3061 use formatted=False for userprefs titles
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2380
diff changeset
32 _ = self._
3122
a1322262398a refactored _() getText calls to match new api
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2423
diff changeset
33 self.title = _("OpenID settings")
2380
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
34 openid_auth = False
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
35 if not _openid_disabled:
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
36 for authm in self.request.cfg.auth:
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
37 if isinstance(authm, OpenIDAuth):
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
38 openid_auth = True
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
39 break
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
40 if not openid_auth:
0d04c8c82eb6 slightly optimise openid preferences
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2370
diff changeset
41 self.allowed = lambda: False
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 def _handle_remove(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 _ = self.request.getText
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
45 if not hasattr(self.request.user, 'openids'):
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
46 return
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 openids = self.request.user.openids[:]
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 for oid in self.request.user.openids:
4363
817d99d715fe remove direct usage of deprecated sha module - use hashlib, if possible
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
49 name = "rm-%s" % hash_new('sha1', oid).hexdigest()
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50 if name in self.request.form:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 openids.remove(oid)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 if not openids and len(self.request.cfg.auth) == 1:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
53 return 'error', _("Cannot remove all OpenIDs.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 self.request.user.openids = openids
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 self.request.user.save()
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
56 return 'info', _("The selected OpenIDs have been removed.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 def _handle_add(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61
4428
ded09bdc3902 pre-1.9: request.form has qs args and post data, 1.9: .form only post data, .args only qs args, .values both
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4376
diff changeset
62 openid_id = request.form.get('openid_identifier', '')
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
63 if not openid_id:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
64 return 'error', _("No OpenID given.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
66 if (hasattr(self.request.user, 'openids') and
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
67 openid_id in request.user.openids):
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
68 return 'error', _("OpenID is already present.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70 oidconsumer = consumer.Consumer(request.session,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71 MoinOpenIDStore(self.request))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 try:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
73 oidreq = oidconsumer.begin(openid_id)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
74 except HTTPFetchingError:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
75 return 'error', _('Failed to resolve OpenID.')
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
76 except DiscoveryFailure:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
77 return 'error', _('OpenID discovery failure, not a valid OpenID.')
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
78 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
79 if oidreq is None:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
80 return 'error', _("No OpenID given.") # ??
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
81
4565
68d29223dfa9 page.url() should preferably get a query dict, not a query string. It calls wikiutil.makeQueryString() internally.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4544
diff changeset
82 qstr = {'action': 'userprefs',
68d29223dfa9 page.url() should preferably get a query dict, not a query string. It calls wikiutil.makeQueryString() internally.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4544
diff changeset
83 'handler': 'oid',
68d29223dfa9 page.url() should preferably get a query dict, not a query string. It calls wikiutil.makeQueryString() internally.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4544
diff changeset
84 'oid.return': '1'}
3234
a739558ca3dc Page.url() default changed to relative=False
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
85 return_to = request.getQualifiedURL(request.page.url(request, qstr))
4334
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4237
diff changeset
86 trust_root = request.url_root
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
87 if oidreq.shouldSendRedirect():
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
88 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
89 request.http_redirect(redirect_url)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
90 else:
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
91 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
92 form_tag_attrs={'id': 'openid_message'})
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
93 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
94
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
95
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
96 def _handle_oidreturn(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
97 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
98 _ = request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
99
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
100 oidconsumer = consumer.Consumer(request.session,
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
101 MoinOpenIDStore(request))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
102 query = {}
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
103 for key in request.form:
4237
162002d5b148 Replaced some references to request.form with MultiDict-compatible semantics
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4235
diff changeset
104 query[key] = request.form[key]
4565
68d29223dfa9 page.url() should preferably get a query dict, not a query string. It calls wikiutil.makeQueryString() internally.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4544
diff changeset
105 qstr = {'action': 'userprefs',
68d29223dfa9 page.url() should preferably get a query dict, not a query string. It calls wikiutil.makeQueryString() internally.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4544
diff changeset
106 'handler': 'oid',
68d29223dfa9 page.url() should preferably get a query dict, not a query string. It calls wikiutil.makeQueryString() internally.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4544
diff changeset
107 'oid.return': '1'}
3234
a739558ca3dc Page.url() default changed to relative=False
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3122
diff changeset
108 return_to = request.getQualifiedURL(request.page.url(request, qstr))
4544
776cc7f6f6c6 compatibility fix for python-openid 2.x.x (also works with 1.x.x), fixes crash when trying to associate moin user to openid
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4363
diff changeset
109 info = oidconsumer.complete(query, return_to)
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
110 if info.status == consumer.FAILURE:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
111 return 'error', _('OpenID error: %s.') % info.message
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
112 elif info.status == consumer.CANCEL:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
113 return 'info', _('Verification canceled.')
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
114 elif info.status == consumer.SUCCESS:
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
115 if not hasattr(self.request.user, 'openids'):
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
116 request.user.openids = []
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
117
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
118 if info.identity_url in request.user.openids:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
119 return 'error', _("OpenID is already present.")
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
120
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
121 if user.getUserIdByOpenId(request, info.identity_url):
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
122 return 'error', _("This OpenID is already used for another account.")
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
123
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
124 # all fine
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
125 request.user.openids.append(info.identity_url)
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
126 request.user.save()
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
127 return 'info', _("OpenID added successfully.")
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
128 else:
3513
7bfd4232cfb7 builtin userprefs plugins: return appropriate message class
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3326
diff changeset
129 return 'error', _('OpenID failure.')
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
130
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
131
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
132 def handle_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
133 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
134 form = self.request.form
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
135
4428
ded09bdc3902 pre-1.9: request.form has qs args and post data, 1.9: .form only post data, .args only qs args, .values both
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4376
diff changeset
136 if self.request.values.has_key('oid.return'):
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
137 return self._handle_oidreturn()
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 if form.has_key('cancel'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
140 return
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
141
4186
126559845d4b Use Werkzeug-property names instead of old MoinMoin attribute names
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4178
diff changeset
142 if self.request.method != 'POST':
3326
a82b02396941 openid userprefs: ignore non-POST submissions
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3234
diff changeset
143 return
a82b02396941 openid userprefs: ignore non-POST submissions
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3234
diff changeset
144
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
145 if form.has_key('remove'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
146 return self._handle_remove()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
147
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
148 if form.has_key('add'):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
149 return self._handle_add()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
150
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
151 return
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
152
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
153 def _make_form(self):
4235
a6c315ff8d66 Make more use of werkzeugs Href object for URL-generation in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4186
diff changeset
154 action = "%s%s" % (self.request.script_root, self.request.path)
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
155 _form = html.FORM(action=action)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
156 _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
157 _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
158 return _form
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 _make_row(self, label, cell, **kw):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
161 """ Create a row in the form table.
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
162 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
163 self._table.append(html.TR().extend([
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
164 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
165 html.TD().extend(cell),
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
166 ]))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
167
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
168 def _oidlist(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
169 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
170 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
171 for oid in self.request.user.openids:
4363
817d99d715fe remove direct usage of deprecated sha module - use hashlib, if possible
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3513
diff changeset
172 name = "rm-%s" % hash_new('sha1', oid).hexdigest()
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
173 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
174 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
175 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
176 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
177 label = _("Remove selected")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
178 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
179 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
180
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
181 def _addoidform(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
182 _ = self.request.getText
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
183 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
184 # go back to this page
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
185 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
186 label = _("Add OpenID")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
187 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
188 name="openid_identifier",
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
189 id="openididentifier"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
190 form.append(html.BR())
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
191 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
192 self._make_row(_('Add OpenID'), [form])
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 def create_form(self):
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
195 """ Create the complete HTML form code. """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
196 _ = self._
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
197
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
198 ret = html.P()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
199 # Use the user interface language and direction
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
200 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
201 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
202 self._table = html.TABLE(border="0")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
203 ret.append(self._table)
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
204 ret.append(html.Raw("</div>"))
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
205
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
206 request = self.request
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
207
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
208 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
209 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
210 # 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
211 submitjs = """<script type="text/javascript">
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
212 <!--//
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
213 document.getElementById("openid_message").submit();
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
214 //-->
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
215 </script>
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
216 """
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
217 oidhtml = request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
218 del request.session['openid.prefs.form_html']
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
219 return ''.join([txt, oidhtml, submitjs])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
220
2364
96ec23afac7d fix some bugs in openid prefs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2304
diff changeset
221 if hasattr(request.user, 'openids') and request.user.openids:
2301
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
222 self._oidlist()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
223 self._addoidform()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
224
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
225 form = self._make_form()
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
226 label = _("Cancel")
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
227 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
228 self._make_row('', [form])
35617ed953c4 OpenID preferences (allows adding/removing OpenIDs)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
229 return unicode(ret)