U
    %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   sd   |t krt }|| _|pg | _|p"d| _|p,d| _|p6d| _t | _t | _	|r`t
t|| j| _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d S )Nz Must supply serialized macaroon.)r   deserializer   )clsZ
serialized
serializerr   r   r   r   +   s    

zMacaroon.deserializec                 C   s
   t | jS N)r   	_locationr   r   r   r   r   5   s    zMacaroon.locationc                 C   s   t || _d S r"   )r   r#   r   Zstring_or_bytesr   r   r   r   9   s    c                 C   s   | j S r"   )r   r$   r   r   r   r   =   s    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   s   | j S r"   )r'   r$   r   r   r   r   G   s    zMacaroon.identifier_bytesc                 C   s   t || _d S r"   )r   r'   r%   r   r   r   r   K   s    c                 C   s
   t | jS r"   )r   
_signaturer$   r   r   r   r   O   s    zMacaroon.signaturec                 C   s   t || _d S r"   )r   r(   r%   r   r   r   r   S   s    c                 C   s   | j S r"   )r(   r$   r   r   r   signature_bytesW   s    zMacaroon.signature_bytesc                 C   s
   t | S r"   )copydeepcopyr$   r   r   r   r*   [   s    zMacaroon.copyc                 C   s   |pt  }|| S r"   )r   	serialize)r   r!   r   r   r   r,   ^   s    
zMacaroon.serializec              	   C   s   dj | jd}z|dj | jd7 }W n. tk
rR   |dj tt| jd7 }Y nX | jD ]}z|dj t|jd7 }W n. tk
r   |dj tt|j	d7 }Y nX |j
rZ|jrZtt|j
}|d	j |d
7 }|dj |jd7 }qZ|dj | jd7 }|S )Nzlocation {loc}
)Z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caveatr.   r   r   r   inspectb   s0    
zMacaroon.inspectc                 C   s   dd | j D S )Nc                 S   s   g | ]}|  r|qS r   )Zfirst_party.0r1   r   r   r   
<listcomp>|   s      z0Macaroon.first_party_caveats.<locals>.<listcomp>r   r$   r   r   r   first_party_caveats{   s    zMacaroon.first_party_caveatsc                 C   s   dd | j D S )Nc                 S   s   g | ]}|  r|qS r   )Zthird_partyr3   r   r   r   r5      s      z0Macaroon.third_party_caveats.<locals>.<listcomp>r6   r$   r   r   r   third_party_caveats~   s    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
        )r*   r   Zbind)r   Zdischarge_macaroonZ	protectedr   r   r   prepare_for_request   s    	zMacaroon.prepare_for_requestc                 K   s   | j j| |f|S r"   )r   add_first_party_caveat)r   Z	predicatekwargsr   r   r   r:      s     zMacaroon.add_first_party_caveatc                 K   s   | j j| |||f|S r"   )r   add_third_party_caveat)r   r   r   Zkey_idr;   r   r   r   r<      s       zMacaroon.add_third_party_caveat)N)N)__name__
__module____qualname__r&   r   classmethodr   propertyr   setterr   r   r   r   r)   r*   r,   r2   r7   r8   r9   r:   r<   r   r   r   r   r      sF   
	









r   )Z
__future__r   r*   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   