# HG changeset patch # User paulb # Date 1085935248 0 # Node ID ea5edac6046d4302be9cbbbd472cec371d780ba6 # Parent d62e5d3e633ed5f51e3150e5bb6b942b7f8cf7db [project @ 2004-05-30 16:40:48 by paulb] Added a logout function. diff -r d62e5d3e633e -r ea5edac6046d examples/Common/LoginRedirect/__init__.py --- a/examples/Common/LoginRedirect/__init__.py Sun May 30 16:40:26 2004 +0000 +++ b/examples/Common/LoginRedirect/__init__.py Sun May 30 16:40:48 2004 +0000 @@ -3,13 +3,15 @@ "Login redirection." from WebStack.Helpers.Auth import get_token +import WebStack.Generic class LoginRedirectResource: "A resource redirecting to a login URL." def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None, - anonymous_username="anonymous"): + anonymous_username="anonymous", logout_parameter_name=None, logout_url="/", + use_logout_redirect=1): """ Initialise the resource with a 'login_url', an 'app_url' where the 'resource' for @@ -19,6 +21,13 @@ of that name in the URL will not be authenticated, but then such clients will get a predefined user identity associated with them, configurable using the optional 'anonymous_username'. + + If the optional 'logout_parameter_name' is set, clients providing a parameter of + that name in the URL will become logged out. After logging out, clients are + redirected to a location which can be configured by the optional 'logout_url'. + + If the optional 'use_logout_redirect' flag is set to 0, a confirmation screen is + given instead of redirecting the user to the 'logout_url'. """ self.login_url = login_url @@ -27,14 +36,35 @@ self.authenticator = authenticator self.anonymous_parameter_name = anonymous_parameter_name self.anonymous_username = anonymous_username + self.logout_parameter_name = logout_parameter_name + self.logout_url = logout_url + self.use_logout_redirect = use_logout_redirect def respond(self, trans): fields_path = trans.get_fields_from_path() + # Check for the logout parameter, if appropriate. + + if self.logout_parameter_name is not None and fields_path.has_key(self.logout_parameter_name): + + # Remove the special cookie token, then pass on the transaction. + + self.authenticator.unset_token(trans) + + # Redirect to the logout URL. + + if self.use_logout_redirect: + trans.set_header_value("Location", self.logout_url) + trans.set_response_code(307) + + # Show the logout confirmation anyway. + + self._show_logout(trans, self.logout_url) + # Check the authentication details with the specified authenticator. - if self.authenticator.authenticate(trans): + elif self.authenticator.authenticate(trans): # If successful, pass on the transaction. @@ -64,6 +94,24 @@ return url.replace("?", "%3f").replace("&", "%26") + def _show_logout(self, trans, redirect): + + # When logout takes place, show the login screen. + + trans.set_content_type(WebStack.Generic.ContentType("text/html")) + out = trans.get_response_stream() + out.write(""" + + + Logout + + +

Logout Successful

+

Please proceed to the application.

+ + +""" % redirect) + class LoginRedirectAuthenticator: """ @@ -100,7 +148,7 @@ def set_token(self, trans, username): - "Set an authentication in the 'trans' with the given 'username'." + "Set an authentication token in 'trans' with the given 'username'." trans.set_cookie_value( self.cookie_name, @@ -111,4 +159,10 @@ trans.set_user(username) + def unset_token(self, trans): + + "Unset the authentication token in 'trans'." + + trans.delete_cookie(self.cookie_name) + # vim: tabstop=4 expandtab shiftwidth=4