B
    ÜÇ‡b—$  ã               @   sÖ   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
 ejeef Zeje Zejeje ef ZG dd	„ d	ƒZG d
d„ deƒZG dd„ deƒZeeje dœdd„ZG dd„ dƒZdS )é    Né   )Ú_base64_alphabet)Úbase64_decode)Úbase64_encode)Ú
want_bytes)ÚBadSignaturec               @   s6   e Zd ZdZeeedœdd„Zeeeedœdd„ZdS )	ÚSigningAlgorithmzgSubclasses must implement :meth:`get_signature` to provide
    signature generation functionality.
    )ÚkeyÚvalueÚreturnc             C   s
   t ƒ ‚dS )z2Returns the signature for the given key and value.N)ÚNotImplementedError)Úselfr	   r
   © r   úg/var/www/downstreamdata.science/rtclock/rtclock-venv/lib/python3.7/site-packages/itsdangerous/signer.pyÚget_signature   s    zSigningAlgorithm.get_signature)r	   r
   Úsigr   c             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)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úbytesr   Úboolr   r   r   r   r   r      s   r   c               @   s"   e Zd ZdZeeedœdd„ZdS )ÚNoneAlgorithmz`Provides an algorithm that does not perform any signing and
    returns an empty signature.
    )r	   r
   r   c             C   s   dS )Nó    r   )r   r	   r
   r   r   r   r   %   s    zNoneAlgorithm.get_signatureN)r   r   r   r   r   r   r   r   r   r   r       s   r   c               @   sJ   e Zd ZU dZeejƒZej	e
d< d
ej	dœdd„Zeeedœdd	„ZdS )ÚHMACAlgorithmz*Provides signature generation using HMACs.Údefault_digest_methodN)Údigest_methodc             C   s   |d kr| j }|| _d S )N)r   r   )r   r   r   r   r   Ú__init__1   s    zHMACAlgorithm.__init__)r	   r
   r   c             C   s   t j||| jd}| ¡ S )N)ÚmsgÚ	digestmod)r   Únewr   Údigest)r   r	   r
   Úmacr   r   r   r   7   s    zHMACAlgorithm.get_signature)N)r   r   r   r   ÚstaticmethodÚhashlibÚsha1r   Ú_tÚAnyÚ__annotations__r    r   r   r   r   r   r   r   )   s   
r   )Ú
secret_keyr   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>@   s    z#_make_keys_list.<locals>.<listcomp>)Ú
isinstanceÚstrr   r   )r,   r   r   r   Ú_make_keys_list<   s    
r2   c               @   sÖ   e Zd ZU dZeejƒZej	e
d< dZee
d< deeeeje ejej	 eje dœd	d
„Zeedœdd„ƒZdeedœdd„Zeedœdd„Zeedœdd„Zeeedœdd„Zeedœdd„Zeedœdd„ZdS ) Ú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   zdjango-concatÚdefault_key_derivationó   itsdangerous.Signeró   .N)r,   ÚsaltÚsepÚkey_derivationr   Ú	algorithmc             C   s†   t |ƒ| _t|ƒ| _| jtkr&tdƒ‚|d k	r8t|ƒ}nd}|| _|d krP| j}|| _|d krd| j	}|| _
|d k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.s   itsdangerous.Signer)r2   Úsecret_keysr   r8   r   Ú
ValueErrorr7   r4   r9   r   r   r   r:   )r   r,   r7   r8   r9   r   r:   r   r   r   r    x   s$    




zSigner.__init__)r   c             C   s
   | j d S )zThe newest (last) entry in the :attr:`secret_keys` list. This
        is for compatibility from before key rotation support was added.
        éÿÿÿÿ)r;   )r   r   r   r   r,   ¦   s    zSigner.secret_key)r,   r   c             C   s´   |dkr| j d }nt|ƒ}| jdkrBt t|  | j| ¡ ¡ ¡S | jdkrlt t|  | jd | ¡ ¡ ¡S | jdkršt	j
|| jd}| | j¡ | ¡ S | jdkr¨|S td	ƒ‚dS )
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.
        Nr=   Úconcatzdjango-concats   signerr   )r"   ÚnonezUnknown key derivation method)r;   r   r9   r)   Úcastr   r   r7   r$   r   r#   ÚupdateÚ	TypeError)r   r,   r%   r   r   r   Ú
derive_key­   s    



zSigner.derive_key)r
   r   c             C   s&   t |ƒ}|  ¡ }| j ||¡}t|ƒS )z*Returns the signature for the given value.)r   rC   r:   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   r8   r   )r   r
   r   r   r   ÚsignÕ   s    zSigner.sign)r
   r   r   c             C   s`   yt |ƒ}W n tk
r    dS X t|ƒ}x0t| jƒD ]"}|  |¡}| j |||¡r6dS q6W dS )z+Verifies the signature for the given value.FT)r   Ú	Exceptionr   Úreversedr;   rC   r:   r   )r   r
   r   r,   r	   r   r   r   r   Ú   s    
zSigner.verify_signature)Úsigned_valuer   c             C   s^   t |ƒ}| j|kr$td| j›dƒ‚| | jd¡\}}|  ||¡rF|S td|›d|d‚dS )zUnsigns the given string.zNo z found in valuer   z
Signature z does not match)ÚpayloadN)r   r8   r   Úrsplitr   )r   rG   r
   r   r   r   r   Úunsignë   s    
zSigner.unsignc             C   s(   y|   |¡ dS  tk
r"   dS X dS )znOnly validates the given signed value. Returns ``True`` if
        the signature exists and is valid.
        TFN)rJ   r   )r   rG   r   r   r   Úvalidateù   s
    
zSigner.validate)r5   r6   NNN)N)r   r   r   r   r&   r'   r(   r   r)   r*   r+   r4   r1   Ú_t_secret_keyÚ_t_opt_str_bytesÚ_t_str_bytesÚOptionalr   r    Úpropertyr   r,   rC   r   rD   r   r   rJ   rK   r   r   r   r   r3   C   s    
$    .'!r3   )r'   r   Útypingr)   Úencodingr   r   r   r   Úexcr   ÚUnionr1   r   rN   rO   rM   ÚIterablerL   r   r   r   ÚListr2   r3   r   r   r   r   Ú<module>   s   
	