WebStack

Annotated examples/Common/Auth/__init__.py

79:d53db0bc6ece
2004-02-29 paulb [project @ 2004-02-29 00:06:37 by paulb] Added an authenticator class.
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
        trans.set_content_type(WebStack.Generic.ContentType("text/html"))
paulb@54 13
paulb@54 14
        # Write out confirmation, otherwise.
paulb@54 15
paulb@54 16
        out = trans.get_response_stream()
paulb@54 17
        out.write("""
paulb@54 18
<html>
paulb@54 19
  <body>
paulb@54 20
    <h1>Authorised</h1>
paulb@54 21
    <p>Hello user %s!</p>
paulb@54 22
  </body>
paulb@54 23
</html>
paulb@54 24
""" % (
paulb@79 25
    trans.get_user(),
paulb@54 26
))
paulb@54 27
paulb@79 28
class AuthAuthenticator:
paulb@79 29
paulb@79 30
    "An authenticator for the application."
paulb@79 31
paulb@79 32
    def authenticate(self, trans):
paulb@79 33
        user = trans.get_user()
paulb@79 34
        return user is not None
paulb@79 35
paulb@79 36
    def get_auth_type(self):
paulb@79 37
        return "Basic"
paulb@79 38
paulb@79 39
    def get_realm(self):
paulb@79 40
        return "AuthResource"
paulb@79 41
paulb@54 42
# vim: tabstop=4 expandtab shiftwidth=4