U
    /v¶h%'  ã                   @  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rbd 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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] | 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-c3xdgso7/werkzeug/datastructures/auth.pyÚ__init__*   s    zAuthorization.__init__©Únamer   c                 C  s   | j  |¡S r   ©r   Úget©r   r   r   r   r   Ú__getattr__B   s    zAuthorization.__getattr__c                 C  s   | j  |¡S r   r   r   r   r   r   Ú__getitem__E   s    zAuthorization.__getitem__©ÚkeyÚdefaultr   c                 C  s   | j  ||¡S r   r   ©r   r!   r"   r   r   r   r   H   s    zAuthorization.getÚbool©r!   r   c                 C  s
   || j kS r   ©r   ©r   r!   r   r   r   Ú__contains__K   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__N   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 )zÄParse 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   )Úclsr2   ÚschemeÚ_Úrestr6   r7   r   r   r   Úfrom_headerX   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
        r4   r5   ÚasciizBasic Nr3   )r   r<   Ú	b64encoder6   r7   Úencoder>   r   Útitler   r   ©r   r2   r   r   r   Ú	to_headerw   s    
ÿþ

zAuthorization.to_headerc                 C  s   |   ¡ S r   ©rN   ©r   r   r   r   Ú__str__‡   s    zAuthorization.__str__c                 C  s   dt | ƒj› d|  ¡ › d�S ©Nú<r3   ú>©r   Ú__name__rN   rP   r   r   r   Ú__repr__Š   s    zAuthorization.__repr__)NN)N)rV   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r(   r/   ÚclassmethodrG   rN   rQ   rW   r   r   r   r   r	      s     ü
r	   c                      sˆ  e Zd ZdZd<d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
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%d&„Zdd	d!œd'd(„Zdd)dœd*d+„Zd,d)d-œd.d/„Zd=dddd0œd1d2„Zedd3dœd4d5„ƒZdd
œd6d7„Zdd
œd8d9„Zdd
œd:d;„Z‡  ZS )>Ú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   )r   Úvaluesr   c                   s.   |  ¡ ˆ _t|‡ fdd„ƒˆ _|ˆ _d ˆ _d S )Nc                   s   ˆ   ¡ S r   ©Ú_trigger_on_update©rE   rP   r   r   Ú<lambda>°   ó    z*WWWAuthenticate.__init__.<locals>.<lambda>)r:   Ú_typer   Ú_parametersÚ_tokenÚ
_on_update)r   r   r]   r   r   rP   r   r   ¨   s    
 
ÿzWWWAuthenticate.__init__r   rH   c                 C  s   | j d k	r|   | ¡ d S r   )rf   rP   r   r   r   r_   µ   s    
z"WWWAuthenticate._trigger_on_updatec                 C  s   | j S )zDThe authorization scheme, like ``basic``, ``digest``, or ``bearer``.)rc   rP   r   r   r   r   ¹   s    zWWWAuthenticate.typer1   c                 C  s   || _ |  ¡  d S r   )rc   r_   rM   r   r   r   r   ¾   s    zdict[str, str | None]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.
        )rd   rP   r   r   r   r   Ã   s    zWWWAuthenticate.parameterszdict[str, str]c                   s    t |‡ fdd„ƒˆ _ˆ  ¡  d S )Nc                   s   ˆ   ¡ S r   r^   r`   rP   r   r   ra   Ì   rb   z,WWWAuthenticate.parameters.<locals>.<lambda>)r   rd   r_   rM   r   rP   r   r   Ê   s    c                 C  s   | j S rg   )re   rP   r   r   r   r   Ï   s    zWWWAuthenticate.tokenc                 C  s   || _ |  ¡  dS )z–A token for the header. Only one of this or :attr:`parameters` should have a
        value for a given scheme.

        .. versionadded:: 2.3
        N)re   r_   rM   r   r   r   r   Ö   s    r%   c                 C  s   | j  |¡S r   r   r'   r   r   r   r   à   s    zWWWAuthenticate.__getitem__)r!   r2   r   c                 C  s2   |d kr|| j kr&| j |= n
|| j |< |  ¡  d S r   ©r   r_   )r   r!   r2   r   r   r   Ú__setitem__ã   s
    


zWWWAuthenticate.__setitem__c                 C  s   || j kr| j |= |  ¡  d S r   rh   r'   r   r   r   Ú__delitem__ì   s    
zWWWAuthenticate.__delitem__r   c                 C  s   | | S r   r   r   r   r   r   r   ñ   s    zWWWAuthenticate.__getattr__)r   r2   r   c                   s$   |dkrt ƒ  ||¡ n|| |< d S )N>   rc   re   rd   rf   )ÚsuperÚ__setattr__)r   r   r2   ©Ú	__class__r   r   rl   ô   s    zWWWAuthenticate.__setattr__c                 C  s
   | |= d S r   r   r   r   r   r   Ú__delattr__ú   s    zWWWAuthenticate.__delattr__r$   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   )r,   r\   r-   r   r   r   r.   r   r   r   r/      s    

ÿ
ýzWWWAuthenticate.__eq__r    c                 C  s   | j  ||¡S r   r   r#   r   r   r   r   
  s    zWWWAuthenticate.getr0   c                 C  sR   |sdS |  d¡\}}}| ¡ }| ¡ }d| d¡krF| |t|ƒdƒS | |d|ƒS )zÆParse 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
        Nr3   r8   )r9   r:   r;   rB   r   )rC   r2   rD   rE   rF   r   r   r   rG     s    	zWWWAuthenticate.from_headerc                 C  sž   | j dk	r | j ¡ › d| j › �S | jdkr„g }| 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.Nr3   Údigest>   ÚrealmÚnonceÚopaqueÚqopÚdomainF)Zallow_tokenr8   zDigest z, )	r   r   rL   r   Úitemsr   ÚappendÚjoinr   )r   rv   r!   r2   r   r   r   rN   $  s    

zWWWAuthenticate.to_headerc                 C  s   |   ¡ S r   rO   rP   r   r   r   rQ   8  s    zWWWAuthenticate.__str__c                 C  s   dt | ƒj› d|  ¡ › d�S rR   rU   rP   r   r   r   rW   ;  s    zWWWAuthenticate.__repr__)NN)N)rV   rX   rY   rZ   r   r_   Úpropertyr   Úsetterr   r   r   ri   rj   r   rl   ro   r(   r/   r   r[   rG   rN   rQ   rW   Ú__classcell__r   r   rm   r   r\   Ž   s>     ü		
r\   )Ú
__future__r   r<   r?   ÚtypingÚtÚhttpr   r   r   Ú
structuresr   ÚTYPE_CHECKINGZtyping_extensionsÚter	   r\   r   r   r   r   Ú<module>   s   ~