o
    9ef'                     @  s   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 ejr1d dlZG d	d
 d
ZG dd dZdS )    )annotationsN   )dump_header)parse_dict_header)quote_header_value   )CallbackDictc                   @  s   e Zd ZdZ		d'd(ddZd)ddZd)ddZd*d+ddZd,ddZd-ddZ	e
d.dd Zd/d!d"Zd/d#d$Zd/d%d&ZdS )0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	auth_typestrdatadict[str, str | None] | Nonetoken
str | NonereturnNonec                 C  s&   || _ 	 |d u r
i }|| _	 || _d S N)type
parametersr   )selfr
   r   r    r   <lib/python3.10/site-packages/werkzeug/datastructures/auth.py__init__*   s   zAuthorization.__init__namec                 C     | j |S r   r   getr   r   r   r   r   __getattr__B      zAuthorization.__getattr__c                 C  r   r   r   r   r   r   r   __getitem__E   r   zAuthorization.__getitem__keydefaultc                 C     | j ||S r   r   r   r!   r"   r   r   r   r   H      zAuthorization.getboolc                 C  
   || j v S r   r   r   r!   r   r   r   __contains__K      
zAuthorization.__contains__otherobjectc                 C  2   t |tstS |j| jko|j| jko|j| jkS r   )
isinstancer	   NotImplementedr   r   r   r   r,   r   r   r   __eq__N      


zAuthorization.__eq__valuete.Self | Nonec              	   C  s   |sdS | d\}}}| }| }|dkr=zt|  d\}}}W n tjtfy4   Y dS w | |||dS d|	dv rL| |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base64Z	b64decodedecodebinasciiErrorUnicodeErrorrstripr   )clsr4   scheme_restr9   r:   r   r   r   from_headerX   s   	zAuthorization.from_headerc                 C  sp   | j dkrt| j d| j  d}d| S | jdur+| j   d| j S | j   dt	| j
 S )ziProduce an ``Authorization`` header value representing this data.

        .. versionadded:: 2.0
        r7   r8   utf8zBasic Nr6   )r   r?   Z	b64encoder9   r:   encoder@   r   titler   r   r   r4   r   r   r   	to_headerw   s   


zAuthorization.to_headerc                 C     |   S r   rN   r   r   r   r   __str__      zAuthorization.__str__c                 C     dt | j d|   dS N<r6   >r   __name__rN   rQ   r   r   r   __repr__      zAuthorization.__repr__NN)r
   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   r"   r   r   r   r!   r   r   r&   r,   r-   r   r&   r4   r   r   r5   r   r   )rY   
__module____qualname____doc__r   r   r    r   r*   r2   classmethodrI   rN   rR   rZ   r   r   r   r   r	      s    






r	   c                      s  e Zd ZdZ		d<d=d	d
Zd>ddZed?ddZejd@ddZedAddZ	e	jdBddZ	edCddZ
e
jdDddZ
dEddZdFddZdGd d!ZdHd#d$ZdI fd%d&ZdJd'd(ZdKd*d+ZdLd.d/ZdMdNd1d2ZedOd4d5Zd?d6d7Zd?d8d9Zd?d:d;Z  ZS )P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   valuesr   r   r   c                   s.   |   _t| fdd _| _d  _d S )Nc                         S r   _trigger_on_updaterG   rQ   r   r   <lambda>       z*WWWAuthenticate.__init__.<locals>.<lambda>)r=   _typer   _parameters_token
_on_update)r   r
   rh   r   r   rQ   r   r      s   

zWWWAuthenticate.__init__r   r   c                 C  s   | j d ur|  |  d S d S r   )rr   rQ   r   r   r   rk      s   
z"WWWAuthenticate._trigger_on_updatec                 C     | j S )zDThe authorization scheme, like ``basic``, ``digest``, or ``bearer``.)ro   rQ   r   r   r   r      s   zWWWAuthenticate.typer4   c                 C  s   || _ |   d S r   )ro   rk   rM   r   r   r   r      s   dict[str, str | None]c                 C  rs   zA dict of parameters for the header. Only one of this or :attr:`token` should
        have a value for a given scheme.
        )rp   rQ   r   r   r   r         zWWWAuthenticate.parametersdict[str, str]c                   s    t | fdd _   d S )Nc                   ri   r   rj   rl   rQ   r   r   rm      rn   z,WWWAuthenticate.parameters.<locals>.<lambda>)r   rp   rk   rM   r   rQ   r   r      s   c                 C  rs   ru   )rq   rQ   r   r   r   r      rv   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)rq   rk   rM   r   r   r   r      s   r!   c                 C  r   r   r   r)   r   r   r   r       r   zWWWAuthenticate.__getitem__c                 C  s2   |d u r|| j v r| j |= n|| j |< |   d S r   r   rk   )r   r!   r4   r   r   r   __setitem__   s   

zWWWAuthenticate.__setitem__c                 C  s"   || j v r| j |= |   d S d S r   rx   r)   r   r   r   __delitem__   s   
zWWWAuthenticate.__delitem__r   c                 C  s   | | S r   r   r   r   r   r   r      rS   zWWWAuthenticate.__getattr__c                   s&   |dv rt  || d S || |< d S )N>   rq   rp   rr   ro   )super__setattr__)r   r   r4   	__class__r   r   r|      s   zWWWAuthenticate.__setattr__c                 C  s
   | |= d S r   r   r   r   r   r   __delattr__   r+   zWWWAuthenticate.__delattr__r&   c                 C  r'   r   r(   r)   r   r   r   r*      r+   zWWWAuthenticate.__contains__r,   r-   c                 C  r.   r   )r/   rg   r0   r   r   r   r1   r   r   r   r2     r3   zWWWAuthenticate.__eq__r"   c                 C  r#   r   r   r$   r   r   r   r     r%   zWWWAuthenticate.getr5   c                 C  sR   |sdS | d\}}}| }| }d|dv r#| |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
        Nr6   r;   )r<   r=   r>   rD   r   )rE   r4   rF   rG   rH   r   r   r   rI     s   	zWWWAuthenticate.from_headerc                 C  s   | j dur| j  d| j  S | jdkrBg }| j D ]\}}|dv r+t|dd}nt|}|| d|  qdd	| S | j  dt| j S )
zCProduce a ``WWW-Authenticate`` header value representing this data.Nr6   Zdigest>   ZrealmZopaqueZnoncedomainZqopF)Zallow_tokenr;   zDigest z, )	r   r   rL   r   itemsr   appendjoinr   )r   r   r!   r4   r   r   r   rN   &  s   

zWWWAuthenticate.to_headerc                 C  rO   r   rP   rQ   r   r   r   rR   :  rS   zWWWAuthenticate.__str__c                 C  rT   rU   rX   rQ   r   r   r   rZ   =  r[   zWWWAuthenticate.__repr__r\   )r
   r   rh   r   r   r   )r   r   rb   )r4   r   r   r   )r   rt   )r4   rw   r   r   )r   r   )r4   r   r   r   )r!   r   r   r   )r!   r   r4   r   r   r   )r!   r   r   r   r]   )r   r   r4   r   r   r   )r   r   r   r   r_   r`   r   r^   ra   )rY   rc   rd   re   r   rk   propertyr   setterr   r   r    ry   rz   r   r|   r   r*   r2   r   rf   rI   rN   rR   rZ   __classcell__r   r   r}   r   rg      s@    

	

	






rg   )Z
__future__r   r?   rA   typingthttpr   r   r   Z
structuresr   ZTYPE_CHECKINGZtyping_extensionster	   rg   r   r   r   r   <module>   s    ~