%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python3/dist-packages/josepy/__pycache__/
Upload File :
Create Path :
Current File : //usr/lib/python3/dist-packages/josepy/__pycache__/json_util.cpython-312.pyc

�

���etH���dZddlZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZm
Z
ddlmZddlmZmZmZmZej*e�Z				d*dededed	eeegefd
eeegefdefd�ZGd
�d�ZGd�de�ZGd�dej:�Ze
dd��ZGd�dej@ejBe��Z"de#defd�Z$d+dedee%dede#fd�Z&de#defd�Z'd+dedee%dede#fd�Z(d ejRdefd!�Z*d"edejRfd#�Z+d$ejRdefd%�Z,d"edejRfd&�Z-e
d'd(��Z.Gd)�d(e"�Z/y),z�JSON (de)serialization framework.

The framework presented here is somewhat based on `Go's "json" package`_
(especially the ``omitempty`` functionality).

.. _`Go's "json" package`: http://golang.org/pkg/encoding/json/

�N)	�Any�Callable�Dict�Iterable�List�Mapping�Optional�Type�TypeVar)�crypto)�b64�errors�
interfaces�util�	json_name�default�	omitempty�decoder�encoder�returnc�"�t|||||��S)a4Convenient function to declare a :class:`Field` with proper type annotations.

    This function allows to write the following code:

    import josepy
    class JSON(josepy.JSONObjectWithFields):
        typ: str = josepy.field('type')

        def other_type(self) -> str:
            return self.typ

    �rrrrr)�_TypedFieldrs     �2/usr/lib/python3/dist-packages/josepy/json_util.py�fieldrs��&��W�	�7�\c���c�(�eZdZdZdZ				ddedededee	egefdee	egefd	dfd
�Z
eded	efd��Zded	efd
�Z
ded	dfd�Zde	egefd	dfd�Zde	egefd	dfd�Zded	efd�Zded	efd�Zeded	efd��Zeded	efd��Zy)�Fielda�JSON object field.

    :class:`Field` is meant to be used together with
    :class:`JSONObjectWithFields`.

    ``encoder`` (``decoder``) is a callable that accepts a single
    parameter, i.e. a value to be encoded (decoded), and returns the
    serialized (deserialized) value. In case of errors it should raise
    :class:`~josepy.errors.SerializationError`
    (:class:`~josepy.errors.DeserializationError`).

    Note, that ``decoder`` should perform partial serialization only.

    :ivar str json_name: Name of the field when encoded to JSON.
    :ivar default: Default value (used when not present in JSON object).
    :ivar bool omitempty: If ``True`` and the field value is empty, then
        it will not be included in the serialized JSON object, and
        ``default`` will be used for deserialization. Otherwise, if ``False``,
        field is considered as required, value will always be included in the
        serialized JSON objected, and it must also be present when
        deserializing.

    )rrr�fdec�fencNrrrrrrc��||_||_||_|�|jn||_|�|j
|_y||_y�N)rrr�default_decoderr�default_encoderr )�selfrrrrrs      r�__init__zField.__init__RsC��#������"���,3�O�D�(�(���	�,3�O�D�(�(��	���	r�valuec�.�t|t�xr|S)z�Is the provided value considered "empty" for this field?

        This is useful for subclasses that might want to override the
        definition of being empty, e.g. for some more exotic data types.

        )�
isinstance�bool��clsr's  r�_emptyzField._emptyas���e�T�*�*�8�5�y�8rc�@�|j|�xr|jS)zOmit the value in output?)r-r�r%r's  r�omitz
Field.omitks���{�{�5�!�4�d�n�n�4r�kwargsc��|j|j|j|j|jd�|�}t|�di|��S)Nr�)rrrrr �type)r%r1�currents   r�_update_paramszField._update_paramsosL������|�|�����y�y��y�y�
��

���t�D�z�$�G�$�$rrc�&�|j|��S)z6Descriptor to change the decoder on JSON object field.)r�r6)r%rs  rrz
Field.decoderz����"�"�4�"�0�0rr c�&�|j|��S)z6Descriptor to change the encoder on JSON object field.)rr8)r%r s  rrz
Field.encoder~r9rc�$�|j|�S)z4Decode a value, optionally with context JSON object.)rr/s  r�decodezField.decode�����y�y���rc�$�|j|�S)z4Encode a value, optionally with context JSON object.)r r/s  r�encodezField.encode�r=rc
�&��t|t�rt�fd�|D��St|t�rUt	j
|j
�D��cic]&\}}�j|��j|���(c}}�S|Scc}}w)z�Default decoder.

        Recursively deserialize into immutable types (
        :class:`josepy.util.frozendict` instead of
        :func:`dict`, :func:`tuple` instead of :func:`list`).

        c3�@�K�|]}�j|����y�wr")r#)�.0�subvaluer,s  �r�	<genexpr>z(Field.default_decoder.<locals>.<genexpr>�s�����M�8��,�,�X�6�M�s�)r)�list�tuple�dictr�
frozendict�itemsr#)r,r'�keys`  rr#zField.default_decoder�s�����e�T�"��M�u�M�M�M�
��t�
$��?�?�',�k�k�m��"��U��'�'��,�c�.A�.A�%�.H�H���
��L��
s�+B
c��|S)zDefault (passthrough) encoder.r3r+s  rr$zField.default_encoder�s	��
�r�NFNN)�__name__�
__module__�__qualname__�__doc__�	__slots__�strrr*r	rr&�classmethodr-r0r6rrr<r?r#r$r3rrrr7sq���0F�I�
��26�26�

I��
I��
I��	
I�
�(�C�5�#�:�.�/�
I��(�C�5�#�:�.�/�

I�
�
I��9�3�9�4�9��9�5�#�5�$�5�	%�s�	%�w�	%�1�H�c�U�C�Z�0�1�W�1�1�H�c�U�C�Z�0�1�W�1� �C� �C� � �C� �C� ���C��C����*��C��C���rrc��eZdZdZy)raSpecialized class to mark a JSON object field with typed annotations.

    This class is kept private because fields are supposed to be declared
    using the :function:`field` in this situation.

    In the future the :class:`Field` may be removed in favor of this one.N)rMrNrOrPr3rrrr�s��Mrrc�b�eZdZUdZiZeeefed<e	eed<dede
edeeefddfd�Zy	)
�JSONObjectWithFieldsMetaa�Metaclass for :class:`JSONObjectWithFields` and its subclasses.

    It makes sure that, for any class ``cls`` with ``__metaclass__``
    set to ``JSONObjectWithFieldsMeta``:

    1. All fields (attributes of type :class:`Field`) in the class
       definition are moved to the ``cls._fields`` dictionary, where
       keys are field attribute names and values are fields themselves.

    2. ``cls.__slots__`` is extended by all field attribute names
       (i.e. not :attr:`Field.json_name`). Original ``cls.__slots__``
       are stored in ``cls._orig_slots``.

    In a consequence, for a field attribute name ``some_field``,
    ``cls.some_field`` will be a slot descriptor and not an instance
    of :class:`Field`. For example::

      some_field = Field('someField', default=())

      class Foo:
          __metaclass__ = JSONObjectWithFieldsMeta
          __slots__ = ('baz',)
          some_field = some_field

      assert Foo.__slots__ == ('some_field', 'baz')
      assert Foo._orig_slots == ()
      assert Foo.some_field is not Field

      assert Foo._fields.keys() == ['some_field']
      assert Foo._fields['some_field'] is some_field

    As an implementation note, this metaclass inherits from
    :class:`abc.ABCMeta` (and not the usual :class:`type`) to mitigate
    the metaclass conflict (:class:`ImmutableMap` and
    :class:`JSONDeSerializable`, parents of :class:`JSONObjectWithFields`,
    use :class:`abc.ABCMeta` as its metaclass).

    �_fields�_orig_slots�name�bases�	namespacerc�.�i}|D]}|jt|di��� |j�j�D]`\}}t	|t
�s�t	|t�r&||jdi�vrtd|�d|�d���|j|�||<�b|jdd�|d<tt|d�t|j��z�|d<||d<tjj||||�S)	NrW�__annotations__zField `z` in JSONObject `z` has no type annotation.rQr3rX)�update�getattr�copyrIr)rr�get�
ValueError�poprFrE�keys�abc�ABCMeta�__new__)�mcsrYrZr[�fields�baserJr's        rrgz JSONObjectWithFieldsMeta.__new__�s��$&���	8�D��M�M�'�$�	�2�6�7�	8�$�.�.�*�0�0�2�		1�J�C���%��'��e�[�1��)�-�-�0A�2�"F�F�(�%�c�U�*;�D�6�AZ�[���(�m�m�C�0��s��		1�$-�=�=��b�#A�	�-� �!&�t�I�m�,D�'E��V�[�[�]�H[�'[�!\�	�+��%�	�)���{�{�"�"�3��e�Y�?�?rN)
rMrNrOrPrWrrRrr]rrrrgr3rrrVrV�s]��%�N!#�G�T�#�u�*�
�"��#���@��@�#�C�y�@�59�#�s�(�^�@�	#�@rrV�GenericJSONObjectWithFields�JSONObjectWithFields)�boundc���eZdZdZedeeeffd��Zdeddf�fd�Z	dedefd�Z
deeeffd	�Zdeeeffd
�Zede
eefddfd��Zede
eefdefd
��Zedeede
eefdefd��Z�xZS)rla�JSON object with fields.

    Example::

      class Foo(JSONObjectWithFields):
          bar = Field('Bar')
          empty = Field('Empty', omitempty=True)

          @bar.encoder
          def bar(value):
              return value + 'bar'

          @bar.decoder
          def bar(value):
              if not value.endswith('bar'):
                  raise errors.DeserializationError('No bar suffix!')
              return value[:-3]

      assert Foo(bar='baz').to_partial_json() == {'Bar': 'bazbar'}
      assert Foo.from_json({'Bar': 'bazbar'}) == Foo(bar='baz')
      assert (Foo.from_json({'Bar': 'bazbar', 'Empty': '!'})
              == Foo(bar='baz', empty='!'))
      assert Foo(bar='baz').bar == 'baz'

    rc�~�|jj�D��cic]\}}||j��c}}Scc}}w)zGet default fields values.)rWrIr)r,�slotrs   r�	_defaultszJSONObjectWithFields._defaultss2��8;�{�{�7H�7H�7J�K���e��e�m�m�#�K�K��Ks�9r1Nc�H��t�|�dii|j��|���y�Nr3)�superr&rq)r%r1�	__class__s  �rr&zJSONObjectWithFields.__init__s%���
���:�9�d�n�n�.�9�&�9�:rrYc��	|j|}|jt
||��S#t$r%tjdj	|���wxYw)z�Encode a single field.

        :param str name: Name of the field to be encoded.

        :raises errors.SerializationError: if field cannot be serialized
        :raises errors.Error: if field could not be found

        zField not found: {0})rW�KeyErrorr�Error�formatr?r_)r%rYrs   rr?zJSONObjectWithFields.encodes\��	D��L�L��&�E��|�|�G�D�$�/�0�0���	D��,�,�5�<�<�T�B�C�C�	D�s	�,�.Ac	��i}t�}|jj�D]U\}}t||�}|j	|�r|j||f��7	|j
|�||j<�W|S#tj$r+}tjdj|||���d}~wwxYw)zSerialize fields to JSON.zCould not encode {0} ({1}): {2}N)�setrWrIr_r0�addr?rr�SerializationErrorry)r%�jobj�omittedrprr'�errors       r�fields_to_partial_jsonz+JSONObjectWithFields.fields_to_partial_json/s������%���<�<�-�-�/�	�K�D�%��D�$�'�E��z�z�%� ����T�5�M�*��,1�L�L��,?�D����)�	����	�0�0�� �3�3�9�@�@��u�e�T�����s�B�B?�&B:�:B?c�"�|j�Sr")r�)r%s r�to_partial_jsonz$JSONObjectWithFields.to_partial_jsonAs���*�*�,�,rr~c�4�t�}|jj�D]<\}}|jr�|j|vs�"|j|j��>|r3t
jdjdj|����y)Nz&The following fields are required: {0}�,)
r{rWrIrrr|r�DeserializationErrorry�join)r,r~�missing�_rs     r�_check_requiredz$JSONObjectWithFields._check_requiredDs���%�����)�)�+�	-�H�A�u��?�?�u���d�'B����E�O�O�,�	-���-�-�8�?�?�����@Q�R��
�rc	��|j|�i}|jj�D]S\}}|j|vr|jr|j
||<�0||j}	|j
|�||<�U|S#tj$r+}tjdj|||���d}~wwxYw)zDeserialize fields from JSON.z#Could not decode {0!r} ({1!r}): {2}N)
r�rWrIrrrr<rr�ry)r,r~rirprr'r�s       r�fields_from_jsonz%JSONObjectWithFields.fields_from_jsonPs���	���D�!����;�;�,�,�.�
	�K�D�%����d�*�u���$�}�}��t���U�_�_�-���#(�<�<��#6�F�4�L�
	��
��	�2�2�� �5�5�=�D�D�T�5�RW�X�����s�.B�C�&B?�?Cr,c�0�|di|j|���Srs)r�)r,r~s  r�	from_jsonzJSONObjectWithFields.from_jsonbs���0�S�)�)�$�/�0�0r)rMrNrOrPrSrrRrrqr&r?r�r�rr�r�r
rkr��
__classcell__)rus@rrlrl�s
����4�L�$�s�C�x�.�L��L�;��;��;�1�3�1�3�1� ��S�#�X���$-��c�3�h��-��	�7�3��8�#4�	��	��	���G�C��H�$5��#����"�1�
�-�
.�1�6=�c�3�h�6G�1�	$�1��1r)�	metaclass�datac�J�tj|�jd�S)zJEncode JOSE Base-64 field.

    :param bytes data:
    :rtype: `str`

    �ascii)r
�	b64encoder<)r�s r�encode_b64joser�is���=�=���%�%�g�.�.r�size�minimumc�:�	tj|j��}|�D|st|�|k7s|r2t|�|kr$tjdj|���|S#tj$r}tj|��d}~wwxYw)aDecode JOSE Base-64 field.

    :param unicode data:
    :param int size: Required length (after decoding).
    :param bool minimum: If ``True``, then `size` will be treated as
        minimum required length, as opposed to exact equality.

    :rtype: bytes

    Nz&Expected at least or exactly {0} bytes)	r
�	b64decoder?�binasciirxrr��lenry)r�r�r��decodedr�s     r�decode_b64joser�ts���1��-�-����
�.����
��W���-�7�s�7�|�d�?R��)�)�*R�*Y�*Y�Z^�*_�`�`��N���>�>�1��)�)�%�0�0��1�s�#A-�-B�B�Br'c�H�tj|�j�S)z;Hexlify.

    :param bytes value:
    :rtype: unicode

    )r��hexlifyr<)r's r�encode_hex16r��s�����E�"�)�)�+�+rc�&�|j�}|�:|st|�|dzk7s|r%t|�|dzkrtj��	t	j
|�S#tj$r}tj|��d}~wwxYw)aDecode hexlified field.

    :param unicode value:
    :param int size: Required length (after decoding).
    :param bool minimum: If ``True``, then `size` will be treated as
        minimum required length, as opposed to exact equality.

    :rtype: bytes

    N�)r?r�rr�r��	unhexlifyrx)r'r�r��value_br�s     r�decode_hex16r��s����l�l�n�G���
��W�����1�w�3�w�<�RV�YZ�RZ�CZ��)�)�+�+�1��!�!�'�*�*���>�>�1��)�)�%�0�0��1�s�A#�#B�6B�B�certc���t|jtj�rt	d��ttjtj|j��S)z�Encode certificate as JOSE Base-64 DER.

    :type cert: `OpenSSL.crypto.X509` wrapped in `.ComparableX509`
    :rtype: unicode

    z.Error input is actually a certificate request.)r)�wrappedr�X509Reqrbr��dump_certificate�
FILETYPE_ASN1)r�s r�encode_certr��sD���$�,�,����/��I�J�J��&�1�1�&�2F�2F����U�V�Vr�b64derc���	tjtjtjt|���S#tj$r}tj|��d}~wwxYw)z�Decode JOSE Base-64 DER-encoded certificate.

    :param unicode b64der:
    :rtype: `OpenSSL.crypto.X509` wrapped in `.ComparableX509`

    N)	r�ComparableX509r�load_certificater�r�rxrr��r�r�s  r�decode_certr��s\��1��"�"��#�#�F�$8�$8�.��:P�Q�
�	
���<�<�1��)�)�%�0�0��1���?A�A/�A*�*A/�csrc���t|jtj�rt	d��ttjtj|j��S)zEncode CSR as JOSE Base-64 DER.

    :type csr: `OpenSSL.crypto.X509Req` wrapped in `.ComparableX509`
    :rtype: unicode

    z&Error input is actually a certificate.)r)r�r�X509rbr��dump_certificate_requestr�)r�s r�
encode_csrr��sG���#�+�+�v�{�{�+��A�B�B��&�9�9�&�:N�:N�PS�P[�P[�\�]�]rc���	tjtjtjt|���S#tj$r}tj|��d}~wwxYw)z�Decode JOSE Base-64 DER-encoded CSR.

    :param unicode b64der:
    :rtype: `OpenSSL.crypto.X509Req` wrapped in `.ComparableX509`

    N)	rr�r�load_certificate_requestr�r�rxrr�r�s  r�
decode_csrr��s]��1��"�"��+�+�F�,@�,@�.�QW�BX�Y�
�	
���<�<�1��)�)�%�0�0��1�r�� GenericTypedJSONObjectWithFields�TypedJSONObjectWithFieldsc	���eZdZUdZeZeed<	dZeed<	eZ	e
eefed<	e	ddee
deedee
fd	��Zed
eeefdedfd��Zde
eeffd�Zed
eeefddfd
��Zy)r�zJSON object with type.�typr4�type_field_name�TYPESN�type_clsrc�D�|�|jn|}||j|<|S)z(Register class for JSON deserialization.)r�r�)r,r�r�s   r�registerz"TypedJSONObjectWithFields.register�s%��
"�k�h�l�l�s��!��	�	�#���rr~c���||jj�vr>|j|vr.tjdj|j���|St
|t�s$tjdj|���	||j}	|j|S#t$rtjd��wxYw#t$rtj||��wxYw)z&Get the registered class for ``jobj``.zMissing type field ({0})z{0} is not a dictionary objectzmissing type field)
r��valuesr�rr�ryr)rGrw�UnrecognizedTypeError)r,r~r�s   r�get_type_clsz&TypedJSONObjectWithFields.get_type_clss����#�)�)�"�"�$�$��"�"�$�.��1�1�.�5�5�c�6I�6I�J����J��$��%��-�-�.N�.U�.U�VZ�.[�\�\�	D��s�*�*�+�C�	:��9�9�S�>�!��	�	D��-�-�.B�C�C�	D��
�	:��.�.�s�D�9�9�	:�s�B/� C�/C� C1c�X�|j�}|j||j<|S)aGet JSON serializable object.

        :returns: Serializable JSON object representing ACME typed object.
            :meth:`validate` will almost certainly not work, due to reasons
            explained in :class:`josepy.interfaces.IJSONSerializable`.
        :rtype: dict

        )r�r�r�)r%r~s  rr�z)TypedJSONObjectWithFields.to_partial_jsons+���*�*�,��%)�X�X��T�
!�
!�"��rc�R�|j|�}|di|j|���S)z�Deserialize ACME object from valid JSON object.

        :raises josepy.errors.UnrecognizedTypeError: if type
            of the ACME object has not been registered.

        r3)r�r�)r,r~r�s   rr�z#TypedJSONObjectWithFields.from_json(s-���#�#�D�)���:�(�3�3�D�9�:�:rr")rMrNrOrP�NotImplementedr�rRr]r�r�rr
rSr�r	r�rrr�r�r�r3rrr�r��s��� ��C���7�!�O�S�!��,�E�4��T�	�?�+�3��TX���<�=��DL�S�M��	
�.�	/�����:���S�� 1�:�d�;V�6W�:��:�0��c�3�h����	;�W�S�#�X�.�	;�3N�	;��	;rrL)NF)0rPrer��logging�typingrrrrrrr	r
r�OpenSSLr�josepyr
rrr�	getLoggerrM�loggerrRr*rrrrfrVrk�ImmutableMap�JSONDeSerializablerl�bytesr��intr�r�r�r�r�r�r�r�r�r�r3rr�<module>r�s������
�
�
��0�0�	��	�	�8�	$��
��.2�.2����
�����h��u�c�z�*�
+�	�
�h��u�c�z�*�
+��	�
�0n�n�bM�%�M�C@�s�{�{�C@�L&�&C�Ka�b��l1����z�4�4�@X�l1�^/��/�3�/����H�S�M��4��TY��0,��,�#�,�1��1�8�C�=�1�$�1�SX�1�,
W�d�)�)�
W�c�
W�1��1�� 3� 3�1�
^�D�'�'�
^�C�
^�1�s�1�t�2�2�1�$+�&�.I�$� �
I;� 4�I;r

Zerion Mini Shell 1.0