o
    i                     @   s   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ ddlmZ eeeef ef Zd	gZG d
d dZG dd dZdS )    N)AnyDictOptionalUnion)ClientAuthenticationError)ContentDecodePolicy)HttpRequestHttpResponse)PipelineResponse   )build_pipelinePOSTc                   @   sN   e Zd ZdZdeddfddZedefddZede	fd	d
Z
dd ZdS )MsalResponsezWraps HttpResponse according to msal.oauth2cli.http.

    :param response: The response to wrap.
    :type response: ~azure.core.rest.HttpResponse
    responsereturnNc                 C   s$   || _ |jr|jj| _d S i | _d S N)	_responsehttp_responseheaders)selfr    r   [/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/_internal/msal_client.py__init__   s   zMsalResponse.__init__c                 C   s
   | j jjS r   )r   r   status_coder   r   r   r   r      s   
zMsalResponse.status_codec                 C   s   | j jjddS )Nutf-8)encoding)r   r   textr   r   r   r   r   !   s   zMsalResponse.textc                 C   s   | j dk rd S tj| jjv rC| jjtj }|sd}n+d|v s"d|v r0d|dp-|d}ndD ]
}||v r<d||< q2d|}nd}t|| jjd	)
N  z+Unexpected response from Microsoft Entra IDerrorZerror_descriptionzAuthentication failed: {})Zaccess_tokenZrefresh_tokenz***z1Unexpected response from Microsoft Entra ID: "{}")messager   )	r   r   CONTEXT_NAMEr   contextformatgetr   r   )r   contentr    secretr   r   r   raise_for_status%   s   
zMsalResponse.raise_for_status)__name__
__module____qualname____doc__r
   r   propertyintr   strr   r'   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdeddfddZd!ddZd	eddfd
dZd"ddZ			d#de	de
ee	e	f  de
e de
ee	e	f  dedefddZ	d$de	de
ee	e	f  de
ee	e	f  dedef
ddZdede
e fddZdeddfddZdee	ef fddZdee	ef ddfdd ZdS )%
MsalClientz/Wraps Pipeline according to msal.oauth2cli.httpkwargsr   Nc                 K   s   t  | _tdi || _d S )Nr   )	threadinglocal_localr   	_pipeline)r   r0   r   r   r   r   =   s   
zMsalClient.__init__c                 C   s   | j   | S r   )r4   	__enter__r   r   r   r   r5   A   s   
zMsalClient.__enter__argsc                 G   s   | j j|  d S r   )r4   __exit__)r   r6   r   r   r   r7   E   s   zMsalClient.__exit__c                 C   s   |    d S r   )r7   r   r   r   r   closeH   s   zMsalClient.closeurlparamsdatar   c                 K   s   |rt |ni }d }d }|r)t|t rd|d< |}nt|tr%|d}ntdtd|||||d}	| jj|	dtd}
| 	|
 t
|
S )	Nz!application/x-www-form-urlencodedzContent-Typer   z$expected "data" to be text or a dictr   )r   r:   r;   r%   F)streamZretry_on_methods)dict
isinstancer.   encode
ValueErrorr   r4   run_POST_store_auth_errorr   )r   r9   r:   r;   r   r0   Zrequest_headersr%   Zrequest_datarequestr   r   r   r   postK   s   	


zMsalClient.postc                 K   s2   t d|||d}| jj|dd}| | t|S )NGET)r   r:   F)r<   )r   r4   rA   rC   r   )r   r9   r:   r   r0   rD   r   r   r   r   r$   f   s   
zMsalClient.getmsal_resultc                 C   s,   t | jdd\}}|r||dkr|S dS )a  Get the HTTP response associated with an MSAL error.

        :param msal_result: The result of an MSAL request.
        :type msal_result: dict
        :return: The HTTP response associated with the error, if any.
        :rtype: ~azure.core.rest.HttpResponse or None
        r   NNN)getattrr3   r$   )r   rG   
error_coder   r   r   r   get_error_responseo   s   zMsalClient.get_error_responser   c                 C   sZ   |j jdkr'|jtj}|r)d|v r+t|tr|d nd}||j f| j_	d S d S d S d S )Nr   r   Zoauth_error)
r   r   r"   r$   r   r!   r>   r=   r3   r   )r   r   r%   rJ   r   r   r   rC   |   s   zMsalClient._store_auth_errorc                 C   s   | j  }|d= |S )Nr3   )__dict__copyr   stater   r   r   __getstate__   s   
zMsalClient.__getstate__rO   c                 C   s   | j | t | _d S r   )rL   updater1   r2   r3   rN   r   r   r   __setstate__   s   zMsalClient.__setstate__)r   r/   )r   N)NNNrH   )r(   r)   r*   r+   r   r   r5   r7   r8   r.   r   r   RequestDatar   rE   r$   r	   rK   r
   rC   rP   rR   r   r   r   r   r/   :   sL    



		r/   )r1   typingr   r   r   r   Zazure.core.exceptionsr   Zazure.core.pipeline.policiesr   Zazure.core.restr   r	   Zazure.core.pipeliner
   Zpipeliner   r.   rS   rB   r   r/   r   r   r   r   <module>   s   (