1.1 --- a/examples/Common/LoginRedirect/__init__.py Sun May 30 16:40:26 2004 +0000
1.2 +++ b/examples/Common/LoginRedirect/__init__.py Sun May 30 16:40:48 2004 +0000
1.3 @@ -3,13 +3,15 @@
1.4 "Login redirection."
1.5
1.6 from WebStack.Helpers.Auth import get_token
1.7 +import WebStack.Generic
1.8
1.9 class LoginRedirectResource:
1.10
1.11 "A resource redirecting to a login URL."
1.12
1.13 def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None,
1.14 - anonymous_username="anonymous"):
1.15 + anonymous_username="anonymous", logout_parameter_name=None, logout_url="/",
1.16 + use_logout_redirect=1):
1.17
1.18 """
1.19 Initialise the resource with a 'login_url', an 'app_url' where the 'resource' for
1.20 @@ -19,6 +21,13 @@
1.21 of that name in the URL will not be authenticated, but then such clients will get
1.22 a predefined user identity associated with them, configurable using the optional
1.23 'anonymous_username'.
1.24 +
1.25 + If the optional 'logout_parameter_name' is set, clients providing a parameter of
1.26 + that name in the URL will become logged out. After logging out, clients are
1.27 + redirected to a location which can be configured by the optional 'logout_url'.
1.28 +
1.29 + If the optional 'use_logout_redirect' flag is set to 0, a confirmation screen is
1.30 + given instead of redirecting the user to the 'logout_url'.
1.31 """
1.32
1.33 self.login_url = login_url
1.34 @@ -27,14 +36,35 @@
1.35 self.authenticator = authenticator
1.36 self.anonymous_parameter_name = anonymous_parameter_name
1.37 self.anonymous_username = anonymous_username
1.38 + self.logout_parameter_name = logout_parameter_name
1.39 + self.logout_url = logout_url
1.40 + self.use_logout_redirect = use_logout_redirect
1.41
1.42 def respond(self, trans):
1.43
1.44 fields_path = trans.get_fields_from_path()
1.45
1.46 + # Check for the logout parameter, if appropriate.
1.47 +
1.48 + if self.logout_parameter_name is not None and fields_path.has_key(self.logout_parameter_name):
1.49 +
1.50 + # Remove the special cookie token, then pass on the transaction.
1.51 +
1.52 + self.authenticator.unset_token(trans)
1.53 +
1.54 + # Redirect to the logout URL.
1.55 +
1.56 + if self.use_logout_redirect:
1.57 + trans.set_header_value("Location", self.logout_url)
1.58 + trans.set_response_code(307)
1.59 +
1.60 + # Show the logout confirmation anyway.
1.61 +
1.62 + self._show_logout(trans, self.logout_url)
1.63 +
1.64 # Check the authentication details with the specified authenticator.
1.65
1.66 - if self.authenticator.authenticate(trans):
1.67 + elif self.authenticator.authenticate(trans):
1.68
1.69 # If successful, pass on the transaction.
1.70
1.71 @@ -64,6 +94,24 @@
1.72
1.73 return url.replace("?", "%3f").replace("&", "%26")
1.74
1.75 + def _show_logout(self, trans, redirect):
1.76 +
1.77 + # When logout takes place, show the login screen.
1.78 +
1.79 + trans.set_content_type(WebStack.Generic.ContentType("text/html"))
1.80 + out = trans.get_response_stream()
1.81 + out.write("""
1.82 +<html>
1.83 + <head>
1.84 + <title>Logout</title>
1.85 + </head>
1.86 + <body>
1.87 + <h1>Logout Successful</h1>
1.88 + <p>Please proceed <a href="%s">to the application</a>.</p>
1.89 + </body>
1.90 +</html>
1.91 +""" % redirect)
1.92 +
1.93 class LoginRedirectAuthenticator:
1.94
1.95 """
1.96 @@ -100,7 +148,7 @@
1.97
1.98 def set_token(self, trans, username):
1.99
1.100 - "Set an authentication in the 'trans' with the given 'username'."
1.101 + "Set an authentication token in 'trans' with the given 'username'."
1.102
1.103 trans.set_cookie_value(
1.104 self.cookie_name,
1.105 @@ -111,4 +159,10 @@
1.106
1.107 trans.set_user(username)
1.108
1.109 + def unset_token(self, trans):
1.110 +
1.111 + "Unset the authentication token in 'trans'."
1.112 +
1.113 + trans.delete_cookie(self.cookie_name)
1.114 +
1.115 # vim: tabstop=4 expandtab shiftwidth=4