Пример #1
0
 def test_dumps_loads(self):
     """dumps and loads be reversible for any JSON serializable object"""
     objects = [
         ['a', 'list'],
         'a unicode string \u2019',
         {
             'a': 'dictionary'
         },
     ]
     for o in objects:
         self.assertNotEqual(o, signing.dumps(o))
         self.assertEqual(o, signing.loads(signing.dumps(o)))
         self.assertNotEqual(o, signing.dumps(o, compress=True))
         self.assertEqual(o, signing.loads(signing.dumps(o, compress=True)))
Пример #2
0
 def test_dumps_loads(self):
     "dumps and loads be reversible for any JSON serializable object"
     objects = [
         ['a', 'list'],
         'a unicode string \u2019',
         {'a': 'dictionary'},
     ]
     if six.PY2:
         objects.append(b'a byte string')
     for o in objects:
         self.assertNotEqual(o, signing.dumps(o))
         self.assertEqual(o, signing.loads(signing.dumps(o)))
         self.assertNotEqual(o, signing.dumps(o, compress=True))
         self.assertEqual(o, signing.loads(signing.dumps(o, compress=True)))
Пример #3
0
    def digital_sign(self, days: int = None):
        if days:
            dt = timezone.datetime.now() + timezone.timedelta(days=days)
            timed_sign = jwt.encode(payload={
                'sign': signing.dumps(self.id),
                'exp': dt
            },
                                    key=settings.SECRET_KEY,
                                    algorithm='HS256')

            return timed_sign.decode('utf-8')
        else:
            sign = jwt.encode(payload={
                'sign': signing.dumps(self.id),
            },
                              key=settings.SECRET_KEY,
                              algorithm='HS256')

            return sign.decode('utf-8')
Пример #4
0
 def test_decode_detects_tampering(self):
     """loads should raise exception for tampered objects"""
     transforms = (
         lambda s: s.upper(),
         lambda s: s + 'a',
         lambda s: 'a' + s[1:],
         lambda s: s.replace(':', ''),
     )
     value = {
         'foo': 'bar',
         'baz': 1,
     }
     encoded = signing.dumps(value)
     self.assertEqual(value, signing.loads(encoded))
     for transform in transforms:
         with self.assertRaises(signing.BadSignature):
             signing.loads(transform(encoded))
Пример #5
0
 def test_decode_detects_tampering(self):
     """loads should raise exception for tampered objects"""
     transforms = (
         lambda s: s.upper(),
         lambda s: s + 'a',
         lambda s: 'a' + s[1:],
         lambda s: s.replace(':', ''),
     )
     value = {
         'foo': 'bar',
         'baz': 1,
     }
     encoded = signing.dumps(value)
     self.assertEqual(value, signing.loads(encoded))
     for transform in transforms:
         with self.assertRaises(signing.BadSignature):
             signing.loads(transform(encoded))