o
    �õ±j�  ã                   @   sº   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	 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 d dlmZ G dd„ dƒZG dd„ deƒZG dd„ deƒZdS )é    N)Úsha256)Úlogger)ÚUniProto)ÚRequestManager)ÚISC_REQ)ÚNTLMCredential)ÚKerberosCredential)ÚSPNEGOCredential)ÚCREDSSPCredential)ÚUniCredentialc                   @   s0   e Zd Zdd„ Zdd„ Zed
defdd„ƒZd	S )ÚHTTPAuthManagerc                 C   s    || _ d| _| ¡ | _|| _d S )NÚstart)ÚsessionÚstatusÚbuild_contextÚauthobjÚ
credential©Úselfr   r   © r   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asysocks/unicomm/protocol/client/http/auth.pyÚ__init__   s   

zHTTPAuthManager.__init__c                 Ã   s   �d S )Nr   )r   Úrequestmanagerr   r   r   Úauthenticate   s   €zHTTPAuthManager.authenticateÚautoÚ	auth_typec                 C   s¤  |  ¡ }t|tƒr| ¡ }|dkr4t|tƒdu rt| |ƒS t|ttfƒdu r0t|gƒ}t| |ƒS tdƒ‚|dkr~t|tƒdu rDt| |ƒS t|ttfƒdu rWt|gƒ}t| |ƒS t	|dƒdu rvt|j
ttfƒdu rvtt |j
¡gƒ}t| |ƒS td|jj ƒ‚|dkrÈt|tƒdu rŽt| |ƒS t|ttfƒdu r¡t|gƒ}t| |ƒS t	|dƒdu rÀt|j
ttfƒdu rÀtt |j
¡gƒ}t| |ƒS td|jj ƒ‚td	||jjf ƒ‚)
Nr   TzNo supported auth type found!Úspnegor   z<Credential type %s cannot be used for SPNEGO authentication!Úcredsspz=Credential type %s cannot be used for CREDSSP authentication!z2Unsupported auth type! Authtype: %s Credential: %s)ÚlowerÚ
isinstancer   r   r	   ÚHTTPAuthManagerNegotiater   r   Ú	ExceptionÚhasattrr   ÚcopyÚdeepcopyÚ	__class__Ú__name__r
   ÚHTTPAuthManagerCredSSP)r   r   r   r   r   r   Úfrom_credential   s>   






"



"
zHTTPAuthManager.from_credentialN)r   )r&   Ú
__module__Ú__qualname__r   r   ÚstaticmethodÚstrr(   r   r   r   r   r      s
    r   c                   @   ó"   e Zd Zdefdd„Zdd„ ZdS )r'   r   c                 C   ó   t  | ||¡ d| _d | _d S ©NF©r   r   Ú
auth_stepsÚcb_datar   r   r   r   r   C   ó   
zHTTPAuthManagerCredSSP.__init__c              	   Ã   s„  �d| _ d }d}| jjD ]}|j} 	 tjtjB tjB }d|jj	 
¡ |f }| jj|||d�I d H \}}}	|	d ur;|	‚|du rF|
j dkrF|
S dt |¡ ¡  }g }| d	|f¡ |jD ]}| |¡ q[t| j|j|j||j|j|jd
�4 I d H š°}
|du r‰|
j dkr‰td|
j  ƒ‚|
 ¡ I d H  |
jjd ur |
jjdu r |
j| _|
 d¡}|d u s¯t|ƒdkrÈ|du rÄ|
