mirror of
https://github.com/django/django.git
synced 2024-12-25 10:35:48 +00:00
Backed out [16356] due to later rejection of #16182.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16426 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
222a5ed69e
commit
838a16ec20
@ -158,12 +158,8 @@ class Signer(object):
|
|||||||
|
|
||||||
|
|
||||||
class TimestampSigner(Signer):
|
class TimestampSigner(Signer):
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
self.time_func = kwargs.pop('time', time.time)
|
|
||||||
super(TimestampSigner, self).__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
def timestamp(self):
|
def timestamp(self):
|
||||||
return baseconv.base62.encode(int(self.time_func() * 10000))
|
return baseconv.base62.encode(int(time.time()))
|
||||||
|
|
||||||
def sign(self, value):
|
def sign(self, value):
|
||||||
value = smart_str('%s%s%s' % (value, self.sep, self.timestamp()))
|
value = smart_str('%s%s%s' % (value, self.sep, self.timestamp()))
|
||||||
@ -172,10 +168,10 @@ class TimestampSigner(Signer):
|
|||||||
def unsign(self, value, max_age=None):
|
def unsign(self, value, max_age=None):
|
||||||
result = super(TimestampSigner, self).unsign(value)
|
result = super(TimestampSigner, self).unsign(value)
|
||||||
value, timestamp = result.rsplit(self.sep, 1)
|
value, timestamp = result.rsplit(self.sep, 1)
|
||||||
timestamp = baseconv.base62.decode(timestamp) / 10000.0
|
timestamp = baseconv.base62.decode(timestamp)
|
||||||
if max_age is not None:
|
if max_age is not None:
|
||||||
# Check timestamp is not older than max_age
|
# Check timestamp is not older than max_age
|
||||||
age = self.time_func() - timestamp
|
age = time.time() - timestamp
|
||||||
if age > max_age:
|
if age > max_age:
|
||||||
raise SignatureExpired(
|
raise SignatureExpired(
|
||||||
'Signature age %s > %s seconds' % (age, max_age))
|
'Signature age %s > %s seconds' % (age, max_age))
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
from django.core import signing
|
from django.core import signing
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils.encoding import force_unicode
|
from django.utils.encoding import force_unicode
|
||||||
@ -96,21 +98,19 @@ class TestTimestampSigner(TestCase):
|
|||||||
|
|
||||||
def test_timestamp_signer(self):
|
def test_timestamp_signer(self):
|
||||||
value = u'hello'
|
value = u'hello'
|
||||||
signer = signing.TimestampSigner('predictable-key',
|
_time = time.time
|
||||||
time=lambda: 123456789)
|
time.time = lambda: 123456789
|
||||||
ts = signer.sign(value)
|
try:
|
||||||
self.assertNotEqual(ts,
|
signer = signing.TimestampSigner('predictable-key')
|
||||||
signing.Signer('predictable-key').sign(value))
|
ts = signer.sign(value)
|
||||||
|
self.assertNotEqual(ts,
|
||||||
|
signing.Signer('predictable-key').sign(value))
|
||||||
|
|
||||||
self.assertEqual(signer.unsign(ts), value)
|
self.assertEqual(signer.unsign(ts), value)
|
||||||
signer = signing.TimestampSigner('predictable-key',
|
time.time = lambda: 123456800
|
||||||
time=lambda: 123456800)
|
self.assertEqual(signer.unsign(ts, max_age=12), value)
|
||||||
self.assertEqual(signer.unsign(ts, max_age=12), value)
|
self.assertEqual(signer.unsign(ts, max_age=11), value)
|
||||||
self.assertEqual(signer.unsign(ts, max_age=11), value)
|
self.assertRaises(
|
||||||
self.assertRaises(
|
signing.SignatureExpired, signer.unsign, ts, max_age=10)
|
||||||
signing.SignatureExpired, signer.unsign, ts, max_age=10)
|
finally:
|
||||||
|
time.time = _time
|
||||||
def test_timestamp_precision(self):
|
|
||||||
one = signing.TimestampSigner('key', time=lambda: 123.4567).sign('v')
|
|
||||||
two = signing.TimestampSigner('key', time=lambda: 123.4568).sign('v')
|
|
||||||
self.assertNotEqual(one, two)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user