root/AuthKit/trunk/examples/user/database-model/app.py

Revision 152 (checked in by thejimmyg, 8 months ago)

Added a series of updates to SQLAlchemy code plus a fix to the OpenID support and an encoding problem with form.py

Line 
1 """
2 See the README.txt file for how to setup and use this example.
3
4 Note that the AuthKit middleware is by default only setup to intercept
5 401 responses due to NotAuthenticated errors. This means if you try to
6 access a resource when you aren't signed in and you don't have access
7 to it you will be prompted to sign in. If you are signed in you will
8 be shown the server's default 403 error page. If you want to be prompted
9 to sign in under these circumstances too, uncomment this line to the
10 middleware setup at the end of this example::
11
12     # setup_intercept = "401, 403",
13
14 """
15 from paste.httpexceptions import HTTPExceptionHandler
16 from authkit.authenticate import middleware
17 from authkit.authorize import authorize
18 from authkit.users.sqlalchemy_driver import UsersFromDatabase
19 from authkit.permissions import ValidAuthKitUser, HasAuthKitRole, HasAuthKitGroup
20
21 class SampleApp:
22    
23     # Application setup
24     def __call__(self, environ, start_response):
25         path = environ.get('PATH_INFO')
26         if path == '/user':
27             return self.user(environ, start_response)
28         elif path == '/admin':
29             return self.admin(environ, start_response)
30         elif path == '/group':
31             return self.group(environ, start_response)
32         elif path == '/':
33             return self.index(environ, start_response)
34         else:
35             start_response("404 Not Found", [("Content-type","text/plain")])
36             return ["Not Found"]
37
38     def _access_granted(self, start_response, message):
39         start_response("200 OK", [("Content-type","text/html")])
40         return [
41             "<html><head><title>AuthKit Database Example</title></head>",
42             "<body><h1>AuthKit Database Example</h1>",
43             message,
44             "</body></html>"
45         ]
46
47     # Induvidual pages
48     def index(self, environ, start_response):
49         return self._access_granted(
50             start_response,
51             """
52             <p>This page is public, try visiting the following pages:</p>
53             <ul>
54             <li><a href="/user">Any signed in user can access this page</a></li>
55             <li><a href="/admin">Only signed in users with the <tt>admin</tt> role have access [ben]</a></li>
56             <li><a href="/group">Only signed in users in the <tt>pylons</tt> group have access [james]</a></li>
57             </ul>
58             """
59         )
60        
61     @authorize(ValidAuthKitUser()) # Note we don't use RemoteUser() here because we only want users in the AuthKit database
62     def user(self, environ, start_response):
63         return self._access_granted(start_response, "Any user in the database can access this")
64        
65     @authorize(HasAuthKitRole(["admin"]))
66     def admin(self, environ, start_response):
67         return self._access_granted(start_response, "You have the <tt>admin</tt> role.")
68        
69     @authorize(HasAuthKitGroup(["pylons"]))
70     def group(self, environ, start_response):
71         return self._access_granted(start_response, "You are in the <tt>pylons</tt> group.")
72
73 app = SampleApp()
74 # Not needed, included by the authenticate middleware
75 #app = HTTPExceptionHandler(app)
76 app = middleware(
77     app,
78     setup_method='form,cookie',
79     cookie_secret='secret encryption string',
80     form_authenticate_user_type = "authkit.users.sqlalchemy_driver:UsersFromDatabase",
81     form_authenticate_user_data = "model",
82     cookie_signoutpath = '/signout',
83     # setup_intercept = "401, 403",
84 )
85 app = HTTPExceptionHandler(app)
86 if __name__ == '__main__':
87     import logging
88     logging.basicConfig(level=logging.DEBUG,
89                     format='%(asctime)s %(levelname)-8s %(message)s',
90                     datefmt='%a, %d %b %Y %H:%M:%S',
91                     filename='test.log',
92                     filemode='w')
93
94     from paste.httpserver import serve
95     serve(app, host='0.0.0.0', port=8080)
Note: See TracBrowser for help on using the browser.