
    rp0jbJ                        % S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	J
r
Jr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr   " S S\SS9r\R4                  \R4                  \R6                  \R6                  \R8                  \R8                  \R8                  \R8                  \R:                  \R:                  \R:                  \R:                  S.r\\ \\!/S4   4   \"S'   \RF                  " \RH                  S:  a  SOS5      r%Sr&\	\'\
S   S4      \"S'   \'" \(" \RS                  5       5      5      r*\	\'\ S4      \"S'   \+" 1 Sk5      r,\	\+\       \"S'   S\ S\ 4S jr-S\ S\ 4S jr.S \ S\\ \ 4   4S! jr/ " S" S#5      r0g)$av  
Digest authentication middleware for aiohttp client.

This middleware implements HTTP Digest Authentication according to RFC 7616,
providing a more secure alternative to Basic Authentication. It supports all
standard hash algorithms including MD5, SHA, SHA-256, SHA-512 and their session
variants, as well as both 'auth' and 'auth-int' quality of protection (qop) options.
    N)Callable)FinalLiteral	TypedDict)URL   )hdrs)ClientError)ClientHandlerType)ClientRequestClientResponse)Payloadc                   \    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   S	rg
)DigestAuthChallenge   realmnonceqop	algorithmopaquedomainstale N)__name__
__module____qualname____firstlineno__str__annotations____static_attributes__r       _/home/wildlama/miniconda3/lib/python3.13/site-packages/aiohttp/client_middleware_digest_auth.pyr   r      s%    JJ	HNKKJr!   r   F)total)MD5zMD5-SESSSHAzSHA-SESSSHA256zSHA256-SESSzSHA-256zSHA-256-SESSSHA512zSHA512-SESSzSHA-512zSHA-512-SESSzhashlib._HashDigestFunctions)      z:(?:^|\s|,\s*)(\w+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s,]+))z>(?:^|\s|,\s*)((?>\w+))\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s,]+)))r   r   r   r   r   r   r   .CHALLENGE_FIELDSSUPPORTED_ALGORITHMS>   urir   r   cnoncer   responseusernameQUOTED_AUTH_FIELDSvaluereturnc                 &    U R                  SS5      $ )z,Escape double quotes for HTTP header values."\"replacer2   s    r"   escape_quotesr:   l   s    ==e$$r!   c                 &    U R                  SS5      $ )z-Unescape double quotes in HTTP header values.r6   r5   r7   r9   s    r"   unescape_quotesr<   q   s    ==$$r!   headerc           	          [         R                  U 5       VVVs0 s H3  u  pnUR                  5       =n(       d  M  XB(       a  [        U5      OU_M5     snnn$ s  snnnf )aI  
Parse key-value pairs from WWW-Authenticate or similar HTTP headers.

This function handles the complex format of WWW-Authenticate header values,
supporting both quoted and unquoted values, proper handling of commas in
quoted values, and whitespace variations per RFC 7616.

Examples of supported formats:
  - key1="value1", key2=value2
  - key1 = "value1" , key2="value, with, commas"
  - key1=value1,key2="value2"
  - realm="example.com", nonce="12345", qop="auth"

Args:
    header: The header value string to parse

Returns:
    Dictionary mapping parameter names to their values
)_HEADER_PAIRS_PATTERNfindallstripr<   )r=   key
quoted_valunquoted_valstripped_keys        r"   parse_header_pairsrF   v   s\    , .C-J-J6-R-R)C\IIK'L' 	RZoj1\Q-R  s
   AAc            	           \ rS rSrSr SS\S\S\SS4S jjrS	\S
\S\	\
S   -  S\4S jrS
\S\4S jrS\S\4S jrS\S\S\4S jrSrg)DigestAuthMiddleware   a  
HTTP digest authentication middleware for aiohttp client.

This middleware intercepts 401 Unauthorized responses containing a Digest
authentication challenge, calculates the appropriate digest credentials,
and automatically retries the request with the proper Authorization header.

Features:
- Handles all aspects of Digest authentication handshake automatically
- Supports all standard hash algorithms:
  - MD5, MD5-SESS
  - SHA, SHA-SESS
  - SHA256, SHA256-SESS, SHA-256, SHA-256-SESS
  - SHA512, SHA512-SESS, SHA-512, SHA-512-SESS
- Supports 'auth' and 'auth-int' quality of protection modes
- Properly handles quoted strings and parameter parsing
- Includes replay attack protection with client nonce count tracking
- Supports preemptive authentication per RFC 7616 Section 3.6

Origin scoping:
The credentials are scoped to the origin of the first request the
middleware handles. A request to a different origin is passed through
untouched, so it never receives a digest response computed from those
credentials, unless that origin falls within a protection space the
anchor origin advertised through the RFC 7616 ``domain`` directive. Make
the first request through the middleware against the intended origin, as
the anchor is pinned to it and not reset for the life of the instance.

Standards compliance:
- RFC 7616: HTTP Digest Access Authentication (primary reference)
- RFC 2617: HTTP Authentication (deprecated by RFC 7616)
- RFC 1945: Section 11.1 (username restrictions)

Implementation notes:
The core digest calculation is inspired by the implementation in
https://github.com/requests/requests/blob/v2.18.4/requests/auth.py
with added support for modern digest auth features and error handling.
loginpassword
preemptiver3   Nc                    Uc  [        S5      eUc  [        S5      eSU;   a  [        S5      eXl        UR                  S5      U l        UR                  S5      U l        SU l        SU l        0 U l        X0l        / U l	        S U l
        g )Nz"None is not allowed as login valuez%None is not allowed as password value:z8A ":" is not allowed in username (RFC 1945#section-11.1)utf-8r!   r   )
ValueError
_login_strencode_login_bytes_password_bytes_last_nonce_bytes_nonce_count
_challenge_preemptive_protection_space_origin)selfrJ   rK   rL   s       r"   __init__DigestAuthMiddleware.__init__   s     =ABBDEE%<WXX&+*/,,w*?-5__W-E!$/1!+,.#'r!   methodurlbodyr!   c           
      x	  ^#^$#    U R                   nSU;  a  [        S5      eSU;  a  [        S5      eUS   nUS   nU(       d  [        S5      eUR                  SS5      nUR                  SS	5      nUR                  5       n	UR                  S
S5      n
UR	                  S5      nUR	                  S5      n[        U5      R                  nSnSnU(       a  SS1R                  UR                  S5       Vs1 s H*  nUR                  5       (       d  M  UR                  5       iM,     sn5      nU(       d  [        SU 35      eSU;   a  SOSnUR	                  S5      nU	[        ;  a$  [        SU	 SSR                  [        5       35      e[        U	   m$S[        S[        4U$4S jjm#S[        S[        S[        4U#4S jjnSR                  U R                  XR                  45      nUR                  5        SU 3R	                  5       nUS:X  aK  [!        U["        5      (       a  UR%                  5       I Sh  vN nOUnT#" U5      nSR                  UU45      nT#" U5      nT#" U5      nXR&                  :X  a  U =R(                  S-  sl        OSU l        Xl        U R(                  S nUR	                  S5      n[*        R,                  " SR                  [/        U R(                  5      R	                  S5      U[0        R2                  " 5       R	                  S5      [4        R6                  " S5      /5      5      R9                  5       SS  nUR	                  S5      nU	R                  5       R;                  S!5      (       a  T#" SR                  UUU45      5      nU(       a   SR                  UUUUU45      nU" UU5      nOU" USR                  UU45      5      n[=        U R>                  5      [=        U5      [=        U5      UURA                  5       US".nU
(       a  [=        U
5      US
'   U(       a  UUS'   UUS#'   UUS$'   / n URC                  5        H?  u  n!n"U![D        ;   a  U RG                  U! S%U" S&35        M)  U RG                  U! S'U" 35        MA     S(SR                  U 5       3$ s  snf  GN{7f))a}  
Build digest authorization header for the current challenge.

Args:
    method: The HTTP method (GET, POST, etc.)
    url: The request URL
    body: The request body (used for qop=auth-int)

Returns:
    A fully formatted Digest authorization header string

Raises:
    ClientError: If the challenge is missing required parameters or
                 contains unsupported values

r   z:Malformed Digest auth challenge: Missing 'realm' parameterr   z:Malformed Digest auth challenge: Missing 'nonce' parameterzBSecurity issue: Digest auth challenge contains empty 'nonce' valuer    r   r$   r   rO   r!   authzauth-int,zEDigest auth error: Unsupported Quality of Protection (qop) value(s): z/Digest auth error: Unsupported hash algorithm: z. Supported algorithms: z, xr3   c                 L   > T" U 5      R                  5       R                  5       $ )z<RFC 7616 Section 3: Hash function H(data) = hex(hash(data)).)	hexdigestrR   )re   hash_fns    r"   H'DigestAuthMiddleware._encode.<locals>.H   s    1:'')0022r!   sdc                 4   > T" SR                  X45      5      $ )zDRFC 7616 Section 3: KD(secret, data) = H(concat(secret, ":", data)).   :)join)rk   rl   ri   s     r"   KD(DigestAuthMiddleware._encode.<locals>.KD$  s    TYYv&''r!   rn   rN   Nr   08x      z-SESS)r0   r   r   r-   r/   r   ncr.   z="r5   =zDigest )$rW   r
   getupperrR   r   raw_path_qsintersectionsplitrA   r(   ro   r,   bytesrS   rT   
isinstancer   as_bytesrU   rV   hashlibsha1r   timectimeosurandomrg   endswithr:   rQ   decodeitemsr1   append)%r[   r^   r_   r`   	challenger   r   qop_rawalgorithm_originalr   r   nonce_bytesrealm_bytespathr   	qop_bytesq
valid_qopsrp   A1A2entity_bytesentity_hashHA1HA2ncvaluencvalue_bytesr.   cnonce_bytesnoncebitresponse_digestheader_fieldspairsfieldr2   ri   rh   s%                                      @@r"   _encodeDigestAuthMiddleware._encode   s    " OO	)#L  )#L 
 '"'" T  --r*&]];>&,,.	x, ll7+ll7+
 3x## 	 *-::$+MM#$6D$6q!'')$6DJ ![\c[de  !+j 8*fC

7+IO+A) M))-3G)H(IK  )3	3 	35 	3	(% 	(E 	(e 	(
 YY));8L8LMNq'..0*$((%)]]_4#L/KB,-Bee 000" !D!,&&s+w/ HH))*11':JJL''0JJqM		
 )+cr	 }}W- ??%%g..DIIsK>?@C yym\9cJH !h/O diic0B&CDO &doo6"5)"5)'..0+
 &3F&;M(# #&M% ")M$&,M(# )//1LE5**wbq12waw/0	 2 5)*++S E<  5s&   C3R:7R2R2(DR:<R7=I;R:c                     [        U5      nU R                   HR  nUR                  U5      (       d  M  [        U5      [        U5      :X  d	  US   S:X  a    gU[        U5         S:X  d  MR    g   g)z
Check if the given URL is within the current protection space.

According to RFC 7616, a URI is in the protection space if any URI
in the protection space is a prefix of it (after both have been made absolute).
/TF)r   rY   
startswithlen)r[   r_   request_str	space_strs       r"   _in_protection_space)DigestAuthMiddleware._in_protection_spacez  si     #h//I)))44;3y>1Yr]c5I3y>*c1 0 r!   r/   c           
         UR                   S:w  a  gUR                  R                  SS5      nU(       d  gUR                  S5      u  p4nU(       d  gUR	                  5       S:w  a  gU(       d  g[        U5      =n(       d  g0 U l        [         H'  nUR                  U5      =nc  M  XR                  U'   M)     UR                  R                  5       n	U R                  R                  S5      =n
(       a  / U l
        U
R                  5        H  nUR                  S5      nUR                  S	5      (       a>  U R                  R                  [        U	R!                  [#        U5      5      5      5        Mh  U R                  R                  [        [#        U5      5      5        M     O[        U	5      /U l
        [%        U R                  5      $ )
zr
Takes the given response and tries digest-auth, if needed.

Returns true if the original request must be resent.
i  Fzwww-authenticaterb    digestr   r5   r   )statusheadersrw   	partitionlowerrF   rW   r+   r_   originrY   r{   rA   r   r   r   ro   r   bool)r[   r/   auth_headerr^   sepr   header_pairsr   r2   r   r   r-   s               r"   _authenticate"DigestAuthMiddleware._authenticate  su    ??c!&&**+=rB*44S9W<<>X% !37 ;;; %E%))%00=).& &
 $$&__((2262%'D"||~iin>>#&&**11#fkk#c(6K2LM **11#c#h-@ & '*&k]D" DOO$$r!   requesthandlerc                   #    UR                   R                  5       nU R                  c  X0l        O?X0R                  :w  a0  U R                  UR                   5      (       d  U" U5      I Sh  vN $ Sn[	        S5       H  nUS:  dB  U R
                  (       a  U R                  (       at  U R                  UR                   5      (       aT  U R                  UR                  UR                   UR                  5      I Sh  vN UR                  [        R                  '   U" U5      I Sh  vN nU R                  U5      (       a  M    O   Uc   eU$  N NT N+7f)zRun the digest auth middleware.N   r   )r_   r   rZ   r   rangerX   rW   r   r^   r`   r   r	   AUTHORIZATIONr   )r[   r   r   r   r/   retry_counts         r"   __call__DigestAuthMiddleware.__call__  s     ##%<<!L||#D,E,Egkk,R,R ))) 8K Q  OO--gkk::<@LLNNGKK= 7 2 23
 %W--H %%h//% $* ###3 *7
 .s=   A)E+E,BE<E=*E'E(EEEE)	rW   rU   rS   rQ   rV   rZ   rT   rX   rY   )T)r   r   r   r   __doc__r   r   r\   r   r   r   r   r   r   r   r   r   r   r    r   r!   r"   rH   rH      s    %V  	(( ( 	(
 
(8c,C c,c c,73<9O c,TW c,J  (9%n 9% 9%v%$%/@%	%r!   rH   )1r   r   r   resysr   collections.abcr   typingr   r   r   yarlr   rb   r	   client_exceptionsr
   client_middlewaresr   client_reqrepr   r   payloadr   r   md5r   sha256sha512r(   dictr   r|   r   compileversion_infor?   r+   tuplesortedkeysr,   	frozensetr1   r:   r<   rF   rH   r   r!   r"   <module>r      s    	 	 
  $ , ,   * 1 8 )5  ;;<<nn>>~~NNnn>>~~NNBc8UG_$<==> " 


'! B	J <	 %	QRTWW   05VO<P<P<R5S/T eE#s(O, T -6I- E)C.) 
% % %
%3 %3 %
s tCH~ 6] ]r!   