
    iw,                        S SK Jr  S SKrS SKrS SKrS SKJr  S SKrS SK	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  S S	KJr   " S
 S5      r " S S\5      rSS jrSS jr " S S\5      r " S S\5      rg)    )annotationsN)urlparse)x509)UnsupportedAlgorithm)default_backend)hashes)AuthBase)HTTPResponsec                  J    \ rS rSrSrS
S jrSS jrSS jrSS jrSS jr	Sr
g	)ShimSessionSecurity   z5Shim used for backwards compatibility with ntlm-auth.c                    Xl         g N_context)selfcontexts     U/home/james-whalen/.local/lib/python3.13/site-packages/requests_ntlm/requests_ntlm.py__init__ShimSessionSecurity.__init__   s        c                x    U R                   R                  USS9nUR                  S S nUR                  SS  nXC4$ )NT)encrypt   )r   wrapdata)r   messagewrap_res	signaturer   s        r   r   ShimSessionSecurity.wrap   sC    ==%%gt%<MM#2&	}}RS!r   c                T    X!-   nU R                   R                  U5      R                  $ r   )r   unwrapr   )r   r   r   r   s       r   r"   ShimSessionSecurity.unwrap   s%    "}}##D)...r   c                8    U R                   R                  U5      $ r   )r   sign)r   r   s     r   get_signature!ShimSessionSecurity.get_signature#   s    }}!!'**r   c                :    U R                   R                  X5        g r   )r   verify)r   r   r   s      r   verify_signature$ShimSessionSecurity.verify_signature&   s    W0r   r   N)r   zspnego.ContextProxyreturnNone)r   bytesr,   ztuple[bytes, bytes])r   r.   r   r.   r,   r.   )r   r.   r,   r.   )r   r.   r   r.   r,   r-   )__name__
__module____qualname____firstlineno____doc__r   r   r"   r&   r*   __static_attributes__ r   r   r   r      s    ? /+1r   r   c                  |    \ rS rSrSr  S
         SS jjr            SS jrSS jrSS jrSS jr	S	r
g)HttpNtlmAuth*   zI
HTTP NTLM Authentication Handler for Requests.

Supports pass-the-hash.
Nc                6    Xl         X l        X@l        SU l        g)a&  Create an authentication handler for NTLM over HTTP.

