
    i                     f   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j                  e      Z G d de
      Z G d d	e
      Z G d
 de      Zdedee   fdZdedefdZdededej&                  deeef   f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Zy)    )Optional)AuthorizationServerMetadata)AsyncOAuth2ClientN)	BaseModelc                   ^    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<   y)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        V/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/mistralai/extra/mcp/auth.pyr   r      s6    TN9!%K#%!%K#%r   r   c                   0    e Zd ZU dZeed<   eed<   eed<   y)OAuthParamsz"Required params for authorization.scheme	client_idclient_secretN)r   r   r   r   r   r   r   r   r   r   r   r      s    ,%%Nr   r   c                   *    e Zd ZdZededd fd       Zy)r   zWSubclass of the Async httpx oauth client which provides a constructor from OAuthParams.oauth_paramsreturnc                 h     | |j                   |j                  |j                  j                        S )N)r   r   r   )r   r   r   r   )clsr   s     r   from_oauth_paramsz#AsyncOAuth2Client.from_oauth_params"   s0    ",,&44%%++
 	
r   N)r   r   r   r   classmethodr   r"   r   r   r   r   r      s&    a
[ 
=P 
 
r   r   
server_urlr   c                 p  K   |  d}t        j                         j                  |       d{   }d|j                  cxk  rdk  r/n n,	 t	        di |j                         }|j                          |S t        j                  d|         y7 a# t        $ r t        j                  d       Y yw xY ww)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_metadatas       r   ,get_well_known_authorization_server_metadatar4   +   s      #|#JKN&&(,,^<<H
h""(S(	9LHMMOLO$$&""
 	DZLQR =  	HI	s3   ,B6BB6*B 6B6B30B62B33B6c                    K   t        |        d{   }||S t        | |  d|  d|  ddgdgdd	gd
gddg	      S 7 ,w)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)r4   r   )r$   metadatas     r   get_oauth_server_metadatarH   B   sw      BZXXH '",Z8$V,'L	2"(")3_E/D.E*0'):
 
 Ys   A?-ArA   redirect_urlasync_clientc                 (  K   dddgddg|gd}|j                  | |       d{   }	 |j                          |j                         }|d	   }|d
   }||fS 7 3# t        $ r+}t	        d|j
                   d|j                         |d}~ww xY ww)zkTry to register the client dynamically with an MCP server.

    Returns a client_id and client_secret.
    MistralSDKClientr9   r:   r;   r7   )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)	rA   rI   rJ   registration_payloadr2   registration_infor   r   es	            r   dynamic_client_registrationrY   [   s      *,o>&;!(& "&&'8?S&TTH!!#$MMO%k2	)/:
 m## U  1(2F2F1GxPXP]P]_
	s2   #BAB*A B	B$&B

BBr   r   r   c                   K   t        |        d{   }t        |j                  |j                  |xs g d|j                  v r|j                  nd      }|r|rt        |||      S |r$t        |j                  ||       d{   \  }}	nPt        j                         4 d{   }t        |j                  ||       d{   \  }}	ddd      d{    t        	|      S 7 7 h7 I7 +7 # 1 d{  7  sw Y   *xY ww)z8Get issuer metadata and build the oauth required params.r6   Nr:   )r	   r
   r   r   )r   r   r   )rA   rI   rJ   )
rH   r   r?   r@   rD   r   rY   registration_endpointr'   r(   )
r$   rI   r   r   r   rJ   rG   oauth_schemereg_client_idreg_client_secrets
             r   build_oauth_paramsr_   {   s(     /*EEH,"99))krh<<< ++L ]'
 	
 1L&<<%%2
 ,
(( $$& 	 	,5P"*"@"@))6 0,M,	 	 ' = F$,
	0	 	 	 	s|   DC.A4DC0 D(C2)D,C8C4	C8DC6D0D2D4C86D8D
>D?D
D)NNNN)typingr   authlib.oauth2.rfc8414r   !authlib.integrations.httpx_clientr   AsyncOAuth2ClientBaser'   loggingmistralai.typesr   	getLoggerr   r.   r   r   r   r4   rH   r(   tuplerY   r   r_   r   r   r   <module>rh      s&    > X   %			8	$&	 &) 	
- 	
)*. 8S 2$$$ ##$ 38_	$F  $#'!%04+++ }+ C=	+
 DI+ 5,,-+ +r   