U
    d>                     @  s   d dl mZ d dl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 d	d
lmZ d	dlmZ ejrd dlZG dd dZdddddddZG dd dZdd ZdD ]*ZeeeeZeeee eeee qdS )    )annotationsNwraps   )dump_header)parse_dict_header)parse_set_header)quote_header_value   )CallbackDict)	HeaderSetc                   @  s   e Zd ZdZd%ddddddd	Zddd
ddZddd
ddZd&ddddddZdddddZdddddZ	e
dddddZdddd Zddd!d"Zddd#d$ZdS )'Authorizationa  Represents the parts of an ``Authorization`` request header.

    :attr:`.Request.authorization` returns an instance if the header is set.

    An instance can be used with the test :class:`.Client` request methods' ``auth``
    parameter to send the header in test requests.

    Depending on the auth scheme, either :attr:`parameters` or :attr:`token` will be
    set. The ``Basic`` scheme's token is decoded into the ``username`` and ``password``
    parameters.

    For convenience, ``auth["key"]`` and ``auth.key`` both access the key in the
    :attr:`parameters` dict, along with ``auth.get("key")`` and ``"key" in auth``.

    .. versionchanged:: 2.3
        The ``token`` parameter and attribute was added to support auth schemes that use
        a token instead of parameters, such as ``Bearer``.

    .. versionchanged:: 2.3
        The object is no longer a ``dict``.

    .. versionchanged:: 0.5
        The object is an immutable dict.
    Nstrdict[str, str] | None
str | NoneNone)	auth_typedatatokenreturnc                 C  s"   || _ |d kri }|| _|| _d S N)type
parametersr   )selfr   r   r    r   @/tmp/pip-unpacked-wheel-m99gisyz/werkzeug/datastructures/auth.py__init__.   s    zAuthorization.__init__namer   c                 C  s   | j |S r   r   getr   r   r   r   r   __getattr__F   s    zAuthorization.__getattr__c                 C  s   | j |S r   r   r!   r   r   r   __getitem__I   s    zAuthorization.__getitem__keydefaultr   c                 C  s   | j ||S r   r   r   r%   r&   r   r   r   r    L   s    zAuthorization.getboolr%   r   c                 C  s
   || j kS r   r   r   r%   r   r   r   __contains__O   s    zAuthorization.__contains__objectotherr   c                 C  s2   t |tstS |j| jko0|j| jko0|j| jkS r   )
isinstancer   NotImplementedr   r   r   r   r/   r   r   r   __eq__R   s    


zAuthorization.__eq__te.Self | Nonevaluer   c              	   C  s   |sdS | d\}}}| }| }|dkr|zt|  d\}}}W n tjtfk
rj   Y dS X | |||dS d|	dkr| |t
|dS | |d|S )zParse an ``Authorization`` header value and return an instance, or ``None``
        if the value is empty.

        :param value: The header value to parse.

        .. versionadded:: 2.3
        N basic:)usernamepassword=)	partitionlowerstripbase64	b64decodedecodebinasciiErrorUnicodeErrorrstripr   )clsr6   scheme_restr:   r;   r   r   r   from_header\   s    	zAuthorization.from_headerr   c                 C  sp   | j dkr6t| j d| j  d}d| S | jdk	rV| j   d| j S | j   dt	| j
 S )ziProduce an ``Authorization`` header value representing this data.

        .. versionadded:: 2.0
        r8   r9   utf8zBasic Nr7   )r   r@   	b64encoder:   r;   encoderB   r   titler   r   r   r6   r   r   r   	to_header{   s    


zAuthorization.to_headerc                 C  s   |   S r   rR   r   r   r   r   __str__   s    zAuthorization.__str__c                 C  s   dt | j d|   dS N<r7   >r   __name__rR   rT   r   r   r   __repr__   s    zAuthorization.__repr__)NN)N)rZ   
__module____qualname____doc__r   r"   r#   r    r,   r3   classmethodrK   rR   rU   r[   r   r   r   r   r      s     
r   r   r   property)r   docr   c                   s2   t jdtdd  fdd}t fdd||dS )	a*  A static helper function for Authentication subclasses to add
    extra authentication system properties onto a class::

        class FooAuthenticate(WWWAuthenticate):
            special_realm = auth_property('special_realm')

    .. deprecated:: 2.3
        Will be removed in Werkzeug 3.0.
    zB'auth_property' is deprecated and will be removed in Werkzeug 3.0.r   
