o
    h.                     @   s   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mZ ejrCd dl m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 )    N)	b64encode)parse_http_list   )ProtocolError)CookiesRequestResponse)to_bytesto_strunquote)_Hashc                   @   sp   e Zd ZdZdZdZdedejee	df fddZ
dedejee	df fdd	Zdedejee	f fd
dZdS )Autha  
    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requestreturnNc                 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   A/var/www/html/ai/venv/lib/python3.10/site-packages/httpx/_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   flowresponser   r   r   sync_auth_flow9   s   	
zAuth.sync_auth_flowc                 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   areadr   r   r   r   r   r   r   r   r   async_auth_flowR   s   	
zAuth.async_auth_flow)__name__
__module____qualname____doc__r   r   r   typing	Generatorr   r   r   AsyncGeneratorr    r   r   r   r   r      s    
r   c                   @   sL   e Zd ZdZdejegef ddfddZdedejee	df fdd	Z
dS )
FunctionAuthz
    Allows the 'auth' argument to be passed as a simple callable function,
    that takes the request, and returns a new, modified request.
    funcr   Nc                 C   s
   || _ d S N_func)r   r)   r   r   r   __init__r   s   
zFunctionAuth.__init__r   c                 c   s    |  |V  d S r*   r+   r   r   r   r   r   u   s   zFunctionAuth.auth_flow)r!   r"   r#   r$   r%   Callabler   r-   r&   r   r   r   r   r   r   r(   l   s    "r(   c                   @   s   e Zd ZdZdejeef dejeef ddfddZde	dej
e	edf fd	d
Zdejeef dejeef defddZdS )	BasicAuthzy
    Allows the 'auth' argument to be passed as a (username, password) pair,
    and uses HTTP Basic authentication.
    usernamepasswordr   Nc                 C   s   |  ||| _d S r*   )_build_auth_header_auth_headerr   r0   r1   r   r   r   r-      s   zBasicAuth.__init__r   c                 c   s    | j |jd< |V  d S )NAuthorization)r3   headersr   r   r   r   r      s   
zBasicAuth.auth_flowc                 C   ,   d t|t|f}t| }d| S N   :zBasic joinr	   r   decoder   r0   r1   userpasstokenr   r   r   r2         
zBasicAuth._build_auth_header)r!   r"   r#   r$   r%   Unionstrbytesr-   r   r&   r   r   r2   r   r   r   r   r/   y   s"    
r/   c                   @   sr   e Zd ZdZddeje ddfddZdedej	ee
df fdd	Zd
ejeef dejeef defddZdS )	NetRCAuthzT
    Use a 'netrc' file to lookup basic auth credentials based on the url host.
    Nfiler   c                 C   s   dd l }| || _d S )Nr   )netrc_netrc_info)r   rE   rF   r   r   r   r-      s   zNetRCAuth.__init__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   )r0   r1   r5   )rG   authenticatorsurlhostr2   r6   )r   r   	auth_infor   r   r   r      s   

zNetRCAuth.auth_flowr0   r1   c                 C   r7   r8   r:   r=   r   r   r   r2      r@   zNetRCAuth._build_auth_headerr*   )r!   r"   r#   r$   r%   OptionalrB   r-   r   r&   r   r   rA   rC   r2   r   r   r   r   rD      s    rD   c                	   @   s  e Zd ZU ejejejejejejejejdZe	j
ee	jegdf f ed< de	jeef de	jeef ddfdd	Zd
ede	jeedf fddZd
edededdfddZd
edddefddZdededefddZde	j
eef defddZde	je d
ede	je fddZdS )
DigestAuth)MD5zMD5-SESSSHAzSHA-SESSzSHA-256zSHA-256-SESSzSHA-512zSHA-512-SESSr   _ALGORITHM_TO_HASH_FUNCTIONr0   r1   r   Nc                 C   s$   t || _t || _d | _d| _d S )Nr   )r	   	_username	_password_last_challenge_nonce_countr4   r   r   r   r-      s   


