1.1 --- a/WebStack/Resources/LoginRedirect.py Thu Jan 18 19:56:34 2007 +0000
1.2 +++ b/WebStack/Resources/LoginRedirect.py Thu Jan 18 22:40:07 2007 +0000
1.3 @@ -4,7 +4,7 @@
1.4 Login redirection resources, sending unauthenticated users to a login screen
1.5 URL.
1.6
1.7 -Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2004, 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -33,7 +33,7 @@
1.13 def __init__(self, resource, authenticator, login_url=None, app_url=None,
1.14 anonymous_parameter_name=None, anonymous_username="anonymous",
1.15 logout_parameter_name=None, logout_url="/", use_logout_redirect=1,
1.16 - urlencoding=None):
1.17 + urlencoding=None, path_encoding=None):
1.18
1.19 """
1.20 Initialise the resource with a 'resource' for the application being
1.21 @@ -58,8 +58,13 @@
1.22 If the optional 'use_logout_redirect' flag is set to 0, a confirmation
1.23 screen is given instead of redirecting the user to the 'logout_url'.
1.24
1.25 - The optional 'urlencoding' parameter allows a special encoding to be
1.26 - used in producing the redirection path.
1.27 + The optional 'path_encoding' parameter (previously 'urlencoding', which
1.28 + is still supported) allows a special encoding to be used in producing
1.29 + the redirection path.
1.30 +
1.31 + To change the page used by this resource, either redefine the
1.32 + 'logout_page' attribute in instances of this class or a subclass, or
1.33 + override the 'show_logout' method.
1.34 """
1.35
1.36 self.login_url = login_url
1.37 @@ -71,13 +76,13 @@
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 - self.urlencoding = urlencoding or self.encoding
1.42 + self.path_encoding = path_encoding or urlencoding or self.encoding
1.43
1.44 def respond(self, trans):
1.45
1.46 "Respond using the given transaction 'trans'."
1.47
1.48 - fields_path = trans.get_fields_from_path(self.urlencoding)
1.49 + fields_path = trans.get_fields_from_path(self.path_encoding)
1.50
1.51 # Check for the logout parameter, if appropriate.
1.52
1.53 @@ -95,7 +100,7 @@
1.54
1.55 # Show the logout confirmation anyway.
1.56
1.57 - self._show_logout(trans, self.logout_url)
1.58 + self.show_logout(trans, self.logout_url)
1.59
1.60 # Check the authentication details with the specified authenticator.
1.61
1.62 @@ -118,24 +123,41 @@
1.63
1.64 # Redirect to the login URL.
1.65
1.66 - path = trans.get_path_without_query(self.urlencoding)
1.67 + path = trans.get_path_without_query(self.path_encoding)
1.68 qs = trans.get_query_string()
1.69 if qs:
1.70 qs = "?" + qs
1.71 trans.redirect("%s?app=%s&path=%s&qs=%s" % (
1.72 self.get_login_url(trans),
1.73 - trans.encode_path(self.get_app_url(trans), self.urlencoding),
1.74 - trans.encode_path(path, self.urlencoding),
1.75 - trans.encode_path(qs, self.urlencoding))
1.76 + trans.encode_path(self.get_app_url(trans), self.path_encoding),
1.77 + trans.encode_path(path, self.path_encoding),
1.78 + trans.encode_path(qs, self.path_encoding))
1.79 )
1.80
1.81 def get_app_url(self, trans):
1.82 +
1.83 + """
1.84 + Return the application URL, using 'trans' if necessary, in order to
1.85 + provide a complete URL to redirect an authenticated user to their
1.86 + originally requested page. If the application URL is empty, any
1.87 + redirects will be within the same application, rather than to
1.88 + potentially completely different applications residing at arbitrary
1.89 + locations.
1.90 + """
1.91 +
1.92 return self.app_url
1.93
1.94 def get_login_url(self, trans):
1.95 +
1.96 + """
1.97 + Return the login URL, using 'trans' if necessary, in order to
1.98 + provide a complete URL to redirect an authenticated user to their
1.99 + originally requested page.
1.100 + """
1.101 +
1.102 return self.login_url
1.103
1.104 - def _show_logout(self, trans, redirect):
1.105 + def show_logout(self, trans, redirect):
1.106
1.107 """
1.108 Write a confirmation page to 'trans' containing the 'redirect' URL which the
1.109 @@ -146,7 +168,9 @@
1.110
1.111 trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
1.112 out = trans.get_response_stream()
1.113 - out.write("""
1.114 + out.write(self.logout_page % redirect)
1.115 +
1.116 + logout_page = """
1.117 <html>
1.118 <head>
1.119 <title>Logout</title>
1.120 @@ -156,7 +180,7 @@
1.121 <p>Please proceed <a href="%s">to the application</a>.</p>
1.122 </body>
1.123 </html>
1.124 -""" % redirect)
1.125 +"""
1.126
1.127 class LoginRedirectAuthenticator:
1.128