paulb@127 | 1 | #!/usr/bin/env python |
paulb@127 | 2 | |
paulb@127 | 3 | "Login redirection." |
paulb@127 | 4 | |
paulb@143 | 5 | from WebStack.Helpers.Auth import get_token |
paulb@152 | 6 | import WebStack.Generic |
paulb@127 | 7 | |
paulb@127 | 8 | class LoginRedirectResource: |
paulb@127 | 9 | |
paulb@127 | 10 | "A resource redirecting to a login URL." |
paulb@127 | 11 | |
paulb@149 | 12 | def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None, |
paulb@152 | 13 | anonymous_username="anonymous", logout_parameter_name=None, logout_url="/", |
paulb@152 | 14 | use_logout_redirect=1): |
paulb@127 | 15 | |
paulb@127 | 16 | """ |
paulb@131 | 17 | Initialise the resource with a 'login_url', an 'app_url' where the 'resource' for |
paulb@131 | 18 | the application being protected should be reachable, and an 'authenticator'. |
paulb@131 | 19 | |
paulb@131 | 20 | If the optional 'anonymous_parameter_name' is set, clients providing a parameter |
paulb@149 | 21 | of that name in the URL will not be authenticated, but then such clients will get |
paulb@149 | 22 | a predefined user identity associated with them, configurable using the optional |
paulb@149 | 23 | 'anonymous_username'. |
paulb@152 | 24 | |
paulb@152 | 25 | If the optional 'logout_parameter_name' is set, clients providing a parameter of |
paulb@152 | 26 | that name in the URL will become logged out. After logging out, clients are |
paulb@152 | 27 | redirected to a location which can be configured by the optional 'logout_url'. |
paulb@152 | 28 | |
paulb@152 | 29 | If the optional 'use_logout_redirect' flag is set to 0, a confirmation screen is |
paulb@152 | 30 | given instead of redirecting the user to the 'logout_url'. |
paulb@127 | 31 | """ |
paulb@127 | 32 | |
paulb@127 | 33 | self.login_url = login_url |
paulb@127 | 34 | self.app_url = app_url |
paulb@127 | 35 | self.resource = resource |
paulb@127 | 36 | self.authenticator = authenticator |
paulb@131 | 37 | self.anonymous_parameter_name = anonymous_parameter_name |
paulb@149 | 38 | self.anonymous_username = anonymous_username |
paulb@152 | 39 | self.logout_parameter_name = logout_parameter_name |
paulb@152 | 40 | self.logout_url = logout_url |
paulb@152 | 41 | self.use_logout_redirect = use_logout_redirect |
paulb@127 | 42 | |
paulb@127 | 43 | def respond(self, trans): |
paulb@127 | 44 | |
paulb@149 | 45 | fields_path = trans.get_fields_from_path() |
paulb@149 | 46 | |
paulb@152 | 47 | # Check for the logout parameter, if appropriate. |
paulb@152 | 48 | |
paulb@152 | 49 | if self.logout_parameter_name is not None and fields_path.has_key(self.logout_parameter_name): |
paulb@152 | 50 | |
paulb@152 | 51 | # Remove the special cookie token, then pass on the transaction. |
paulb@152 | 52 | |
paulb@152 | 53 | self.authenticator.unset_token(trans) |
paulb@152 | 54 | |
paulb@152 | 55 | # Redirect to the logout URL. |
paulb@152 | 56 | |
paulb@152 | 57 | if self.use_logout_redirect: |
paulb@152 | 58 | trans.set_header_value("Location", self.logout_url) |
paulb@152 | 59 | trans.set_response_code(307) |
paulb@152 | 60 | |
paulb@152 | 61 | # Show the logout confirmation anyway. |
paulb@152 | 62 | |
paulb@152 | 63 | self._show_logout(trans, self.logout_url) |
paulb@152 | 64 | |
paulb@127 | 65 | # Check the authentication details with the specified authenticator. |
paulb@127 | 66 | |
paulb@152 | 67 | elif self.authenticator.authenticate(trans): |
paulb@149 | 68 | |
paulb@149 | 69 | # If successful, pass on the transaction. |
paulb@149 | 70 | |
paulb@127 | 71 | self.resource.respond(trans) |
paulb@149 | 72 | |
paulb@149 | 73 | # Check for the anonymous parameter, if appropriate. |
paulb@146 | 74 | |
paulb@149 | 75 | elif self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name): |
paulb@149 | 76 | |
paulb@149 | 77 | # Make a special cookie token, then pass on the transaction. |
paulb@146 | 78 | |
paulb@149 | 79 | self.authenticator.set_token(trans, self.anonymous_username) |
paulb@149 | 80 | self.resource.respond(trans) |
paulb@149 | 81 | |
paulb@149 | 82 | else: |
paulb@146 | 83 | |
paulb@127 | 84 | # Redirect to the login URL. |
paulb@127 | 85 | |
paulb@149 | 86 | trans.set_header_value("Location", "%s?redirect=%s%s" % ( |
paulb@149 | 87 | self.login_url, self.app_url, self._encode(trans.get_path())) |
paulb@146 | 88 | ) |
paulb@127 | 89 | trans.set_response_code(307) |
paulb@127 | 90 | |
paulb@136 | 91 | def _encode(self, url): |
paulb@136 | 92 | |
paulb@136 | 93 | "Encode the given 'url' for redirection purposes." |
paulb@136 | 94 | |
paulb@136 | 95 | return url.replace("?", "%3f").replace("&", "%26") |
paulb@136 | 96 | |
paulb@152 | 97 | def _show_logout(self, trans, redirect): |
paulb@152 | 98 | |
paulb@152 | 99 | # When logout takes place, show the login screen. |
paulb@152 | 100 | |
paulb@152 | 101 | trans.set_content_type(WebStack.Generic.ContentType("text/html")) |
paulb@152 | 102 | out = trans.get_response_stream() |
paulb@152 | 103 | out.write(""" |
paulb@152 | 104 | <html> |
paulb@152 | 105 | <head> |
paulb@152 | 106 | <title>Logout</title> |
paulb@152 | 107 | </head> |
paulb@152 | 108 | <body> |
paulb@152 | 109 | <h1>Logout Successful</h1> |
paulb@152 | 110 | <p>Please proceed <a href="%s">to the application</a>.</p> |
paulb@152 | 111 | </body> |
paulb@152 | 112 | </html> |
paulb@152 | 113 | """ % redirect) |
paulb@152 | 114 | |
paulb@127 | 115 | class LoginRedirectAuthenticator: |
paulb@127 | 116 | |
paulb@127 | 117 | """ |
paulb@127 | 118 | An authenticator which verifies the credentials provided in a special login cookie. |
paulb@127 | 119 | """ |
paulb@127 | 120 | |
paulb@131 | 121 | def __init__(self, secret_key, cookie_name=None): |
paulb@127 | 122 | |
paulb@131 | 123 | "Initialise the authenticator with a 'secret_key' and an optional 'cookie_name'." |
paulb@127 | 124 | |
paulb@127 | 125 | self.secret_key = secret_key |
paulb@131 | 126 | self.cookie_name = cookie_name or "LoginAuthenticator" |
paulb@127 | 127 | |
paulb@127 | 128 | def authenticate(self, trans): |
paulb@131 | 129 | |
paulb@131 | 130 | "Authenticate the originator of 'trans', updating the object if successful." |
paulb@131 | 131 | |
paulb@131 | 132 | cookie = trans.get_cookie(self.cookie_name) |
paulb@136 | 133 | if cookie is None or cookie.value is None: |
paulb@127 | 134 | return 0 |
paulb@127 | 135 | |
paulb@127 | 136 | # Test the token from the cookie against a recreated token using the |
paulb@127 | 137 | # given information. |
paulb@127 | 138 | |
paulb@127 | 139 | username, code = cookie.value.split(":") |
paulb@143 | 140 | if cookie.value == get_token(username, self.secret_key): |
paulb@131 | 141 | |
paulb@131 | 142 | # Update the transaction with the user details. |
paulb@131 | 143 | |
paulb@131 | 144 | trans.set_user(username) |
paulb@131 | 145 | return 1 |
paulb@131 | 146 | else: |
paulb@131 | 147 | return 0 |
paulb@127 | 148 | |
paulb@149 | 149 | def set_token(self, trans, username): |
paulb@149 | 150 | |
paulb@152 | 151 | "Set an authentication token in 'trans' with the given 'username'." |
paulb@149 | 152 | |
paulb@149 | 153 | trans.set_cookie_value( |
paulb@149 | 154 | self.cookie_name, |
paulb@149 | 155 | get_token(username, self.secret_key) |
paulb@149 | 156 | ) |
paulb@149 | 157 | |
paulb@149 | 158 | # Update the transaction with the user details. |
paulb@149 | 159 | |
paulb@149 | 160 | trans.set_user(username) |
paulb@149 | 161 | |
paulb@152 | 162 | def unset_token(self, trans): |
paulb@152 | 163 | |
paulb@152 | 164 | "Unset the authentication token in 'trans'." |
paulb@152 | 165 | |
paulb@152 | 166 | trans.delete_cookie(self.cookie_name) |
paulb@152 | 167 | |
paulb@127 | 168 | # vim: tabstop=4 expandtab shiftwidth=4 |