OpenQuizz
Une application de gestion des contenus pédagogiques
Serializer Class Reference
Inheritance diagram for Serializer:
Collaboration diagram for Serializer:

Public Member Functions

def __init__ (self, secret_key, salt=b"itsdangerous", serializer=None, serializer_kwargs=None, signer=None, signer_kwargs=None, fallback_signers=None)
 
def load_payload (self, payload, serializer=None)
 
def dump_payload (self, obj)
 
def make_signer (self, salt=None)
 
def iter_unsigners (self, salt=None)
 
def dumps (self, obj, salt=None)
 
def dump (self, obj, f, salt=None)
 
def loads (self, s, salt=None)
 
def load (self, f, salt=None)
 
def loads_unsafe (self, s, salt=None)
 
def load_unsafe (self, f, *args, **kwargs)
 

Data Fields

 secret_key
 
 salt
 
 serializer
 
 is_text_serializer
 
 signer
 
 signer_kwargs
 
 fallback_signers
 
 serializer_kwargs
 

Static Public Attributes

 default_serializer
 
 default_signer
 
 default_fallback_signers
 

Detailed Description

This class provides a serialization interface on top of the
signer. It provides a similar API to json/pickle and other modules
but is structured differently internally. If you want to change the
underlying implementation for parsing and loading you have to
override the :meth:`load_payload` and :meth:`dump_payload`
functions.

This implementation uses simplejson if available for dumping and
loading and will fall back to the standard library's json module if
it's not available.

You do not need to subclass this class in order to switch out or
customize the :class:`.Signer`. You can instead pass a different
class to the constructor as well as keyword arguments as a dict that
should be forwarded.

.. code-block:: python

    s = Serializer(signer_kwargs={'key_derivation': 'hmac'})

You may want to upgrade the signing parameters without invalidating
existing signatures that are in use. Fallback signatures can be
given that will be tried if unsigning with the current signer fails.

Fallback signers can be defined by providing a list of
``fallback_signers``. Each item can be one of the following: a
signer class (which is instantiated with ``signer_kwargs``,
``salt``, and ``secret_key``), a tuple
``(signer_class, signer_kwargs)``, or a dict of ``signer_kwargs``.

For example, this is a serializer that signs using SHA-512, but will
unsign using either SHA-512 or SHA1:

.. code-block:: python

    s = Serializer(
        signer_kwargs={"digest_method": hashlib.sha512},
        fallback_signers=[{"digest_method": hashlib.sha1}]
    )

.. versionchanged:: 0.14:
    The ``signer`` and ``signer_kwargs`` parameters were added to
    the constructor.

.. versionchanged:: 1.1.0:
    Added support for ``fallback_signers`` and configured a default
    SHA-512 fallback. This fallback is for users who used the yanked
    1.0.0 release which defaulted to SHA-512.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  secret_key,
  salt = b"itsdangerous",
  serializer = None,
  serializer_kwargs = None,
  signer = None,
  signer_kwargs = None,
  fallback_signers = None 
)

Reimplemented in JSONWebSignatureSerializer.

Member Function Documentation

◆ dump()

def dump (   self,
  obj,
  f,
  salt = None 
)
Like :meth:`dumps` but dumps into a file. The file handle has
to be compatible with what the internal serializer expects.

◆ dump_payload()

def dump_payload (   self,
  obj 
)
Dumps the encoded object. The return value is always bytes.
If the internal serializer returns text, the value will be
encoded as UTF-8.

◆ dumps()

def dumps (   self,
  obj,
  salt = None 
)
Returns a signed string serialized with the internal
serializer. The return value can be either a byte or unicode
string depending on the format of the internal serializer.

◆ iter_unsigners()

def iter_unsigners (   self,
  salt = None 
)
Iterates over all signers to be tried for unsigning. Starts
with the configured signer, then constructs each signer
specified in ``fallback_signers``.

◆ load()

def load (   self,
  f,
  salt = None 
)
Like :meth:`loads` but loads from a file.

◆ load_payload()

def load_payload (   self,
  payload,
  serializer = None 
)
Loads the encoded object. This function raises
:class:`.BadPayload` if the payload is not valid. The
``serializer`` parameter can be used to override the serializer
stored on the class. The encoded ``payload`` should always be
bytes.

◆ load_unsafe()

def load_unsafe (   self,
  f,
args,
**  kwargs 
)
Like :meth:`loads_unsafe` but loads from a file.

.. versionadded:: 0.15

◆ loads()

def loads (   self,
  s,
  salt = None 
)
Reverse of :meth:`dumps`. Raises :exc:`.BadSignature` if the
signature validation fails.

◆ loads_unsafe()

def loads_unsafe (   self,
  s,
  salt = None 
)
Like :meth:`loads` but without verifying the signature. This
is potentially very dangerous to use depending on how your
serializer works. The return value is ``(signature_valid,
payload)`` instead of just the payload. The first item will be a
boolean that indicates if the signature is valid. This function
never fails.

Use it for debugging only and if you know that your serializer
module is not exploitable (for example, do not use it with a
pickle serializer).

.. versionadded:: 0.15

◆ make_signer()

def make_signer (   self,
  salt = None 
)
Creates a new instance of the signer to be used. The default
implementation uses the :class:`.Signer` base class.

Field Documentation

◆ default_fallback_signers

default_fallback_signers
static

◆ default_serializer

default_serializer
static

◆ default_signer

default_signer
static

◆ fallback_signers

fallback_signers

◆ is_text_serializer

is_text_serializer

◆ salt

salt

◆ secret_key

secret_key

◆ serializer

serializer

◆ serializer_kwargs

serializer_kwargs

◆ signer

signer

◆ signer_kwargs

signer_kwargs

The documentation for this class was generated from the following file: