diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/laterlinks2/pinlib.py	Thu Jun 02 00:27:50 2016 -0700
     1.3 @@ -0,0 +1,36 @@
     1.4 +import urlparse
     1.5 +
     1.6 +
     1.7 +PIN_KEY = "llpin"
     1.8 +PIN_FN = "_%s" % PIN_KEY
     1.9 +
    1.10 +
    1.11 +class PinFailError(Exception):
    1.12 +	pass
    1.13 +
    1.14 +
    1.15 +def load():
    1.16 +	ret = None
    1.17 +
    1.18 +	try:
    1.19 +		with open(PIN_FN) as pin_f:
    1.20 +			ret = pin_f.read().strip()
    1.21 +	except IOError:
    1.22 +		pass
    1.23 +
    1.24 +	return ret
    1.25 +
    1.26 +
    1.27 +def parse_cookies(environ):
    1.28 +	return urlparse.parse_qs(environ.get("HTTP_COOKIE", ""))
    1.29 +
    1.30 +
    1.31 +def check(cookies):
    1.32 +	if PIN_KEY not in cookies:
    1.33 +		raise PinFailError()
    1.34 +
    1.35 +	pin = cookies[PIN_KEY][0]
    1.36 +	if pin != load():
    1.37 +		raise PinFailError()
    1.38 +
    1.39 +	return pin