o
    %Ya                     @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZ d dlmZmZ d	Zd
ZG dd deZdS )    )unicode_literalsN)standard_b64encode)HashSignaturesBinder)BinarySerializer)MacaroonInitException)convert_to_bytesconvert_to_stringcreate_initial_signature)FirstPartyCaveatDelegateThirdPartyCaveatDelegate      c                   @   s   e Zd ZdddddefddZed%ddZedd Zej	dd Zed	d
 Z
edd Zedd Zej	dd Zedd Zej	dd Zedd Zdd Zd%ddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ ZdS )&MacaroonNc                 C   sh   |t krt }|| _|pg | _|pd| _|pd| _|pd| _t | _t | _	|r2t
t|| j| _d S d S )N )MACAROON_V2_versioncaveatslocation
identifier	signaturer
   first_party_caveat_delegater   third_party_caveat_delegater	   r   identifier_bytes)selfr   r   keyr   r   version r   6/usr/lib/python3/dist-packages/pymacaroons/macaroon.py__init__   s   





zMacaroon.__init__c                 C   s    |pt  }|r||S td)Nz Must supply serialized macaroon.)r   deserializer   )clsZ
serialized
serializerr   r   r   r   +   s   

zMacaroon.deserializec                 C   
   t | jS N)r   	_locationr   r   r   r   r   5      
zMacaroon.locationc                 C      t || _d S r#   )r   r$   r   Zstring_or_bytesr   r   r   r   9      c                 C      | j S r#   )r   r%   r   r   r   r   =      zMacaroon.versionc                 C   s   | j tkr
t| jS | jS r#   )r   MACAROON_V1r   _identifierr%   r   r   r   r   A   s   

zMacaroon.identifierc                 C   r*   r#   )r-   r%   r   r   r   r   G   r+   zMacaroon.identifier_bytesc                 C   r'   r#   )r   r-   r(   r   r   r   r   K   r)   c                 C   r"   r#   )r   
_signaturer%   r   r   r   r   O   r&   zMacaroon.signaturec                 C   r'   r#   )r   r.   r(   r   r   r   r   S   r)   c                 C   r*   r#   )r.   r%   r   r   r   signature_bytesW   r+   zMacaroon.signature_bytesc                 C   s
   t | S r#   )copyZdeepcopyr%   r   r   r   r0   [   s   
zMacaroon.copyc                 C   s   |pt  }|| S r#   )r   	serialize)r   r!   r   r   r   r1   ^   s   

zMacaroon.serializec              	   C   s   dj | jd}z|dj | jd7 }W n ty(   |dj tt| jd7 }Y nw | jD ]D}z|dj t|jd7 }W n tyQ   |dj tt|j	d7 }Y nw |j
rp|jrptt|j
}|d	j |d
7 }|dj |jd7 }q,|dj | jd7 }|S )Nzlocation {loc}
)loczidentifier {id}
)idzidentifier64 {id}
z
cid {cid}
)Zcidzcid64 {cid}
z
vid {vid}
)vidzcl {cl}
)Zclzsignature {sig})Zsig)formatr   r   UnicodeEncodeErrorr   r   r   r   Z	caveat_idZcaveat_id_bytesZverification_key_idr   )r   Zcombinedcaveatr4   r   r   r   inspectb   s6   
zMacaroon.inspectc                 C      dd | j D S )Nc                 S      g | ]}|  r|qS r   )Zfirst_party.0r7   r   r   r   
<listcomp>|       z0Macaroon.first_party_caveats.<locals>.<listcomp>r   r%   r   r   r   first_party_caveats{      zMacaroon.first_party_caveatsc                 C   r9   )Nc                 S   r:   r   )Zthird_partyr;   r   r   r   r=      r>   z0Macaroon.third_party_caveats.<locals>.<listcomp>r?   r%   r   r   r   third_party_caveats~   rA   zMacaroon.third_party_caveatsc                 C   s   |  }t| |S )a$   Return a new discharge macaroon bound to the receiving macaroon's
        current signature so that it can be used in a request.

        This must be done before a discharge macaroon is sent to a server.

        :param discharge_macaroon:
        :return: bound discharge macaroon
        )r0   r   Zbind)r   Zdischarge_macaroonZ	protectedr   r   r   prepare_for_request   s   	zMacaroon.prepare_for_requestc                 K   s   | j j| |fi |S r#   )r   add_first_party_caveat)r   Z	predicatekwargsr   r   r   rD      s
   zMacaroon.add_first_party_caveatc                 K   s   | j j| |||fi |S r#   )r   add_third_party_caveat)r   r   r   Zkey_idrE   r   r   r   rF      s
   zMacaroon.add_third_party_caveatr#   )__name__
__module____qualname__r,   r   classmethodr   propertyr   setterr   r   r   r   r/   r0   r1   r8   r@   rB   rC   rD   rF   r   r   r   r   r      sH    
	









r   )Z
__future__r   r0   base64r   Zpymacaroons.bindersr   Z)pymacaroons.serializers.binary_serializerr   Zpymacaroons.exceptionsr   Zpymacaroons.utilsr   r   r	   Zpymacaroons.caveat_delegatesr
   r   r,   r   objectr   r   r   r   r   <module>   s    