paulo@78: import urlparse paulo@78: paulo@78: paulo@82: def parse_cookies(environ): paulo@83: return urlparse.parse_qs(environ.get("HTTP_COOKIE", "").replace("; ", "&")) paulo@78: paulo@78: paulo@78: class PinFailError(Exception): paulo@78: pass paulo@78: paulo@78: paulo@82: class PinMan(object): paulo@82: def __init__(self, key): paulo@82: assert key paulo@82: self._key = key paulo@82: self._fn = "_%s" % key paulo@78: paulo@82: def load(self): paulo@82: ret = None paulo@82: paulo@82: try: paulo@82: with open(self._fn) as pin_f: paulo@82: ret = pin_f.read().strip() paulo@82: except IOError: paulo@82: pass paulo@82: paulo@82: return ret paulo@82: paulo@82: def check(self, cookies): paulo@82: if self._key not in cookies: paulo@82: raise PinFailError() paulo@82: paulo@82: pin = cookies[self._key][0] paulo@82: if pin != self.load(): paulo@82: raise PinFailError() paulo@82: paulo@82: return pin