
    hs.                    X   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mZ ej        rd d	lmZ g d
Z G d d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d dej                  ZdS )    )annotationsN)	b64encode)parse_http_list   )ProtocolError)CookiesRequestResponse)to_bytesto_strunquote)_Hash)Auth	BasicAuth
DigestAuth	NetRCAuthc                  2    e Zd ZdZdZdZddZddZ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              #     K   |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   s     M/var/www/html/prod/cognitive/venv/lib/python3.11/site-packages/httpx/_auth.py	auth_flowzAuth.auth_flow&   s      ,     c              #    K   | j         r|                                 |                     |          }t          |          }	 |V }| j        r|                                 	 |                    |          }n# t          $ r Y dS w xY wH)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responses       r   sync_auth_flowzAuth.sync_auth_flow>   s       % 	LLNNN~~g&&t**	$}}H*  ))H--    	s   #A9 9
BB(typing.AsyncGenerator[Request, Response]c               0  K   | j         r|                                 d{V  |                     |          }t          |          }	 |W V }| j        r|                                 d{V  	 |                    |          }n# t          $ r Y dS w xY wO)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%   s       r   async_auth_flowzAuth.async_auth_flowW   s       % 	"--//!!!!!!!~~g&&t**	$}}}H* 'nn&&&&&&&&&))H--    	s   0B 
BBNr   r	   r   r   )r   r	   r   r)   )	__name__
__module____qualname____doc__r   r"   r   r(   r,   r   r   r   r   r      sh        
 
 ""   0   2     r   r   c                  "    e Zd ZdZddZ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.
    func#typing.Callable[[Request], Request]r   Nonec                    || _         d S N_func)r   r4   s     r   __init__zFunctionAuth.__init__w   s    


r   r   r	   r   c              #  8   K   |                      |          V  d S r8   r9   r   s     r   r   zFunctionAuth.auth_flowz   s$      jj!!!!!!!r   N)r4   r5   r   r6   r-   )r.   r/   r0   r1   r;   r   r   r   r   r3   r3   q   sF         
   " " " " " "r   r3   c                  *    e Zd ZdZddZddZ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   r6   c                <    |                      ||          | _        d S r8   )_build_auth_header_auth_headerr   r>   r@   s      r   r;   zBasicAuth.__init__   s      33HhGGr   r   r	   r   c              #  0   K   | j         |j        d<   |V  d S )NAuthorization)rC   headersr   s     r   r   zBasicAuth.auth_flow   s"      +/+<(r   strc                    d                     t          |          t          |          f          }t          |                                          }d| S N   :zBasic joinr   r   decoder   r>   r@   userpasstokens        r   rB   zBasicAuth._build_auth_header   O    99hx00(82D2DEFF(##**,,r   Nr>   r?   r@   r?   r   r6   r-   r>   r?   r@   r?   r   rH   r.   r/   r0   r1   r;   r   rB   r   r   r   r   r   ~   s^         
H H H H              r   r   c                  ,    e Zd ZdZdddZddZ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   r6   c                B    dd l }|                     |          | _        d S )Nr   )netrc_netrc_info)r   rW   rZ   s      r   r;   zNetRCAuth.__init__   s'     	 ;;t,,r   r   r	   r   c              #     K   | j                             |j        j                  }||d         s|V  d S |                     |d         |d                   |j        d<   |V  d S )N   r   )r>   r@   rF   )r[   authenticatorsurlhostrB   rG   )r   r   	auth_infos      r   r   zNetRCAuth.auth_flow   s}      $33GK4DEE	IaLMMMMM 04/F/F"1	! 0G 0 0GOO, MMMMMr   r>   r?   r@   rH   c                    d                     t          |          t          |          f          }t          |                                          }d| S rJ   rL   rO   s        r   rB   zNetRCAuth._build_auth_header   rR   r   r8   )rW   rX   r   r6   r-   rT   rU   r   r   r   r   r      s_         - - - - -
 
 
 
           r   r   c            	          e Zd ZU ej        ej        ej        ej        ej        ej        ej        ej        dZde	d<   d"d	Z
d#dZd$dZd%dZd&dZd'dZd(d Zd!S ))r   )MD5zMD5-SESSSHAzSHA-SESSzSHA-256zSHA-256-SESSzSHA-512zSHA-512-SESSz*dict[str, typing.Callable[[bytes], _Hash]]_ALGORITHM_TO_HASH_FUNCTIONr>   r?   r@   r   r6   c                r    t          |          | _        t          |          | _        d | _        d| _        d S )Nr   )r   	_username	_password_last_challenge_nonce_countrD   s      r   r;   zDigestAuth.__init__   s6    !(++!(++<@r   r   r	   r   c              #    K   | j         r#|                     || j                   |j        d<   |V }|j        dk    s	d|j        vrd S |j                            d          D ]+}|                                                    d          r n,d S |                     |||          | _         d| _        |                     || j                   |j        d<   |j	        r(t          |j	                                      |           |V  d S )NrF   i  zwww-authenticatezdigest r   r   )rj   rB   rG   status_codeget_listlower
startswith_parse_challengerk   cookiesr   set_cookie_header)r   r   r'   auth_headers       r   r   zDigestAuth.auth_flow   s:      	/3/F/F-0 0GOO, !==3&&*<HDT*T*T F#+445GHH 	 	K  ""--i88 
 F#44WhTT+/+B+BT),
 ,
