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