3
%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|j|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 )N)r   r"   )r   string_or_bytesr   r   r   r   9   s    c             C   s   | j S )N)r   )r   r   r   r   r   =   s    zMacaroon.versionc             C   s   | j tkrt| jS | jS )N)r   MACAROON_V1r   _identifier)r   r   r   r   r   A   s    

zMacaroon.identifierc             C   s   | j S )N)r%   )r   r   r   r   r   G   s    zMacaroon.identifier_bytesc             C   s   t || _d S )N)r   r%   )r   r#   r   r   r   r   K   s    c             C   s
   t | jS )N)r   
_signature)r   r   r   r   r   O   s    zMacaroon.signaturec             C   s   t || _d S )N)r   r&   )r   r#   r   r   r   r   S   s    c             C   s   | j S )N)r&   )r   r   r   r   signature_bytesW   s    zMacaroon.signature_bytesc             C   s
   t j| S )N)copydeepcopy)r   r   r   r   r(   [   s    zMacaroon.copyc             C   s   |pt  }|j| S )N)r   	serialize)r   r!   r   r   r   r*   ^   s    
zMacaroon.serializec             C   s   dj | jd}y|dj | jd7 }W n. tk
rR   |dj tt| jd7 }Y nX x| jD ]}y|dj t|jd7 }W n. tk
r   |dj tt|j	d7 }Y nX |j
r\|jr\tt|j
}|d	j |d
7 }|dj |jd7 }q\W |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caveatr-   r   r   r   inspectb   s(    zMacaroon.inspectc             C   s   dd | j D S )Nc             S   s   g | ]}|j  r|qS r   )Zfirst_party).0r0   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 | ]}|j  r|qS r   )Zthird_party)r2   r0   r   r   r   r3      s    z0Macaroon.third_party_caveats.<locals>.<listcomp>)r   )r   r   r   r   third_party_caveats~   s    zMacaroon.third_party_caveatsc             C   s   |j  }t| j|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 )N)r   add_first_party_caveat)r   Z	predicatekwargsr   r   r   r7      s    zMacaroon.add_first_party_caveatc             K   s   | j j| |||f|S )N)r   add_third_party_caveat)r   r   r   Zkey_idr8   r   r   r   r9      s    zMacaroon.add_third_party_caveat)N)N)__name__
__module____qualname__r$   r   classmethodr   propertyr   setterr   r   r   r   r'   r(   r*   r1   r4   r5   r6   r7   r9   r   r   r   r   r      s2   	
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   