stacklevelc                   s&   |d kr|   d  nt||  < d S r   )popr   rQ   r   r   r   
_set_value   s    z!auth_property.<locals>._set_valuec                   s
   |   S r   )r    )xre   r   r   <lambda>       zauth_property.<locals>.<lambda>)ra   )warningswarnDeprecationWarningr`   )r   ra   rf   r   re   r   auth_property   s    
rm   c                	      s  e Zd ZdZdMddddddZdd	d
dZedd	ddZejdddddZedd	ddZ	e	jdddddZ	edd	ddZ
e
jdddddZ
dNdddddZdOdddddddd d!d"Zddd#d$d%Zdddd&d'd(Zddd#d)d*Zddd+d,d-Zdddd. fd/d0Zddd+d1d2Zddd#d3d4Zd5dd6d7d8ZdPdddd9d:d;Zedd<dd=d>Zdd	d?d@Zdd	dAdBZdd	dCdDZedEd	dFdGZedHd	dIdJZejdKdddLdJZeeZ  ZS )QWWWAuthenticatea  Represents the parts of a ``WWW-Authenticate`` response header.

    Set :attr:`.Response.www_authenticate` to an instance of list of instances to set
    values for this header in the response. Modifying this instance will modify the
    header value.

    Depending on the auth scheme, either :attr:`parameters` or :attr:`token` should be
    set. The ``Basic`` scheme will encode ``username`` and ``password`` parameters to a
    token.

    For convenience, ``auth["key"]`` and ``auth.key`` both act on the :attr:`parameters`
    dict, and can be used to get, set, or delete parameters. ``auth.get("key")`` and
    ``"key" in auth`` are also provided.

    .. versionchanged:: 2.3
        The ``token`` parameter and attribute was added to support auth schemes that use
        a token instead of parameters, such as ``Bearer``.

    .. versionchanged:: 2.3
        The object is no longer a ``dict``.

    .. versionchanged:: 2.3
        The ``on_update`` parameter was removed.
    Nr   r   )r   valuesr   c                   sJ   |d krt jdtdd d}|  _t| fdd _| _d  _d S )NzvAn auth type must be given as the first parameter. Assuming 'basic' is deprecated and will be removed in Werkzeug 3.0.r   rb   r8   c                   s      S r   _trigger_on_updaterI   rT   r   r   rh      ri   z*WWWAuthenticate.__init__.<locals>.<lambda>)	rj   rk   rl   r>   _typer   _parameters_token
_on_update)r   r   ro   r   r   rT   r   r      s    
 
zWWWAuthenticate.__init__r   rL   c                 C  s   | j d k	r|  |  d S r   )rv   rT   r   r   r   rq      s    
z"WWWAuthenticate._trigger_on_updater   c                 C  s   | j S )zDThe authorization scheme, like ``basic``, ``digest``, or ``bearer``.)rs   rT   r   r   r   r      s    zWWWAuthenticate.typer5   c                 C  s   || _ |   d S r   )rs   rq   rQ   r   r   r   r      s    zdict[str, str]c                 C  s   | j S zA dict of parameters for the header. Only one of this or :attr:`token` should
        have a value for a given scheme.
        )rt   rT   r   r   r   r      s    zWWWAuthenticate.parametersc                   s    t | fdd _   d S )Nc                   s      S r   rp   rr   rT   r   r   rh      ri   z,WWWAuthenticate.parameters.<locals>.<lambda>)r   rt   rq   rQ   r   rT   r   r      s
     
c                 C  s   | j S rw   )ru   rT   r   r   r   r      s    zWWWAuthenticate.tokenc                 C  s   || _ |   dS )zA token for the header. Only one of this or :attr:`parameters` should have a
        value for a given scheme.

        .. versionadded:: 2.3
        N)ru   rq   rQ   r   r   r   r      s    authentication required)realmr   c                 C  s@   t d d| _t| j t| jd|i d| _|   dS )zClear any existing data and set a ``Basic`` challenge.

        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. Create and assign an instance instead.
        zpThe 'set_basic' method is deprecated and will be removed in Werkzeug 3.0. Create and assign an instance instead.r8   ry   N)	rj   rk   rs   dictclearr   updateru   rq   )r   ry   r   r   r   	set_basic  s    zWWWAuthenticate.set_basicauthFzt.Sequence[str]r(   )ry   nonceqopopaque	algorithmstaler   c                 C  sx   t d d| _t| j ||d||r0dndd}|dk	rH||d< |dk	rX||d	< t| j| d| _| 	  dS )
zClear any existing data and set a ``Digest`` challenge.

        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. Create and assign an instance instead.
        zqThe 'set_digest' method is deprecated and will be removed in Werkzeug 3.0. Create and assign an instance instead.digest, TRUEFALSE)ry   r   r   r   Nr   r   )
rj   rk   rs   rz   r{   r   joinr|   ru   rq   )r   ry   r   r   r   r   r   r   r   r   r   
set_digest  s"    
zWWWAuthenticate.set_digestr)   c                 C  s   | j |S r   r   r+   r   r   r   r#   @  s    zWWWAuthenticate.__getitem__)r%   r6   r   c                 C  s2   |d kr|| j kr&| j |= n
|| j |< |   d S r   r   rq   )r   r%   r6   r   r   r   __setitem__C  s
    


zWWWAuthenticate.__setitem__c                 C  s   || j kr| j |= |   d S r   r   r+   r   r   r   __delitem__L  s    
zWWWAuthenticate.__delitem__r   c                 C  s   | | S r   r   r!   r   r   r   r"   Q  s    zWWWAuthenticate.__getattr__)r   r6   r   c                   s$   |dkrt  || n|| |< d S )N>   rs   rv   rt   ru   )super__setattr__)r   r   r6   	__class__r   r   r   T  s    zWWWAuthenticate.__setattr__c                 C  s
   | |= d S r   r   r!   r   r   r   __delattr__Z  s    zWWWAuthenticate.__delattr__c                 C  s
   || j kS r   r*   r+   r   r   r   r,   ]  s    zWWWAuthenticate.__contains__r-   r.   c                 C  s2   t |tstS |j| jko0|j| jko0|j| jkS r   )r0   rn   r1   r   r   r   r2   r   r   r   r3   `  s    


