o
    i                     @   s   d dl Z d dlmZ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mZ d dlmZmZmZmZ d dlmZ G dd	 d	eZG d
d deZdS )    N)	AwaitableCallable)Any)uuid4)	BaseModelField)OAuthClientProviderOAuthFlowErrorOAuthTokenErrorTokenStorage)OAuthClientMetadatac                   @   s   e Zd ZU dZedddZedB ed< edddZedB ed< edddZ	edB ed	< edd
dZ
edB ed< edddZeeef dB ed< edddZedB ed< edddZedB ed< edddZeed< ddedB defddZdS )JWTParameterszJWT parameters.NzeJWT assertion for JWT authentication. Will be used instead of generating a new assertion if provided.)defaultdescription	assertionzIssuer for JWT assertions.issuerz&Subject identifier for JWT assertions.subjectzAudience for JWT assertions.audiencez%Additional claims for JWT assertions.claimsRS256z%Algorithm for signing JWT assertions.jwt_signing_algorithmzPrivate key for JWT signing.jwt_signing_keyi,  z%Lifetime of generated JWT in seconds.jwt_lifetime_secondswith_audience_fallbackreturnc                 C   s   | j d ur
| j }|S | jstd| jstd| jstd| jr%| jn|}|s-tdtt }| j| j||| j |t	t
 d}|| jpJi  tj|| j| jpUdd}|S )Nz(Missing signing key for JWT bearer grantz#Missing issuer for JWT bearer grantz$Missing subject for JWT bearer grantz%Missing audience for JWT bearer grant)isssubaudexpiatjtir   )	algorithm)r   r   r	   r   r   r   inttimer   strr   updater   jwtencoder   )selfr   r   r   nowr    r*   s/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mcp/client/auth/extensions/client_credentials.pyto_assertion   s6   
zJWTParameters.to_assertionN)__name__
__module____qualname____doc__r   r   r$   __annotations__r   r   r   r   dictr   r   r   r   r"   r,   r*   r*   r*   r+   r      s   
  r   c                       s   e Zd ZU dZdZedB ed< 				ddedede	de
eged f dB d	e
g eeeedB f  f dB d
ededB ddf fddZdddededeeef dB dejf fddZdejf fddZdeeef fddZdejfddZ  ZS )RFC7523OAuthClientProviderz*OAuth client provider for RFC7532 clients.Njwt_parameters     r@
server_urlclient_metadatastorageredirect_handlercallback_handlertimeoutr   c                    s    t  |||||| || _d S r-   )super__init__r5   )r(   r7   r8   r9   r:   r;   r<   r5   	__class__r*   r+   r>   G   s   

z#RFC7523OAuthClientProvider.__init__
token_data	auth_codecode_verifierrB   c                   s<   |pi }| j jjdkr| j|d t j|||dI dH S )z9Build token exchange request for authorization_code flow.private_key_jwtrA   N)contextr8   token_endpoint_auth_method_add_client_authentication_jwtr=   "_exchange_token_authorization_code)r(   rC   rD   rB   r?   r*   r+   rI   T   s
   z=RFC7523OAuthClientProvider._exchange_token_authorization_codec                    s2   d| j jjv r|  I dH }|S t  I dH S )zPerform the authorization flow.+urn:ietf:params:oauth:grant-type:jwt-bearerN)rF   r8   grant_types_exchange_token_jwt_bearerr=   _perform_authorization)r(   token_requestr?   r*   r+   rM   ]   s
   z1RFC7523OAuthClientProvider._perform_authorizationc                C   s\   | j std| jjstdt| jjj}| j j|d}||d< d|d< | j |d< dS )	zIAdd JWT assertion for client authentication to token endpoint parameters.z/Missing JWT parameters for private_key_jwt flowz/Missing OAuth metadata for private_key_jwt flowr   client_assertionz6urn:ietf:params:oauth:client-assertion-type:jwt-bearerclient_assertion_typer   N)r5   r
   rF   oauth_metadatar$   r   r,   get_resource_url)r(   rB   r   r   r*   r*   r+   rH   e   s   z9RFC7523OAuthClientProvider._add_client_authentication_jwtc                    s   | j js	td| jstd| j jstdt| j jj}| jj|d}d|d}| j 	| j j
r:| j  |d< | j jjrF| j jj|d< |  }tjd	||d
didS )z2Build token exchange request for JWT bearer grant.zMissing client infozMissing JWT parameterszMissing OAuth metadatarO   rJ   )
grant_typer   resourcescopePOSTzContent-Typez!application/x-www-form-urlencoded)dataheaders)rF   client_infor	   r5   rR   r
   r$   r   r,   should_include_resource_paramprotocol_versionrS   r8   rV   _get_token_endpointhttpxRequest)r(   r   r   rB   	token_urlr*   r*   r+   rL   x   s(   
z5RFC7523OAuthClientProvider._exchange_token_jwt_bearer)NNr6   N)r.   r/   r0   r1   r5   r   r2   r$   r   r   r   r   tuplefloatr>   r3   r   r^   r_   rI   rM   rH   rL   __classcell__r*   r*   r?   r+   r4   B   sJ   
 		r4   )r#   collections.abcr   r   typingr   uuidr   r^   r&   pydanticr   r   mcp.client.authr   r	   r
   r   mcp.shared.authr   r   r4   r*   r*   r*   r+   <module>   s    4