U
    %YU                     @   sP   d dl Z d dlZd dlmZ G dd deZdd Zdd Zd	d
 Zdd Z	dS )    N)utilsc                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )JsonSerializerz8Serializer used to produce JSON macaroon format v1.
    c                 C   s,   ddl m} |j|jkr"| |S | |S )zSerialize 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    
zJsonSerializer.serializec                 C   sH   t |j|jd}|jr$|j|d< |jr>dd |jD |d< t|S )zSerialize the macaroon in JSON format v1.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon.
        )
identifier	signaturelocationc                 S   s   g | ]}t |qS r   )_caveat_v1_to_dict.0caveatr   r   r   
<listcomp>!   s    z0JsonSerializer._serialize_v1.<locals>.<listcomp>caveats)r   Zconvert_to_stringr   r   r   r   jsondumpsr
   r   
serializedr   r   r   r      s    


zJsonSerializer._serialize_v1c                 C   sZ   i }t |j|d t t|j|d |jr6|j|d< |jrPdd |jD |d< t|S )zSerialize the macaroon in JSON format v2.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon in v2 format.
        islc                 S   s   g | ]}t |qS r   )_caveat_v2_to_dictr   r   r   r   r   4   s    z0JsonSerializer._serialize_v2.<locals>.<listcomp>c)	_add_json_binary_fieldZidentifier_bytesbinasciiZ	unhexlifyZsignature_bytesr   r   r   r   r   r   r   r   r	   &   s     

zJsonSerializer._serialize_v2c                 C   s0   t |}|ddkr"| |S | |S dS )zDeserialize a JSON macaroon depending on the format.

        @param serialized the macaroon in JSON format.
        @return the macaroon object.
        r   N)r   loadsget_deserialize_v2_deserialize_v1)r
   r   deserializedr   r   r   deserialize9   s    

zJsonSerializer.deserializec                 C   s   ddl m}m} ddlm} g }|dg D ]L}||d |drPt|d nd|drd|d nd|d	}|| q,||d
|d ||d |dS )zDeserialize 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_idr   r   r   r   r   r   r   r   r   r   )	pymacaroons.macaroonr)   r   pymacaroons.caveatr+   r$   r   Zraw_b64decodeappend)r
   r'   r)   r   r+   r   r    r   r   r   r   r&   E   s&    zJsonSerializer._deserialize_v1c              	   C   s   ddl m}m} ddlm} g }|dg D ]0}|t|dt|dt|d|d}|| q,|t|dt|d|t	t|d	|d
S )zDeserialize a JSON macaroon v2.

        @param serialized the macaroon in JSON format v2.
        @return the macaroon object.
        r   )r)   MACAROON_V2r*   r    r   vr   r/   r   r2   )
r3   r)   r6   r4   r+   r$   _read_json_binary_fieldr5   r"   Zhexlify)r
   r'   r)   r6   r+   r   r    r   r   r   r   r%   e   s(    zJsonSerializer._deserialize_v2N)
__name__
__module____qualname____doc__r   r   r	   r(   r&   r%   r   r   r   r   r      s    r   c                 C   sL   i }t | jdkr| j|d< | jr8t| jd|d< | jrH| j|d< |S )zT Return a caveat as a dictionary for export as the JSON
    macaroon v1 format.
    r   r,   utf-8r-   r.   )lenr0   r1   r   raw_urlsafe_b64encodedecoder   r    r   r   r   r   r      s    

r   c                 C   sH   i }t | jdkr t| j|d | jr4t| j|d | jrD| j|d< |S )zT Return a caveat as a dictionary for export as the JSON
    macaroon v2 format.
    r   r   r7   r   )r>   Zcaveat_id_bytesr!   r1   r   rA   r   r   r   r      s    
r   c                 C   sL   z|  d}|||< W n0 tk
rF   t|  d}|||d < Y nX dS )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@   UnicodeDecodeErrorr   r?   )br   fieldvalr   r   r   r!      s    
r!   c                 C   s@   |  |}|dk	rt|S |  |d }|dkr6dS t|S )zJ Read the value of a JSON field that may be string or base64-encoded.
    NrB   )r$   r   Zconvert_to_bytesZraw_urlsafe_b64decode)r'   rE   rF   r   r   r   r8      s    

r8   )
r"   r   r   r   objectr   r   r   r!   r8   r   r   r   r   <module>   s   z