o
    *is                     @   s~   d dl Z d dlZ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 G dd deZG d	d
 d
ZdS )    N)Any)unquote)Request) OAuthAuthorizationServerProvider)OAuthClientInformationFullc                   @   s   e Zd ZdefddZdS )AuthenticationErrormessagec                 C   s
   || _ d S )N)r   )selfr    r
   c/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/mcp/server/auth/middleware/client_auth.py__init__   s   
zAuthenticationError.__init__N)__name__
__module____qualname__strr   r
   r
   r
   r   r      s    r   c                   @   s:   e Zd ZdZdeeeef fddZdedefddZ	d	S )
ClientAuthenticatora  
    ClientAuthenticator is a callable which validates requests from a client
    application, used to verify /token calls.
    If, during registration, the client requested to be issued a secret, the
    authenticator asserts that /token calls must be authenticated with
    that same token.
    NOTE: clients can opt for no authentication during registration, in which case this
    logic is skipped.
    providerc                 C   s
   || _ dS )zx
        Initialize the dependency.

        Args:
            provider: Provider to look up client information
        N)r   )r	   r   r
   r
   r   r      s   
zClientAuthenticator.__init__requestreturnc              
      s  |  I dH }|d}|std| jt|I dH }|s$tdd}|jdd}|jdkr{|ds;td	z0|d
d }t	
|d}d|vrRtd|dd\}	}t|	}	t|}|	|krjtdW n3 tttjfyz   tdw |jdkr|d}
t|
trt|
}n|jdkrd}ntd|j |jr|stdt|j | std|jr|jtt k rtd|S )a  
        Authenticate a client from an HTTP request.

        Extracts client credentials from the appropriate location based on the
        client's registered authentication method and validates them.

        Args:
            request: The HTTP request containing client credentials

        Returns:
            The authenticated client information

        Raises:
            AuthenticationError: If authentication fails
        N	client_idzMissing client_idzInvalid client_idAuthorization client_secret_basiczBasic z?Missing or invalid Basic authentication in Authorization header   zutf-8:zInvalid Basic auth format   z Client ID mismatch in Basic authz#Invalid Basic authentication headerclient_secret_postclient_secretnonezUnsupported auth method: zClient secret is requiredzInvalid client_secretzClient secret has expired)formgetr   r   
get_clientr   headerstoken_endpoint_auth_method
startswithbase64	b64decodedecode
ValueErrorsplitr   UnicodeDecodeErrorbinasciiError
isinstancer   hmaccompare_digestencodeclient_secret_expires_atinttime)r	   r   	form_datar   clientrequest_client_secretauth_headerencoded_credentialsdecodedbasic_client_idraw_form_datar
   r
   r   authenticate_request'   sZ   







z(ClientAuthenticator.authenticate_requestN)
r   r   r   __doc__r   r   r   r   r   r<   r
   r
   r
   r   r      s    
	r   )r%   r+   r.   r3   typingr   urllib.parser   starlette.requestsr   mcp.server.auth.providerr   mcp.shared.authr   	Exceptionr   r   r
   r
   r
   r   <module>   s    