o
    i%                     @   s&  d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	m
Z
mZ d dlmZmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZmZmZmZ d dlmZm Z  e !e"Z#G dd deZ$G dd deZ%de&ddeB eB de&dB fddZ'G dd de
e Z(eG dd dZ)dS )    N)	dataclass)AnyLiteral)AnyUrl	BaseModelField	RootModelValidationError)FormDataQueryParams)Request)RedirectResponseResponse)stringify_pydantic_error)PydanticJSONResponse)AuthorizationErrorCodeAuthorizationParamsAuthorizeError OAuthAuthorizationServerProviderconstruct_redirect_uri)InvalidRedirectUriErrorInvalidScopeErrorc                   @   s   e Zd ZU edddZeed< edddZedB ed< edddZ	e
d	 ed
< edddZeed< edddZe
d 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< dS )AuthorizationRequest.zThe client ID)description	client_idNz&URL to redirect to after authorizationredirect_uriz*Must be 'code' for authorization code flowcoderesponse_typezPKCE code challengecode_challengeS256z(PKCE code challenge method, must be S256code_challenge_methodzOptional state parameterstatezOOptional scope; if specified, should be a space-separated list of scope stringsscopezIRFC 8707 resource indicator - the MCP server this token will be used withresource)__name__
__module____qualname__r   r   str__annotations__r   r   r   r   r   r    r!   r"   r#    r)   r)   h/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mcp/server/auth/handlers/authorize.pyr      s   
 r   c                   @   sB   e Zd ZU eed< edB ed< dZedB ed< dZedB ed< dS )AuthorizationErrorResponseerrorNerror_description	error_urir!   )	r$   r%   r&   r   r(   r'   r.   r   r!   r)   r)   r)   r*   r+   ,   s
   
 r+   keyparamsreturnc                 C   s(   |d u rd S | | }t|tr|S d S )N)get
isinstancer'   )r/   r0   valuer)   r)   r*   best_effort_extract_string4   s   

r5   c                   @   s   e Zd ZU eed< dS )AnyUrlModelrootN)r$   r%   r&   r   r(   r)   r)   r)   r*   r6   =   s   
 r6   c                   @   s2   e Zd ZU eeeef ed< dedefddZdS )AuthorizationHandlerproviderrequestr1   c           	   
      s  d d d  d 	ddt dtd B dtf fdd}z |jdkr)|jn| I d H tdz
t}|j	W n6 t
yu } z*d	}| D ]}|d
 dkr_|d dkr_d} nqM||t|I d H W  Y d }~W S d }~ww j|jI d H   s|d	d|j dddI d H W S z |jW n ty } z|d	|jdI d H W  Y d }~W S d }~ww z |j}W n ty } z|d|jdI d H W  Y d }~W S d }~ww t||j|jd u|jd}ztj |I d H dddidW W S  ty } z||j|jdI d H W  Y d }~W S d }~ww  tyB } zt j!d|d |dddI d H W  Y d }~S d }~ww )NTr,   r-   attempt_load_clientc              	      s    d u r|rt d}|rj|I d H nd  d u rF rFzd ur+dvr+d }n	tt dj} |W n ttfyE   Y nw d u rOt dt	| |d}ro rot
ttfi |jddddd	id
S td|dd	idS )Nr   r   r!   )r,   r-   r!   T)exclude_none.  Cache-Controlno-storeurlstatus_codeheadersi  )rB   contentrC   )r5   r9   
get_clientr6   model_validater7   validate_redirect_urir	   r   r+   r   r   r'   
model_dumpr   )r,   r-   r;   r   raw_redirect_uri
error_respclientr0   r   selfr!   r)   r*   error_responseN   sF   

z3AuthorizationHandler.handle.<locals>.error_responseGETr!   invalid_requestloc)r   typeliteral_errorunsupported_response_typezClient ID 'z' not foundF)r,   r-   r;   )r,   r-   invalid_scope)r!   scopesr   r    redirect_uri_provided_explicitlyr#   r=   r>   r?   r@   z)Unexpected error in authorization_handler)exc_infoserver_errorzAn unexpected error occurred)T)"r   r'   boolmethodquery_paramsformr5   r   rF   r!   r	   errorsr   r9   rE   r   rG   r   r   messagevalidate_scoper"   r   r   r   r#   r   	authorizer   r,   r-   	Exceptionlogger	exception)	rM   r:   rN   auth_requestvalidation_errorr,   erV   auth_paramsr)   rK   r*   handleE   s   @



"	
	
$zAuthorizationHandler.handleN)	r$   r%   r&   r   r   r(   r   r   ri   r)   r)   r)   r*   r8   A   s   
 r8   )*loggingdataclassesr   typingr   r   pydanticr   r   r   r   r	   starlette.datastructuresr
   r   starlette.requestsr   starlette.responsesr   r   mcp.server.auth.errorsr   mcp.server.auth.json_responser   mcp.server.auth.providerr   r   r   r   r   mcp.shared.authr   r   	getLoggerr$   rc   r   r+   r'   r5   r6   r8   r)   r)   r)   r*   <module>   s$    
"	