1.1 --- a/examples/Common/LoginRedirect/__init__.py Sun May 30 15:34:05 2004 +0000
1.2 +++ b/examples/Common/LoginRedirect/__init__.py Sun May 30 15:58:18 2004 +0000
1.3 @@ -8,15 +8,17 @@
1.4
1.5 "A resource redirecting to a login URL."
1.6
1.7 - def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None):
1.8 + def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None,
1.9 + anonymous_username="anonymous"):
1.10
1.11 """
1.12 Initialise the resource with a 'login_url', an 'app_url' where the 'resource' for
1.13 the application being protected should be reachable, and an 'authenticator'.
1.14
1.15 If the optional 'anonymous_parameter_name' is set, clients providing a parameter
1.16 - of that name in the URL will have that parameter sent specially to the login
1.17 - application.
1.18 + of that name in the URL will not be authenticated, but then such clients will get
1.19 + a predefined user identity associated with them, configurable using the optional
1.20 + 'anonymous_username'.
1.21 """
1.22
1.23 self.login_url = login_url
1.24 @@ -24,27 +26,35 @@
1.25 self.resource = resource
1.26 self.authenticator = authenticator
1.27 self.anonymous_parameter_name = anonymous_parameter_name
1.28 + self.anonymous_username = anonymous_username
1.29
1.30 def respond(self, trans):
1.31
1.32 + fields_path = trans.get_fields_from_path()
1.33 +
1.34 # Check the authentication details with the specified authenticator.
1.35
1.36 if self.authenticator.authenticate(trans):
1.37 +
1.38 + # If successful, pass on the transaction.
1.39 +
1.40 self.resource.respond(trans)
1.41 - else:
1.42 - # Define anonymous mode, if appropriate.
1.43 +
1.44 + # Check for the anonymous parameter, if appropriate.
1.45
1.46 - fields_path = trans.get_fields_from_path()
1.47 + elif self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name):
1.48 +
1.49 + # Make a special cookie token, then pass on the transaction.
1.50
1.51 - if self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name):
1.52 - anonymous_parameter = "%s=%s&" % (self.anonymous_parameter_name, fields_path[self.anonymous_parameter_name][0])
1.53 - else:
1.54 - anonymous_parameter = ""
1.55 + self.authenticator.set_token(trans, self.anonymous_username)
1.56 + self.resource.respond(trans)
1.57 +
1.58 + else:
1.59
1.60 # Redirect to the login URL.
1.61
1.62 - trans.set_header_value("Location", "%s?%sredirect=%s%s" % (
1.63 - self.login_url, anonymous_parameter, self.app_url, self._encode(trans.get_path()))
1.64 + trans.set_header_value("Location", "%s?redirect=%s%s" % (
1.65 + self.login_url, self.app_url, self._encode(trans.get_path()))
1.66 )
1.67 trans.set_response_code(307)
1.68
1.69 @@ -88,4 +98,17 @@
1.70 else:
1.71 return 0
1.72
1.73 + def set_token(self, trans, username):
1.74 +
1.75 + "Set an authentication in the 'trans' with the given 'username'."
1.76 +
1.77 + trans.set_cookie_value(
1.78 + self.cookie_name,
1.79 + get_token(username, self.secret_key)
1.80 + )
1.81 +
1.82 + # Update the transaction with the user details.
1.83 +
1.84 + trans.set_user(username)
1.85 +
1.86 # vim: tabstop=4 expandtab shiftwidth=4