o
    Þý°j¯%  ã                   @  sÂ   d dl mZ d dlmZ d dlZd dl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 G d	d
„ d
ƒZG dd„ deƒZdddd„ZG dd„ deƒZddd„ZG dd„ dƒZdS ) é    )ÚannotationsNé   )Ú_base64_alphabet)Úbase64_decode)Úbase64_encode©Ú
want_bytes)ÚBadSignaturec                   @  s$   e Zd ZdZddd„Zdd
d„ZdS )ÚSigningAlgorithmzgSubclasses must implement :meth:`get_signature` to provide
    signature generation functionality.
    ÚkeyÚbytesÚvalueÚreturnc                 C  s   t ƒ ‚)z2Returns the signature for the given key and value.)ÚNotImplementedError©Úselfr   r   © r   ú†/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/itsdangerous/signer.pyÚget_signature   s   zSigningAlgorithm.get_signatureÚsigÚboolc                 C  s   t  ||  ||¡¡S )zMVerifies the given signature matches the expected
        signature.
        )ÚhmacÚcompare_digestr   )r   r   r   r   r   r   r   Úverify_signature   s   z!SigningAlgorithm.verify_signatureN©r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r
      s    
r
   c                   @  s   e Zd ZdZd	dd„ZdS )
ÚNoneAlgorithmz`Provides an algorithm that does not perform any signing and
    returns an empty signature.
    r   r   r   r   c                 C  s   dS )Nó    r   r   r   r   r   r   $   s   zNoneAlgorithm.get_signatureNr   )r   r   r   r   r   r   r   r   r   r      s    r   r    Ústringr   r   út.Anyc                 C  s
   t  | ¡S )zÈDon't access ``hashlib.sha1`` until runtime. FIPS builds may not include
    SHA-1, in which case the import and use as a default would fail before the
    developer can configure something else.
    )ÚhashlibÚsha1)r!   r   r   r   Ú
_lazy_sha1(   ó   
r%   c                   @  s8   e Zd ZU dZeeƒZded< dddd„Zddd„Z	dS )ÚHMACAlgorithmz*Provides signature generation using HMACs.r"   Údefault_digest_methodNÚdigest_methodc                 C  s   |d u r| j }|| _d S ©N)r(   r)   )r   r)   r   r   r   Ú__init__8   s   
zHMACAlgorithm.__init__r   r   r   r   c                 C  s   t j||| jd�}| ¡ S )N)ÚmsgÚ	digestmod)r   Únewr)   Údigest)r   r   r   Úmacr   r   r   r   >   s   zHMACAlgorithm.get_signaturer*   )r)   r"   r   )
r   r   r   r   Ústaticmethodr%   r(   Ú__annotations__r+   r   r   r   r   r   r'   0   s
   
 r'   Ú
secret_keyú7str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes]úlist[bytes]c                 C  s&   t | ttfƒrt| ƒgS dd„ | D ƒS )Nc                 S  s   g | ]}t |ƒ‘qS r   r   )Ú.0Úsr   r   r   Ú
<listcomp>I   s    z#_make_keys_list.<locals>.<listcomp>)Ú
isinstanceÚstrr   r   )r3   r   r   r   Ú_make_keys_listC   s   
r;   c                   @  s�   e Zd ZU dZeeƒZded< dZded< 								d,d-dd„Z	e
d.dd„ƒZd/d0dd„Zd1dd „Zd1d!d"„Zd2d%d&„Zd3d(d)„Zd4d*d+„Zd	S )5ÚSigneraÖ  A signer securely signs bytes, then unsigns them to verify that
    the value hasn't been changed.

    The secret key should be a random string of ``bytes`` and should not
    be saved to code or version control. Different salts should be used
    to distinguish signing in different contexts. See :doc:`/concepts`
    for information about the security of the secret key and salt.

    :param secret_key: The secret key to sign and verify with. Can be a
        list of keys, oldest to newest, to support key rotation.
    :param salt: Extra key to combine with ``secret_key`` to distinguish
        signatures in different contexts.
    :param sep: Separator between the signature and value.
    :param key_derivation: How to derive the signing key from the secret
        key and salt. Possible values are ``concat``, ``django-concat``,
        or ``hmac``. Defaults to :attr:`default_key_derivation`, which
        defaults to ``django-concat``.
    :param digest_method: Hash function to use when generating the HMAC
        signature. Defaults to :attr:`default_digest_method`, which
        defaults to :func:`hashlib.sha1`. Note that the security of the
        hash alone doesn't apply when used intermediately in HMAC.
    :param algorithm: A :class:`SigningAlgorithm` instance to use
        instead of building a default :class:`HMACAlgorithm` with the
        ``digest_method``.

    .. versionchanged:: 2.0
        Added support for key rotation by passing a list to
        ``secret_key``.

    .. versionchanged:: 0.18
        ``algorithm`` was added as an argument to the class constructor.

    .. versionchanged:: 0.14
        ``key_derivation`` and ``digest_method`` were added as arguments
        to the class constructor.
    r"   r(   údjango-concatr:   Údefault_key_derivationó   itsdangerous.Signeró   .Nr3   r4   Úsaltústr | bytes | NoneÚsepústr | bytesÚkey_derivationú
str | Noner)   út.Any | NoneÚ	algorithmúSigningAlgorithm | Nonec                 C  s†   t |ƒ| _t|ƒ| _| jtv rtdƒ‚|d urt|ƒ}nd}|| _|d u r(| j}|| _|d u r2| j	}|| _
|d u r>t| j
ƒ}|| _d S )NzŠThe given separator cannot be used because it may be contained in the signature itself. ASCII letters, digits, and '-_=' must not be used.r?   )r;   Úsecret_keysr   rC   r   Ú
ValueErrorrA   r>   rE   r(   r)   r'   rH   )r   r3   rA   rC   rE   r)   rH   r   r   r   r+   �   s&   


ÿ


zSigner.__init__r   r   c                 C  s
   | j d S )z�The newest (last) entry in the :attr:`secret_keys` list. This
        is for compatibility from before key rotation support was added.
        éÿÿÿÿ)rJ   )r   r   r   r   r3   ¯   r&   zSigner.secret_keyc                 C  s°   |du r
| j d }nt|ƒ}| jdkr!t t|  | j| ¡ ¡ ¡S | jdkr6t t|  | jd | ¡ ¡ ¡S | jdkrMt	j
|| jd�}| | j¡ | ¡ S | jdkrT|S td	ƒ‚)
aü  This method is called to derive the key. The default key
        derivation choices can be overridden here. Key derivation is not
        intended to be used as a security method to make a complex key
        out of a short password. Instead you should use large random
        secret keys.

        :param secret_key: A specific secret key to derive from.
            Defaults to the last item in :attr:`secret_keys`.

        .. versionchanged:: 2.0
            Added the ``secret_key`` parameter.
        NrL   Úconcatr=   s   signerr   )r-   ÚnonezUnknown key derivation method)rJ   r   rE   ÚtÚcastr   r)   rA   r/   r   r.   ÚupdateÚ	TypeError)r   r3   r0   r   r   r   Ú
derive_key¶   s    

ÿ

zSigner.derive_keyr   c                 C  s&   t |ƒ}|  ¡ }| j ||¡}t|ƒS )z*Returns the signature for the given value.)r   rS   rH   r   r   )r   r   r   r   r   r   r   r   ×   s   zSigner.get_signaturec                 C  s   t |ƒ}|| j |  |¡ S )zSigns the given string.)r   rC   r   )r   r   r   r   r   ÚsignÞ   s   zSigner.signr   r   c                 C  s^   zt |ƒ}W n
 ty   Y dS w t|ƒ}t| jƒD ]}|  |¡}| j |||¡r, dS qdS )z+Verifies the signature for the given value.FT)r   Ú	Exceptionr   ÚreversedrJ   rS   rH   r   )r   r   r   r3   r   r   r   r   r   ã   s   ÿ
ÿzSigner.verify_signatureÚsigned_valuec                 C  sZ   t |ƒ}| j|vrtd| j›d�ƒ‚| | jd¡\}}|  ||¡r#|S td|›d�|d�‚)zUnsigns the given string.zNo z found in valuer   z
Signature z does not match)Úpayload)r   rC   r	   Úrsplitr   )r   rW   r   r   r   r   r   Úunsignô   s   
zSigner.unsignc                 C  s&   z|   |¡ W dS  ty   Y dS w )znOnly validates the given signed value. Returns ``True`` if
        the signature exists and is valid.
        TF)rZ   r	   )r   rW   r   r   r   Úvalidate  s   
ÿzSigner.validate)r?   r@   NNN)r3   r4   rA   rB   rC   rD   rE   rF   r)   rG   rH   rI   )r   r   r*   )r3   rB   r   r   )r   rD   r   r   )r   rD   r   rD   r   r   )rW   rD   r   r   )rW   rD   r   r   )r   r   r   r   r1   r%   r(   r2   r>   r+   Úpropertyr3   rS   r   rT   r   rZ   r[   r   r   r   r   r<   L   s$   
 +ù.
!


r<   )r    )r!   r   r   r"   )r3   r4   r   r5   )Ú
__future__r   Úcollections.abcÚabcÚcabcr#   r   ÚtypingrO   Úencodingr   r   r   r   Úexcr	   r
   r   r%   r'   r;   r<   r   r   r   r   Ú<module>   s     	
	