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@127 | 6 | |
paulb@127 | 7 | class LoginRedirectResource: |
paulb@127 | 8 | |
paulb@127 | 9 | "A resource redirecting to a login URL." |
paulb@127 | 10 | |
paulb@149 | 11 | def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None, |
paulb@149 | 12 | anonymous_username="anonymous"): |
paulb@127 | 13 | |
paulb@127 | 14 | """ |
paulb@131 | 15 | Initialise the resource with a 'login_url', an 'app_url' where the 'resource' for |
paulb@131 | 16 | the application being protected should be reachable, and an 'authenticator'. |
paulb@131 | 17 | |
paulb@131 | 18 | If the optional 'anonymous_parameter_name' is set, clients providing a parameter |
paulb@149 | 19 | of that name in the URL will not be authenticated, but then such clients will get |
paulb@149 | 20 | a predefined user identity associated with them, configurable using the optional |
paulb@149 | 21 | 'anonymous_username'. |
paulb@127 | 22 | """ |
paulb@127 | 23 | |
paulb@127 | 24 | self.login_url = login_url |
paulb@127 | 25 | self.app_url = app_url |
paulb@127 | 26 | self.resource = resource |
paulb@127 | 27 | self.authenticator = authenticator |
paulb@131 | 28 | self.anonymous_parameter_name = anonymous_parameter_name |
paulb@149 | 29 | self.anonymous_username = anonymous_username |
paulb@127 | 30 | |
paulb@127 | 31 | def respond(self, trans): |
paulb@127 | 32 | |
paulb@149 | 33 | fields_path = trans.get_fields_from_path() |
paulb@149 | 34 | |
paulb@127 | 35 | # Check the authentication details with the specified authenticator. |
paulb@127 | 36 | |
paulb@146 | 37 | if self.authenticator.authenticate(trans): |
paulb@149 | 38 | |
paulb@149 | 39 | # If successful, pass on the transaction. |
paulb@149 | 40 | |
paulb@127 | 41 | self.resource.respond(trans) |
paulb@149 | 42 | |
paulb@149 | 43 | # Check for the anonymous parameter, if appropriate. |
paulb@146 | 44 | |
paulb@149 | 45 | elif self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name): |
paulb@149 | 46 | |
paulb@149 | 47 | # Make a special cookie token, then pass on the transaction. |
paulb@146 | 48 | |
paulb@149 | 49 | self.authenticator.set_token(trans, self.anonymous_username) |
paulb@149 | 50 | self.resource.respond(trans) |
paulb@149 | 51 | |
paulb@149 | 52 | else: |
paulb@146 | 53 | |
paulb@127 | 54 | # Redirect to the login URL. |
paulb@127 | 55 | |
paulb@149 | 56 | trans.set_header_value("Location", "%s?redirect=%s%s" % ( |
paulb@149 | 57 | self.login_url, self.app_url, self._encode(trans.get_path())) |
paulb@146 | 58 | ) |
paulb@127 | 59 | trans.set_response_code(307) |
paulb@127 | 60 | |
paulb@136 | 61 | def _encode(self, url): |
paulb@136 | 62 | |
paulb@136 | 63 | "Encode the given 'url' for redirection purposes." |
paulb@136 | 64 | |
paulb@136 | 65 | return url.replace("?", "%3f").replace("&", "%26") |
paulb@136 | 66 | |
paulb@127 | 67 | class LoginRedirectAuthenticator: |
paulb@127 | 68 | |
paulb@127 | 69 | """ |
paulb@127 | 70 | An authenticator which verifies the credentials provided in a special login cookie. |
paulb@127 | 71 | """ |
paulb@127 | 72 | |
paulb@131 | 73 | def __init__(self, secret_key, cookie_name=None): |
paulb@127 | 74 | |
paulb@131 | 75 | "Initialise the authenticator with a 'secret_key' and an optional 'cookie_name'." |
paulb@127 | 76 | |
paulb@127 | 77 | self.secret_key = secret_key |
paulb@131 | 78 | self.cookie_name = cookie_name or "LoginAuthenticator" |
paulb@127 | 79 | |
paulb@127 | 80 | def authenticate(self, trans): |
paulb@131 | 81 | |
paulb@131 | 82 | "Authenticate the originator of 'trans', updating the object if successful." |
paulb@131 | 83 | |
paulb@131 | 84 | cookie = trans.get_cookie(self.cookie_name) |
paulb@136 | 85 | if cookie is None or cookie.value is None: |
paulb@127 | 86 | return 0 |
paulb@127 | 87 | |
paulb@127 | 88 | # Test the token from the cookie against a recreated token using the |
paulb@127 | 89 | # given information. |
paulb@127 | 90 | |
paulb@127 | 91 | username, code = cookie.value.split(":") |
paulb@143 | 92 | if cookie.value == get_token(username, self.secret_key): |
paulb@131 | 93 | |
paulb@131 | 94 | # Update the transaction with the user details. |
paulb@131 | 95 | |
paulb@131 | 96 | trans.set_user(username) |
paulb@131 | 97 | return 1 |
paulb@131 | 98 | else: |
paulb@131 | 99 | return 0 |
paulb@127 | 100 | |
paulb@149 | 101 | def set_token(self, trans, username): |
paulb@149 | 102 | |
paulb@149 | 103 | "Set an authentication in the 'trans' with the given 'username'." |
paulb@149 | 104 | |
paulb@149 | 105 | trans.set_cookie_value( |
paulb@149 | 106 | self.cookie_name, |
paulb@149 | 107 | get_token(username, self.secret_key) |
paulb@149 | 108 | ) |
paulb@149 | 109 | |
paulb@149 | 110 | # Update the transaction with the user details. |
paulb@149 | 111 | |
paulb@149 | 112 | trans.set_user(username) |
paulb@149 | 113 | |
paulb@127 | 114 | # vim: tabstop=4 expandtab shiftwidth=4 |