-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
44 lines (34 loc) · 1.3 KB
/
utils.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
33
34
35
36
37
38
39
40
41
42
43
from django.conf import settings
from django.utils.crypto import constant_time_compare, salted_hmac
import base64
def b64_encode(s):
return base64.urlsafe_b64encode(s).strip('=')
def b64_decode(s):
pad = '=' * (-len(s) % 4)
return base64.urlsafe_b64decode(s + pad)
def base64_hmac(salt, value, key):
return b64_encode(salted_hmac(salt, value, key).digest())
class LegacySigner(object):
'''
Limited functionality signer
'''
def __init__(self, key=None, sep=':', salt=None):
self.sep = sep
self.key = key or settings.SECRET_KEY
self.salt = salt or ('%s.%s' %
(self.__class__.__module__, self.__class__.__name__))
def signature(self, value):
return base64_hmac(self.salt + 'signer', value, self.key)
def sign(self, value):
return '%s%s%s' % (value, self.sep, self.signature(value))
def unsign(self, signed_value):
if not self.sep in signed_value:
raise ValueError('No "%s" found in value' % self.sep)
value, sig = signed_value.rsplit(self.sep, 1)
if constant_time_compare(sig, self.signature(value)):
return value
raise ValueError('Signature "%s" does not match' % sig)
try:
from django.core.signing import Signer
except ImportError:
Signer = LegacySigner