1.1 --- a/WebStack/Resources/Login.py Thu Jan 18 19:56:34 2007 +0000
1.2 +++ b/WebStack/Resources/Login.py Thu Jan 18 22:40:07 2007 +0000
1.3 @@ -41,6 +41,10 @@
1.4
1.5 The optional 'urlencoding' parameter allows a special encoding to be
1.6 used in producing the redirection path.
1.7 +
1.8 + To change the pages employed by this resource, either redefine the
1.9 + 'login_page' and 'success_page' attributes in instances of this class or
1.10 + a subclass, or override the 'show_login' and 'show_success' methods.
1.11 """
1.12
1.13 self.authenticator = authenticator
1.14 @@ -84,16 +88,32 @@
1.15 def show_login(self, trans, app, path, qs):
1.16
1.17 """
1.18 - Writes a login screen using the transaction 'trans', including details of the
1.19 - 'app', 'path' and 'qs' which the client was attempting to access.
1.20 + Writes a login screen using the transaction 'trans', including details
1.21 + of the 'app', 'path' and 'qs' which the client was attempting to access.
1.22 """
1.23
1.24 trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
1.25 out = trans.get_response_stream()
1.26 - out.write("""
1.27 + out.write(self.login_page % (app, path, qs))
1.28 +
1.29 + def show_success(self, trans, app, path, qs):
1.30 +
1.31 + """
1.32 + Writes a success screen using the transaction 'trans', including details
1.33 + of the 'app', 'path' and 'qs' which the client was attempting to access.
1.34 + """
1.35 +
1.36 + # When authentication fails or is yet to take place, show the login
1.37 + # screen.
1.38 +
1.39 + trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
1.40 + out = trans.get_response_stream()
1.41 + out.write(self.success_page % (app, trans.encode_path(path, self.urlencoding), qs))
1.42 +
1.43 + login_page = """
1.44 <html>
1.45 <head>
1.46 - <title>Login Example</title>
1.47 + <title>Login</title>
1.48 </head>
1.49 <body>
1.50 <h1>Login</h1>
1.51 @@ -107,16 +127,9 @@
1.52 </form>
1.53 </body>
1.54 </html>
1.55 -""" % (app, path, qs))
1.56 -
1.57 - def show_success(self, trans, app, path, qs):
1.58 +"""
1.59
1.60 - # When authentication fails or is yet to take place, show the login
1.61 - # screen.
1.62 -
1.63 - trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
1.64 - out = trans.get_response_stream()
1.65 - out.write("""
1.66 + success_page = """
1.67 <html>
1.68 <head>
1.69 <title>Login Example</title>
1.70 @@ -126,7 +139,7 @@
1.71 <p>Please proceed <a href="%s%s%s">to the application</a>.</p>
1.72 </body>
1.73 </html>
1.74 -""" % (app, trans.encode_path(path, self.urlencoding), qs))
1.75 +"""
1.76
1.77 class LoginAuthenticator:
1.78
2.1 --- a/WebStack/Resources/LoginRedirect.py Thu Jan 18 19:56:34 2007 +0000
2.2 +++ b/WebStack/Resources/LoginRedirect.py Thu Jan 18 22:40:07 2007 +0000
2.3 @@ -4,7 +4,7 @@
2.4 Login redirection resources, sending unauthenticated users to a login screen
2.5 URL.
2.6
2.7 -Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2004, 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This library is free software; you can redistribute it and/or
2.11 modify it under the terms of the GNU Lesser General Public
2.12 @@ -33,7 +33,7 @@
2.13 def __init__(self, resource, authenticator, login_url=None, app_url=None,
2.14 anonymous_parameter_name=None, anonymous_username="anonymous",
2.15 logout_parameter_name=None, logout_url="/", use_logout_redirect=1,
2.16 - urlencoding=None):
2.17 + urlencoding=None, path_encoding=None):
2.18
2.19 """
2.20 Initialise the resource with a 'resource' for the application being
2.21 @@ -58,8 +58,13 @@
2.22 If the optional 'use_logout_redirect' flag is set to 0, a confirmation
2.23 screen is given instead of redirecting the user to the 'logout_url'.
2.24
2.25 - The optional 'urlencoding' parameter allows a special encoding to be
2.26 - used in producing the redirection path.
2.27 + The optional 'path_encoding' parameter (previously 'urlencoding', which
2.28 + is still supported) allows a special encoding to be used in producing
2.29 + the redirection path.
2.30 +
2.31 + To change the page used by this resource, either redefine the
2.32 + 'logout_page' attribute in instances of this class or a subclass, or
2.33 + override the 'show_logout' method.
2.34 """
2.35
2.36 self.login_url = login_url
2.37 @@ -71,13 +76,13 @@
2.38 self.logout_parameter_name = logout_parameter_name
2.39 self.logout_url = logout_url
2.40 self.use_logout_redirect = use_logout_redirect
2.41 - self.urlencoding = urlencoding or self.encoding
2.42 + self.path_encoding = path_encoding or urlencoding or self.encoding
2.43
2.44 def respond(self, trans):
2.45
2.46 "Respond using the given transaction 'trans'."
2.47
2.48 - fields_path = trans.get_fields_from_path(self.urlencoding)
2.49 + fields_path = trans.get_fields_from_path(self.path_encoding)
2.50
2.51 # Check for the logout parameter, if appropriate.
2.52
2.53 @@ -95,7 +100,7 @@
2.54
2.55 # Show the logout confirmation anyway.
2.56
2.57 - self._show_logout(trans, self.logout_url)
2.58 + self.show_logout(trans, self.logout_url)
2.59
2.60 # Check the authentication details with the specified authenticator.
2.61
2.62 @@ -118,24 +123,41 @@
2.63
2.64 # Redirect to the login URL.
2.65
2.66 - path = trans.get_path_without_query(self.urlencoding)
2.67 + path = trans.get_path_without_query(self.path_encoding)
2.68 qs = trans.get_query_string()
2.69 if qs:
2.70 qs = "?" + qs
2.71 trans.redirect("%s?app=%s&path=%s&qs=%s" % (
2.72 self.get_login_url(trans),
2.73 - trans.encode_path(self.get_app_url(trans), self.urlencoding),
2.74 - trans.encode_path(path, self.urlencoding),
2.75 - trans.encode_path(qs, self.urlencoding))
2.76 + trans.encode_path(self.get_app_url(trans), self.path_encoding),
2.77 + trans.encode_path(path, self.path_encoding),
2.78 + trans.encode_path(qs, self.path_encoding))
2.79 )
2.80
2.81 def get_app_url(self, trans):
2.82 +
2.83 + """
2.84 + Return the application URL, using 'trans' if necessary, in order to
2.85 + provide a complete URL to redirect an authenticated user to their
2.86 + originally requested page. If the application URL is empty, any
2.87 + redirects will be within the same application, rather than to
2.88 + potentially completely different applications residing at arbitrary
2.89 + locations.
2.90 + """
2.91 +
2.92 return self.app_url
2.93
2.94 def get_login_url(self, trans):
2.95 +
2.96 + """
2.97 + Return the login URL, using 'trans' if necessary, in order to
2.98 + provide a complete URL to redirect an authenticated user to their
2.99 + originally requested page.
2.100 + """
2.101 +
2.102 return self.login_url
2.103
2.104 - def _show_logout(self, trans, redirect):
2.105 + def show_logout(self, trans, redirect):
2.106
2.107 """
2.108 Write a confirmation page to 'trans' containing the 'redirect' URL which the
2.109 @@ -146,7 +168,9 @@
2.110
2.111 trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding))
2.112 out = trans.get_response_stream()
2.113 - out.write("""
2.114 + out.write(self.logout_page % redirect)
2.115 +
2.116 + logout_page = """
2.117 <html>
2.118 <head>
2.119 <title>Logout</title>
2.120 @@ -156,7 +180,7 @@
2.121 <p>Please proceed <a href="%s">to the application</a>.</p>
2.122 </body>
2.123 </html>
2.124 -""" % redirect)
2.125 +"""
2.126
2.127 class LoginRedirectAuthenticator:
2.128