Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 838a16ec2055cd9736c9d426acf9fdd092e72469 1 parent 222a5ed
Andrew Godwin authored June 17, 2011
10  django/core/signing.py
@@ -158,12 +158,8 @@ def unsign(self, signed_value):
158 158
 
159 159
 
160 160
 class TimestampSigner(Signer):
161  
-    def __init__(self, *args, **kwargs):
162  
-        self.time_func = kwargs.pop('time', time.time)
163  
-        super(TimestampSigner, self).__init__(*args, **kwargs)
164  
-    
165 161
     def timestamp(self):
166  
-        return baseconv.base62.encode(int(self.time_func() * 10000))
  162
+        return baseconv.base62.encode(int(time.time()))
167 163
 
168 164
     def sign(self, value):
169 165
         value = smart_str('%s%s%s' % (value, self.sep, self.timestamp()))
@@ -172,10 +168,10 @@ def sign(self, value):
172 168
     def unsign(self, value, max_age=None):
173 169
         result =  super(TimestampSigner, self).unsign(value)
174 170
         value, timestamp = result.rsplit(self.sep, 1)
175  
-        timestamp = baseconv.base62.decode(timestamp) / 10000.0
  171
+        timestamp = baseconv.base62.decode(timestamp)
176 172
         if max_age is not None:
177 173
             # Check timestamp is not older than max_age
178  
-            age = self.time_func() - timestamp
  174
+            age = time.time() - timestamp
179 175
             if age > max_age:
180 176
                 raise SignatureExpired(
181 177
                     'Signature age %s > %s seconds' % (age, max_age))
34  tests/regressiontests/signing/tests.py
... ...
@@ -1,3 +1,5 @@
  1
+import time
  2
+
1 3
 from django.core import signing
2 4
 from django.test import TestCase
3 5
 from django.utils.encoding import force_unicode
@@ -96,21 +98,19 @@ class TestTimestampSigner(TestCase):
96 98
 
97 99
     def test_timestamp_signer(self):
98 100
         value = u'hello'
99  
-        signer = signing.TimestampSigner('predictable-key',
100  
-            time=lambda: 123456789)
101  
-        ts = signer.sign(value)
102  
-        self.assertNotEqual(ts,
103  
-            signing.Signer('predictable-key').sign(value))
  101
+        _time = time.time
  102
+        time.time = lambda: 123456789
  103
+        try:
  104
+            signer = signing.TimestampSigner('predictable-key')
  105
+            ts = signer.sign(value)
  106
+            self.assertNotEqual(ts,
  107
+                signing.Signer('predictable-key').sign(value))
104 108
 
105  
-        self.assertEqual(signer.unsign(ts), value)
106  
-        signer = signing.TimestampSigner('predictable-key',
107  
-            time=lambda: 123456800)
108  
-        self.assertEqual(signer.unsign(ts, max_age=12), value)
109  
-        self.assertEqual(signer.unsign(ts, max_age=11), value)
110  
-        self.assertRaises(
111  
-            signing.SignatureExpired, signer.unsign, ts, max_age=10)
112  
-    
113  
-    def test_timestamp_precision(self):
114  
-        one = signing.TimestampSigner('key', time=lambda: 123.4567).sign('v')
115  
-        two = signing.TimestampSigner('key', time=lambda: 123.4568).sign('v')
116  
-        self.assertNotEqual(one, two)
  109
+            self.assertEqual(signer.unsign(ts), value)
  110
+            time.time = lambda: 123456800
  111
+            self.assertEqual(signer.unsign(ts, max_age=12), value)
  112
+            self.assertEqual(signer.unsign(ts, max_age=11), value)
  113
+            self.assertRaises(
  114
+                signing.SignatureExpired, signer.unsign, ts, max_age=10)
  115
+        finally:
  116
+            time.time = _time

0 notes on commit 838a16e

Please sign in to comment.
Something went wrong with that request. Please try again.