(  	IH$%%777HHHr   r'   r
   ru   rH   _DigestAuthChallengec                   |                     d          \  }}}|                                dk    sJ i }t          |          D ]?}|                                                    dd          \  }	}
t          |
          ||	<   @	 |d                                         }|d                                         }|                    dd          }d	|v r|d	                                         nd
}d|v r|d                                         nd
}t          |||||          S # t          $ r}d}t          ||          |d
}~ww xY 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	algorithmrd   opaqueNqop)r{   r|   r}   r~   r   z(Malformed Digest WWW-Authenticate headerrm   )	partitionrp   r   stripsplitr   encodegetrv   KeyErrorr   )r   r   r'   ru   scheme_fieldsheader_dictfieldkeyvaluer{   r|   r}   r~   r   excmessages                     r   rr   zDigestAuth._parse_challenge   s{    (11#666 ||~~))))&($V,, 	. 	.E,,S!44JC&u~~K	C(//11E(//11E#U;;I7?;7N7N[*11333TXF16+1E1E+e$++---4C'5IfRU     	C 	C 	C@G999sB	Cs   BD$ $
E.EE	challengec           	        | j         |j                                                 dfd}d                    | j        |j        | j        f          }|j        j        }d                    |j	        
                                |f          } ||          }d| j        z  }|                     | j        |j                  }	| xj        dz  c_         ||          }
|j                                                            d          r& |d                    |
|j        |	f                    }
|                     |j        |	          }||
|j        |g}n|
|j        ||	||g}| j        |j        |j        | |d                    |                    |j        
                                d
}|j        r
|j        |d<   |rd|d<   ||d<   |	|d<   d|                     |          z   S )Ndatabytesr   c                b     |                                                                            S r8   )	hexdigestr   )r   	hash_funcs    r   ry   z-DigestAuth._build_auth_header.<locals>.digest  s)    9T??,,..55777r   rK   s   %08xr   z-sessrm   )r>   r{   r|   urir'   r}   r~      authr   nccnoncezDigest )r   r   r   r   )rf   r}   upperrM   rh   r{   ri   r_   raw_pathmethodr   rk   _get_client_noncer|   rp   endswith_resolve_qopr   r~   _get_header_value)r   r   r   ry   A1pathA2HA2nc_valuer   HA1r   digest_dataformat_argsr   s                 @r   rB   zDigestAuth._build_auth_header   s    4Y5H5N5N5P5PQ		8 	8 	8 	8 	8 	8 YY	HII{#YY--//677fRjjT..''(99?KKQfRjj$$&&//88 	D&C&#ABBCCC	w??;	5KK 	63LK __tyy5566",3355
 
  	5$-$4K! 	+!(K (K$*K!411+>>>>r   nonce_countintr|   r   c                L   t          |                                          }||z  }|t          j                                                    z  }|t	          j        d          z  }t          j        |                                          d d                                         S )N      )	rH   r   timectimeosurandomhashlibsha1r   )r   r   r|   ss       r   r   zDigestAuth._get_client_nonce/  s    ##%%	U
	TZ\\  """	RZ]]|A((**3B3/66888r   header_fieldsdict[str, bytes]c                    d}d}d}d}t          |                                          D ]A\  }\  }}|dk    r|dz  }||vr|n|}	||	                    |t          |                    z  }B|S )N)r}   r   r   z{}="{}"z{}={} r   z, )	enumerateitemsformatr   )
r   r   NON_QUOTED_FIELDSQUOTED_TEMPLATENON_QUOTED_TEMPLATEheader_valueir   r   templates
             r   r   zDigestAuth._get_header_value7  s    6#%!*=+>+>+@+@!A!A 	B 	BA~u1uu$  111  ( 
 HOOE6%==AAALLr   r   bytes | Nonec                    |d S t          j        d|          }d|v rdS |dgk    rt          d          d|d}t          ||          )Ns   , ?r   s   auth-intz.Digest auth-int support is not yet implementedzUnexpected qop value "z" in digest authrm   )rer   NotImplementedErrorr   )r   r   r   qopsr   s        r   r   zDigestAuth._resolve_qopI  sj    ;4x$$d??7K=  %&VWWWB3BBBGW5555r   NrS   r-   )r   r	   r'   r
   ru   rH   r   rv   )r   r	   r   rv   r   rH   )r   r   r|   r   r   r   )r   r   r   rH   )r   r   r   r	   r   r   )r.   r/   r0   r   md5r   sha256sha512rf   __annotations__r;   r   rr   rB   r   r   r   r   r   r   r   r      s         {K|L>>	O 	O 	 	 	 	      >C C C C>.? .? .? .?`9 9 9 9   $6 6 6 6 6 6r   r   c                  B    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	S )
rv   r   r{   r|   rH   r}   r   r~   r   N)r.   r/   r0   r   r   r   r   rv   rv   W  sE         LLLLLLNNNr   rv   )
__future__r   r   r   r   r   typingbase64r   urllib.requestr   _exceptionsr   _modelsr   r	   r
   _utilsr   r   r   TYPE_CHECKINGr   __all__r   r3   r   r   r   
NamedTuplerv   r   r   r   <module>r      s   " " " " " "  				 				         * * * * * * & & & & & & / / / / / / / / / / - - - - - - - - - -	  ;
:
:X X X X X X X Xv
" 
" 
" 
" 
"4 
" 
" 
"               &               <e6 e6 e6 e6 e6 e6 e6 e6P    6,     r   