B
    -[                 @   s   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 ddl	m
Z
 G d	d
 d
eZG dd deZG dd deZG dd deZdS )    N   )constant_time_compare)_base64_alphabet)base64_decode)base64_encode)
want_bytes)BadSignaturec               @   s    e Zd ZdZdd Zdd ZdS )SigningAlgorithmzgSubclasses must implement :meth:`get_signature` to provide
    signature generation functionality.
    c             C   s
   t  dS )z2Returns the signature for the given key and value.N)NotImplementedError)selfkeyvalue r   2lib/python3.7/site-packages/itsdangerous/signer.pyget_signature   s    zSigningAlgorithm.get_signaturec             C   s   t || ||S )zMVerifies the given signature matches the expected
        signature.
        )r   r   )r   r   r   sigr   r   r   verify_signature   s    z!SigningAlgorithm.verify_signatureN)__name__
__module____qualname____doc__r   r   r   r   r   r   r	      s   r	   c               @   s   e Zd ZdZdd ZdS )NoneAlgorithmz`Provides an algorithm that does not perform any signing and
    returns an empty signature.
    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      s   r   c               @   s,   e Zd ZdZeejZdddZdd Z	dS )HMACAlgorithmz*Provides signature generation using HMACs.Nc             C   s   |d kr| j }|| _d S )N)default_digest_methoddigest_method)r   r   r   r   r   __init__-   s    zHMACAlgorithm.__init__c             C   s   t j||| jd}| S )N)msg	digestmod)hmacnewr   digest)r   r   r   macr   r   r   r   2   s    zHMACAlgorithm.get_signature)N)
r   r   r   r   staticmethodhashlibsha1r   r   r   r   r   r   r   r   %   s   

r   c               @   sX   e Zd ZdZeejZ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 )Signera  This class can sign and unsign bytes, validating the signature
    provided.

    Salt can be used to namespace the hash, so that a signed string is
    only valid for a given namespace. Leaving this at the default value
    or re-using a salt value across different parts of your application
    where the same signed value in one part can mean something different
    in another part is a security risk.

    See :ref:`the-salt` for an example of what the salt is doing and how
    you can utilize it.

    .. versionadded:: 0.14
        ``key_derivation`` and ``digest_method`` were added as arguments
        to the class constructor.

    .. versionadded:: 0.18
        ``algorithm`` was added as an argument to the class constructor.
    zdjango-concatN.c             C   s|   t || _t || _| jtkr&td|d kr2dn|| _|d krF| j}|| _|d krZ| j}|| _	|d krrt
| j	}|| _d S )NzThe given separator cannot be used because it may be contained in the signature itself. Alphanumeric characters and `-_=` must not be used.zitsdangerous.Signer)r   
secret_keysepr   
ValueErrorsaltdefault_key_derivationkey_derivationr   r   r   	algorithm)r   r(   r+   r)   r-   r   r.   r   r   r   r   [   s    	



zSigner.__init__c             C   s   t | j}| jdkr(| || j  S | jdkrJ| |d | j  S | jdkrxtj| j| jd}|| | S | jdkr| j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.
        concatzdjango-concats   signerr   )r   ZnonezUnknown key derivation methodN)
r   r+   r-   r   r(   r!   r   r    update	TypeError)r   r+   r"   r   r   r   
derive_keyw   s    





zSigner.derive_keyc             C   s&   t |}|  }| j||}t|S )z*Returns the signature for the given value.)r   r2   r.   r   r   )r   r   r   r   r   r   r   r      s    zSigner.get_signaturec             C   s   t |t | j | | S )zSigns the given string.)r   r)   r   )r   r   r   r   r   sign   s    zSigner.signc             C   s:   |   }yt|}W n tk
r(   dS X | j|||S )z+Verifies the signature for the given value.F)r2   r   	Exceptionr.   r   )r   r   r   r   r   r   r   r      s    zSigner.verify_signaturec             C   s\   t |}t | j}||kr(td| j ||d\}}| ||rH|S td| |ddS )zUnsigns the given string.zNo %r found in valuer   zSignature %r does not match)ZpayloadN)r   r)   r   rsplitr   )r   signed_valuer)   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)r7   r   )r   r6   r   r   r   validate   s
    
zSigner.validate)Nr'   NNN)r   r   r   r   r#   r$   r%   r   r,   r   r2   r   r3   r   r7   r8   r   r   r   r   r&   7   s   
    
	r&   )r$   r   Z_compatr   encodingr   r   r   r   excr   objectr	   r   r   r&   r   r   r   r   <module>   s   	