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 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 get_token 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, login_url, app_url, resource, authenticator, anonymous_parameter_name=None, 34 anonymous_username="anonymous", logout_parameter_name=None, logout_url="/", 35 use_logout_redirect=1, urlencoding=None): 36 37 """ 38 Initialise the resource with a 'login_url', an 'app_url' where the 39 'resource' for the application being protected should be reachable, and 40 an 'authenticator'. 41 42 If the optional 'anonymous_parameter_name' is set, clients providing a 43 parameter of that name in the URL will not be authenticated, but then 44 such clients will get a predefined user identity associated with them, 45 configurable using the optional 'anonymous_username'. 46 47 If the optional 'logout_parameter_name' is set, clients providing a 48 parameter of that name in the URL will become logged out. After logging 49 out, clients are redirected to a location which can be configured by the 50 optional 'logout_url'. 51 52 If the optional 'use_logout_redirect' flag is set to 0, a confirmation 53 screen is given instead of redirecting the user to the 'logout_url'. 54 55 The optional 'urlencoding' parameter allows a special encoding to be 56 used in producing the redirection path. 57 """ 58 59 self.login_url = login_url 60 self.app_url = app_url 61 self.resource = resource 62 self.authenticator = authenticator 63 self.anonymous_parameter_name = anonymous_parameter_name 64 self.anonymous_username = anonymous_username 65 self.logout_parameter_name = logout_parameter_name 66 self.logout_url = logout_url 67 self.use_logout_redirect = use_logout_redirect 68 self.urlencoding = urlencoding or self.encoding 69 70 def respond(self, trans): 71 72 "Respond using the given transaction 'trans'." 73 74 fields_path = trans.get_fields_from_path(self.urlencoding) 75 76 # Check for the logout parameter, if appropriate. 77 78 if self.logout_parameter_name is not None and fields_path.has_key(self.logout_parameter_name): 79 80 # Remove the special cookie token, then pass on the transaction. 81 82 self.authenticator.unset_token(trans) 83 84 # Redirect to the logout URL. 85 86 if self.use_logout_redirect: 87 trans.set_header_value("Location", self.logout_url) 88 trans.set_response_code(302) # was 307 89 90 # Show the logout confirmation anyway. 91 92 self._show_logout(trans, self.logout_url) 93 94 # Check the authentication details with the specified authenticator. 95 96 elif self.authenticator.authenticate(trans): 97 98 # If successful, pass on the transaction. 99 100 self.resource.respond(trans) 101 102 # Check for the anonymous parameter, if appropriate. 103 104 elif self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name): 105 106 # Make a special cookie token, then pass on the transaction. 107 108 self.authenticator.set_token(trans, self.anonymous_username) 109 self.resource.respond(trans) 110 111 else: 112 113 # Redirect to the login URL. 114 115 path = trans.get_path_without_query(self.urlencoding) 116 qs = trans.get_query_string() 117 if qs: 118 qs = "?" + qs 119 trans.redirect("%s?app=%s&path=%s&qs=%s" % ( 120 self.login_url, 121 trans.encode_path(self.app_url, self.urlencoding), 122 trans.encode_path(path, self.urlencoding), 123 trans.encode_path(qs, self.urlencoding)) 124 ) 125 126 def _show_logout(self, trans, redirect): 127 128 """ 129 Write a confirmation page to 'trans' containing the 'redirect' URL which the 130 client should be sent to upon logout. 131 """ 132 133 # When logout takes place, show the login screen. 134 135 trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) 136 out = trans.get_response_stream() 137 out.write(""" 138 <html> 139 <head> 140 <title>Logout</title> 141 </head> 142 <body> 143 <h1>Logout Successful</h1> 144 <p>Please proceed <a href="%s">to the application</a>.</p> 145 </body> 146 </html> 147 """ % redirect) 148 149 class LoginRedirectAuthenticator: 150 151 """ 152 An authenticator which verifies the credentials provided in a special login cookie. 153 """ 154 155 def __init__(self, secret_key, cookie_name=None): 156 157 "Initialise the authenticator with a 'secret_key' and an optional 'cookie_name'." 158 159 self.secret_key = secret_key 160 self.cookie_name = cookie_name or "LoginAuthenticator" 161 162 def authenticate(self, trans): 163 164 """ 165 Authenticate the originator of 'trans', updating the object if successful and 166 returning 1 (true) if successful, 0 (false) otherwise. 167 """ 168 169 cookie = trans.get_cookie(self.cookie_name) 170 if cookie is None or cookie.value is None: 171 return 0 172 173 # Test the token from the cookie against a recreated token using the 174 # given information. 175 176 username = cookie.value.split(":")[0] 177 if cookie.value == get_token(username, self.secret_key): 178 179 # Update the transaction with the user details. 180 181 trans.set_user(username) 182 return 1 183 else: 184 return 0 185 186 def set_token(self, trans, username): 187 188 "Set an authentication token in 'trans' with the given 'username'." 189 190 trans.set_cookie_value( 191 self.cookie_name, 192 get_token(username, self.secret_key), 193 path="/" 194 ) 195 196 # Update the transaction with the user details. 197 198 trans.set_user(username) 199 200 def unset_token(self, trans): 201 202 "Unset the authentication token in 'trans'." 203 204 trans.delete_cookie(self.cookie_name) 205 206 # vim: tabstop=4 expandtab shiftwidth=4