| 1 |
|
|---|
| 2 |
import cherrypy |
|---|
| 3 |
|
|---|
| 4 |
def auth_user(username, password): |
|---|
| 5 |
if username=='foo' and password=='bar': |
|---|
| 6 |
return None |
|---|
| 7 |
else: |
|---|
| 8 |
return u"access denyed" |
|---|
| 9 |
|
|---|
| 10 |
class PublicContents: |
|---|
| 11 |
def index(self, **kwargs): |
|---|
| 12 |
return """<html><head><title>welcome</title></head> |
|---|
| 13 |
<body> |
|---|
| 14 |
<p>user:foo / password:bar</p> |
|---|
| 15 |
<p><a href="%s">go my private page.</a></p> |
|---|
| 16 |
<p><a href="%s">direct link to secure resource.</a> (required login, but error!)</p> |
|---|
| 17 |
</body></html> |
|---|
| 18 |
""" % (cherrypy.url('/secure/'), cherrypy.url('/secure/subpage/some/path')) |
|---|
| 19 |
|
|---|
| 20 |
class SecureContents: |
|---|
| 21 |
def index(self, **kwargs): |
|---|
| 22 |
return """<html><head><title>welcome to secure contents</title></head> |
|---|
| 23 |
<body> |
|---|
| 24 |
<p>Hello, %s.</p> |
|---|
| 25 |
<p>example link: <a href="%s">go my subpage.</a></p> |
|---|
| 26 |
<p>example link: <a href="%s">go my subpage/some/path.</a></p> |
|---|
| 27 |
<p>example link: <a href="%s">go my sub/page.</a> (...not work now by another reason)</p> |
|---|
| 28 |
<p><a href="%s">HOME</a></p> |
|---|
| 29 |
<p align="right"><a href="%s">LOGOUT</a></p> |
|---|
| 30 |
</body></html> |
|---|
| 31 |
""" % (cherrypy.request.login, |
|---|
| 32 |
cherrypy.url('/secure/subpage'), |
|---|
| 33 |
cherrypy.url('/secure/subpage/some/path'), |
|---|
| 34 |
cherrypy.url('/secure/sub/page'), |
|---|
| 35 |
cherrypy.url('/'), |
|---|
| 36 |
cherrypy.url('/secure/do_logout')) |
|---|
| 37 |
|
|---|
| 38 |
def subpage(self, subpath, **kwargs): |
|---|
| 39 |
return """<html><head><title>secure contents subpath</title></head> |
|---|
| 40 |
<body> |
|---|
| 41 |
<p>This is secure resource for: <b>%s</b>.</p> |
|---|
| 42 |
<p><a href="%s">MY PAGE</a></p> |
|---|
| 43 |
</body></html> |
|---|
| 44 |
""" % (subpath or "[TOP]", cherrypy.url('/secure')) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
insecure = PublicContents() |
|---|
| 55 |
secure = SecureContents() |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
d = cherrypy.dispatch.RoutesDispatcher() |
|---|
| 59 |
d.connect('insecure', '', controller=insecure, action='index') |
|---|
| 60 |
d.connect('secure', 'secure', controller=secure, action='index') |
|---|
| 61 |
d.connect('secure_subpage', 'secure/subpage/*subpath', controller=secure, action='subpage', subpath='') |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
cherrypy.tree.mount(None, "", { |
|---|
| 65 |
'/' : { |
|---|
| 66 |
'request.dispatch' : d |
|---|
| 67 |
}, |
|---|
| 68 |
'/secure' : { |
|---|
| 69 |
'tools.sessions.on' : True, |
|---|
| 70 |
'tools.session_auth.on' : True, |
|---|
| 71 |
'tools.session_auth.check_username_and_password' : auth_user |
|---|
| 72 |
} |
|---|
| 73 |
}) |
|---|
| 74 |
cherrypy.server.quickstart() |
|---|
| 75 |
cherrypy.engine.start() |
|---|