Esempio n. 1
0
 def test_signature_with_salt(self):
     """signature(value, salt=...) should work"""
     signer = signing.Signer('predictable-secret', salt='extra-salt')
     self.assertEqual(
         signer.signature('hello'),
         signing.base64_hmac('extra-salt' + 'signer', 'hello',
                             'predictable-secret').decode())
     self.assertNotEqual(
         signing.Signer('predictable-secret',
                        salt='one').signature('hello'),
         signing.Signer('predictable-secret',
                        salt='two').signature('hello'))
Esempio n. 2
0
 def test_signature(self):
     """signature() method should generate a signature"""
     signer = signing.Signer('predictable-secret')
     signer2 = signing.Signer('predictable-secret2')
     for s in (
             b'hello',
             b'3098247:529:087:',
             '\u2019'.encode('utf-8'),
     ):
         self.assertEqual(
             signer.signature(s),
             signing.base64_hmac(signer.salt + 'signer', s,
                                 'predictable-secret').decode())
         self.assertNotEqual(signer.signature(s), signer2.signature(s))
Esempio n. 3
0
 def test_invalid_sep(self):
     """should warn on invalid separator"""
     msg = 'Unsafe Signer separator: %r (cannot be empty or consist of only A-z0-9-_=)'
     separators = ['', '-', 'abc']
     for sep in separators:
         with self.assertRaisesMessage(ValueError, msg % sep):
             signing.Signer(sep=sep)
Esempio n. 4
0
 def test_valid_sep(self):
     separators = ['/', '*sep*', ',']
     for sep in separators:
         signer = signing.Signer('predictable-secret', sep=sep)
         self.assertEqual(
             'foo%sLQ8wXoKVFLoLwqvrZsOL9FWEwOy1XDzvduylmAZwNaI' % sep,
             signer.sign('foo'))
Esempio n. 5
0
 def test_unsign_detects_tampering(self):
     """unsign should raise an exception if the value has been tampered with"""
     signer = signing.Signer('predictable-secret')
     value = 'Another string'
     signed_value = signer.sign(value)
     transforms = (
         lambda s: s.upper(),
         lambda s: s + 'a',
         lambda s: 'a' + s[1:],
         lambda s: s.replace(':', ''),
     )
     self.assertEqual(value, signer.unsign(signed_value))
     for transform in transforms:
         with self.assertRaises(signing.BadSignature):
             signer.unsign(transform(signed_value))
Esempio n. 6
0
 def test_sign_unsign(self):
     """sign/unsign should be reversible"""
     signer = signing.Signer('predictable-secret')
     examples = [
         'q;wjmbk;wkmb',
         '3098247529087',
         '3098247:529:087:',
         'jkw osanteuh ,rcuh nthu aou oauh ,ud du',
         '\u2019',
     ]
     for example in examples:
         signed = signer.sign(example)
         self.assertIsInstance(signed, str)
         self.assertNotEqual(force_str(example), signed)
         self.assertEqual(example, signer.unsign(signed))
Esempio n. 7
0
    def test_timestamp_signer(self):
        value = 'hello'
        with freeze_time(123456789):
            signer = signing.TimestampSigner('predictable-key')
            ts = signer.sign(value)
            self.assertNotEqual(ts,
                                signing.Signer('predictable-key').sign(value))
            self.assertEqual(signer.unsign(ts), value)

        with freeze_time(123456800):
            self.assertEqual(signer.unsign(ts, max_age=12), value)
            # max_age parameter can also accept a datetime.timedelta object
            self.assertEqual(
                signer.unsign(ts, max_age=datetime.timedelta(seconds=11)),
                value)
            with self.assertRaises(signing.SignatureExpired):
                signer.unsign(ts, max_age=10)
Esempio n. 8
0
    def test_works_with_non_ascii_keys(self):
        binary_key = b'\xe7'  # Set some binary (non-ASCII key)

        s = signing.Signer(binary_key)
        self.assertEqual('foo:fc5zKyRI0Ktcf8db752abovGMa_u2CW9kPCaw5Znhag',
                         s.sign('foo'))