
    Kis                         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y)    N)Any)unquote)Request) OAuthAuthorizationServerProvider)OAuthClientInformationFullc                       e Zd ZdefdZy)AuthenticationErrormessagec                     || _         y )N)r
   )selfr
   s     r/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/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                   8    e Zd ZdZdeeeef   fdZdedefdZ	y)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                     || _         y)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   |j                          d{   }|j                  d      }|st        d      | j                  j	                  t        |             d{   }|st        d      d}|j                  j                  dd      }|j                  dk(  r|j                  d      st        d	      	 |d
d }t        j                  |      j                  d      }d|vrt        d      |j                  dd      \  }	}t        |	      }	t        |      }|	|k7  rt        d      nf|j                  dk(  r-|j                  d      }
t%        |
t
              r6t        |
      }n*|j                  dk(  rd}nt        d|j                         |j&                  r|st        d      t)        j*                  |j&                  j-                         |j-                               st        d      |j.                  r5|j.                  t1        t3        j2                               k  rt        d      |S 7 #7 # t        t        t         j"                  f$ r t        d      w xY ww)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'   sP      ",,.(	MM+.	%&9::}}//I??%&9::,0oo))/2>,,0EE))(3)*kllQ&1!"o# **+>?FFwOg%$%@AA9@sA9N6!6 #*/":(/0E(F%"i/-.PQQ 0
 ..2FF%MM/:M--(+M(:%..&8$(!%+F,M,M+NO  ()*EFF
 &&v';';'B'B'DF[FbFbFde)*ABB..63R3RUXY]YbYbYdUe3e)*EFFy )
 @0  2HNNC Q)*OPPQs=   I,H9A	I, H<!AI,<A3H? /DI,<I,?*I))I,N)
r   r   r   __doc__r   r   r   r   r   rC   r   r   r   r   r      s6    !!A#sC-!P !L' L>X 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      s8           & E 6) 
` `r   