
    Ǆg%                        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
      Z G d de      ZdddZ G d de      Z	 	 	 	 ddZ G d d      Zy)    )annotationsN   )_base64_alphabet)base64_decode)base64_encode)
want_bytes)BadSignaturec                       e Zd ZdZddZddZy)SigningAlgorithmzgSubclasses must implement :meth:`get_signature` to provide
    signature generation functionality.
    c                    t               )z2Returns the signature for the given key and value.)NotImplementedErrorselfkeyvalues      [/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/itsdangerous/signer.pyget_signaturezSigningAlgorithm.get_signature   s    !##    c                N    t        j                  || j                  ||            S )zMVerifies the given signature matches the expected
        signature.
        )hmaccompare_digestr   )r   r   r   sigs       r   verify_signaturez!SigningAlgorithm.verify_signature   s$     ""3(:(:3(FGGr   Nr   bytesr   r   returnr   )r   r   r   r   r   r   r   bool)__name__
__module____qualname____doc__r   r    r   r   r   r      s    $Hr   r   c                      e Zd ZdZddZy)NoneAlgorithmz`Provides an algorithm that does not perform any signing and
    returns an empty signature.
    c                     y)Nr   r"   r   s      r   r   zNoneAlgorithm.get_signature$   s    r   Nr   )r   r   r    r!   r   r"   r   r   r$   r$      s    r   r$   c                ,    t        j                  |       S )zDon'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)strings    r   
_lazy_sha1r*   (   s    
 <<r   c                  >    e Zd ZU dZ ee      Zded<   dddZd	dZ	y)
HMACAlgorithmz*Provides signature generation using HMACs.t.Anydefault_digest_methodNc                .    || j                   }|| _        y N)r.   digest_method)r   r1   s     r   __init__zHMACAlgorithm.__init__8   s      66M$1r   c                f    t        j                  ||| j                        }|j                         S )N)msg	digestmod)r   newr1   digest)r   r   r   macs       r   r   zHMACAlgorithm.get_signature>   s&    hhs1C1CDzz|r   r0   )r1   r-   r   )
r   r   r    r!   staticmethodr*   r.   __annotations__r2   r   r"   r   r   r,   r,   0   s!    4
 $0
#;5;2r   r,   c                    t        | t        t        f      rt        |       gS | D cg c]  }t        |       c}S c c}w r0   )
isinstancestrr   r   )
secret_keyss     r   _make_keys_listr@   C   s8     *sEl+:&''#-.aJqM...s   <c                      e Zd ZU dZ ee      Zded<   dZded<   	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	e
dd	       Zddd
ZddZddZddZddZddZy)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_derivationNc                <   t        |      | _        t        |      | _        | j                  t        v rt        d      |t        |      }nd}|| _        || j                  }|| _        || j                  }|| _
        |t        | j                        }|| _        y )NzThe given separator cannot be used because it may be contained in the signature itself. ASCII letters, digits, and '-_=' must not be used.   itsdangerous.Signer)r@   secret_keysr   sepr   
ValueErrorsaltrD   key_derivationr.   r1   r,   	algorithm)r   r>   rJ   rH   rK   r1   rL   s          r   r2   zSigner.__init__   s     )8
(C$S/88''7  d#D)D	!!88N#1  66M$1%d&8&89I+4r   c                     | j                   d   S )zThe newest (last) entry in the :attr:`secret_keys` list. This
        is for compatibility from before key rotation support was added.
        )rG   )r   s    r   r>   zSigner.secret_key   s    
 ##r   c                |   || j                   d   }nt        |      }| j                  dk(  rDt        j                  t
        | j                  | j                  |z         j                               S | j                  dk(  rGt        j                  t
        | j                  | j                  dz   |z         j                               S | j                  dk(  rLt        j                  || j                        }|j                  | j                         |j                         S | j                  dk(  r|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.
        rN   concatrC   s   signerr   )r5   nonezUnknown key derivation method)rG   r   rK   tcastr   r1   rJ   r7   r   r6   update	TypeError)r   r>   r8   s      r   
derive_keyzSigner.derive_key   s    ))"-J#J/J(*66%!3!3DII
4J!K!R!R!TUU  O366t))$))i*?**LMTTV    F*((:1C1CDCJJtyy!::<  F*;<<r   c                    t        |      }| j                         }| j                  j                  ||      }t	        |      S )z*Returns the signature for the given value.)r   rV   rL   r   r   )r   r   r   r   s       r   r   zSigner.get_signature   s9    5!oonn**36S!!r   c                Z    t        |      }|| j                  z   | j                  |      z   S )zSigns the given string.)r   rH   r   )r   r   s     r   signzSigner.sign   s+    5!txx$"4"4U";;;r   c                    	 t        |      }t        |      }t        | j                        D ]2  }| j                  |      }| j                  j                  |||      s2 y y# t        $ r Y yw xY w)z+Verifies the signature for the given value.FT)r   	Exceptionr   reversedrG   rV   rL   r   )r   r   r   r>   r   s        r   r   zSigner.verify_signature   sw    	$C 5!"4#3#34 	J//*-C~~..sE3?		   		s   A# #	A/.A/c                    t        |      }| j                  |vrt        d| j                  d      |j                  | j                  d      \  }}| j	                  ||      r|S t        d|d|      )zUnsigns the given string.zNo z found in valuer   z
Signature z does not match)payload)r   rH   r	   rsplitr   )r   signed_valuer   r   s       r   unsignzSigner.unsign   su    !,/88<'TXXL@AA!((15
s  ,LZwo>NNr   c                F    	 | j                  |       y# t        $ r Y yw xY w)znOnly validates the given signed value. Returns ``True`` if
        the signature exists and is valid.
        TF)ra   r	   )r   r`   s     r   validatezSigner.validate  s(    	KK% 		s    	  )rF      .NNN)r>   7str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes]rJ   str | bytes | NonerH   str | bytesrK   z
str | Noner1   zt.Any | NonerL   zSigningAlgorithm | None)r   r   r0   )r>   rf   r   r   )r   rg   r   r   )r   rg   r   rg   r   r   )r`   rg   r   r   )r`   rg   r   r   )r   r   r    r!   r9   r*   r.   r:   rD   r2   propertyr>   rV   r   rY   r   ra   rc   r"   r   r   rB   rB   L   s    #V $0
#;5; #2C1
 $:%)&*-1,5K,5 !,5 	,5
 #,5 $,5 +,5\ $ $=B"<
"Or   rB   )r   )r)   r   r   r-   )r>   re   r   zlist[bytes])
__future__r   collections.abcabccabcr'   r   typingrR   encodingr   r   r   r   excr	   r   r$   r*   r,   r@   rB   r"   r   r   <module>rp      so    "     & # #   H H $  $ &/G//~ ~r   