Mercurial > hg > index.fcgi > www > www-1
comparison pinlib/pinlib.py @ 82:d7d67887102f
move pinlib to a shared location and have pics use it
author | paulo |
---|---|
date | Wed, 29 Jun 2016 00:11:55 -0700 |
parents | laterlinks2/pinlib.py@f833a888c548 |
children | d521df55f56c |
comparison
equal
deleted
inserted
replaced
0:a68e5533ad83 | 0:4bcbd4e5a550 |
---|---|
1 import urlparse | 1 import urlparse |
2 | 2 |
3 | 3 |
4 PIN_KEY = "llpin" | 4 def parse_cookies(environ): |
5 PIN_FN = "_%s" % PIN_KEY | 5 return urlparse.parse_qs(environ.get("HTTP_COOKIE", "")) |
6 | 6 |
7 | 7 |
8 class PinFailError(Exception): | 8 class PinFailError(Exception): |
9 pass | 9 pass |
10 | 10 |
11 | 11 |
12 def load(): | 12 class PinMan(object): |
13 ret = None | 13 def __init__(self, key): |
14 assert key | |
15 self._key = key | |
16 self._fn = "_%s" % key | |
14 | 17 |
15 try: | 18 def load(self): |
16 with open(PIN_FN) as pin_f: | 19 ret = None |
17 ret = pin_f.read().strip() | 20 |
18 except IOError: | 21 try: |
19 pass | 22 with open(self._fn) as pin_f: |
20 | 23 ret = pin_f.read().strip() |
21 return ret | 24 except IOError: |
22 | 25 pass |
23 | 26 |
24 def parse_cookies(environ): | 27 return ret |
25 return urlparse.parse_qs(environ.get("HTTP_COOKIE", "")) | 28 |
26 | 29 def check(self, cookies): |
27 | 30 if self._key not in cookies: |
28 def check(cookies): | 31 raise PinFailError() |
29 if PIN_KEY not in cookies: | 32 |
30 raise PinFailError() | 33 pin = cookies[self._key][0] |
31 | 34 if pin != self.load(): |
32 pin = cookies[PIN_KEY][0] | 35 raise PinFailError() |
33 if pin != load(): | 36 |
34 raise PinFailError() | 37 return pin |
35 | |
36 return pin |