forked from mcdonc/bikes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
executable file
·93 lines (85 loc) · 3.05 KB
/
Copy pathapp2.py
File metadata and controls
executable file
·93 lines (85 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPForbidden
from waitress import serve
class BlogentryViews(object):
def __init__(self, request):
self.request = request
@view_config(route_name='blogentry_show')
def show(self):
print self.request.cookies.get('userid')
return Response('Shown')
# [1]
@view_config(route_name='blogentry_delete')
def delete(self):
userid = self.request.cookies.get('userid')
if userid is None:
raise HTTPForbidden()
return Response('Deleted')
# [2]
@view_config(route_name='login')
def login(self):
userid = self.request.params.get('userid')
headers = [('Set-Cookie',
'userid=%s' % str(userid))]
return Response(
'Logged in as %s' % userid,
headers=headers
)
# [3]
@view_config(route_name='logout')
def logout(self):
headers = [
('Set-Cookie',
'userid=deleted; Expires=Thu, 01-Jan-1970 00:00:01 GMT')
]
return Response(
'Logged out',
headers=headers
)
# [4]
if __name__ == '__main__':
config = Configurator()
config.add_route('blogentry_show', '/blog/{id}')
config.add_route('blogentry_delete', '/blog/{id}/delete')
config.add_route('login', '/login')
config.add_route('logout', '/logout')
config.scan()
app = config.make_wsgi_app()
serve(app)
# Basic security; only authenticated users can delete blog entries. all others
# can view blog entries.
#
# New features:
#
# [4] Wiring up login and logout views into config.
# [2] Login view to service authorization checks. Just a stub, a real app
# would require password checking.
# [3] Logout view to forget login credentials.
# [1] Imperative authorization code to check whether a logged in user can delete
#
# Noteworthy:
#
# - No frameworky bits, it's all your code.
#
# - Imperative code to check whether a logged in user can delete must
# be repeated everywhere to be useful.
#
# - Married to cookie-based authentication throughout codebase (everywhere:
# [1], [2], and [3]). A change to the authentication mechanism implies
# visiting each place the imperative security checking is done.
#
# - Authentication is "who you are". Authorization is "what you can do". In
# this application, authentication and authorization are intertwined.
#
# - While authorization typically depends on authentication, authentication is
# almost always logically independent of authorization. Our application does
# not take this into consideration, however. It has no abstractions that
# would allow them to be changed independently.
#
# - Curiosity: httpexceptions can either be raised or returned. Typically you
# raise if you want the work done in the current transaction to be rolled
# back. We raise HTTPForbidden above, but return HTTPFound, for this
# notional reason.