o
    ¨ÝjÁ.  ã                   @  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Zd dlmZ d dl	m
Z
 ddlmZ ddlmZmZmZ ddlmZmZ ejrMd d	lmZ d d
lmZ g d¢ZG dd„ dƒ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G dd„ dejƒZdS )é    )ÚannotationsN)Ú	b64encode)Úparse_http_listé   )ÚProtocolError)ÚCookiesÚRequestÚResponse)Úto_bytesÚto_str)ÚCallable)Ú_Hash)ÚAuthÚ	BasicAuthÚ
DigestAuthÚFunctionAuthÚ	NetRCAuthc                   @  s6   e Zd ZdZdZdZddd„Zdd	d
„Zddd„ZdS )r   aô  
    Base class for all authentication schemes.

    To implement a custom authentication scheme, subclass `Auth` and override
    the `.auth_flow()` method.

    If the authentication scheme does I/O such as disk access or network calls, or uses
    synchronization primitives such as locks, you should override `.sync_auth_flow()`
    and/or `.async_auth_flow()` instead of `.auth_flow()` to provide specialized
    implementations that will be used by `Client` and `AsyncClient` respectively.
    FÚrequestr   Úreturnú)typing.Generator[Request, Response, None]c                 c  s   � |V  dS )a  
        Execute the authentication flow.

        To dispatch a request, `yield` it:

        ```
        yield request
        ```

        The client will `.send()` the response back into the flow generator. You can
        access it like so:

        ```
        response = yield request
        ```

        A `return` (or reaching the end of the generator) will result in the
        client returning the last response obtained from the server.

        You can dispatch as many requests as is necessary.
        N© ©Úselfr   r   r   úG/var/www/html/chatgpt/venv/lib/python3.10/site-packages/httpx2/_auth.pyÚ	auth_flow&   s   €
zAuth.auth_flowc                 c  s^   � | j r| ¡  |  |¡}t|ƒ}	 |V }| jr| ¡  z| |¡}W n
 ty-   Y dS w q)zè
        Execute the authentication flow synchronously.

        By default, this defers to `.auth_flow()`. You should override this method
        when the authentication scheme does I/O and/or uses concurrency primitives.
        TN)Úrequires_request_bodyÚreadr   ÚnextÚrequires_response_bodyÚsendÚStopIteration©r   r   ZflowÚresponser   r   r   Úsync_auth_flow>   s   €
ÿùzAuth.sync_auth_flowú(typing.AsyncGenerator[Request, Response]c                 C sj   �| j r| ¡ I dH  |  |¡}t|ƒ}	 |V }| jr"| ¡ I dH  z| |¡}W n
 ty3   Y dS w q)zé
        Execute the authentication flow asynchronously.

        By default, this defers to `.auth_flow()`. You should override this method
        when the authentication scheme does I/O and/or uses concurrency primitives.
        N)r   Zareadr   r   r   r   r    r!   r   r   r   Úasync_auth_flowU   s   €
ÿùzAuth.async_auth_flowN©r   r   r   r   )r   r   r   r$   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r#   r%   r   r   r   r   r      s    

r   c                   @  s$   e Zd ZdZddd„Zddd„ZdS )r   z‘
    Allows the 'auth' argument to be passed as a simple callable function,
    that takes the request, and returns a new, modified request.
    Úfuncú#typing.Callable[[Request], Request]r   ÚNonec                 C  s
   || _ d S ©N©Z_func)r   r+   r   r   r   Ú__init__s   s   
zFunctionAuth.__init__r   r   r   c                 c  s   � |   |¡V  d S r.   r/   r   r   r   r   r   v   s   €zFunctionAuth.auth_flowN)r+   r,   r   r-   r&   )r'   r(   r)   r*   r0   r   r   r   r   r   r   m   s    
r   c                   @  s.   e Zd ZdZddd„Zddd„Zddd„ZdS )r   zy
    Allows the 'auth' argument to be passed as a (username, password) pair,
    and uses HTTP Basic authentication.
    Úusernameústr | bytesÚpasswordr   r-   c                 C  s   |   ||¡| _d S r.   )Ú_build_auth_headerÚ_auth_header©r   r1   r3   r   r   r   r0   €   s   zBasicAuth.__init__r   r   r   c                 c  s   � | j |jd< |V  d S )NÚAuthorization)r5   Úheadersr   r   r   r   r   ƒ   s   €
