annotate pinlib/pinlib.py @ 137:e508b772c817

myrss2: FEEDS: add waxy.org
author paulo
date Mon, 09 Jan 2023 12:10:14 +0000
parents d7d67887102f
children
rev   line source
paulo@78 1 import urlparse
paulo@78 2
paulo@78 3
paulo@82 4 def parse_cookies(environ):
paulo@83 5 return urlparse.parse_qs(environ.get("HTTP_COOKIE", "").replace("; ", "&"))
paulo@78 6
paulo@78 7
paulo@78 8 class PinFailError(Exception):
paulo@78 9 pass
paulo@78 10
paulo@78 11
paulo@82 12 class PinMan(object):
paulo@82 13 def __init__(self, key):
paulo@82 14 assert key
paulo@82 15 self._key = key
paulo@82 16 self._fn = "_%s" % key
paulo@78 17
paulo@82 18 def load(self):
paulo@82 19 ret = None
paulo@82 20
paulo@82 21 try:
paulo@82 22 with open(self._fn) as pin_f:
paulo@82 23 ret = pin_f.read().strip()
paulo@82 24 except IOError:
paulo@82 25 pass
paulo@82 26
paulo@82 27 return ret
paulo@82 28
paulo@82 29 def check(self, cookies):
paulo@82 30 if self._key not in cookies:
paulo@82 31 raise PinFailError()
paulo@82 32
paulo@82 33 pin = cookies[self._key][0]
paulo@82 34 if pin != self.load():
paulo@82 35 raise PinFailError()
paulo@82 36
paulo@82 37 return pin