j dkrÄ|
W  d   ƒI d H  S tdƒ‚|D ]}| d¡rÕ|} nqÊ|du rë|
j dkrë|
W  d   ƒI d H  S tdƒ‚| dd¡  ¡  !¡ }|dk�r|du �r|
j dk�r|
W  d   ƒI d H  S tdƒ‚t "|¡}W d   ƒI d H  n1 I d H �s/w   Y  |du �rA|
j dk�rA|
S q)NÚprogressÚUNKNOWNTú
HTTP/%s@%s)ÚspnÚflagsFéÈ   zCredSSP ÚAuthorization©ÚheadersÚdataÚneed_lengthÚ	transportúAuthentication failed! %súwww-authenticater   z8Authentication failed! No www-authenticate header found!ÚCredSSPz/Authentication failed! No CredSSP header found!Ú ó    z2Authentication failed! Empty CredSSP header found!)#r   r   ÚcredentialsÚdomainr   Ú
CONNECTIONÚCONFIDENTIALITYÚ	INTEGRITYr?   ÚtargetÚget_hostname_or_ipr   r   Úbase64Ú	b64encodeÚdecodeÚappendr<   r   r   ÚurlÚreq_typer=   r>   r!   ÚreadÚ
connectionÚ	can_reuseÚ
getheadersÚlenÚ
startswithÚreplaceÚstripÚencodeÚ	b64decode)r   r   Ú	authreplyrF   Úcredr8   Ú
target_spnÚauthdataÚto_continueÚerrÚresponseÚauth_headerÚ
newheadersÚentryÚresponse_auth_headerÚrauthhÚresponse_header_data_b64r   r   r   r   H   sh   €
,
ö
þî
ç*äÑz#HTTPAuthManagerCredSSP.authenticateN)r&   r)   r*   r
   r   r   r   r   r   r   r'   B   ó    r'   c                   @   r-   )r    r   c                 C   r.   r/   r0   r   r   r   r   r   ‚   r3   z!HTTPAuthManagerNegotiate.__init__c              	   Ã   sB  �d| _ d }d}| jjD ]}|j} 	 tjtjB tjB tjB tj	B }|j
jjtjkrB| jd u rB|j
j ¡ }|d urBdt|ƒ ¡  | _d|j
j ¡ |f }| jj|||| jd�I d H \}}	}
|
d urc|
‚|	du rs|j dkrst d	¡ |S d
t |¡ ¡  }g }| d|f¡ |jD ]}| |¡ qˆt d| ¡ t| j|j |j!||j"|j#|j
d�4 I d H šV}t d|j  ¡ t d|j ¡ |	du rÍ|j dkrÍt$d|j  ƒ‚| %¡ I d H  |j
jd urä|j
j&du rä|j
| _
| 'd¡d  (d
d¡ )¡  *¡ }t +|¡}W d   ƒI d H  n1 I d H �s	w   Y  |	du �r |j dk�r t d	¡ |S q)Nr4   r5   Ts   tls-server-end-point:r6   )r7   r8   r2   Fr9   zAuthentication successful!z
Negotiate r:   zUpdated Headers: %sr;   zResponse Status: %szResponse Headers: %sr@   rA   r   rC   ),r   r   rE   rF   r   rG   rH   rI   ÚUSE_DCE_STYLEÚMUTUAL_AUTHr?   rJ   Úprotocolr   ÚCLIENT_SSL_TCPr2   rS   Úget_peer_certificater   ÚdigestrK   r   r   r   ÚdebugrL   rM   rN   rO   r<   r   r   rP   rQ   r=   r>   r!   rR   rT   rU   rX   rY   rZ   r[   )r   r   r\   rF   r]   r8   Úcertdatar^   r_   r`   ra   rb   rc   rd   re   r   r   r   r   ‡   sT   €
"

,*õ
Øz%HTTPAuthManagerNegotiate.authenticateN)r&   r)   r*   r	   r   r   r   r   r   r   r    �   ri   r    )rL   r#   Úhashlibr   Ú%asysocks.unicomm.protocol.client.httpr   Úasysocks.unicomm.common.targetr   Ú0asysocks.unicomm.protocol.client.http.requestmgrr   Úasyauth.common.winapi.constantsr   Úasyauth.common.credentials.ntlmr   Ú#asyauth.common.credentials.kerberosr   Ú!asyauth.common.credentials.spnegor	   Ú"asyauth.common.credentials.credsspr
   Úasyauth.common.credentialsr   r   r'   r    r   r   r   r   Ú<module>   s    2?