zBasicAuth.auth_flowÚstrc                 C  ó,   d  t|ƒt|ƒf¡}t|ƒ ¡ }d|› �S ©Nó   :zBasic ©Újoinr
   r   Údecode©r   r1   r3   ZuserpassÚtokenr   r   r   r4   ‡   ó   
zBasicAuth._build_auth_headerN©r1   r2   r3   r2   r   r-   r&   ©r1   r2   r3   r2   r   r9   ©r'   r(   r)   r*   r0   r   r4   r   r   r   r   r   z   s
    

r   c                   @  s0   e Zd ZdZdddd„Zddd„Zddd„ZdS )r   zT
    Use a 'netrc' file to lookup basic auth credentials based on the url host.
    NÚfileú
str | Noner   r-   c                 C  s   dd l }|  |¡| _d S )Nr   )ÚnetrcÚ_netrc_info)r   rF   rH   r   r   r   r0   ’   s   zNetRCAuth.__init__r   r   r   c                 c  sR   � | j  |jj¡}|d u s|d s|V  d S | j|d |d d�|jd< |V  d S )Né   r   )r1   r3   r7   )rI   ÚauthenticatorsÚurlÚhostr4   r8   )r   r   Z	auth_infor   r   r   r   ™   s   €

zNetRCAuth.auth_flowr1   r2   r3   r9   c                 C  r:   r;   r=   r@   r   r   r   r4   £   rB   zNetRCAuth._build_auth_headerr.   )rF   rG   r   r-   r&   rD   rE   r   r   r   r   r   �   s
    

r   c                	   @  sh   e Zd ZdddddddddœZd+dd„Zd,dd„Zd-dd„Zd.dd„Zd/d d!„Zd0d$d%„Z	d1d(d)„Z
d*S )2r   Úmd5Úsha1Úsha256Úsha512)ÚMD5zMD5-SESSÚSHAzSHA-SESSzSHA-256zSHA-256-SESSzSHA-512zSHA-512-SESSr1   r2   r3   r   r-   c                 C  s$   t |ƒ| _t |ƒ| _d | _d| _d S )Nr   )r
   Ú	_usernameÚ	_passwordÚ_last_challengeÚ_nonce_countr6   r   r   r   r0   ¶   s   


zDigestAuth.__init__r   r   r   c                 c  s®   � | j r|  || j ¡|jd< |V }|jdksd|jvrd S |j d¡D ]}| ¡  d¡r. nq#d S |  |||¡| _ d| _|  || j ¡|jd< |j	rRt
|j	ƒj|d� |V  d S )Nr7   i‘  zwww-authenticatezdigest r   ©r   )rV   r4   r8   Ústatus_codeZget_listÚlowerÚ
startswithÚ_parse_challengerW   Úcookiesr   Zset_cookie_header)r   r   r"   Úauth_headerr   r   r   r   ¼   s"   €ÿ
zDigestAuth.auth_flowr"   r	   r^   r9   Ú_DigestAuthChallengec              
   C  sè   |  d¡\}}}| ¡ dksJ ‚i }t|ƒD ]}| ¡  dd¡\}	}
|
 d¡||	< qz4|d  ¡ }|d  ¡ }| dd	¡}d
|v rG|d
  ¡ nd}d|v rS|d  ¡ nd}t|||||d�W S  tys } z	d}t	||d�|‚d}~ww )zÁ
        Returns a challenge from a Digest WWW-Authenticate header.
        These take the form of:
        `Digest realm="realm@host.com",qop="auth,auth-int",nonce="abc",opaque="xyz"`
        ú Údigestú=r   ú"ÚrealmÚnonceÚ	algorithmrR   ÚopaqueNÚqop)rd   re   rf   rg   rh   z(Malformed Digest WWW-Authenticate headerrX   )
Ú	partitionrZ   r   ÚstripÚsplitÚencodeÚgetr_   ÚKeyErrorr   )r   r   r"   r^   ÚschemeÚ_ÚfieldsZheader_dictÚfieldÚkeyÚvaluerd   re   rf   rg   rh   ÚexcÚmessager   r   r   r\   ×   s$   €þzDigestAuth._parse_challengeÚ	challengec              
     sŠ  zt t| j|j ¡  ƒ‰ W n ttfy( } zd|j›�}t||d�|‚d }~ww d‡ fdd„}d | j	|j
