o
    h9Ra(                     @   s  d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
mZ ddlmZmZ ddlZddlZddlmZ ddlmZmZ ded	efd
dZG dd dZG dd dZG dd deZG dd deZG dd deeZG dd deeZG dd dZeeeeje  eje < dS )zJOSE utilities.    N)
ModuleType)UnionAnyCallableIteratorTupleListcast)HashableMapping)crypto)ecrsafuncreturnc                 C   s   t t| S N)classmethodabcabstractmethod)r    r   -/usr/lib/python3/dist-packages/josepy/util.pyabstractclassmethod      r   c                   @   s   e Zd ZdZdeejejf ddfddZde	de
fdd	Zejfd
edefddZde
defddZdefddZde	fddZdS )ComparableX509zWrapper for OpenSSL.crypto.X509** objects that supports __eq__.

    :ivar wrapped: Wrapped certificate or certificate request.
    :type wrapped: `OpenSSL.crypto.X509` or `OpenSSL.crypto.X509Req`.

    wrappedr   Nc                 C   s&   t |tjst |tjsJ || _d S r   )
isinstancer   X509X509Reqr   selfr   r   r   r   __init__   s   
zComparableX509.__init__namec                 C      t | j|S r   )getattrr   r   r!   r   r   r   __getattr__      zComparableX509.__getattr__filetypec                 C   s*   t | jtjrt|| jS t|| jS )aN  Dumps the object into a buffer with the specified encoding.

        :param int filetype: The desired encoding. Should be one of
            `OpenSSL.crypto.FILETYPE_ASN1`,
            `OpenSSL.crypto.FILETYPE_PEM`, or
            `OpenSSL.crypto.FILETYPE_TEXT`.

        :returns: Encoded X509 object.
        :rtype: bytes

        )r   r   r   r   dump_certificatedump_certificate_request)r   r'   r   r   r   _dump"   s   zComparableX509._dumpotherc                 C   s    t || jstS |  | kS r   )r   	__class__NotImplementedr*   r   r+   r   r   r   __eq__4   s   zComparableX509.__eq__c                 C   s   t | j|  fS r   )hashr,   r*   r   r   r   r   __hash__:      zComparableX509.__hash__c                 C      d | jj| jS Nz<{0}({1!r})>)formatr,   __name__r   r1   r   r   r   __repr__=   r3   zComparableX509.__repr__)r7   
__module____qualname____doc__r   r   r   r   r    strr   r%   FILETYPE_ASN1intbytesr*   boolr/   r2   r8   r   r   r   r   r      s    r   c                   @   s   e Zd ZU dZeZeg ef ed< de	e
je
jejejf fddZdedefdd	Zd
edefddZdefddZdddZdS )ComparableKeyznComparable wrapper for ``cryptography`` keys.

    See https://github.com/pyca/cryptography/issues/2122.

    r2   r   c                 C   s
   || _ d S r   )_wrappedr   r   r   r   r    I   s   
zComparableKey.__init__r!   r   c                 C   r"   r   )r#   rB   r$   r   r   r   r%   Q   r&   zComparableKey.__getattr__r+   c                 C   s\   t || jr| jj|jjurtS t| jdr|  | kS t| jdr,|  | kS tS )Nprivate_numberspublic_numbers)r   r,   rB   r-   hasattrrC   rD   r.   r   r   r   r/   T   s   zComparableKey.__eq__c                 C   r4   r5   )r6   r,   r7   rB   r1   r   r   r   r8   `   r3   zComparableKey.__repr__c                 C   s(   t | jtjtjfr| S | | j S )zGet wrapped public key.)r   rB   r   RSAPublicKeyWithSerializationr   'EllipticCurvePublicKeyWithSerializationr,   
public_keyr1   r   r   r   rH   c   s
   
zComparableKey.public_keyN)r   rA   )r7   r9   r:   r;   r-   r2   r   r>   __annotations__r   r   RSAPrivateKeyWithSerializationrF   r   (EllipticCurvePrivateKeyWithSerializationrG   r    r<   r   r%   r@   r/   r8   rH   r   r   r   r   rA   A   s    
 
rA   c                   @      e Zd ZdZdefddZdS )ComparableRSAKeyzWrapper for ``cryptography`` RSA keys.

    Wraps around:

    - :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey`
    - :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey`

    r   c              	   C   st   t | jtjr"|  }|j}t| j|j|j	|j
|j|j|j|jfS t | jtjr7|  }t| j|j|jfS t r   )r   rB   r   rJ   rC   rD   r0   r,   pqdmp1dmq1iqmpnerF   NotImplementedErrorr   privpubr   r   r   r2   v   s   zComparableRSAKey.__hash__Nr7   r9   r:   r;   r>   r2   r   r   r   r   rM   l   s    	rM   c                   @   rL   )ComparableECKeyzWrapper for ``cryptography`` RSA keys.
    Wraps around:
    - :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey`
    - :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
    r   c                 C   sp   t | jtjr|  }|j}t| j|jj	|j
|j|jfS t | jtjr5|  }t| j|jj	|j
|jfS t r   )r   rB   r   rK   rC   rD   r0   r,   curver!   xyprivate_valuerG   rU   rV   r   r   r   r2      s   zComparableECKey.__hash__NrY   r   r   r   r   rZ      s    rZ   c                   @   s   e Zd ZU dZdZeedf ed< 	 deddfdd	Z	dedd fd
dZ
dedefddZdee fddZdefddZdefddZdededdfddZdefddZdS )ImmutableMapz5Immutable key to value mapping with attribute access.r   .	__slots__kwargsr   Nc                 K   s`   t |t | jkrtdd| j|rd|d| jD ]}t| ||| q!d S )NzA__init__() takes exactly the following arguments: {0} ({1} given), none)setr`   	TypeErrorr6   joinobject__setattr__pop)r   ra   slotr   r   r   r       s   
zImmutableMap.__init__c                 K   s   i | |}t | di |S )zReturn updated map.Nr   )type)r   ra   itemsr   r   r   update   s   zImmutableMap.updatekeyc                 C   s$   zt | |W S  ty   t|w r   )r#   AttributeErrorKeyErrorr   rn   r   r   r   __getitem__   
   zImmutableMap.__getitem__c                 C   
   t | jS r   )iterr`   r1   r   r   r   __iter__      
