o
    i                     @   s  d dl mZ d dlmZ d dlmZ d dlZd dlZd dl	m
Z
 eeZG dd de
ZG dd	 d	e
ZG d
d deZdedee fddZdedefddZdededejdeeef fddZ				ddededee dee deee  deej defddZdS )    )Optional)AuthorizationServerMetadata)AsyncOAuth2ClientN)	BaseModelc                   @   sN   e Zd ZU dZeed< eed< ee ed< dZee ed< dZ	ee ed< dS )Oauth2AuthorizationSchemezJInformation about the oauth flow to perform with the authorization server.authorization_url	token_urlscopeNdescriptionrefresh_url)
__name__
__module____qualname____doc__str__annotations__listr
   r   r    r   r   ^/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mistralai/extra/mcp/auth.pyr      s   
 r   c                   @   s*   e Zd ZU dZeed< eed< eed< dS )OAuthParamsz"Required params for authorization.scheme	client_idclient_secretN)r   r   r   r   r   r   r   r   r   r   r   r      s
   
 r   c                   @   s&   e Zd ZdZededd fddZdS )r   zWSubclass of the Async httpx oauth client which provides a constructor from OAuthParams.oauth_paramsreturnc                 C   s   | |j |j|jjdS )N)r   r   r	   )r   r   r   r	   )clsr   r   r   r   from_oauth_params"   s
   z#AsyncOAuth2Client.from_oauth_paramsN)r   r   r   r   classmethodr   r   r   r   r   r   r      s    r   
server_urlr   c                    s   |  d}t  |I dH }d|j  krdk r=n n ztdi | }|  |W S  ty<   t	d Y dS w t
d|   dS )zFetch the metadata from the well-known location.

    This should be available on MCP servers as described by the specification:
    https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization#2-3-server-metadata-discovery.
    z'/.well-known/oauth-authorization-serverN   i,  z)Failed to parse oauth well-known metadataz-Failed to get oauth well-known metadata from r   )httpxAsyncClientgetstatus_coder   jsonvalidate
ValueErrorlogger	exceptionerror)r   well_known_urlresponseserver_metadatar   r   r   ,get_well_known_authorization_server_metadata+   s   

r-   c                    sX   t | dI dH }|dur|S t| |  d|  d|  ddgdgdd	gd
gddgd	S )zKFetch the metadata from the authorization server to perform the oauth flow.r   Nz
/authorizez/tokenz	/registercodequeryauthorization_coderefresh_tokenclient_secret_basicS256plain)	issuerauthorization_endpointtoken_endpointregister_endpointresponse_types_supportedresponse_modes_supportedgrant_types_supported%token_endpoint_auth_methods_supported code_challenge_methods_supported)r-   r   )r   metadatar   r   r   get_oauth_server_metadataB   s   r@   r9   redirect_urlasync_clientc           	   
      s   dddgddg|gd}|j | |dI dH }z|  | }|d	 }|d
 }W ||fS  tyE } ztd|j d|j |d}~ww )zkTry to register the client dynamically with an MCP server.

    Returns a client_id and client_secret.
    MistralSDKClientr1   r2   r3   r/   )client_namegrant_typestoken_endpoint_auth_methodresponse_typesredirect_uris)r$   Nr   r   z#Client registration failed: status=z, error=)postraise_for_statusr$   	Exceptionr&   r#   text)	r9   rA   rB   registration_payloadr+   registration_infor   r   er   r   r   dynamic_client_registration[   s,   	
rP   r   r   r	   c           
   	      s   t | dI dH }t|j|j|pg d|jv r|jndd}|r(|r(t|||dS |r8t|j||dI dH \}}	n+t	 4 I dH }t|j||dI dH \}}	W d  I dH  n1 I dH s^w   Y  t||	|dS )z8Get issuer metadata and build the oauth required params.r.   Nr2   )r   r   r	   r   )r   r   r   )r9   rA   rB   )
r@   r   r7   r8   r<   r   rP   registration_endpointr    r!   )
r   rA   r   r   r	   rB   r?   oauth_schemereg_client_idreg_client_secretr   r   r   build_oauth_params{   sD   	
(rU   )NNNN)typingr   authlib.oauth2.rfc8414r   !authlib.integrations.httpx_clientr   AsyncOAuth2ClientBaser    loggingmistralai.typesr   	getLoggerr   r'   r   r   r   r-   r@   r!   tuplerP   r   rU   r   r   r   r   <module>   sX    




#
