fernandotakai / herd (https://ubiquity.mozilla.com/)
The new Ubiquity Herd
Clone this repository (size: 655.8 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/fernandotakai/herd/
| commit 126: | 9a43feabbadd |
| parent 125: | bdfa82b7ccdd |
| branch: | default |
Changed herd to use beaker as its session backend
Changed (Δ341 bytes):
raw changeset »
api.py (18 lines added, 6 lines removed)
herd.py (3 lines added, 1 lines removed)
weboid/oid.py (38 lines added, 26 lines removed)
| … | … | @@ -13,6 +13,8 @@ from pygments.formatters import HtmlForm |
13 |
13 |
|
14 |
14 |
from weboid import oid as weboid |
15 |
15 |
|
16 |
from beaker.middleware import SessionMiddleware |
|
17 |
||
16 |
18 |
from cache import cache |
17 |
19 |
from storage import storage |
18 |
20 |
|
| … | … | @@ -33,11 +35,20 @@ urls = ( |
33 |
35 |
|
34 |
36 |
app = web.application(urls, locals(), autoreload=True) |
35 |
37 |
|
36 |
web.config.session_parameters['ignore_expiry'] = True |
|
37 |
web.config.session_parameters['ignore_change_ip'] = True |
|
38 |
web.config.session_parameters['timeout'] = 31104000 |
|
38 |
session_opts = { |
|
39 |
'session.auto': True, |
|
40 |
'session.secret': "1ca7f1589dc5b0d2ed9220489e500931", |
|
41 |
} |
|
39 |
42 |
|
40 |
session = utils.make_session(app) |
|
43 |
application = SessionMiddleware(app, key='session', secret='cfeb56a47bcc0cbc8680e6ea74555ac0', config=session_opts) |
|
44 |
||
45 |
def session(): |
|
46 |
session = web.ctx.environ['beaker.session'] |
|
47 |
||
48 |
if not session.get("urls", None): |
|
49 |
session['urls'] = [] |
|
50 |
||
51 |
return session |
|
41 |
52 |
|
42 |
53 |
class index(object): |
43 |
54 |
def GET(self): |
| … | … | @@ -45,6 +56,7 @@ class index(object): |
45 |
56 |
|
46 |
57 |
class submit(object): |
47 |
58 |
def POST(self): |
59 |
session = session() |
|
48 |
60 |
data = web.input(json = None).json |
49 |
61 |
|
50 |
62 |
if not data: |
| … | … | @@ -67,13 +79,13 @@ class submit(object): |
67 |
79 |
feed['_id'] = _id |
68 |
80 |
feed['subscribers'] = 1 |
69 |
81 |
feed['first_seen'] = datetime.now() |
70 |
session |
|
82 |
session['urls'].append(feed['url']) |
|
71 |
83 |
storage.save(feed) |
72 |
84 |
# Let's save the url on the user's session |
73 |
85 |
elif feed['url'] not in session.urls: |
74 |
86 |
feed_from_db['subscribers'] = feed_from_db['subscribers'] + 1 |
75 |
87 |
storage.update(feed_from_db) |
76 |
session |
|
88 |
session['urls'].append(feed['url']) |
|
77 |
89 |
|
78 |
90 |
return "Information submitted." |
79 |
91 |
| … | … | @@ -4,6 +4,8 @@ import api |
4 |
4 |
from weboid import oid |
5 |
5 |
from decorators import content_type |
6 |
6 |
|
7 |
from beaker.middleware import SessionMiddleware |
|
8 |
||
7 |
9 |
import os |
8 |
10 |
|
9 |
11 |
urls = ( |
| … | … | @@ -38,4 +40,4 @@ class editor(object): |
38 |
40 |
return render.editor() |
39 |
41 |
|
40 |
42 |
if __name__ == '__main__': |
41 |
app.run( |
|
43 |
app.run(SessionMiddleware) |
Up to file-list weboid/oid.py:
| … | … | @@ -31,14 +31,12 @@ class OidSession(object): |
31 |
31 |
def session(): |
32 |
32 |
def fget(self): |
33 |
33 |
if not getattr(self, '_session', None): |
34 |
self._session = web.session.Session(web.ctx.app_stack[0], |
|
35 |
MongoDBStore("oid_session"), |
|
36 |
|
|
34 |
self._session = web.ctx.environ['beaker.session'] |
|
37 |
35 |
|
38 |
36 |
return self._session |
39 |
37 |
|
40 |
38 |
def fdel(self): |
41 |
|
|
39 |
self._session['userid'] = None |
|
42 |
40 |
|
43 |
41 |
return (fget, None, fdel) |
44 |
42 |
|
| … | … | @@ -64,17 +62,20 @@ def to_square_one(err): |
64 |
62 |
class OidLogin(): |
65 |
63 |
""" Should be mapped from OID_VERIFY_URL """ |
66 |
64 |
def GET(self): |
67 |
osession=oid_session.session |
|
68 |
userid=osession.get('userid','') |
|
69 |
continue_url=osession.get('continue_url',web.ctx.home+'/') |
|
70 |
message=web.input().get('message', |
|
65 |
osession = oid_session.session |
|
66 |
userid = osession.get('userid','') |
|
67 |
continue_url = osession.get('continue_url',web.ctx.home+'/') |
|
68 |
message = web.input().get('message', |
|
71 |
69 |
userid and 'Log in as someone else (currently: %s)' % userid or |
72 |
70 |
'Please login with your OpenID URL') |
71 |
||
73 |
72 |
web.header('Content-Type','text/html; charset=UTF-8') |
73 |
||
74 |
74 |
return oid_view.login_template(ctx=web.ctx,message=message, |
75 |
75 |
action=OID_VERIFY_URL, |
76 |
76 |
openid_url=oid_session.session.get('user_input',''), |
77 |
77 |
continue_url=continue_url) |
78 |
||
78 |
79 |
class OidLogout: |
79 |
80 |
""" clear oid session and redirect to OID_LOGIN_URL """ |
80 |
81 |
def GET(self): |
| … | … | @@ -86,14 +87,18 @@ class OidVerify: |
86 |
87 |
def POST(self): |
87 |
88 |
""" Process the form submission, initiating OpenID verification. """ |
88 |
89 |
# TODO form validation (or something) |
89 |
inp |
|
90 |
inp = web.input() |
|
91 |
||
90 |
92 |
openid_url = inp.get('openid_url','').strip() |
93 |
||
91 |
94 |
if not openid_url: |
92 |
95 |
to_square_one('Error: Empty OpenID URL') |
93 |
osession=oid_session.session |
|
94 |
osession.user_input=openid_url |
|
95 |
osession.continue_url=inp.get('continue_url',web.ctx.home) |
|
96 |
||
97 |
osession = oid_session.session |
|
98 |
osession['user_input'] = openid_url |
|
99 |
osession['continue_url'] = inp.get('continue_url',web.ctx.home) |
|
96 |
100 |
oidconsumer = oid_session.get_consumer() |
101 |
||
97 |
102 |
try: |
98 |
103 |
request = oidconsumer.begin(openid_url) |
99 |
104 |
except HTTPFetchingError, exc: |
| … | … | @@ -118,7 +123,7 @@ class OidVerify: |
118 |
123 |
|
119 |
124 |
def GET(self): |
120 |
125 |
""" if people use "back" to get here, redirect to login page """ |
121 |
raise web.seeother(web.ctx.home |
|
126 |
raise web.seeother(web.ctx.home + OID_LOGIN_URL) |
|
122 |
127 |
|
123 |
128 |
class OidProcess: |
124 |
129 |
def GET(self): |
| … | … | @@ -131,7 +136,7 @@ class OidProcess: |
131 |
136 |
# either None or a string containing more information about |
132 |
137 |
# the return type. |
133 |
138 |
oidconsumer = oid_session.get_consumer() |
134 |
info = oidconsumer.complete(web.input(), |
|
139 |
info = oidconsumer.complete(web.input(), web.ctx.home + web.ctx.path) |
|
135 |
140 |
|
136 |
141 |
if info.status == consumer.FAILURE and info.identity_url: |
137 |
142 |
# In the case of failure, if info is non-None, it is the |
| … | … | @@ -139,16 +144,20 @@ class OidProcess: |
139 |
144 |
# message to help the user figure out what happened. |
140 |
145 |
fmt = "Verification of %s failed" |
141 |
146 |
to_square_one(fmt % info.identity_url) |
147 |
||
142 |
148 |
elif info.status == consumer.SUCCESS: |
143 |
osession=oid_session.session |
|
144 |
osession.identity_url=info.identity_url |
|
145 |
osession.userid=strip_url(info.identity_url) |
|
146 |
osession.user_input='' |
|
147 |
osession.canonical_id=info.endpoint.canonicalID or '' |
|
148 |
raise web.seeother(osession.get('continue_url',web.ctx.home+'/')) |
|
149 |
osession = oid_session.session |
|
150 |
osession['identity_url'] = info.identity_url |
|
151 |
osession['userid'] = strip_url(info.identity_url) |
|
152 |
osession['user_input'] = '' |
|
153 |
osession['canonical_id'] = info.endpoint.canonicalID or '' |
|
154 |
||
155 |
raise web.seeother(osession.get('continue_url', web.ctx.home+'/')) |
|
156 |
||
149 |
157 |
elif info.status == consumer.CANCEL: |
150 |
158 |
# cancelled |
151 |
159 |
to_square_one('Verification cancelled') |
160 |
||
152 |
161 |
else: |
153 |
162 |
# Either we don't understand the code or there is no |
154 |
163 |
# openid_url included with the error. Give a generic |
| … | … | @@ -158,7 +167,7 @@ class OidProcess: |
158 |
167 |
|
159 |
168 |
def simple_membership_callback(oid,args): |
160 |
169 |
""" args could be strings or lists. returns whether oid is in any of them """ |
161 |
return oid in (type(args) |
|
170 |
return oid in (type(args) == type([]) and args or [args]) |
|
162 |
171 |
# |
163 |
172 |
## The checkaccess decorator |
164 |
173 |
# |
| … | … | @@ -174,17 +183,20 @@ def checkaccess(access_args=None,access_ |
174 |
183 |
only allows me ;) """ |
175 |
184 |
if access_args and not access_callback: |
176 |
185 |
access_callback=simple_membership_callback |
186 |
||
177 |
187 |
def decorator(func): |
178 |
188 |
def proxyfunc(self, *args, **kw): |
179 |
189 |
osession = oid_session.session |
180 |
|
|
190 |
||
191 |
osession['continue_url'] = web.ctx.home + web.ctx.fullpath |
|
192 |
||
181 |
193 |
if not osession.get('userid',''): |
182 |
to_square_one(auth_error % {'path': |
|
194 |
to_square_one(auth_error % {'path': web.ctx.path}) |
|
183 |
195 |
return |
184 |
196 |
if access_callback and not access_callback( |
185 |
osession.userid,access_args): |
|
186 |
to_square_one(access_error % {'userid':osession.userid, |
|
187 |
|
|
197 |
osession['userid'], access_args): |
|
198 |
to_square_one(access_error % {'userid': osession['userid'], |
|
199 |
'path': web.ctx.path}) |
|
188 |
200 |
return func(self, *args, **kw) |
189 |
201 |
return proxyfunc |
190 |
202 |
return decorator |