zWWWAuthenticate.__eq__r$   c                 C  s   | j ||S r   r   r'   r   r   r   r    j  s    zWWWAuthenticate.getr4   c                 C  sR   |sdS | d\}}}| }| }d|dkrF| |t|dS | |d|S )zParse a ``WWW-Authenticate`` header value and return an instance, or ``None``
        if the value is empty.

        :param value: The header value to parse.

        .. versionadded:: 2.3
        Nr7   r<   )r=   r>   r?   rF   r   )rG   r6   rH   rI   rJ   r   r   r   rK   m  s    	zWWWAuthenticate.from_headerc                 C  s   | j dk	r | j  d| j  S | jdkrg }| j D ]:\}}|dkrVt|dd}nt|}|| d|  q8dd	| S | j  dt| j S )
zCProduce a ``WWW-Authenticate`` header value representing this data.Nr7   r   >   ry   r   domainr   r   F)Zallow_tokenr<   zDigest r   )	r   r   rP   r   itemsr	   appendr   r   )r   r   r%   r6   r   r   r   rR     s    

zWWWAuthenticate.to_headerc                 C  s   |   S r   rS   rT   r   r   r   rU     s    zWWWAuthenticate.__str__c                 C  s   dt | j d|   dS rV   rY   rT   r   r   r   r[     s    zWWWAuthenticate.__repr__zset[str]c                   s6   t jdtdd ddd fdd}t jd	|S )
zThe ``qop`` parameter as a set.

        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. It will become the same as other
            parameters, returning a string.
        zThe 'qop' property is deprecated and will be removed in Werkzeug 3.0. It will become the same as other parameters, returning a string.r   rb   r   r   r5   c                   s(   | sd kr d= d S |    jd< d S )Nr   )rR   r   )r6   rT   r   r   	on_update  s
    z&WWWAuthenticate.qop.<locals>.on_updater   )rj   rk   rl   r   r   r    )r   r   r   rT   r   r     s    	zWWWAuthenticate.qopzbool | Nonec                 C  s0   t jdtdd d| jkr,| jd  dkS dS )zThe ``stale`` parameter as a boolean.

        .. deprecated:: 2.3
            Will be removed in Werkzeug 3.0. It will become the same as other
            parameters, returning a string.
        zThe 'stale' property is deprecated and will be removed in Werkzeug 3.0. It will become the same as other parameters, returning a string.r   rb   r   trueN)rj   rk   rl   r   r>   rT   r   r   r   r     s    
zWWWAuthenticate.stalezbool | str | Nonec                 C  sZ   |d krd| j kr| j d= d S t|trLtjdtdd |r@dnd| j d< n
|| j d< d S )Nr   z\Setting the 'stale' property to a boolean is deprecated and will be removed in Werkzeug 3.0.r   rb   r   r   )r   r0   r(   rj   rk   rl   rQ   r   r   r   r     s    

)NNN)rx   )r~   NNF)N) rZ   r\   r]   r^   r   rq   r`   r   setterr   r   r}   r   r#   r   r   r"   r   r   r,   r3   r    r_   rK   rR   rU   r[   r   r   staticmethodrm   __classcell__r   r   r   r   rn      sZ      	    %	
rn   c                   s   t   fdd}|S )Nc                    s   t jdtdd  | |S )NzTreating 'Authorization' and 'WWWAuthenticate' as a dict is deprecated and will be removed in Werkzeug 3.0. Use the 'parameters' attribute instead.r   rb   )rj   rk   rl   )argskwargsfr   r   wrapper  s    z(_deprecated_dict_method.<locals>.wrapperr   )r   r   r   r   r   _deprecated_dict_method  s    	r   )
__iter__r{   copyr   keysrd   popitem
setdefaultr|   ro   )N)
__future__r   r@   rC   typingtrj   	functoolsr   httpr   r   r   r	   
structuresr   r   TYPE_CHECKINGZtyping_extensionster   rm   rn   r   r   getattrrz   r   setattrr   r   r   r   <module>   s.   ~  9