annotate laterlinks2/pinlib.py @ 78:f833a888c548

add cookie-based PIN system, and update laterlinks to use it
author paulo
date Thu, 02 Jun 2016 00:27:50 -0700
parents
children
rev   line source
paulo@78 1 import urlparse
paulo@78 2
paulo@78 3
paulo@78 4 PIN_KEY = "llpin"
paulo@78 5 PIN_FN = "_%s" % PIN_KEY
paulo@78 6
paulo@78 7
paulo@78 8 class PinFailError(Exception):
paulo@78 9 pass
paulo@78 10
paulo@78 11
paulo@78 12 def load():
paulo@78 13 ret = None
paulo@78 14
paulo@78 15 try:
paulo@78 16 with open(PIN_FN) as pin_f:
paulo@78 17 ret = pin_f.read().strip()
paulo@78 18 except IOError:
paulo@78 19 pass
paulo@78 20
paulo@78 21 return ret
paulo@78 22
paulo@78 23
paulo@78 24 def parse_cookies(environ):
paulo@78 25 return urlparse.parse_qs(environ.get("HTTP_COOKIE", ""))
paulo@78 26
paulo@78 27
paulo@78 28 def check(cookies):
paulo@78 29 if PIN_KEY not in cookies:
paulo@78 30 raise PinFailError()
paulo@78 31
paulo@78 32 pin = cookies[PIN_KEY][0]
paulo@78 33 if pin != load():
paulo@78 34 raise PinFailError()
paulo@78 35
paulo@78 36 return pin