
    )`is                         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e          Z G d	 d
          ZdS )    N)Any)unquote)Request) OAuthAuthorizationServerProvider)OAuthClientInformationFullc                       e Zd ZdefdZdS )AuthenticationErrormessagec                     || _         d S )N)r
   )selfr
   s     z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mcp/server/auth/middleware/client_auth.py__init__zAuthenticationError.__init__   s        N)__name__
__module____qualname__strr    r   r   r	   r	      s/              r   r	   c                   @    e Zd ZdZdeeeef         fdZdedef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                     || _         dS )zx
        Initialize the dependency.

        Args:
            provider: Provider to look up client information
        N)r   )r   r   s     r   r   zClientAuthenticator.__init__   s     !r   requestreturnc                 <  K   |                                  d{V }|                    d          }|st          d          | j                            t          |                     d{V }|st          d          d}|j                            dd          }|j        dk    r|                    d          st          d	          	 |d
d         }t          j
        |                              d          }d|vrt          d          |                    dd          \  }	}t          |	          }	t          |          }|	|k    rt          d          n# t          t          t           j        f$ r t          d          w xY w|j        dk    r:|                    d          }
t%          |
t
                    rt          |
          }n%|j        dk    rd}nt          d|j                   |j        r|st          d          t)          j        |j                                        |                                          st          d          |j        r8|j        t1          t3          j                              k     rt          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_datas              r   authenticate_requestz(ClientAuthenticator.authenticate_request'   s       ",,..((((((	MM+..	 	;%&9:::}//I???????? 	;%&9:::,0o))/2>>,0EEE))(33 m)*klllQ&1!""o# *+>??FFwOOg%%$%@AAA9@sA9N9N6!6 #*/":":(/0E(F(F%"i//-.PQQQ 0 2HNC Q Q Q)*OPPPQ .2FFF%MM/::M--- ;(+M(:(:%.&88$(!!%OF,MOO    	G( G)*EFFF
 &v';'B'B'D'DF[FbFbFdFdee C)*ABBB. G63RUXY]YbYdYdUeUe3e3e)*EFFFs   BE ,FN)
r   r   r   __doc__r   r   r   r   r   rC   r   r   r   r   r      sp         !!A#sC-!P ! ! ! !L' L>X L L L L L Lr   r   )r,   r2   r5   r:   typingr   urllib.parser   starlette.requestsr   mcp.server.auth.providerr   mcp.shared.authr   	Exceptionr	   r   r   r   r   <module>rK      s                          & & & & & & E E E E E E 6 6 6 6 6 6    )   
` ` ` ` ` ` ` ` ` `r   