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