o
    E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
mZ ddlmZmZ ddlmZmZ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ZdS )    N)	b64encode)parse_http_list   )ProtocolErrorRequestBodyUnavailable)RequestResponse)to_bytesto_strunquotec                   @   s6   e Zd ZdZdZdZdedejee	df fddZ
dS )Authz4
    Base class for all authentication schemes.
    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   F/var/www/html/env_mimamsha/lib/python3.10/site-packages/httpx/_auth.py	auth_flow   s   
zAuth.auth_flow)__name__
__module____qualname____doc__requires_request_bodyrequires_response_bodyr   typing	Generatorr   r   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r   )r   r   r   r   r   __init__5   s   
zFunctionAuth.__init__r   c                 c   s    |  |V  d S r   r   r   r   r   r   r   8   s   zFunctionAuth.auth_flow)r   r   r   r   r   Callabler   r    r   r   r   r   r   r   r   r   /   s    "r   c                   @   s~   e Zd ZdZdejeef dejeef 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passwordc                 C   s   |  ||| _d S r   )build_auth_headerauth_headerr   r#   r$   r   r   r   r    B   s   zBasicAuth.__init__r   r   Nc                 c   s    | j |jd< |V  d S )NAuthorization)r&   headersr   r   r   r   r   G   s   
zBasicAuth.auth_flowc                 C   s0   d t|t|f}t|  }d| S )N   :zBasic )joinr	   r   decodestrip)r   r#   r$   userpasstokenr   r   r   r%   K   s   
zBasicAuth.build_auth_header)r   r   r   r   r   Unionstrbytesr    r   r   r   r   r%   r   r   r   r   r"   <   s    
r"   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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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	je fddZdS )
DigestAuth)MD5zMD5-SESSSHAzSHA-SESSzSHA-256zSHA-256-SESSzSHA-512zSHA-512-SESSALGORITHM_TO_HASH_FUNCTIONr#   r$   r   Nc                 C   s   t || _t || _d S r   )r	   r#   r$   r'   r   r   r   r    _   s   
zDigestAuth.__init__r   c                 c   s~    |j  s
td|V }|jdksd|jvrd S |jd }zt|}W n ty0   tdw | 	|||jd< |V  d S )Nz$Request body is no longer available.i  zwww-authenticatez&Malformed Digest authentication headerr(   )
stream
can_replayr   status_coder)   DigestAuthChallengefrom_header
ValueErrorr   _build_auth_header)r   r   responseheader	challenger   r   r   r   e   s   


zDigestAuth.auth_flowr@   r:   c              	      sL  | j |j  dtdtf fdd}d| j|j| jf}|jj	d}d|j
	 |f}||}d}d| }	| ||j}
||}|j d	rU|d||j|
f}| |j}|d u rf||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   )	hexdigestencode)rA   	hash_funcr   r   digest}   s   z-DigestAuth._build_auth_header.<locals>.digestr*   zutf-8r   s   %08xz-sess)r#   realmnonceurir>   	algorithmopaque   authqopnccnoncezDigest )r6   rJ   r2   r+   r#   rG   r$   url	full_pathrC   method_get_client_noncerH   lowerendswith_resolve_qoprM   rK   _get_header_value)r   r   r@   rF   A1pathA2HA2nonce_countnc_valuerO   HA1rM   digest_data
key_digestformat_argsr   rD   r   r=   x   s>   

zDigestAuth._build_auth_headerr\   rH   c                 C   sL   t | }||7 }|t  7 }|td7 }t| d d  S )N      )	r1   rC   timectimeosurandomhashlibsha1rB   )r   r\   rH   sr   r   r   rS      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)rJ   rM   rN   z{}="{}"z{}={} r   z, )	enumerateitemsformatr
   )
r   rk   NON_QUOTED_FIELDSQUOTED_TEMPLATENON_QUOTED_TEMPLATEheader_valueifieldvaluetemplater   r   r   rW      s   zDigestAuth._get_header_valuerM   c                 C   sF   |d u rd S t d|}d|v rdS |dgkrtdtd|d)Ns   , ?rL   s   auth-intz.Digest auth-int support is not yet implementedzUnexpected qop value "z" in digest auth)resplitNotImplementedErrorr   )r   rM   qopsr   r   r   rV      s   
zDigestAuth._resolve_qop)r   r   r   rh   md5ri   sha256sha512r6   r   Dictr1   r!   __annotations__r0   r2   r    r   r   r   r   r=   intrS   rW   OptionalrV   r   r   r   r   r3   S   s8   
 

/"r3   c                   @   sn   e Zd Z			ddedededeje deje ddfdd	Zed
edd fddZ	ede
dd fddZdS )r:   NrG   rH   rJ   rK   rM   r   c                 C   s&   || _ || _|p	d| _|| _|| _d S )Nr4   )rG   rH   rJ   rK   rM   )r   rG   rH   rJ   rK   rM   r   r   r   r       s
   

zDigestAuthChallenge.__init__r?   c           
   
   C   s   | d\}}}| dkrtdi }t|D ]}| dd\}}t|||< qz| |W S  tyB }	 ztd|	d}	~	ww )zReturns 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"`
         rF   z#Header does not start with 'Digest'=r   z(Malformed Digest WWW-Authenticate headerN)		partitionrT   r<   r   r-   ry   r   from_header_dictKeyError)
clsr?   scheme_fieldsheader_dictru   keyrv   excr   r   r   r;      s   
zDigestAuthChallenge.from_headerr   c                 C   sd   |d   }|d   }d|v r|d   nd }d|v r"|d   nd }|d}| |||||dS )NrG   rH   rM   rK   rJ   )rG   rH   rM   rK   rJ   )rC   get)r   r   rG   rH   rM   rK   rJ   r   r   r   r      s   

z$DigestAuthChallenge.from_header_dict)NNN)r   r   r   r2   r1   r   r   r    classmethodr;   dictr   r   r   r   r   r:      s*    
r:   )rh   rf   rx   rd   r   base64r   urllib.requestr   _exceptionsr   r   _modelsr   r   _utilsr	   r
   r   r   r   r"   r3   r:   r   r   r   r   <module>   s    !{