Commits

Martin von Löwis committed 0fc8410

Streamline session handling.

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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.