# HG changeset patch # User paulb # Date 1085939071 0 # Node ID a86c21397695f348f120d17b8579db8e4ae8c663 # Parent 29f53ea6be11e3459856e7c23b45e747a96927f5 [project @ 2004-05-30 17:44:20 by paulb] Moved Login from the examples to WebStack.Resources. diff -r 29f53ea6be11 -r a86c21397695 examples/BaseHTTPRequestHandler/LoginApp.py --- a/examples/BaseHTTPRequestHandler/LoginApp.py Sun May 30 17:44:17 2004 +0000 +++ b/examples/BaseHTTPRequestHandler/LoginApp.py Sun May 30 17:44:31 2004 +0000 @@ -1,7 +1,7 @@ #!/usr/bin/env python from WebStack.Adapters import BaseHTTPRequestHandler -from Login import LoginResource, LoginAuthenticator +from WebStack.Resources.Login import LoginResource, LoginAuthenticator import BaseHTTPServer # Special magic incantation. diff -r 29f53ea6be11 -r a86c21397695 examples/CGI/LoginHandler.py --- a/examples/CGI/LoginHandler.py Sun May 30 17:44:17 2004 +0000 +++ b/examples/CGI/LoginHandler.py Sun May 30 17:44:31 2004 +0000 @@ -7,7 +7,7 @@ sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") from WebStack.Adapters import CGI -from Login import LoginResource, LoginAuthenticator +from WebStack.Resources.Login import LoginResource, LoginAuthenticator resource = LoginResource( LoginAuthenticator( diff -r 29f53ea6be11 -r a86c21397695 examples/Common/Login/__init__.py --- a/examples/Common/Login/__init__.py Sun May 30 17:44:17 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,162 +0,0 @@ -#!/usr/bin/env python - -"An example login screen." - -import WebStack.Generic -from WebStack.Helpers.Auth import get_token - -class LoginResource: - - "A resource providing a login screen." - - def __init__(self, authenticator, use_redirect=1): - - """ - Initialise the resource with an 'authenticator'. - - If the optional 'use_redirect' flag is set to 0, a confirmation screen is given - instead of redirecting the user back to the original application. - """ - - self.authenticator = authenticator - self.use_redirect = use_redirect - - def respond(self, trans): - - fields_path = trans.get_fields_from_path() - fields_body = trans.get_fields_from_body() - - # NOTE: Handle missing redirects better. - - if fields_body.has_key("redirect"): - redirects = fields_body["redirect"] - redirect = redirects[0] - elif fields_path.has_key("redirect"): - redirects = fields_path["redirect"] - redirect = redirects[0] - else: - redirect = "" - - # Check for a submitted login form. - - if fields_body.has_key("login"): - if self.authenticator.authenticate(trans): - self._redirect(trans, redirect) - return - - # Otherwise, show the login form. - - self._show_login(trans, redirect) - - def _redirect(self, trans, redirect): - - "Redirect the client using 'trans' and the given 'redirect' URL." - - if self.use_redirect: - trans.set_header_value("Location", redirect) - trans.set_response_code(307) - - # Show the success page anyway. - - self._show_success(trans, redirect) - - def _show_login(self, trans, redirect): - - # When authentication fails or is yet to take place, show the login - # screen. - - trans.set_content_type(WebStack.Generic.ContentType("text/html")) - out = trans.get_response_stream() - out.write(""" - - - Login Example - - -

Login

-
-

Username:

-

Password:

-

- -
- - -""" % redirect) - - def _show_success(self, trans, redirect): - - # When authentication fails or is yet to take place, show the login - # screen. - - trans.set_content_type(WebStack.Generic.ContentType("text/html")) - out = trans.get_response_stream() - out.write(""" - - - Login Example - - -

Login Successful

-

Please proceed to the application.

- - -""" % redirect) - - def _decode(self, url): - - "Decode the given 'url' for redirection purposes." - - return url.replace("%3f", "?").replace("%26", "&") - -class LoginAuthenticator: - - def __init__(self, secret_key, credentials, cookie_name=None): - - """ - Initialise the authenticator with a 'secret_key', the authenticator's registry of - 'credentials' and an optional 'cookie_name'. - - The 'credentials' must be an object which supports tests of the form - '(username, password) in credentials'. - """ - - self.secret_key = secret_key - self.credentials = credentials - self.cookie_name = cookie_name or "LoginAuthenticator" - - def authenticate(self, trans): - - # Process any supplied parameters. - - fields = trans.get_fields_from_body() - - if fields.has_key("username") and fields.has_key("password"): - usernames, passwords = fields["username"], fields["password"] - - # Insist on only one username and password. - - if len(usernames) == 1 and len(passwords) == 1: - username, password = usernames[0], passwords[0] - - # Check against the class's credentials. - - if (username, password) in self.credentials: - - # Make a special cookie token. - - self.set_token(trans, username) - return 1 - - return 0 - - def set_token(self, trans, username): - - "Set an authentication token in 'trans' with the given 'username'." - - trans.set_cookie_value( - self.cookie_name, - get_token(username, self.secret_key) - ) - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r 29f53ea6be11 -r a86c21397695 examples/ModPython/LoginApp/LoginHandler.py --- a/examples/ModPython/LoginApp/LoginHandler.py Sun May 30 17:44:17 2004 +0000 +++ b/examples/ModPython/LoginApp/LoginHandler.py Sun May 30 17:44:31 2004 +0000 @@ -7,7 +7,7 @@ sys.path.append("/home/paulb/Software/Python/WebStack/examples/Common") from WebStack.Adapters import ModPython -from Login import LoginResource, LoginAuthenticator +from WebStack.Resources.Login import LoginResource, LoginAuthenticator # NOTE: Not sure if the resource should be maintained in a resource pool. diff -r 29f53ea6be11 -r a86c21397695 examples/Twisted/LoginApp.py --- a/examples/Twisted/LoginApp.py Sun May 30 17:44:17 2004 +0000 +++ b/examples/Twisted/LoginApp.py Sun May 30 17:44:31 2004 +0000 @@ -1,7 +1,7 @@ #!/usr/bin/env python from WebStack.Adapters import Twisted -from Login import LoginResource, LoginAuthenticator +from WebStack.Resources.Login import LoginResource, LoginAuthenticator import twisted.web.server import twisted.internet.reactor diff -r 29f53ea6be11 -r a86c21397695 examples/Webware/LoginApp/__init__.py --- a/examples/Webware/LoginApp/__init__.py Sun May 30 17:44:17 2004 +0000 +++ b/examples/Webware/LoginApp/__init__.py Sun May 30 17:44:31 2004 +0000 @@ -7,7 +7,7 @@ __version__ = "0.1" from WebStack.Adapters.Webware import WebStackServletFactory -from Login import LoginResource, LoginAuthenticator +from WebStack.Resources.Login import LoginResource, LoginAuthenticator # NOTE: Initialising a shared resource. diff -r 29f53ea6be11 -r a86c21397695 examples/Webware/LoginContext/__init__.py --- a/examples/Webware/LoginContext/__init__.py Sun May 30 17:44:17 2004 +0000 +++ b/examples/Webware/LoginContext/__init__.py Sun May 30 17:44:31 2004 +0000 @@ -5,7 +5,7 @@ """ from WebStack.Adapters.Webware import WebStackURLParser -from Login import LoginResource, LoginAuthenticator +from WebStack.Resources.Login import LoginResource, LoginAuthenticator # NOTE: Initialising a shared resource.