-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.py
32 lines (20 loc) · 639 Bytes
/
cookie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3
import hmac
import logging
from secret import SECRET
SEPARATOR = '|'
def hash_str(s):
return hmac.new(SECRET, s).hexdigest()
def make_secure_val(s):
return s + SEPARATOR + hash_str(s)
def check_secure_val(sh):
s, hash_value = sh.split(SEPARATOR)
if hash_str(s) == hash_value:
return s
else:
logging.warning("Something wrong with value or hash: " + sh)
def make_cookie(name, value):
cookie_value = make_secure_val(value)
return "{name}={value}; Path=/".format(name=name, value=cookie_value)
def check_cookie(value):
return value and check_secure_val(value)