:param str username: Username in 'domain\username' format
:param str password: Password
:param str session: Unused. Kept for backwards-compatibility.
:param bool send_cbt: Will send the channel bindings over a HTTPS channel (Default: True)
N)usernamepasswordsend_cbtsession_security)r   r:   r;   sessionr<   s        r   r   HttpNtlmAuth.__init__1   s     !  
 =Ar   c           
       ^ U R                  U5      nS nU(       a   [        R                  R                  SU-   S9n X#R                  R
                  ;   a  U$ [        UR                  R
                  R                  SS5      SS9nUR                  R                  (       ay  [        UR                  R                  S5      (       aT  US:  a(  UR                  R                  R                  U* S	5        O&UR                  R                  R                  SS5        UR                    UR                  R                  5         UR                  R                  5       n	[        R                   " ["        [%        UR&                  5      R(                  5      n
[        R*                  R,                  nU R.                  (       a+  U R0                  (       a  [        R*                  R2                  n[        R4                  " U R.                  U R0                  S
UU
SUS9n[6        R8                  " UR;                  5       =(       d    S5      R=                  5       nU< SU< 3nXR
                  U'   [?        USS9nUR@                  RB                  " U	40 UD6nUR                    UR                  R                  5         UR                  R                  5       n	UR
                  R                  S5      (       a(  UR
                  R                  S5      U	R
                  S'   UR
                  U   nUS-   m[E        U4S jS URG                  S5       5        5       S 5      nU(       d  [I        S5      e[6        RJ                  " U[M        T5      S  RO                  5       5      n[6        R8                  " UR;                  U5      =(       d    S5      R=                  5       nU< SU< 3nXR
                  U'   UR@                  RB                  " U	40 UD6nURP                  RS                  U5        URP                  RS                  U5        [U        U5      U l+        U$ )Ns   tls-server-end-point:)application_datazContent-Length0
   )baseseekr      ntlmhttp)protocolchannel_bindingshostnameserviceoptionsr    F)streamz
set-cookieCookiec              3  t   >#    U  H-  nUR                  T5      (       d  M  UR                  5       v   M/     g 7fr   )
startswithstrip).0s
auth_strips     r   	<genexpr>:HttpNtlmAuth.retry_using_http_NTLM_auth.<locals>.<genexpr>   s.      OA<<
+ 		Os   88c              3  @   #    U  H  oR                  5       v   M     g 7fr   )lstrip)rT   vals     r   rW   rX      s     O2N3**,,2Ns   ,z?Access denied: Server did not respond with NTLM challenge token),_get_server_certspnegorJ   GssChannelBindingsrequestheadersintgetbodyhasattrrE   contentrawrelease_conncopytcaststrr   urlrK   NegotiateOptionsnoner:   r;   use_ntlmclientbase64	b64encodestepdecodedict
connectionsendnextsplitPermissionError	b64decodelenencodehistoryappendr   r=   )r   auth_header_fieldauth_headerresponse	auth_typeargsserver_certificate_hashcbtcontent_lengthr`   target_hostnamespnego_optionsrq   negotiate_messageauthargs_nostream	response2auth_header_valuentlm_header_valuer[   authenticate_message	response3rV   s                         @r   retry_using_http_NTLM_auth'HttpNtlmAuth.retry_using_http_NTLM_authH   s    #'"7"7"A"))<<!9<S!S = C 	J**222O$$(()93?b
   WX-=-=-B-BF%K%K!  %%**N?A>  %%**1a0 	!!#""'')&&hx||&<&E&EF0055==T]] $44==NMMMM $"
 #,,V[[]-AcBIIK#%67'+$ T%0'',,WFF	 	""$##((*
   ..(1(9(9(=(=l(KGOOH% &--.?@_
 O2C2I2I#2NO
 
 !!Q  0Z1BCJJLM%//C0@0GCHOOQ#%9:'+$((--g>>	  *  + 3F ;r   c                   UR                   S:X  aP  UR                  R                  SS5      R                  5       n[	        U5      nUb  U R                  SSUUU5      $  U$ UR                   S:X  aM  UR                  R                  SS5      R                  5       n[	        U5      nUb  U R                  SSUUU5      $ U$ )zThe actual hook handler.i  zwww-authenticate Authorizationi  zproxy-authenticatezProxy-authorization)status_codera   rc   lower_auth_type_from_headerr   )r   rkwargswww_authenticater   proxy_authenticates         r   response_hookHttpNtlmAuth.response_hook   s    ==C yy}}-?DJJL./?@I$66&#  %*  ]]c!!"/CR!H!N!N!P./ABI$66()  r   c                L   U R                   (       a  UR                  n[        U[        5      (       aG  UR                  R
                  R                  R                  n UR                  S5      n[        U5      $ [        R                  " S[        5        g! [         a     gf = f)aX  
Get the certificate at the request_url and return it as a hash. Will get the raw socket from the
original response from the server. This socket is then checked if it is an SSL socket and then used to
get the hash of the certificate. The certificate hash is then used with NTLMv2 authentication for
Channel Binding Tokens support. If the raw object is not a urllib3 HTTPReponse (default with requests)
then no certificate will be returned.

:param response: The original 401 response from the server
:return: The hash of the DER encoded certificate at the request_url or None if not a HTTPS endpoint
TzZRequests is running with a non urllib3 backend, cannot retrieve server certificate for CBTN)r<   rg   
isinstancer
   _fpfp_sockgetpeercert_get_certificate_hashAttributeErrorwarningswarnNoCertificateRetrievedWarning)r   r   raw_responsesocketserver_certificates        r   r]   HttpNtlmAuth._get_server_cert   s     ==#<<L,55%)),,0066E)/););D)A& 11CDDp1
  &  s   B 
B#"B#c                \    SUR                   S'   UR                  SU R                  5        U$ )Nz
Keep-Alive
Connectionr   )ra   register_hookr   )r   r   s     r   __call__HttpNtlmAuth.__call__   s+     #/		,	
D$6$67r   )r;   r<   r=   r:   )NT)
r:   
str | Noner;   r   r>   r-   r<   boolr,   r-   )r   rl   r   rl   r   requests.Responser   rl   r   t.Anyr,   r   )r   r   r   r   r,   r   )r   r   r,   bytes | None)r   requests.PreparedRequestr,   r   )r/   r0   r1   r2   r3   r   r   r   r]   r   r4   r5   r   r   r7   r7   *   s     AA A 	A
 A 
A.vv v $	v
 v v 
vp<>r   r7   c                     SU ;   a  gSU ;   a  gg)z
Given a WWW-Authenticate or Proxy-Authenticate header, returns the
authentication type to use. We prefer NTLM over Negotiate if the server
suppports it.
rG   NTLM	negotiate	NegotiateNr5   )headers    r   r   r     s     		r   c                   [         R                  " U [        5       5      n UR                  nU(       a  UR                  S;   a3  [        R                  " [        R                  " 5       [        5       5      nO[        R                  " U[        5       5      nUR                  U 5        UR                  5       nU$ ! [         a1  n[
        R                  " S[        U5      -  [        5         S nAg S nAff = f)NzWFailed to get signature algorithm from certificate, unable to pass channel bindings: %s)md5sha1)r   load_der_x509_certificater   signature_hash_algorithmr   r   r   rl   UnknownSignatureAlgorithmOIDnamer   HashSHA256updatefinalize)certificate_dercerthash_algorithmexdigestcertificate_hash_bytess         r   r   r     s    ))/?;LMD66 ^00OCV]]_o.?@^_->?
MM/"#__.!!%   247G<(	

 s   B9 9
C4'C//C4c                      \ rS rSrSrg)r   i/  r5   Nr/   r0   r1   r2   r4   r5   r   r   r   r   /      r   r   c                      \ rS rSrSrg)r   i3  r5   Nr   r5   r   r   r   r   3  r   r   r   )r   rl   r,   r   )r   r.   r,   r   )
__future__r   rr   typingrj   r   urllib.parser   requestsr^   cryptographyr   cryptography.exceptionsr   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   requests.authr	   "requests.packages.urllib3.responser
   r   r7   r   r   Warningr   r   r5   r   r   <module>r      sg    "    !    8 8 1 " ;1 10Y8 Yx"6	G 		7 	r   