paulb@155 | 1 | #!/usr/bin/env python |
paulb@155 | 2 | |
paulb@403 | 3 | """ |
paulb@403 | 4 | Login resources which redirect clients back to an application after a successful |
paulb@403 | 5 | login. |
paulb@403 | 6 | |
paulb@610 | 7 | Copyright (C) 2004, 2005, 2006, 2007 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@155 | 23 | |
paulb@155 | 24 | import WebStack.Generic |
paulb@733 | 25 | from WebStack.Helpers.Auth import LoginAuthenticator |
paulb@155 | 26 | |
paulb@155 | 27 | class LoginResource: |
paulb@155 | 28 | |
paulb@155 | 29 | "A resource providing a login screen." |
paulb@155 | 30 | |
paulb@451 | 31 | encoding = "utf-8" |
paulb@451 | 32 | |
paulb@624 | 33 | def __init__(self, authenticator, use_redirect=1, urlencoding=None, encoding=None): |
paulb@155 | 34 | |
paulb@155 | 35 | """ |
paulb@155 | 36 | Initialise the resource with an 'authenticator'. |
paulb@155 | 37 | |
paulb@451 | 38 | If the optional 'use_redirect' flag is set to 0, a confirmation screen |
paulb@451 | 39 | is given instead of redirecting the user back to the original |
paulb@451 | 40 | application. |
paulb@451 | 41 | |
paulb@451 | 42 | The optional 'urlencoding' parameter allows a special encoding to be |
paulb@451 | 43 | used in producing the redirection path. |
paulb@612 | 44 | |
paulb@624 | 45 | The optional 'encoding' parameter allows a special encoding to be used |
paulb@624 | 46 | in producing the login pages. |
paulb@624 | 47 | |
paulb@612 | 48 | To change the pages employed by this resource, either redefine the |
paulb@612 | 49 | 'login_page' and 'success_page' attributes in instances of this class or |
paulb@612 | 50 | a subclass, or override the 'show_login' and 'show_success' methods. |
paulb@155 | 51 | """ |
paulb@155 | 52 | |
paulb@155 | 53 | self.authenticator = authenticator |
paulb@155 | 54 | self.use_redirect = use_redirect |
paulb@624 | 55 | self.urlencoding = urlencoding |
paulb@624 | 56 | self.encoding = encoding or self.encoding |
paulb@155 | 57 | |
paulb@155 | 58 | def respond(self, trans): |
paulb@155 | 59 | |
paulb@155 | 60 | "Respond using the transaction 'trans'." |
paulb@155 | 61 | |
paulb@155 | 62 | # Check for a submitted login form. |
paulb@155 | 63 | |
paulb@610 | 64 | fields_body = trans.get_fields_from_body(self.encoding) |
paulb@610 | 65 | |
paulb@155 | 66 | if fields_body.has_key("login"): |
paulb@610 | 67 | if self.authenticator.authenticate(trans, fields_body.get("username", [None])[0], fields_body.get("password", [None])[0]): |
paulb@733 | 68 | app = fields_body.get("app", [""])[0] |
paulb@733 | 69 | |
paulb@733 | 70 | self._redirect(trans, app) |
paulb@506 | 71 | # The above method does not return. |
paulb@155 | 72 | |
paulb@155 | 73 | # Otherwise, show the login form. |
paulb@155 | 74 | |
paulb@733 | 75 | fields_path = trans.get_fields_from_path(self.urlencoding) |
paulb@733 | 76 | app = fields_path.get("app", [""])[0] |
paulb@451 | 77 | |
paulb@733 | 78 | self.show_login(trans, app) |
paulb@733 | 79 | |
paulb@733 | 80 | def _redirect(self, trans, app): |
paulb@155 | 81 | |
paulb@451 | 82 | """ |
paulb@733 | 83 | Redirect the client using 'trans' and the given 'app' details. |
paulb@451 | 84 | """ |
paulb@155 | 85 | |
paulb@155 | 86 | # Show the success page anyway. |
paulb@155 | 87 | |
paulb@733 | 88 | self.show_success(trans, app) |
paulb@506 | 89 | if self.use_redirect: |
paulb@733 | 90 | trans.redirect(app) |
paulb@506 | 91 | else: |
paulb@506 | 92 | raise WebStack.Generic.EndOfResponse |
paulb@155 | 93 | |
paulb@733 | 94 | def show_login(self, trans, app): |
paulb@155 | 95 | |
paulb@155 | 96 | """ |
paulb@612 | 97 | Writes a login screen using the transaction 'trans', including details |
paulb@733 | 98 | of the 'app' which the client was attempting to access. |
paulb@155 | 99 | """ |
paulb@155 | 100 | |
paulb@451 | 101 | trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) |
paulb@155 | 102 | out = trans.get_response_stream() |
paulb@733 | 103 | out.write(self.login_page % app) |
paulb@612 | 104 | |
paulb@733 | 105 | def show_success(self, trans, app): |
paulb@612 | 106 | |
paulb@612 | 107 | """ |
paulb@612 | 108 | Writes a success screen using the transaction 'trans', including details |
paulb@733 | 109 | of the 'app' which the client was attempting to access. |
paulb@612 | 110 | """ |
paulb@612 | 111 | |
paulb@612 | 112 | trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) |
paulb@612 | 113 | out = trans.get_response_stream() |
paulb@733 | 114 | out.write(self.success_page % (app, app)) |
paulb@612 | 115 | |
paulb@612 | 116 | login_page = """ |
paulb@155 | 117 | <html> |
paulb@155 | 118 | <head> |
paulb@612 | 119 | <title>Login</title> |
paulb@155 | 120 | </head> |
paulb@155 | 121 | <body> |
paulb@155 | 122 | <h1>Login</h1> |
paulb@155 | 123 | <form method="POST"> |
paulb@155 | 124 | <p>Username: <input name="username" type="text" size="12"/></p> |
paulb@610 | 125 | <p>Password: <input name="password" type="password" size="12"/></p> |
paulb@155 | 126 | <p><input name="login" type="submit" value="Login"/></p> |
paulb@451 | 127 | <input name="app" type="hidden" value="%s"/> |
paulb@155 | 128 | </form> |
paulb@155 | 129 | </body> |
paulb@155 | 130 | </html> |
paulb@612 | 131 | """ |
paulb@155 | 132 | |
paulb@612 | 133 | success_page = """ |
paulb@155 | 134 | <html> |
paulb@155 | 135 | <head> |
paulb@155 | 136 | <title>Login Example</title> |
paulb@155 | 137 | </head> |
paulb@155 | 138 | <body> |
paulb@155 | 139 | <h1>Login Successful</h1> |
paulb@733 | 140 | <p>Please proceed to the application: <a href="%s">%s</a></p> |
paulb@155 | 141 | </body> |
paulb@155 | 142 | </html> |
paulb@612 | 143 | """ |
paulb@155 | 144 | |
paulb@155 | 145 | # vim: tabstop=4 expandtab shiftwidth=4 |