| jf¡}|jj}d |j ¡ |f¡}||ƒ}	d	| j }
|  | j|j¡}|  jd
7  _||ƒ}|j ¡  d¡rx|d ||j|f¡ƒ}| j|j|d�}|d u r‹||j|	g}n	||j|
|||	g}| j	|j
|j||d |¡ƒ|j ¡ dœ}|jr°|j|d< |r¾d|d< |
|d< ||d< d|  |¡ S )Nz"Unsupported digest auth algorithm rX   ÚdataÚbytesr   c                   s   ˆ | ƒ  ¡  ¡ S r.   )Ú	hexdigestrl   )rx   ©Z	hash_funcr   r   ra   û   s   z-DigestAuth._build_auth_header.<locals>.digestr<   s   %08xr   z-sess)r1   rd   re   Úurir"   rf   rg   ó   authrh   ÚncÚcnoncezDigest )rx   ry   r   ry   )ÚgetattrÚhashlibÚ_ALGORITHM_TO_HASH_NAMErf   Úupperrn   ÚAttributeErrorr   r>   rT   rd   rU   rL   Úraw_pathÚmethodrl   rW   Ú_get_client_noncere   rZ   ÚendswithÚ_resolve_qoprh   rg   Ú_get_header_value)r   r   rw   ru   rv   ra   ÚA1ÚpathÚA2ÚHA2Znc_valuer   ÚHA1rh   Zdigest_dataZformat_argsr   r{   r   r4   ò   sL   ÿ€þ
ú
zDigestAuth._build_auth_headerÚnonce_countÚintre   ry   c                 C  sL   t |ƒ ¡ }||7 }|t ¡  ¡ 7 }|t d¡7 }t |¡ ¡ d d…  ¡ S )Né   é   )	r9   rl   ÚtimeÚctimeÚosÚurandomr�   rO   rz   )r   r�   re   Úsr   r   r   r‡   &  s
   zDigestAuth._get_client_nonceÚheader_fieldsúdict[str, bytes]c           
      C  sb   d}d}d}d}t | ¡ ƒD ] \}\}}|dkr|d7 }||vr"|n|}	||	 |t|ƒ¡7 }q|S )N)rf   rh   r~   z{}="{}"z{}={}Ú r   z, )Ú	enumerateÚitemsÚformatr   )
r   r™   ZNON_QUOTED_FIELDSZQUOTED_TEMPLATEZNON_QUOTED_TEMPLATEÚheader_valueÚirr   rt   Útemplater   r   r   rŠ   .  s   zDigestAuth._get_header_valuerh   úbytes | Nonec                 C  sN   |d u rd S t  d|¡}d|v rdS |dgkrtdƒ‚d|›d�}t||d�‚)Ns   , ?r}   s   auth-intz.Digest auth-int support is not yet implementedzUnexpected qop value "z" in digest authrX   )Úrerk   ÚNotImplementedErrorr   )r   rh   r   Zqopsrv   r   r   r   r‰   <  s   
zDigestAuth._resolve_qopNrC   r&   )r   r   r"   r	   r^   r9   r   r_   )r   r   rw   r_   r   r9   )r�   r‘   re   ry   r   ry   )r™   rš   r   r9   )rh   r¢   r   r   r   r¢   )r'   r(   r)   r‚   r0   r   r\   r4   r‡   rŠ   r‰   r   r   r   r   r   ©   s"    ø




4
r   c                   @  s6   e Zd ZU ded< ded< ded< ded< ded< d	S )
r_   ry   rd   re   r9   rf   r¢   rg   rh   N)r'   r(   r)   Ú__annotations__r   r   r   r   r_   J  s   
 r_   ) Ú
__future__r   r�   r–   r£   r”   ÚtypingÚbase64r   Úurllib.requestr   Ú_exceptionsr   Z_modelsr   r   r	   Z_utilsr
   r   ÚTYPE_CHECKINGÚcollections.abcr   r   Ú__all__r   r   r   r   r   Ú
NamedTupler_   r   r   r   r   Ú<module>   s,    W "