Commits

Martin von Löwis committed 0fc8410

Streamline session handling.

  • Participants
  • Parent commits 57e9d10

Comments (0)

Files changed (1)

openid2rp/testapp.py

 #!/usr/bin/env python
 ################ Test Server #################################
-import BaseHTTPServer, cgi, Cookie, socket
+import BaseHTTPServer, cgi, Cookie, socket, collections
 from openid2rp import *
 from time import time
 
 # is authorized to make assertions about, but that the user doesn't control
 disco = {}
 
-# Mapping from OP Endpoint URL to association responses
-sessions = {}
+# Mapping from OP Endpoint URL to association responses;
+# most recent association is last
+sessions = collections.defaultdict(list)
+
+class _Expired(Exception):
+    'Local exception class to indicate expired sessions.'
+    pass
 
 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
 
                 now = time()
                 try:
                     session, expire = sessions[url][-1]
-                except KeyError:
+                    if now > expire:
+                        raise _Expired
+                except (KeyError, _Expired):
                     try:
                         session = associate(services, url)
                     except ValueError, e:
                         return self.error(str(e))
-                    try:
-                        sessions[url].append((session, now+int(session['expires_in'])))
-                    except KeyError:
-                        sessions[url] = [(session, now+int(session['expires_in']))]
-                else:
-                    if now > expire:
-                        try:
-                            session = associate(services, url)
-                        except ValueError, e:
-                            return self.error(str(e))
-                        sessions[url].append((session, now+int(session['expires_in'])))
+                    sessions[url].append((session, now+int(session['expires_in'])))
 
                 self.send_response(307) # temporary redirect - do not cache
                 self.send_header("Location", request_authentication
                 now = time()
                 try:
                     session, expire = sessions[url][-1]
-                except KeyError:
+                    if now > expire:
+                        raise _Expired
+                except (KeyError, _Expired):
                     try:
                         session = associate(services, url)
                     except ValueError, e:
                         return self.error(str(e))
-                    try:
-                        sessions[url].append((session, now+int(session['expires_in'])))
-                    except KeyError:
-                        sessions[url] = [(session, now+int(session['expires_in']))]
-                else:
-                    if now > expire:
-                        try:
-                            session = associate(services, url)
-                        except ValueError, e:
-                            return self.error(str(e))
-                        sessions[url].append((session, now+int(session['expires_in'])))
+                    sessions[url].append((session, now+int(session['expires_in'])))
 
                 self.send_response(307)
                 self.send_header("Location", request_authentication