zDigestAuth.__init__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 )Nr5   i  zwww-authenticatezdigest r   r   )rT   r2   r6   status_codeget_listlower
startswith_parse_challengerU   cookiesr   set_cookie_header)r   r   r   auth_headerr   r   r   r      s*   


zDigestAuth.auth_flowr   r^   _DigestAuthChallengec              
   C   s   | d\}}}| dksJ i }t|D ]}| dd\}	}
t|
||	< qz4|d  }|d  }|dd}d	|v rF|d	  nd
}d|v rR|d  nd
}t|||||dW S  t	yr } 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	algorithmrO   opaqueNqop)rc   rd   re   rf   rg   z(Malformed Digest WWW-Authenticate headerrV   )	partitionrY   r   stripsplitr   encodegetr_   KeyErrorr   )r   r   r   r^   scheme_fieldsheader_dictfieldkeyvaluerc   rd   re   rf   rg   excmessager   r   r   r[      s(   
zDigestAuth._parse_challenge	challengec              	      s\  | j |j   dtdtf fdd}d| j|j| jf}|jj	}d|j
 |f}||}d| j }| | j|j}	|  jd7  _||}
|j dr[|d|
|j|	f}
| j|j|d	}|d u rn|
|j|g}n|j||	||g}d|}| j|j|j||d|
|f|j d
}|jr|j|d< |rd|d< ||d< |	|d< d| | S )Ndatar   c                    s    |    S r*   )	hexdigestrk   )rx   	hash_funcr   r   ra     s   z-DigestAuth._build_auth_header.<locals>.digestr9   s   %08xr   z-sessrV   )r0   rc   rd   urir   re   rf      authrg   nccnoncezDigest )rQ   re   upperrC   r;   rR   rc   rS   rJ   raw_pathmethodrk   rU   _get_client_noncerd   rY   endswith_resolve_qoprg   rf   _get_header_value)r   r   rw   ra   A1pathA2HA2nc_valuer   HA1rg   digest_data
key_digestformat_argsr   rz   r   r2     s>   


zDigestAuth._build_auth_headernonce_countrd   c                 C   sL   t | }||7 }|t  7 }|td7 }t| d d  S )N      )	rB   rk   timectimeosurandomhashlibsha1ry   )r   r   rd   sr   r   r   r   1  s
   zDigestAuth._get_client_nonceheader_fieldsc           
      C   sb   d}d}d}d}t | D ] \}\}}|dkr|d7 }||vr"|n|}	||	|t|7 }q|S )N)re   rg   r~   z{}="{}"z{}={} r   z, )	enumerateitemsformatr
   )
r   r   NON_QUOTED_FIELDSQUOTED_TEMPLATENON_QUOTED_TEMPLATEheader_valueirr   rt   templater   r   r   r   9  s   zDigestAuth._get_header_valuerg   c                 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 authrV   )rerj   NotImplementedErrorr   )r   rg   r   qopsrv   r   r   r   r   K  s   
zDigestAuth._resolve_qop)r!   r"   r#   r   md5r   sha256sha512rQ   r%   DictrB   r.   rC   __annotations__rA   r-   r   r&   r   r   r[   r2   intr   r   rM   r   r   r   r   r   rN      sV   
 $


/rN   c                   @   sB   e Zd ZU eed< eed< eed< eje ed< eje ed< dS )r_   rc   rd   re   rf   rg   N)r!   r"   r#   rC   r   rB   r%   rM   r   r   r   r   r_   [  s   
 r_   )r   r   r   r   r%   base64r   urllib.requestr   _exceptionsr   _modelsr   r   r   _utilsr	   r
   r   TYPE_CHECKINGr   r   r(   r/   rD   rN   
NamedTupler_   r   r   r   r   <module>   s&    [  ,