Debian Codegraph @

docs/serializer.rst

raw
1 .. module:: itsdangerous.serializer
2
3 Serialization Interface
4 =======================
5
6 The :doc:`/signer` only signs bytes. To sign other types, the
7 :class:`Serializer` class provides a ``dumps``/``loads`` interface
8 similar to Python's :mod:`json` module, which serializes the object to a
9 string then signs that.
10
11 Use :meth:`~Serializer.dumps` to serialize and sign the data:
12
13 .. code-block:: python
14
15 from itsdangerous.serializer import Serializer
16 s = Serializer("secret-key")
17 s.dumps([1, 2, 3, 4])
18 b'[1, 2, 3, 4].r7R9RhGgDPvvWl3iNzLuIIfELmo'
19
20 Use :meth:`~Serializer.loads` to verify the signature and deserialize
21 the data.
22
23 .. code-block:: python
24
25 s.loads('[1, 2, 3, 4].r7R9RhGgDPvvWl3iNzLuIIfELmo')
26 [1, 2, 3, 4]
27
28 By default, data is serialized to JSON with the built-in :mod:`json`
29 module. This internal serializer can be changed by subclassing.
30
31 To record and validate the age of the signature, see :doc:`/timed`.
32 To serialize to a format that is safe to use in URLs, see
33 :doc:`/url_safe`.
34
35
36 Responding to Failure
37 ---------------------
38
39 Exceptions have helpful attributes which allow you to inspect the
40 payload if the signature check failed. This has to be done with extra
41 care because at that point you know that someone tampered with your data
42 but it might be useful for debugging purposes.
43
44 .. code-block:: python
45
46 from itsdangerous.serializer import Serializer
47 from itsdangerous.exc import BadSignature, BadData
48
49 s = URLSafeSerializer("secret-key")
50 decoded_payload = None
51
52 try:
53 decoded_payload = s.loads(data)
54 # This payload is decoded and safe
55 except BadSignature as e:
56 if e.payload is not None:
57 try:
58 decoded_payload = s.load_payload(e.payload)
59 except BadData:
60 pass
61 # This payload is decoded but unsafe because someone
62 # tampered with the signature. The decode (load_payload)
63 # step is explicit because it might be unsafe to unserialize
64 # the payload (think pickle instead of json!)
65
66 If you don't want to inspect attributes to figure out what exactly went
67 wrong you can also use :meth:`~Serializer.loads_unsafe`:
68
69 .. code-block:: python
70
71 sig_okay, payload = s.loads_unsafe(data)
72
73 The first item in the returned tuple is a boolean that indicates if the
74 signature was correct.
75
76
77 Fallback Signers
78 ----------------
79
80 You may want to upgrade the signing parameters without invalidating
81 existing signatures immediately. For example, you may decide that you
82 want to use a different digest method. New signatures should use the new
83 method, but old signatures should still validate.
84
85 A list of ``fallback_signers`` can be given that will be tried if
86 unsigning with the current signer fails. Each item in the list can be:
87
88 - A dict of ``signer_kwargs`` to instantiate the ``signer`` class
89 passed to the serializer.
90 - A :class:`~itsdangerous.signer.Signer` class to instantiated with
91 the ``secret_key``, ``salt``, and ``signer_kwargs`` passed to the
92 serializer.
93 - A tuple of ``(signer_class, signer_kwargs)`` to instantiate the
94 given class with the given args.
95
96 For example, this is a serializer that signs using SHA-512, but will
97 unsign using either SHA-512 or SHA-1:
98
99 .. code-block:: python
100
101 s = Serializer(
102 signer_kwargs={"digest_method": hashlib.sha512},
103 fallback_signers=[{"digest_method": hashlib.sha1}]
104 )
105
106
107
108 API
109 ---
110
111 .. autoclass:: Serializer
112 :members: