Ë
    Eü´jáL  ã                   ó  — U d 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	m
Z
mZ ddlm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  G d„ ded¬«      Zej4                  ej4                  ej6                  ej6                  ej8                  ej8                  ej8                  ej8                  ej:                  ej:                  ej:                  ej:                  dœZee ee!gdf   f   e"d<    ejF                  ejH                  dk  rdnd«      Z%dZ&e	e'e
d   df      e"d<    e' e(ejS                  «       «      «      Z*e	e'e df      e"d<    e+h d£«      Z,e	e+e       e"d<   de de fd„Z-de de fd„Z.d e dee e f   fd!„Z/ G d"„ d#«      Z0y)$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                   óT   — e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   eed<   y)	ÚDigestAuthChallengeÚrealmÚnonceÚqopÚ	algorithmÚopaqueÚdomainÚstaleN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__© ó    úc/root/workspace/ytshorts/venv/lib/python3.12/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,]+)))?zC(?:^|\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                 óF   — | j                  dd«      j                  dd«      S )z=Escape backslashes and double quotes for HTTP quoted-strings.ú\ú\\ú"ú\"©Úreplace©r/   s    r   Úescape_quotesr9   h   s    € à�=‰=˜˜vÓ&×.Ñ.¨s°EÓ:Ð:r   c                 óF   — | j                  dd«      j                  dd«      S )z>Unescape backslashes and double quotes in HTTP quoted-strings.r5   r4   r3   r2   r6   r8   s    r   Úunescape_quotesr;   m   s    € à�=‰=˜ Ó$×,Ñ,¨V°TÓ:Ð:r   Úheaderc                 óÜ   — i }t         j                  | «      D ]Q  }|j                  d«      }|j                  d«      |j                  d«      }}|€|€|r |S Œ@|�t        |«      n|||<   ŒS |S )a�  
    Parse key-value pairs from the first challenge of a WWW-Authenticate header.

    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.

    A single header may carry several challenges
    (https://www.rfc-editor.org/rfc/rfc7235#section-4.1). Parsing
    stops at the next auth-scheme token so a later challenge's parameters cannot
    overwrite the first challenge's values; a leading scheme token is skipped.

    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
    r   é   r&   )Ú_HEADER_PAIRS_PATTERNÚfinditerÚgroupr;   )r<   ÚpairsÚmatchÚkeyÚ
quoted_valÚunquoted_vals         r   Úparse_header_pairsrG   r   s‡   € ð2 €EÜ&×/Ñ/°Ó7ò 
ˆØ�k‰k˜!‹nˆØ#(§;¡;¨q£>°5·;±;¸q³>�Lˆ
ØÐ ,Ð"6ñ Øð
 €Lð	 à+5Ð+AŒO˜JÔ'À|ð 	ˆcŠ
ð
ð €Lr   c            	       ó„   — e Zd ZdZ	 ddedededdfd„Zded	ed
ee	d   z  defd„Z
d	edefd„Zdedefd„Zdededefd„Zy)ÚDigestAuthMiddlewarea1  
    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Ú
preemptiver0   Nc                 ó  — |€t        d«      ‚|€t        d«      ‚d|v rt        d«      ‚|| _        |j                  d«      | _        |j                  d«      | _        d| _        d| _        i | _        || _        g | _	        d | _
        y )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__zDigestAuthMiddleware.__init__Ä   s‘   € ð ˆ=ÜÐAÓBÐBàÐÜÐDÓEÐEà�%‰<ÜÐWÓXÐXà&+ˆŒØ*/¯,©,°wÓ*?ˆÔØ-5¯_©_¸WÓ-EˆÔà!$ˆÔØˆÔØ/1ˆŒØ!+ˆÔà,.ˆÔà#'ˆ�r   ÚmethodÚurlÚbodyr   c           
   ƒ   óð  ‡#‡$K  — | j                   }d|vrt        d«      ‚d|vrt        d«      ‚|d   }|d   }|st        d«      ‚|j                  dd«      }|j                  dd	«      }|j                  «       }	|j                  d