zImmutableMap.__iter__c                 C   rt   r   )lenr`   r1   r   r   r   __len__   rw   zImmutableMap.__len__c                    s   t t fdd jD S )Nc                 3   s    | ]}t  |V  qd S r   )r#   ).0rj   r1   r   r   	<genexpr>   s    z(ImmutableMap.__hash__.<locals>.<genexpr>)r0   tupler`   r1   r   r1   r   r2      s   zImmutableMap.__hash__r!   valuec                 C      t dNzcan't set attributero   r   r!   r}   r   r   r   rh         zImmutableMap.__setattr__c                 C   s$   d | jjddd |  D S )Nz{0}({1})rb   c                 s        | ]\}}d  ||V  qdS z	{0}={1!r}Nr6   rz   rn   r}   r   r   r   r{      s
    

z(ImmutableMap.__repr__.<locals>.<genexpr>)r6   r,   r7   rf   rl   r1   r   r   r   r8      s   
zImmutableMap.__repr__)r7   r9   r:   r;   r`   r   r<   rI   r   r    rm   rr   r   rv   r>   ry   r2   rh   r8   r   r   r   r   r_      s   
 	r_   c                   @   s   e Zd ZdZdZdededdfddZd	edefd
dZde	e fddZ
defddZdeeeef df fddZdefddZdedefddZdededdfddZdefddZdS )
frozendictzFrozen dictionary.)_items_keysargsra   r   Nc                 O   sh   |r	|s	t |}nt|dkrt|d tr|d }nt t| d| t| dtt|	  d S )N   r   r   r   )
dictrx   r   r   re   rg   rh   r|   sortedkeys)r   r   ra   rl   r   r   r   r       s   

zfrozendict.__init__rn   c                 C   s
   | j | S r   )r   rq   r   r   r   rr      rw   zfrozendict.__getitem__c                 C   rt   r   )ru   r   r1   r   r   r   rv      rw   zfrozendict.__iter__c                 C   rt   r   )rx   r   r1   r   r   r   ry      rw   zfrozendict.__len__.c                    s   t  fdd jD S )Nc                 3   s    | ]	}| | fV  qd S r   r   )rz   rn   r1   r   r   r{      s    z+frozendict._sorted_items.<locals>.<genexpr>)r|   r   r1   r   r1   r   _sorted_items   s   zfrozendict._sorted_itemsc                 C   s   t |  S r   )r0   r   r1   r   r   r   r2      r&   zfrozendict.__hash__r!   c                 C   s$   z| j | W S  ty   t|w r   )r   rp   ro   r$   r   r   r   r%      rs   zfrozendict.__getattr__r}   c                 C   r~   r   r   r   r   r   r   rh      r   zfrozendict.__setattr__c                 C   s   d ddd |  D S )Nzfrozendict({0})rb   c                 s   r   r   r   r   r   r   r   r{      s    z&frozendict.__repr__.<locals>.<genexpr>)r6   rf   r   r1   r   r   r   r8      s   
zfrozendict.__repr__)r7   r9   r:   r;   r`   r   r    r<   rr   r   rv   r>   ry   r   r   r2   r%   rh   r8   r   r   r   r   r      s    r   c                   @   sn   e Zd ZdZdeddfddZdedefdd	Zded
eddfddZ	deddfddZ
dee fddZdS )_UtilDeprecationModulez
    Internal class delegating to a module, and displaying warnings when attributes
    related to the deprecated "abstractclassmethod" attributes in the josepy.util module.
    moduler   Nc                 C   s   || j d< d S N_module)__dict__)r   r   r   r   r   r       r   z_UtilDeprecationModule.__init__attrc                 C   s$   |dkrt jdtdd t| j|S )Nr   zThe abstractclassmethod attribute in josepy.util is deprecated and will be removed soon. Please use the built-in decorators @classmethod and @abc.abstractmethod together instead.   )
stacklevel)warningswarnDeprecationWarningr#   r   r   r   r   r   r   r%     s
   z"_UtilDeprecationModule.__getattr__r}   c                 C   s   t | j|| d S r   )setattrr   )r   r   r}   r   r   r   rh   	  r3   z"_UtilDeprecationModule.__setattr__c                 C   s   t | j| d S r   )delattrr   r   r   r   r   __delattr__     z"_UtilDeprecationModule.__delattr__c                 C   s   dgt | j S r   )dirr   r1   r   r   r   __dir__  r   z_UtilDeprecationModule.__dir__)r7   r9   r:   r;   r   r    r<   r   r%   rh   r   r   r   r   r   r   r   r      s    r   )!r;   r   typesr   typingr   r   r   r   r   r   r	   collections.abcr
   r   sysr   OpenSSLr   )cryptography.hazmat.primitives.asymmetricr   r   r   r   r   rA   rM   rZ   r_   r   r   modulesr7   r   r   r   r   <module>   s$    $/+-2