1.1 --- a/examples/Common/LoginRedirect/__init__.py Sat May 29 17:27:29 2004 +0000
1.2 +++ b/examples/Common/LoginRedirect/__init__.py Sat May 29 17:28:14 2004 +0000
1.3 @@ -8,24 +8,36 @@
1.4
1.5 "A resource redirecting to a login URL."
1.6
1.7 - def __init__(self, login_url, app_url, resource, authenticator):
1.8 + def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None):
1.9
1.10 """
1.11 - Initialise the resource with a 'login_url', an 'app_url' where the
1.12 - 'resource' for the application being protected should be reachable, and
1.13 - an 'authenticator'.
1.14 + Initialise the resource with a 'login_url', an 'app_url' where the 'resource' for
1.15 + the application being protected should be reachable, and an 'authenticator'.
1.16 +
1.17 + If the optional 'anonymous_parameter_name' is set, clients providing a parameter
1.18 + of that name in the URL will not be authenticated, but then such clients will not
1.19 + get a user identity associated with them.
1.20 """
1.21
1.22 self.login_url = login_url
1.23 self.app_url = app_url
1.24 self.resource = resource
1.25 self.authenticator = authenticator
1.26 + self.anonymous_parameter_name = anonymous_parameter_name
1.27
1.28 def respond(self, trans):
1.29
1.30 + # Check for the anonymous parameter, if appropriate.
1.31 +
1.32 + fields = trans.get_fields_from_path()
1.33 + if self.anonymous_parameter_name is not None and fields.has_key(self.anonymous_parameter_name):
1.34 + is_anonymous = 1
1.35 + else:
1.36 + is_anonymous = 0
1.37 +
1.38 # Check the authentication details with the specified authenticator.
1.39
1.40 - if self.authenticator.authenticate(trans):
1.41 + if is_anonymous or self.authenticator.authenticate(trans):
1.42 self.resource.respond(trans)
1.43 else:
1.44 # Redirect to the login URL.
1.45 @@ -39,14 +51,18 @@
1.46 An authenticator which verifies the credentials provided in a special login cookie.
1.47 """
1.48
1.49 - def __init__(self, secret_key):
1.50 + def __init__(self, secret_key, cookie_name=None):
1.51
1.52 - "Initialise the authenticator with a 'secret_key'."
1.53 + "Initialise the authenticator with a 'secret_key' and an optional 'cookie_name'."
1.54
1.55 self.secret_key = secret_key
1.56 + self.cookie_name = cookie_name or "LoginAuthenticator"
1.57
1.58 def authenticate(self, trans):
1.59 - cookie = trans.get_cookie("LoginAuthenticator")
1.60 +
1.61 + "Authenticate the originator of 'trans', updating the object if successful."
1.62 +
1.63 + cookie = trans.get_cookie(self.cookie_name)
1.64 if cookie is None:
1.65 return 0
1.66
1.67 @@ -56,6 +72,13 @@
1.68
1.69 username, code = cookie.value.split(":")
1.70 print "*", username, code
1.71 - return code == md5.md5(username + self.secret_key).hexdigest()
1.72 + if code == md5.md5(username + self.secret_key).hexdigest():
1.73 +
1.74 + # Update the transaction with the user details.
1.75 +
1.76 + trans.set_user(username)
1.77 + return 1
1.78 + else:
1.79 + return 0
1.80
1.81 # vim: tabstop=4 expandtab shiftwidth=4