root/oss/httpauthfilter/example.py

Revision 172, 2.0 kB (checked in by sylvain, 3 years ago)

* Added support for hashed passwords (md5)
* Fixed static content handling

Line 
1 # -*- coding: utf-8 -*-
2
3 import sys
4 import os.path
5 import cherrypy
6 from httpauthfilter import HttpAuthFilter
7
8 def retrieveAuthUsers():
9     return {'test':'test'}
10
11 class Son:
12     def index(self):
13         '''Protected by the filter above'''
14         return "Hello son!"
15     index.exposed = True
16
17     def hello(self, name):
18         return "Hello %s" % (name, )
19     hello.exposed = True
20
21 class Admin:
22     _cp_filters = [ HttpAuthFilter(realm='localhost',
23                                    privateKey='duh!',
24                                    unauthorizedPath='/admin/unauthorized',
25                                    retrieveUsersFunc=retrieveAuthUsers) ]
26
27     def __init__(self):
28         self.son = Son()
29
30     def index(self):
31         '''Only available once the user is authenticated'''
32         return """<html>
33 <head>
34     <title>Administration panel</title>
35     <script type="application/javascript" src="static/test.js"> </script>
36 </head>
37 <body>
38    Hello there
39 </body>
40 </html>
41 """
42     index.exposed = True
43
44     def unauthorized(self, *args, **kwargs):
45         '''Must be declared at the same level where you defined the filter'''
46         return "You are not authorized to access that page"
47     unauthorized.exposed = True
48
49 class Root:
50     def __init__(self):
51         self.admin = Admin()
52
53     def index(self):
54         return "hey how you doing?"
55     index.exposed = True
56
57 if __name__ == '__main__':
58     current_dir = os.path.dirname(os.path.abspath(__file__))
59     print current_dir
60     conf = {'global': {'server.socket_host': 'localhost',
61                        'log_debug_info_filter.on': False,
62                        'autoreload.on': False,
63                        'server.log_to_screen': True},
64             '/admin/static': {'static_filter.on': True,
65                               'static_filter.dir': 'static',
66                               'static_filter.root': current_dir}}
67     cherrypy.lowercase_api = True
68     cherrypy.config.update(conf)
69     cherrypy.tree.mount(Root(), '/', conf=conf)
70     cherrypy.server.start()
71    
Note: See TracBrowser for help on using the browser.