d«      }
|j	                  d«      }|j	                  d«      }t        |«      j                  }d}d}|rxddhj                  |j                  d«      D �ch c]#  }|j                  «       sŒ|j                  «       ’Œ% c}«      }|st        d|› �«      ‚d|v rdnd}|j	                  d«      }|	t        vr$t        d|	› ddj                  t        «      › �«      ‚t        |	   Š$dt        dt        fˆ$fd„Š#dt        dt        dt        fˆ#fd„}dj                  | j                  || j                  f«      }|j                  «       › d|› �j	                  «       }|dk(  rFt!        |t"        «      r|j%                  «       ƒ d{  –—† }n|} ‰#|«      }dj                  ||f«      } ‰#|«      } ‰#|«      }|| j&                  k(  r| xj(                  dz  c_        nd| _        || _        | j(                  d›}|j	                  d«      }t+        j,                  dj                  t/        | j(                  «      j	                  d«      |t1        j2                  «       j	                  d«      t5        j6                  d«      g«      «      j9                  «       dd  }|j	                  d«      }|	j                  «       j;                  d!«      r ‰#dj                  |||f«      «      }|r dj                  |||||f«      } |||«      }n ||dj                  ||f«      «      }t=        | j>                  «      t=        |«      t=        |«      ||jA                  «       |d"œ}|
rt=        |
«      |d
<   |r||d<   ||d#<   ||d$<   g } |jC                  «       D ];  \  }!}"|!tD        v r| jG                  |!› d%|"› d&�«       Œ&| jG                  |!› d'|"› �«       Œ= d(dj                  | «      › �S c c}w 7 �Œ`­w))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, Úxr0   c                 óL   •—  ‰| «      j                  «       j                  «       S )z<RFC 7616 Section 3: Hash function H(data) = hex(hash(data)).)Ú	hexdigestrR   )rd   Úhash_fns    €r   ÚHz'DigestAuthMiddleware._encode.<locals>.H+  s   ø€ á˜1“:×'Ñ'Ó)×0Ñ0Ó2Ð2r   ÚsÚdc                 ó6   •—  ‰dj                  | |f«      «      S )zDRFC 7616 Section 3: KD(secret, data) = H(concat(secret, ":", data)).ó   :)Újoin)ri   rj   rh   s     €r   ÚKDz(DigestAuthMiddleware._encode.<locals>.KD/  s   ø€ á�T—Y‘Y  1˜vÓ&Ó'Ð'r   rl   rN   Nr   Ú08xé   é   z-SESS)r-   r   r   r*   r,   r   Úncr+   z="r4   ú=zDigest )$rW   r
   ÚgetÚupperrR   r   Úraw_path_qsÚintersectionÚsplitÚstripr%   rm   r)   ÚbytesrS   rT   Ú
isinstancer   Úas_bytesrU   rV   ÚhashlibÚsha1r   ÚtimeÚctimeÚosÚurandomrf   Úendswithr9   rQ   ÚdecodeÚitemsr.   Úappend)%r[   r]   r^   r_   Ú	challenger   r   Úqop_rawÚalgorithm_originalr   r   Únonce_bytesÚrealm_bytesÚpathr   Ú	qop_bytesÚqÚ
valid_qopsrn   ÚA1ÚA2Úentity_bytesÚentity_hashÚHA1ÚHA2ÚncvalueÚncvalue_bytesr+   Úcnonce_bytesÚnoncebitÚresponse_digestÚheader_fieldsrB   Úfieldr/   rh   rg   s%                                      @@r   Ú_encodezDigestAuthMiddleware._encodeà   s¡  ùè ø€ ð" —O‘Oˆ	Ø˜)Ñ#ÜØLóð ð ˜)Ñ#ÜØLóð ð
 ˜'Ñ"ˆØ˜'Ñ"ˆñ ÜØTóð ð —-‘-  rÓ*ˆà&Ÿ]™]¨;¸Ó>ÐØ&×,Ñ,Ó.ˆ	Ø—‘˜x¨Ó,ˆð —l‘l 7Ó+ˆØ—l‘l 7Ó+ˆô
 �3‹x×#Ñ#ˆð ˆØˆ	ÙØ  *Ð-×:Ñ:Ø$+§M¡M°#Ó$6ÖD˜q¸!¿'¹'½)�—‘•ÒDóˆJñ Ü!Ø[Ð\cÐ[dÐeóð ð !+¨jÑ 8‘*¸fˆCØŸ
