paulb@54 | 1 | #!/usr/bin/env python |
paulb@54 | 2 | |
paulb@54 | 3 | "A test of authentication/authorisation." |
paulb@54 | 4 | |
paulb@54 | 5 | import WebStack.Generic |
paulb@54 | 6 | |
paulb@54 | 7 | class AuthResource: |
paulb@54 | 8 | |
paulb@54 | 9 | "A resource demanding authentication." |
paulb@54 | 10 | |
paulb@54 | 11 | def respond(self, trans): |
paulb@54 | 12 | user = trans.get_user() |
paulb@54 | 13 | if user is None: |
paulb@54 | 14 | trans.set_response_code(401) |
paulb@67 | 15 | trans.set_header_value("WWW-Authenticate", 'Basic realm="AuthResource"') |
paulb@54 | 16 | return |
paulb@54 | 17 | |
paulb@54 | 18 | trans.set_content_type(WebStack.Generic.ContentType("text/html")) |
paulb@54 | 19 | |
paulb@54 | 20 | # Write out confirmation, otherwise. |
paulb@54 | 21 | |
paulb@54 | 22 | out = trans.get_response_stream() |
paulb@54 | 23 | out.write(""" |
paulb@54 | 24 | <html> |
paulb@54 | 25 | <body> |
paulb@54 | 26 | <h1>Authorised</h1> |
paulb@54 | 27 | <p>Hello user %s!</p> |
paulb@54 | 28 | </body> |
paulb@54 | 29 | </html> |
paulb@54 | 30 | """ % ( |
paulb@54 | 31 | user, |
paulb@54 | 32 | )) |
paulb@54 | 33 | |
paulb@54 | 34 | # vim: tabstop=4 expandtab shiftwidth=4 |