
    tc)                     $   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 ddlmZmZmZ ej        rd dl mZ  G d	 d
          Z G d de          Z G d de          Z G d de          Z G d dej                  ZdS )    N)	b64encode)parse_http_list   )ProtocolError)RequestResponse)to_bytesto_strunquote)_Hashc                       e Zd ZdZdZdZdedej        ee	df         fdZ
dedej        ee	df         fdZdedej        ee	f         f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              #      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     -/usr/lib/python3/dist-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_flow9   s       % 	LLNNN~~g&&t**	$}}H*  ))H--    	s   #A9 9
BB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_flowR   s       % 	"--//!!!!!!!~~g&&t**	$}}}H* 'nn&&&&&&&&&))H--    	s   0B 
BB)__name__
__module____qualname____doc__r   r   r   typing	Generatorr   r   r"   AsyncGeneratorr%   r   r   r   r   r      s        
 
 "" V-=gxQU>U-V    0		'8T1	2   2		w0	1     r   r   c                   j    e Zd ZdZdej        egef         ddfdZdedej        ee	df         f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                     || _         d S N_func)r   r/   s     r   __init__zFunctionAuth.__init__r   s    


r   r   c              #   8   K   |                      |          V  d S r1   r2   r   s     r   r   zFunctionAuth.auth_flowu   s$      jj!!!!!!!r   )r&   r'   r(   r)   r*   Callabler   r4   r+   r   r   r   r   r   r.   r.   l   s         
V_gY-?@ T    " "V-=gxQU>U-V " " " " " "r   r.   c                       e Zd ZdZdej        eef         dej        eef         fdZde	dej
        e	edf         fdZdej        eef         dej        eef         defd	ZdS )
	BasicAuthzy
    Allows the 'auth' argument to be passed as a (username, password) pair,
    and uses HTTP Basic authentication.
    usernamepasswordc                 <    |                      ||          | _        d S r1   )_build_auth_header_auth_headerr   r9   r:   s      r   r4   zBasicAuth.__init__   s"     !33HhGGr   r   r   Nc              #   0   K   | j         |j        d<   |V  d S )NAuthorization)r=   headersr   s     r   r   zBasicAuth.auth_flow   s"      +/+<(r   c                     d                     t          |          t          |          f          }t          |                                          }d| S )N   :zBasic )joinr	   r   decode)r   r9   r:   userpasstokens        r   r<   zBasicAuth._build_auth_header   sQ     99hx00(82D2DEFF(##**,,r   )r&   r'   r(   r)   r*   Unionstrbytesr4   r   r+   r   r   r<   r   r   r   r8   r8   y   s         
HS%Z0H<BLe<TH H H H
 V-=gxQU>U-V     S%Z0 <BLe<T 	           r   r8   c            	          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Zd	ede	j        eedf         fd
Zd	edededdfdZd	edddefdZdededefdZde	j
        eef         defdZde	j        e         d	ede	j        e         fdZdS )
DigestAuth)MD5zMD5-SESSSHAzSHA-SESSzSHA-256zSHA-256-SESSzSHA-512zSHA-512-SESSr   _ALGORITHM_TO_HASH_FUNCTIONr9   r:   r   Nc                 r    t          |          | _        t          |          | _        d | _        d| _        d S )Nr   )r	   	_username	_password_last_challenge_nonce_countr>   s      r   r4   zDigestAuth.__init__   s8     "(++!(++FJ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<   |V  d S )Nr@   i  zwww-authenticatezdigest r   )	rS   r<   rA   status_codeget_listlower
startswith_parse_challengerT   )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),
 ,
( r   r!   r[   _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	algorithmrM   opaqueNqop)ra   rb   rc   rd   re   z(Malformed Digest WWW-Authenticate headerr   )	partitionrX   r   stripsplitr   encodegetr\   KeyErrorr   )r   r   r!   r[   scheme_fieldsheader_dictfieldkeyvaluera   rb   rc   rd   re   excmessages                     r   rZ   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t          dt          f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}d                    |          }| j        |j        |j        | |d                    |
|f                    |j                                        d	}|j        r
|j        |d
<   |rd|d<   ||d<   |	|d<   d|                     |          z   S )Ndatar   c                 b     |                                                                            S r1   )	hexdigestrj   )rx   	hash_funcs    r   r_   z-DigestAuth._build_auth_header.<locals>.digest   s)    9T??,,..55777r   rC   s   %08xr   z-sessrf   )r9   ra   rb   urir!   rc   rd      authre   nccnoncezDigest )rO   rc   upperrJ   rD   rQ   ra   rR   urlraw_pathmethodrj   rT   _get_client_noncerb   rX   endswith_resolve_qopre   rd   _get_header_value)r   r   rv   r_   A1pathA2HA2nc_valuer   HA1re   digest_data
key_digestformat_argsr{   s                  @r   r<   zDigestAuth._build_auth_header   s5    4Y5H5N5N5P5PQ		8 	85 	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$?Hfc3GKYY{++
 __tyy#z):;;<<",3355
 
  	5$-$4K! 	+!(K (K$*K!411+>>>>r   nonce_countrb   c                 L   t          |                                          }||z  }|t          j                                                    z  }|t	          j        d          z  }t          j        |                                          d d                                         S )N      )	rI   rj   timectimeosurandomhashlibsha1rz   )r   r   rb   ss       r   r   zDigestAuth._get_client_nonce  s    ##%%	U
	TZ\\  """	RZ]]|A((**3B3/66888r   header_fieldsc                     d}d}d}d}t          |                                          D ]A\  }\  }}|dk    r|dz  }||vr|n|}	||	                    |t          |                    z  }B|S )N)rc   re   r~   z{}="{}"z{}={} r   z, )	enumerateitemsformatr
   )
r   r   NON_QUOTED_FIELDSQUOTED_TEMPLATENON_QUOTED_TEMPLATEheader_valueirq   rs   templates
             r   r   zDigestAuth._get_header_value  s    6#%!*=+>+>+@+@!A!A 	B 	BA~u1uu$  111  ( 
 HOOE6%==AAALLr   re   c                     |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 authrf   )reri   NotImplementedErrorr   )r   re   r   qopsru   s        r   r   zDigestAuth._resolve_qop)  sl     ;4x$$d??7K=  %&VWWWB3BBBGW5555r   )r&   r'   r(   r   md5r   sha256sha512rO   r*   DictrI   r6   rJ   __annotations__rH   r4   r   r+   r   r   rZ   r<   intr   r   Optionalr   r   r   r   rL   rL      s        {K|L>>	X 	XS&/5'7BR2S-S!T 	 	 	S%Z0<BLe<T	    V-=gxQU>U-V    :CC*2CADC	C C C C>-?-?+A-?	-? -? -? -?^9S 9 95 9 9 9 9v{3:/F 3    $6?5)64;6		6 6 6 6 6 6r   rL   c                   n    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\   ra   rb   rc   rd   re   N)r&   r'   r(   rJ   r   rI   r*   r   r   r   r   r\   r\   9  sS         LLLLLLNNNOE""""		r   r\   )r   r   r   r   r*   base64r   urllib.requestr   _exceptionsr   _modelsr   r   _utilsr	   r
   r   TYPE_CHECKINGr   r   r.   r8   rL   
NamedTupler\   r   r   r   <module>r      s    				 				         * * * * * * & & & & & & & & & & & & & & - - - - - - - - - -	 X X X X X X X Xv
" 
" 
" 
" 
"4 
" 
" 
"               .f6 f6 f6 f6 f6 f6 f6 f6R         6,          r   