™
 7Ó+ˆIàœOÑ+ÜØAÀ)Àð M)Ø)-¯©Ô3GÓ)HÐ(IðKóð ô )¨Ñ3ˆð	3”ð 	3œ5õ 	3ð	(”%ð 	(œEð 	(¤eõ 	(ð
 �Y‰Y˜×)Ñ)¨;¸×8LÑ8LÐMÓNˆØ—‘“Ð˜q  Ð'×.Ñ.Ó0ˆØ�*ÒÜ˜$¤Ô(Ø%)§]¡]£_×4‘à#�Ù˜L›/ˆKØ—‘˜B Ð,Ó-ˆBá�‹eˆÙ�‹eˆð ˜$×0Ñ0Ò0Ø×Ò Ñ"Öà !ˆDÔà!,ˆÔØ×&Ñ& sÐ+ˆØŸ™ wÓ/ˆô —‘Ø�H‰Hä˜×)Ñ)Ó*×1Ñ1°'Ó:ØÜ—J‘J“L×'Ñ'¨Ó0Ü—J‘J˜q“Mð	óó	
÷ ‰)‹+�c�rð	ˆð —}‘} WÓ-ˆð �?‰?Ó×%Ñ% gÔ.Ù�D—I‘I˜s K°Ð>Ó?Ó@ˆCñ Ø—y‘yØ˜m¨\¸9ÀcÐJóˆHñ !  hÓ/‰Oá   d§i¡i°¸cÐ0BÓ&CÓDˆOô & d§o¡oÓ6Ü" 5Ó)Ü" 5Ó)ØØ'×.Ñ.Ó0Ø+ñ
ˆñ Ü&3°FÓ&;ˆM˜(Ñ#ñ Ø#&ˆM˜%Ñ Ø")ˆM˜$ÑØ&,ˆM˜(Ñ#ð ˆØ)×/Ñ/Ó1ò 	1‰LˆE�5ØÔ*Ñ*Ø—‘ ˜w b¨¨¨qÐ1Õ2à—‘ ˜w a¨ wÐ/Õ0ð		1ð ˜Ÿ™ 5Ó)Ð*Ð+Ð+ùòS Eð<  5ûs&   „C%Q6Ã)Q.Ã?Q.ÄDQ6ÈQ3ÈI Q6c                 óÈ   — t        |«      }| j                  D ]H  }|j                  |«      sŒt        |«      t        |«      k(  s|d   dk(  r y|t        |«         dk(  sŒH y y)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_spacez)DigestAuthMiddleware._in_protection_space…  sk   € ô ˜#“hˆØ×/Ñ/ò 		ˆIà×)Ñ)¨)Ô4Øä�;Ó¤3 y£>Ò1°Y¸r±]ÀcÒ5IÙàœ3˜y›>Ñ*¨cÓ1Ùð		ð r   r,   c           
      ól  — |j                   dk7  ry|j                  j                  dd«      }|sy|j                  d«      \  }}}|sy|j	                  «       dk7  ry|syt        |«      x}syi | _        t        D ]%  }|j                  |«      x}€Œ|| j                  |<   Œ' |j                  j                  «       }	g | _
        | j                  j                  d«      x}
r¤|
j                  «       D ]‘  }|j                  d«      }|sŒ|j                  d	«      r=| j                  j                  t        |	j!                  t#        |«      «      «      «       Œe| j                  j                  t        t#        |«      «      «       Œ“ | j                  st        |	«      g| _
        t%        | j                  «      S )
zŠ
        Takes the given response and tries digest-auth, if needed.

        Returns true if the original request must be resent.
        i‘  Fzwww-authenticatera   ú Údigestr   r4   r    )ÚstatusÚheadersrt   Ú	partitionÚlowerrG   rW   r(   r^   ÚoriginrY   rx   ry   r¡   r†   r   rm   r   Úbool)r[   r,   Úauth_headerr]   Úseprª   Úheader_pairsrœ   r/   r­   r   r*   s               r   Ú_authenticatez"DigestAuthMiddleware._authenticate™  s‘  € ð �?‰?˜cÒ!Øà×&Ñ&×*Ñ*Ð+=¸rÓBˆÙØà*×4Ñ4°SÓ9Ñˆ��WÙàà�<‰<‹>˜XÒ%àáàô !3°7Ó ;Ð;�Ð;àð ˆŒÜ%ò 	/ˆEØ%×)Ñ)¨%Ó0Ð0�Ñ=Ø).�—‘ Ò&ð	/ð
 —‘×$Ñ$Ó&ˆØ!#ˆÔà—_‘_×(Ñ(¨Ó2Ð2ˆ6Ð2à—|‘|“~ò 
