HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/pymacaroons/serializers/__pycache__/json_serializer.cpython-38.pyc
U

�%�YU�@sPddlZddlZddlmZGdd�de�Zdd�Zdd�Zd	d
�Zdd�Z	dS)
�N)�utilsc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�JsonSerializerz8Serializer used to produce JSON macaroon format v1.
    cCs,ddlm}|j|jkr"|�|�S|�|�S)z�Serialize the macaroon in JSON format indicated by the version field.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon.
        r)�macaroon)�pymacaroonsr�version�MACAROON_V1�
_serialize_v1�
_serialize_v2)�self�mr�r�I/usr/lib/python3/dist-packages/pymacaroons/serializers/json_serializer.py�	serialize	s
zJsonSerializer.serializecCsHt�|j�|jd�}|jr$|j|d<|jr>dd�|jD�|d<t�|�S)z�Serialize the macaroon in JSON format v1.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon.
        )�
identifier�	signature�locationcSsg|]}t|��qSr)�_caveat_v1_to_dict��.0�caveatrrr
�
<listcomp>!sz0JsonSerializer._serialize_v1.<locals>.<listcomp>�caveats)rZconvert_to_stringrrrr�json�dumps�r
r�
serializedrrr
rs
�
�
zJsonSerializer._serialize_v1cCsZi}t|j|d�tt�|j�|d�|jr6|j|d<|jrPdd�|jD�|d<t�|�S)z�Serialize the macaroon in JSON format v2.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon in v2 format.
        �i�s�lcSsg|]}t|��qSr)�_caveat_v2_to_dictrrrr
r4sz0JsonSerializer._serialize_v2.<locals>.<listcomp>�c)	�_add_json_binary_fieldZidentifier_bytes�binasciiZ	unhexlifyZsignature_bytesrrrrrrrr
r	&s�
�
zJsonSerializer._serialize_v2cCs0t�|�}|�d�dkr"|�|�S|�|�SdS)z�Deserialize a JSON macaroon depending on the format.

        @param serialized the macaroon in JSON format.
        @return the macaroon object.
        rN)r�loads�get�_deserialize_v2�_deserialize_v1)r
r�deserializedrrr
�deserialize9s

zJsonSerializer.deserializecCs�ddlm}m}ddlm}g}|�dg�D]L}||d|�d�rPt�|d�nd|�d�rd|dnd|d	�}|�|�q,||�d
�|d||d|d
�S)z�Deserialize a JSON macaroon in v1 format.

        @param serialized the macaroon in v1 JSON format.
        @return the macaroon object.
        r)�Macaroonr��Caveatr�cid�vidN�cl��	caveat_id�verification_key_idrrrrr�rrrrr)	�pymacaroons.macaroonr)r�pymacaroons.caveatr+r$rZ
raw_b64decode�append)r
r'r)rr+rr rrrr
r&Es&��zJsonSerializer._deserialize_v1c	Cs�ddlm}m}ddlm}g}|�dg�D]0}|t|d�t|d�t|d�|d�}|�|�q,|t|d�t|d�|t�	t|d	��|d
�S)z�Deserialize a JSON macaroon v2.

        @param serialized the macaroon in JSON format v2.
        @return the macaroon object.
        r)r)�MACAROON_V2r*r r�vrr/rr2)
r3r)r6r4r+r$�_read_json_binary_fieldr5r"Zhexlify)r
r'r)r6r+rr rrrr
r%es(���zJsonSerializer._deserialize_v2N)
�__name__�
__module__�__qualname__�__doc__rrr	r(r&r%rrrr
rs rcCsLi}t|j�dkr|j|d<|jr8t�|j��d�|d<|jrH|j|d<|S)zT Return a caveat as a dictionary for export as the JSON
    macaroon v1 format.
    rr,�utf-8r-r.)�lenr0r1r�raw_urlsafe_b64encode�decoder�r rrrr
r�s
��
rcCsHi}t|j�dkr t|j|d�|jr4t|j|d�|jrD|j|d<|S)zT Return a caveat as a dictionary for export as the JSON
    macaroon v2 format.
    rrr7r)r>Zcaveat_id_bytesr!r1rrArrr
r�s
rcCsLz|�d�}|||<Wn0tk
rFt�|��d�}|||d<YnXdS)z� Set the given field to the given val (a bytearray) in the serialized
    dictionary.

    If the value isn't valid utf-8, we base64 encode it and use field+"64"
    as the field name.
    r=�64N)r@�UnicodeDecodeErrorrr?)�br�field�valrrr
r!�s
r!cCs@|�|�}|dk	rt�|�S|�|d�}|dkr6dSt�|�S)zJ Read the value of a JSON field that may be string or base64-encoded.
    NrB)r$rZconvert_to_bytesZraw_urlsafe_b64decode)r'rErFrrr
r8�s

r8)
r"rrr�objectrrrr!r8rrrr
�<module>sz