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_challengeZ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(   Y/home/app/Keep/.python/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.   r/   valuer(   r(   r)   best_effort_extract_string4   s   

r4   c                   @   s   e Zd ZU eed< dS )AnyUrlModelrootN)r#   r$   r%   r   r'   r(   r(   r(   r)   r5   =   s   
 r5   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requestr0   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)Zexclude_none.  Cache-Controlno-storeurlstatus_codeheadersi  )r@   contentrA   )r4   r8   
get_clientr5   model_validater6   validate_redirect_urir	   r   r*   r   r   r&   Z
model_dumpr   )r+   r,   r:   r   Zraw_redirect_uriZ
error_respclientr/   r   selfr    r(   r)   error_responseN   sF   

z3AuthorizationHandler.handle.<locals>.error_responseGETr    Zinvalid_requestloc)r   typeZliteral_errorZunsupported_response_typezClient ID 'z' not foundF)r+   r,   r:   )r+   r,   Zinvalid_scope)r    scopesr   r   Z 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methodZquery_paramsformr4   r   rD   r    r	   errorsr   r8   rC   r   rE   r   r   messageZvalidate_scoper!   r   r   r   r"   r   Z	authorizer   r+   r,   	Exceptionlogger	exception)	rH   r9   rI   Zauth_requestZvalidation_errorr+   erM   Zauth_paramsr(   rF   r)   handleE   s   @



"	
	
$zAuthorizationHandler.handleN)	r#   r$   r%   r   r   r'   r   r   rY   r(   r(   r(   r)   r7   A   s   
 r7   )*loggingdataclassesr   typingr   r   Zpydanticr   r   r   r   r	   Zstarlette.datastructuresr
   r   Zstarlette.requestsr   Zstarlette.responsesr   r   Zmcp.server.auth.errorsr   Zmcp.server.auth.json_responser   Zmcp.server.auth.providerr   r   r   r   r   Zmcp.shared.authr   r   	getLoggerr#   rV   r   r*   r&   r4   r5   r7   r(   r(   r(   r)   <module>   s$    
"	