A�à—i‘i “n�ÙØØ—>‘> #Ô&à×*Ñ*×1Ñ1´#°f·k±kÄ#ÀcÃ(Ó6KÓ2LÕMð ×*Ñ*×1Ñ1´#´c¸#³h³-Õ@ð
Að ×%Ò%Ü&)¨&£k ]ˆDÔ"ô �D—O‘OÓ$Ð$r   ÚrequestÚhandlerc              ƒ   ót  K  — |j                   j                  «       }| j                  €|| _        n:|| j                  k7  r+| j                  |j                   «      s ||«      ƒ d{  –—† S d}t	        d«      D ]±  }|dkD  s3| j
                  r{| j                  ro| j                  |j                   «      rT| j                  |j                  |j                   |j                  «      ƒ d{  –—† |j                  t        j                  <    ||«      ƒ d{  –—† }| j                  |«      rŒ± n |€J ‚|S 7 ŒË7 ŒL7 Œ#­w)zRun the digest auth middleware.Nr>   r   )r^   r­   rZ   r¥   ÚrangerX   rW   r�   r]   r_   rª   r	   ÚAUTHORIZATIONr²   )r[   r³   r´   r­   r,   Úretry_counts         r   Ú__call__zDigestAuthMiddleware.__call__Ö  s  è ø€ ð —‘×#Ñ#Ó%ˆØ�<‰<ÐØ!ˆD�LØ�t—|‘|Ò#¨D×,EÑ,EÀgÇkÁkÔ,RÙ  Ó)×)Ð)àˆÜ  ›8ò 	ˆKð ˜QŠØ× Ò Ø—O’OØ×-Ñ-¨g¯k©kÔ:à<@¿L¹LØ—N‘N G§K¡K°·±ó=÷ 7�—‘¤× 2Ñ 2Ñ3ñ
 % WÓ-×-ˆHð ×%Ñ% hÕ/Ùð%	ð* Ð#Ð#Ð#Øˆð3 *øð7øð
 .ús=   ‚A$D8Á&D2Á'B D8Ã'D4Ã(*D8ÄD6ÄD8Ä)
D8Ä4D8Ä6D8)T)r   r   r   Ú__doc__r   r®   r\   r   r   r   r�   r¥   r   r²   r   r   r¹   r   r   r   rI   rI   œ   s­   „ ñ%ðV  ñ	(àð(ð ð(ð ð	(ð
 
ó(ð8c, Cð c,¨cð c,¸À7È3Á<Ñ9Oð c,ÐTWó c,ðJ¨ð °ó ð(;% nð ;%¸ó ;%ðz%Ø$ð%Ø/@ð%à	ô%r   rI   )1rº   r}   r�   ÚreÚsysr   Úcollections.abcr   Útypingr   r   r   Úyarlr   ra   r	   Úclient_exceptionsr
   Úclient_middlewaresr   Úclient_reqrepr   r   Úpayloadr   r   Úmd5r~   Úsha256Úsha512r%   Údictr   rz   r   ÚcompileÚversion_infor?   r(   ÚtupleÚsortedÚkeysr)   Ú	frozensetr.   r9   r;   rG   rI   r   r   r   ú<module>rÎ      s±  ðòó Û 	Û 	Û 
Û Ý $ß ,Ñ ,å å Ý *Ý 1ß 8Ý ô˜)¨5õ ð �;‰;Ø—‘Ø�<‰<Ø—‘Ø�n‰nØ—>‘>Ø�~‰~Ø—N‘NØ�n‰nØ—>‘>Ø�~‰~Ø—N‘NñB€��c˜8 U G¨_Ð$<Ñ=Ð=Ñ>ó ð" #˜Ÿ
™
à
×Ñ˜'Ò!ñ Gà	OóÐ ð4ð	 �%Ø	ØÐQÑRÐTWÐWññó ñ  05±V¸O×<PÑ<PÓ<RÓ5SÓ/TÐ �e˜E # s (™OÑ,Ó Tñ -6ÚIó-Ð �E˜) C™.Ñ)ó ð
;˜ð ; ó ;ð
;˜3ð ; 3ó ;ð
'˜sð ' t¨C°¨H¡~ó '÷T_ò _r   