1 #!/usr/bin/env python 2 3 """ 4 Login redirection resources, sending unauthenticated users to a login screen 5 URL. 6 7 Copyright (C) 2004, 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> 8 9 This library is free software; you can redistribute it and/or 10 modify it under the terms of the GNU Lesser General Public 11 License as published by the Free Software Foundation; either 12 version 2.1 of the License, or (at your option) any later version. 13 14 This library is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Lesser General Public License for more details. 18 19 You should have received a copy of the GNU Lesser General Public 20 License along with this library; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 22 """ 23 24 from WebStack.Helpers.Auth import Verifier 25 import WebStack.Generic 26 27 class LoginRedirectResource: 28 29 "A resource redirecting to a login URL." 30 31 encoding = "utf-8" 32 33 def __init__(self, resource, authenticator, login_url=None, app_url=None, 34 anonymous_parameter_name=None, anonymous_username="anonymous", 35 logout_parameter_name=None, logout_url="/", use_logout_redirect=1, 36 urlencoding=None, path_encoding=None): 37 38 """ 39 Initialise the resource with a 'resource' for the application being 40 protected and an 'authenticator' protecting the resource. 41 42 If the optional 'login_url' and 'app_url' are provided, these values 43 will be used to locate the login application and protected application 44 respectively. Such values, if not provided, must be otherwise set at a 45 later time or provided by 'get_login_url' and 'get_app_url' methods in 46 a subclass of this class. 47 48 If the optional 'anonymous_parameter_name' is set, clients providing a 49 parameter of that name in the URL will not be authenticated, but then 50 such clients will get a predefined user identity associated with them, 51 configurable using the optional 'anonymous_username'. 52 53 If the optional 'logout_parameter_name' is set, clients providing a 54 parameter of that name in the URL will become logged out. After logging 55 out, clients are redirected to a location which can be configured by the 56 optional 'logout_url'. 57 58 If the optional 'use_logout_redirect' flag is set to 0, a confirmation 59 screen is given instead of redirecting the user to the 'logout_url'. 60 61 The optional 'path_encoding' parameter (previously 'urlencoding', which 62 is still supported) allows a special encoding to be used in producing 63 the redirection path. 64 65 To change the page used by this resource, either redefine the 66 'logout_page' attribute in instances of this class or a subclass, or 67 override the 'show_logout' method. 68 """ 69 70 self.login_url = login_url 71 self.app_url = app_url 72 self.resource = resource 73 self.authenticator = authenticator 74 self.anonymous_parameter_name = anonymous_parameter_name 75 self.anonymous_username = anonymous_username 76 self.logout_parameter_name = logout_parameter_name 77 self.logout_url = logout_url 78 self.use_logout_redirect = use_logout_redirect 79 self.path_encoding = path_encoding or urlencoding or self.encoding 80 81 def respond(self, trans): 82 83 "Respond using the given transaction 'trans'." 84 85 fields_path = trans.get_fields_from_path(self.path_encoding) 86 87 # Check for the logout parameter, if appropriate. 88 89 if self.logout_parameter_name is not None and fields_path.has_key(self.logout_parameter_name): 90 91 # Remove the special cookie token, then pass on the transaction. 92 93 self.authenticator.unset_token(trans) 94 95 # Redirect to the logout URL. 96 97 if self.use_logout_redirect: 98 trans.set_header_value("Location", self.logout_url) 99 trans.set_response_code(302) # was 307 100 101 # Show the logout confirmation anyway. 102 103 self.show_logout(trans, self.logout_url) 104 105 # Check the authentication details with the specified authenticator. 106 107 elif self.authenticator.authenticate(trans): 108 109 # If successful, pass on the transaction. 110 111 self.resource.respond(trans) 112 113 # Check for the anonymous parameter, if appropriate. 114 115 elif self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name): 116 117 # Make a special cookie token, then pass on the transaction. 118 119 self.authenticator.set_token(trans, self.anonymous_username) 120 self.resource.respond(trans) 121 122 else: 123 124 # Redirect to the login URL. 125 126 path = trans.get_path(self.path_encoding) 127 trans.redirect("%s?app=%s%s" % ( 128 self.get_login_url(trans), 129 trans.encode_path(self.get_app_url(trans), self.path_encoding), 130 trans.encode_path(path, self.path_encoding), 131 )) 132 133 def get_app_url(self, trans): 134 135 """ 136 Return the application URL, using 'trans' if necessary, in order to 137 provide a complete URL to redirect an authenticated user to their 138 originally requested page. If the application URL is empty, any 139 redirects will be within the same application, rather than to 140 potentially completely different applications residing at arbitrary 141 locations. 142 """ 143 144 return self.app_url 145 146 def get_login_url(self, trans): 147 148 """ 149 Return the login URL, using 'trans' if necessary, in order to 150 provide a complete URL to redirect an authenticated user to their 151 originally requested page. 152 """ 153 154 return self.login_url 155 156 def show_logout(self, trans, redirect): 157 158 """ 159 Write a confirmation page to 'trans' containing the 'redirect' URL which the 160 client should be sent to upon logout. 161 """ 162 163 # When logout takes place, show the logout screen. 164 165 trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) 166 out = trans.get_response_stream() 167 out.write(self.logout_page % redirect) 168 169 logout_page = """ 170 <html> 171 <head> 172 <title>Logout</title> 173 </head> 174 <body> 175 <h1>Logout Successful</h1> 176 <p>Please proceed <a href="%s">to the application</a>.</p> 177 </body> 178 </html> 179 """ 180 181 class LoginRedirectAuthenticator(Verifier): 182 183 """ 184 An authenticator which verifies the credentials provided in a special login 185 cookie. 186 """ 187 188 def authenticate(self, trans): 189 190 """ 191 Authenticate the originator of 'trans', updating the object if 192 successful and returning a true value if successful, or a false value 193 otherwise. 194 """ 195 196 valid = Verifier.authenticate(self, trans) 197 198 # Update the transaction with the user details. 199 200 if valid: 201 username, token = self.get_username_and_token(trans) 202 trans.set_user(username) 203 return valid 204 205 def set_token(self, trans, username): 206 207 "Set an authentication token in 'trans' with the given 'username'." 208 209 Verifier.set_token(self, trans, username) 210 211 # Update the transaction with the user details. 212 213 trans.set_user(username) 214 215 # vim: tabstop=4 expandtab shiftwidth=4