2011-05-21 22:41:14 +08:00
|
|
|
"""
|
|
|
|
Functions for creating and restoring url-safe signed JSON objects.
|
|
|
|
|
|
|
|
The format used looks like this:
|
|
|
|
|
2011-06-27 01:00:24 +08:00
|
|
|
>>> signing.dumps("hello")
|
|
|
|
'ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk'
|
2011-05-21 22:41:14 +08:00
|
|
|
|
2011-06-27 17:00:12 +08:00
|
|
|
There are two components here, separated by a ':'. The first component is a
|
2011-05-21 22:41:14 +08:00
|
|
|
URLsafe base64 encoded JSON of the object passed to dumps(). The second
|
2011-06-27 01:00:24 +08:00
|
|
|
component is a base64 encoded hmac/SHA1 hash of "$first_component:$secret"
|
2011-05-21 22:41:14 +08:00
|
|
|
|
2011-06-27 01:00:24 +08:00
|
|
|
signing.loads(s) checks the signature and returns the deserialised object.
|
2011-05-21 22:41:14 +08:00
|
|
|
If the signature fails, a BadSignature exception is raised.
|
|
|
|
|
2011-06-27 01:00:24 +08:00
|
|
|
>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk")
|
2011-05-21 22:41:14 +08:00
|
|
|
u'hello'
|
2011-06-27 01:00:24 +08:00
|
|
|
>>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified")
|
2011-05-21 22:41:14 +08:00
|
|
|
...
|
2011-06-27 01:00:24 +08:00
|
|
|
BadSignature: Signature failed: ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified
|
2011-05-21 22:41:14 +08:00
|
|
|
|
|
|
|
You can optionally compress the JSON prior to base64 encoding it to save
|
|
|
|
space, using the compress=True argument. This checks if compression actually
|
|
|
|
helps and only applies compression if the result is a shorter string:
|
|
|
|
|
2011-06-27 01:00:24 +08:00
|
|
|
>>> signing.dumps(range(1, 20), compress=True)
|
|
|
|
'.eJwFwcERACAIwLCF-rCiILN47r-GyZVJsNgkxaFxoDgxcOHGxMKD_T7vhAml:1QaUaL:BA0thEZrp4FQVXIXuOvYJtLJSrQ'
|
2011-05-21 22:41:14 +08:00
|
|
|
|
|
|
|
The fact that the string is compressed is signalled by the prefixed '.' at the
|
|
|
|
start of the base64 JSON.
|
|
|
|
|
2011-06-27 01:00:24 +08:00
|
|
|
There are 65 url-safe characters: the 64 used by url-safe base64 and the ':'.
|
2011-05-21 22:41:14 +08:00
|
|
|
These functions make use of all of them.
|
|
|
|
"""
|
|
|
|
import base64
|
|
|
|
import time
|
|
|
|
import zlib
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.utils import baseconv, simplejson
|
|
|
|
from django.utils.crypto import constant_time_compare, salted_hmac
|
|
|
|
from django.utils.encoding import force_unicode, smart_str
|
|
|
|
from django.utils.importlib import import_module
|
|
|
|
|
|
|
|
|
|
|
|
class BadSignature(Exception):
|
|
|
|
"""
|
|
|
|
Signature does not match
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SignatureExpired(BadSignature):
|
|
|
|
"""
|
|
|
|
Signature timestamp is older than required max_age
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
|
|
|
|
def get_cookie_signer(salt='django.core.signing.get_cookie_signer'):
|
|
|
|
modpath = settings.SIGNING_BACKEND
|
|
|
|
module, attr = modpath.rsplit('.', 1)
|
|
|
|
try:
|
|
|
|
mod = import_module(module)
|
|
|
|
except ImportError, e:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Error importing cookie signer %s: "%s"' % (modpath, e))
|
|
|
|
try:
|
|
|
|
Signer = getattr(mod, attr)
|
|
|
|
except AttributeError, e:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Error importing cookie signer %s: "%s"' % (modpath, e))
|
|
|
|
return Signer('django.http.cookies' + settings.SECRET_KEY, salt=salt)
|
|
|
|
|
|
|
|
|
2011-06-27 01:00:24 +08:00
|
|
|
class JSONSerializer(object):
|
|
|
|
"""
|
|
|
|
Simple wrapper around simplejson to be used in signing.dumps and
|
|
|
|
signing.loads.
|
|
|
|
"""
|
|
|
|
def dumps(self, obj):
|
|
|
|
return simplejson.dumps(obj, separators=(',', ':'))
|
|
|
|
|
|
|
|
def loads(self, data):
|
|
|
|
return simplejson.loads(data)
|
|
|
|
|
|
|
|
|
|
|
|
def dumps(obj, key=None, salt='django.core.signing', serializer=JSONSerializer, compress=False):
|
2011-05-21 22:41:14 +08:00
|
|
|
"""
|
|
|
|
Returns URL-safe, sha1 signed base64 compressed JSON string. If key is
|
|
|
|
None, settings.SECRET_KEY is used instead.
|
|
|
|
|
|
|
|
If compress is True (not the default) checks if compressing using zlib can
|
|
|
|
save some space. Prepends a '.' to signify compression. This is included
|
|
|
|
in the signature, to protect against zip bombs.
|
|
|
|
|
2011-06-27 00:52:01 +08:00
|
|
|
Salt can be used to namespace the hash, so that a signed string is
|
|
|
|
only valid for a given namespace. Leaving this at the default
|
|
|
|
value or re-using a salt value across different parts of your
|
|
|
|
application without good cause is a security risk.
|
2011-05-21 22:41:14 +08:00
|
|
|
"""
|
2011-06-27 01:00:24 +08:00
|
|
|
data = serializer().dumps(obj)
|
2011-05-21 22:41:14 +08:00
|
|
|
|
|
|
|
# Flag for if it's been compressed or not
|
|
|
|
is_compressed = False
|
|
|
|
|
|
|
|
if compress:
|
|
|
|
# Avoid zlib dependency unless compress is being used
|
2011-06-27 01:00:24 +08:00
|
|
|
compressed = zlib.compress(data)
|
|
|
|
if len(compressed) < (len(data) - 1):
|
|
|
|
data = compressed
|
2011-05-21 22:41:14 +08:00
|
|
|
is_compressed = True
|
2011-06-27 01:00:24 +08:00
|
|
|
base64d = b64_encode(data)
|
2011-05-21 22:41:14 +08:00
|
|
|
if is_compressed:
|
|
|
|
base64d = '.' + base64d
|
|
|
|
return TimestampSigner(key, salt=salt).sign(base64d)
|
|
|
|
|
|
|
|
|
2011-06-27 01:00:24 +08:00
|
|
|
def loads(s, key=None, salt='django.core.signing', serializer=JSONSerializer, max_age=None):
|
2011-05-21 22:41:14 +08:00
|
|
|
"""
|
|
|
|
Reverse of dumps(), raises BadSignature if signature fails
|
|
|
|
"""
|
|
|
|
base64d = smart_str(
|
|
|
|
TimestampSigner(key, salt=salt).unsign(s, max_age=max_age))
|
|
|
|
decompress = False
|
|
|
|
if base64d[0] == '.':
|
|
|
|
# It's compressed; uncompress it first
|
|
|
|
base64d = base64d[1:]
|
|
|
|
decompress = True
|
2011-06-27 01:00:24 +08:00
|
|
|
data = b64_decode(base64d)
|
2011-05-21 22:41:14 +08:00
|
|
|
if decompress:
|
2011-06-27 01:00:24 +08:00
|
|
|
data = zlib.decompress(data)
|
|
|
|
return serializer().loads(data)
|
2011-05-21 22:41:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Signer(object):
|
|
|
|
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):
|
|
|
|
value = smart_str(value)
|
|
|
|
return '%s%s%s' % (value, self.sep, self.signature(value))
|
|
|
|
|
|
|
|
def unsign(self, signed_value):
|
|
|
|
signed_value = smart_str(signed_value)
|
|
|
|
if not self.sep in signed_value:
|
|
|
|
raise BadSignature('No "%s" found in value' % self.sep)
|
|
|
|
value, sig = signed_value.rsplit(self.sep, 1)
|
|
|
|
if constant_time_compare(sig, self.signature(value)):
|
|
|
|
return force_unicode(value)
|
|
|
|
raise BadSignature('Signature "%s" does not match' % sig)
|
|
|
|
|
|
|
|
|
|
|
|
class TimestampSigner(Signer):
|
2011-06-27 01:00:24 +08:00
|
|
|
|
2011-05-21 22:41:14 +08:00
|
|
|
def timestamp(self):
|
2011-06-17 17:47:08 +08:00
|
|
|
return baseconv.base62.encode(int(time.time()))
|
2011-05-21 22:41:14 +08:00
|
|
|
|
|
|
|
def sign(self, value):
|
|
|
|
value = smart_str('%s%s%s' % (value, self.sep, self.timestamp()))
|
|
|
|
return '%s%s%s' % (value, self.sep, self.signature(value))
|
|
|
|
|
|
|
|
def unsign(self, value, max_age=None):
|
|
|
|
result = super(TimestampSigner, self).unsign(value)
|
|
|
|
value, timestamp = result.rsplit(self.sep, 1)
|
2011-06-17 17:47:08 +08:00
|
|
|
timestamp = baseconv.base62.decode(timestamp)
|
2011-05-21 22:41:14 +08:00
|
|
|
if max_age is not None:
|
|
|
|
# Check timestamp is not older than max_age
|
2011-06-17 17:47:08 +08:00
|
|
|
age = time.time() - timestamp
|
2011-05-21 22:41:14 +08:00
|
|
|
if age > max_age:
|
|
|
|
raise SignatureExpired(
|
|
|
|
'Signature age %s > %s seconds' % (age, max_age))
|
|
|
|
return value
|