Format: application/x-python-code
⬇ Download File (10.3 KB)
   1  o
   2  
   3  C'hs.�@s�ddlmZddlZddlZddlZddlZddlZddlmZddl	m
   4  Z
   5  ddlmZddl
   6  mZmZmZddlmZmZmZejrIdd	lmZgd
   7  �ZGdd�d�ZGd
   8  d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�dej�ZdS)�)�annotationsN)�	b64encode)�parse_http_list�)�
   9  ProtocolError)�Cookies�Request�Response)�to_bytes�to_str�unquote)�_Hash)�Auth�	BasicAuth�
  10  DigestAuth�	NetRCAuthc@s6eZdZdZdZdZddd�Zdd	d
  11  �Zddd
  12  �ZdS)ra�
  13      Base class for all authentication schemes.
  14  
  15      To implement a custom authentication scheme, subclass `Auth` and override
  16      the `.auth_flow()` method.
  17  
  18      If the authentication scheme does I/O such as disk access or network calls, or uses
  19      synchronization primitives such as locks, you should override `.sync_auth_flow()`
  20      and/or `.async_auth_flow()` instead of `.auth_flow()` to provide specialized
  21      implementations that will be used by `Client` and `AsyncClient` respectively.
  22      F�requestr�return�)typing.Generator[Request, Response, None]ccs�|VdS)a
  23          Execute the authentication flow.
  24  
  25          To dispatch a request, `yield` it:
  26  
  27          ```
  28          yield request
  29          ```
  30  
  31          The client will `.send()` the response back into the flow generator. You can
  32          access it like so:
  33  
  34          ```
  35          response = yield request
  36          ```
  37  
  38          A `return` (or reaching the end of the generator) will result in the
  39          client returning the last response obtained from the server.
  40  
  41          You can dispatch as many requests as is necessary.
  42          N���selfrrr�AC:\Users\shubh\Videos\agent\nova\Lib\site-packages\httpx/_auth.py�	auth_flow&s�
  43  zAuth.auth_flowccs^�|jr|��|�|�}t|�}	|V}|jr|��z|�|�}Wn
  44  ty-YdSwq)z�
  45          Execute the authentication flow synchronously.
  46  
  47          By default, this defers to `.auth_flow()`. You should override this method
  48          when the authentication scheme does I/O and/or uses concurrency primitives.
  49          TN)�requires_request_body�readr�next�requires_response_body�send�
  50  StopIteration�rrZflow�responserrr�sync_auth_flow>s�	
  51  ��zAuth.sync_auth_flow�(typing.AsyncGenerator[Request, Response]cCsj�|jr|��IdH|�|�}t|�}	|V}|jr"|��IdHz|�|�}Wn
  52  ty3YdSwq)z�
  53          Execute the authentication flow asynchronously.
  54  
  55          By default, this defers to `.auth_flow()`. You should override this method
  56          when the authentication scheme does I/O and/or uses concurrency primitives.
  57          N)rZareadrrrrrr rrr�async_auth_flowWs�	
  58  ��zAuth.async_auth_flowN�rrrr)rrrr#)	�__name__�
  59  __module__�__qualname__�__doc__rrrr"r$rrrrrs
  60  
  61  rc@s$eZdZdZddd�Zddd�Zd
  62  S)�FunctionAuthz�
  63      Allows the 'auth' argument to be passed as a simple callable function,
  64      that takes the request, and returns a new, modified request.
  65      �func�#typing.Callable[[Request], Request]r�NonecCs
  66  ||_dS�N�Z_func)rr+rrr�__init__ws
  67  zFunctionAuth.__init__rrrccs�|�|�VdSr.r/rrrrrzs�zFunctionAuth.auth_flowN)r+r,rr-r%)r&r'r(r)r0rrrrrr*qs
  68  r*c@s.eZdZdZddd�Zddd
  69  �Zddd�ZdS)rzy
  70      Allows the 'auth' argument to be passed as a (username, password) pair,
  71      and uses HTTP Basic authentication.
  72      �username�str | bytes�passwordrr-cCs|�||�|_dSr.)�_build_auth_header�_auth_header�rr1r3rrrr0�szBasicAuth.__init__rrrccs�|j|jd<|VdS)N�
  73  Authorization)r5�headersrrrrr�s�
  74  zBasicAuth.auth_flow�strcC�,d�t|�t|�f�}t|���}d|��S�N�:zBasic ��joinr
  75  r�decode�rr1r3Zuserpass�tokenrrrr4��
  76  zBasicAuth._build_auth_headerN�r1r2r3r2rr-r%�r1r2r3r2rr9�r&r'r(r)r0rr4rrrrr~s
  77  
  78  
  79  rc@s0eZdZdZdddd�Zddd
  80  �Zddd�ZdS)rzT
  81      Use a 'netrc' file to lookup basic auth credentials based on the url host.
  82      N�file�
  83  str | Nonerr-cCsddl}|�|�|_dS)Nr)�netrc�_netrc_info)rrFrHrrrr0�szNetRCAuth.__init__rrrccsR�|j�|jj�}|dus|ds|VdS|j|d|dd�|jd<|VdS)N�r)r1r3r7)rI�authenticators�url�hostr4r8)rrZ	auth_inforrrr�s�
  84  �
  85  zNetRCAuth.auth_flowr1r2r3r9cCr:r;r=r@rrrr4�rBzNetRCAuth._build_auth_headerr.)rFrGrr-r%rDrErrrrr�s
  86  
  87  rc	@s�eZdZUejejejejejejejejd�Zde	d<d)d	d
  88  �Z
  89  d*dd�Zd+dd�Zd,dd�Z
  90  d-dd�Zd.d"d#�Zd/d&d'�Zd(S)0r)�MD5zMD5-SESS�SHAzSHA-SESSzSHA-256zSHA-256-SESSzSHA-512zSHA-512-SESSz*dict[str, typing.Callable[[bytes], _Hash]]�_ALGORITHM_TO_HASH_FUNCTIONr1r2r3rr-cCs$t|�|_t|�|_d|_d|_dS)Nr)r
  91  �	_username�	_password�_last_challenge�_nonce_countr6rrrr0�s
  92  
  93  
  94  zDigestAuth.__init__rrrccs��|jr|�||j�|jd<|V}|jdksd|jvrdS|j�d�D]}|���d�r.nq#dS|�|||�|_d|_|�||j�|jd<|j	rRt
  95  |j	�j|d�|VdS)Nr7i�zwww-authenticatezdigest r�r)rSr4r8�status_codeZget_list�lower�
  96  startswith�_parse_challengerT�cookiesrZset_cookie_header)rrr!�auth_headerrrrr�s*�
  97  ��
  98  �
  99  zDigestAuth.auth_flowr!r	r[r9�_DigestAuthChallengec
 100  Cs�|�d�\}}}|��dksJ�i}t|�D]}|���dd�\}	}
 101  t|
 102  �||	<qz4|d��}|d��}|�dd�}
 103  d	|vrF|d	��nd
 104  }d|vrR|d��nd
 105  }t|||
 106  ||d�WSt	yr}z	d
 107  }t
 108  ||d�|�d
 109  }~ww)z�
 110          Returns a challenge from a Digest WWW-Authenticate header.
 111          These take the form of:
 112          `Digest realm="realm@host.com",qop="auth,auth-int",nonce="abc",opaque="xyz"`
 113          � �digest�=r�realm�nonce�	algorithmrN�opaqueN�qop)r`rarbrcrdz(Malformed Digest WWW-Authenticate headerrU)�	partitionrWr�strip�splitr�encode�getr\�KeyErrorr)rrr!r[�scheme�_�fieldsZheader_dict�field�key�valuer`rarbrcrd�exc�messagerrrrY�s(
 114  ���zDigestAuth._parse_challenge�	challengecsH|j|j���d�fdd�}d�|j|j|jf�}|jj}d�|j	�
 115  �|f�}||�}d|j}|�|j|j
 116  �}	|jd7_||�}
 117  |j���d	�rW|d�|
 118  |j
 119  |	f��}
 120  |j|j|d
 121  �}|durj|
 122  |j
 123  |g}n	|
 124  |j
 125  ||	||g}|j|j|j
 126  ||d�|��|j�
 127  �d�}
 128  |jr�|j|
 129  d<|r�d
 130  |
 131  d<||
 132  d<|	|
 133  d<d|�|
 134  �S)N�data�bytesrcs�|�����Sr.)�	hexdigestrh)rt�Z	hash_funcrrr^sz-DigestAuth._build_auth_header.<locals>.digestr<s%08xrz-sessrU)r1r`ra�urir!rbrc�authrd�nc�cnoncezDigest )rtrurru)rPrb�upperr>rQr`rRrL�raw_path�methodrhrT�_get_client_noncerarW�endswith�_resolve_qoprdrc�_get_header_value)rrrsr^�A1�path�A2�HA2Znc_valuer{�HA1rdZdigest_dataZformat_argsrrwrr4�s<
 135  �
 136  zDigestAuth._build_auth_header�nonce_count�intrarucCsLt|���}||7}|t����7}|t�d�7}t�|���dd���S)N��)	r9rh�time�ctime�os�urandom�hashlib�sha1rv)rr�ra�srrrr/s
 137  zDigestAuth._get_client_nonce�
 138  header_fields�dict[str, bytes]c
 139  Csbd}d}d}d}t|���D] \}\}}|dkr|d7}||vr"|n|}	||	�|t|��7}q|S)N)rbrdrzz{}="{}"z{}={}�rz, )�	enumerate�items�formatr)
 140  rr�ZNON_QUOTED_FIELDSZQUOTED_TEMPLATEZNON_QUOTED_TEMPLATE�header_value�irnrp�templaterrrr�7s��zDigestAuth._get_header_valuerd�bytes | NonecCsN|durdSt�d|�}d|vrdS|dgkrtd��d|�d�}t||d��)Ns, ?rysauth-intz.Digest auth-int support is not yet implementedzUnexpected qop value "z" in digest authrU)�rerg�NotImplementedErrorr)rrdrZqopsrrrrrr�Is
 141  zDigestAuth._resolve_qopNrCr%)rrr!r	r[r9rr\)rrrsr\rr9)r�r�rarurru)r�r�rr9)rdr�rrrr�)r&r'r(r��md5r��sha256�sha512rP�__annotations__r0rrYr4rr�r�rrrrr�s"
 142  �
 143  
 144  
 145  
 146  
 147  0
 148  rc@s6eZdZUded<ded<ded<ded<ded<d	S)
 149  r\rur`rar9rbr�rcrdN)r&r'r(r�rrrrr\Ws
 150  r\)�
 151  __future__rr�r�r�r��typing�base64r�urllib.requestr�_exceptionsrZ_modelsrrr	�_utilsr
 152  rr�
 153  TYPE_CHECKINGr
 154  �__all__rr*rrr�
 155  NamedTupler\rrrr�<module>s*[
 156  )