
    )`i#                     >   d dl mZm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 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 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! d dl"m#Z#m$Z$ d dl%m&Z& d dl'm(Z( defdZ)dZ*dZ+dZ,dZ-deegeee         z  f         de.e/         defdZ0	 	 	 d.de!eeef         d ed!edz  d"e#dz  d#e$dz  de.e         fd$Z1d ed!edz  d"e#d#e$de(f
d%Z2d&edefd'Z3	 	 	 d.d(ed)e.e         d*e.e/         dz  d+e/dz  d,edz  de.e         fd-Z4dS )/    )	AwaitableCallable)Any)urlparse)
AnyHttpUrl)CORSMiddleware)Request)Response)Routerequest_response)ASGIApp)AuthorizationHandler)MetadataHandler)RegistrationHandler)RevocationHandler)TokenHandler)ClientAuthenticator) OAuthAuthorizationServerProvider)ClientRegistrationOptionsRevocationOptions)MCP_PROTOCOL_VERSION_HEADER)OAuthMetadataurlc                     | j         dk    r;| j        dk    r0| j        )| j                            d          st          d          | j        rt          d          | j        rt          d          dS )z
    Validate that the issuer URL meets OAuth 2.0 requirements.

    Args:
        url: The issuer URL to validate

    Raises:
        ValueError: If the issuer URL is invalid
    https	localhostNz	127.0.0.1zIssuer URL must be HTTPSz#Issuer URL must not have a fragmentz'Issuer URL must not have a query string)schemehost
startswith
ValueErrorfragmentquery)r   s    j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mcp/server/auth/routes.pyvalidate_issuer_urlr$      s     	
gH##X!#(*=*=k*J*J!3444 | @>???
y DBCCCD D    z
/authorizez/tokenz	/registerz/revokehandlerallow_methodsreturnc                 R    t          t          |           d|t          g          }|S )N*)appallow_originsr'   allow_headers)r   r   r   )r&   r'   cors_apps      r#   cors_middlewarer/   8   s6     W%%#23	  H Or%   Nprovider
issuer_urlservice_documentation_urlclient_registration_optionsrevocation_optionsc                 @   t          |           |pt                      }|pt                      }t          ||||          }t	          |           }t          dt          t          |          j        ddg          ddg          t          t          t          |           j        ddg          t          t          t          t          | |          j        ddg          ddg          g}|j        rRt          | |          }|                    t          t           t          |j        ddg          ddg                     |j        rQt#          | |          }	|                    t          t$          t          |	j        ddg          ddg                     |S )Nz'/.well-known/oauth-authorization-serverGETOPTIONSendpointmethodsPOST)options)r$   r   r   build_metadatar   r   r/   r   handleAUTHORIZATION_PATHr   
TOKEN_PATHr   enabledr   appendREGISTRATION_PATHr   REVOCATION_PATH)
r0   r1   r2   r3   r4   metadataclient_authenticatorroutesregistration_handlerrevocation_handlers
             r#   create_auth_routesrJ   E   s    
###"="\AZA\A\+B/@/B/B!#	 H /x88 	5$))0	"  I&	
 	
 	
 	 *(33:FO	
 	
 	
 	$X';<<C#  Y'	
 	
 	
!F4 #* 
2/ 
  
  
 	!((/Y'   +  		
 		
 		
 ! 
.x9MNN(&-Y'   +  		
 		
 		
 Mr%   c                 R   t          t          |                               d          t          z             }t          t          |                               d          t          z             }t          | |||j        dgd ddgddgd |d d d d dg          }|j        r<t          t          |                               d          t          z             |_	        |j        rEt          t          |                               d          t          z             |_        ddg|_        |S )	N/codeauthorization_coderefresh_tokenclient_secret_postclient_secret_basicS256)issuerauthorization_endpointtoken_endpointscopes_supportedresponse_types_supportedresponse_modes_supportedgrant_types_supported%token_endpoint_auth_methods_supported0token_endpoint_auth_signing_alg_values_supportedservice_documentationui_locales_supportedop_policy_uri
op_tos_uriintrospection_endpoint code_challenge_methods_supported)r   strrstripr?   r@   r   valid_scopesrA   rC   registration_endpointrD   revocation_endpoint*revocation_endpoint_auth_methods_supported)r1   r2   r3   r4   authorization_url	token_urlrE   s          r#   r=   r=      s*    #3z??#9#9##>#>AS#STT3z??11#66CDDI 0 4A"(!%3_E/CEZ.[9=7!#*0  H& #* e)3C
OO4J4J34O4ORc4c)d)d& ! l'1#j//2H2H2M2MP_2_'`'`$?SUj>k;Or%   resource_server_urlc                     t          t          |                     }|j        dk    r|j        nd}t          |j         d|j         d|           S )u  
    Build RFC 9728 compliant protected resource metadata URL.

    Inserts /.well-known/oauth-protected-resource between host and resource path
    as specified in RFC 9728 §3.1.

    Args:
        resource_server_url: The resource server URL (e.g., https://example.com/mcp)

    Returns:
        The metadata URL (e.g., https://example.com/.well-known/oauth-protected-resource/mcp)
    rL    z://z%/.well-known/oauth-protected-resource)r   rb   pathr   r   netloc)rj   parsedresource_paths      r#   build_resource_metadata_urlrq      sZ     c-..//F#);##5#5FKK2Mnn6=nn_lnnooor%   resource_urlauthorization_serversrV   resource_nameresource_documentationc                    ddl m} ddlm}  || ||||          } ||          }t	          |           }	t          t          |	                    }
|
j        }t          |t          |j
        ddg          ddg          gS )a}  
    Create routes for OAuth 2.0 Protected Resource Metadata (RFC 9728).

    Args:
        resource_url: The URL of this resource server
        authorization_servers: List of authorization servers that can issue tokens
        scopes_supported: Optional list of scopes supported by this resource

    Returns:
        List of Starlette routes for protected resource metadata
    r   ) ProtectedResourceMetadataHandler)ProtectedResourceMetadata)resourcers   rV   rt   ru   r6   r7   r8   )!mcp.server.auth.handlers.metadatarw   mcp.shared.authrx   rq   r   rb   rm   r   r/   r>   )rr   rs   rV   rt   ru   rw   rx   rE   r&   metadata_urlro   well_known_paths               r#    create_protected_resource_routesr~      s    $ SRRRRR999999((3)#5  H /.x88G /|<<Lc,''((FkO 	$W^eY5GHHI&	
 	
 	
 r%   )NNN)5collections.abcr   r   typingr   urllib.parser   pydanticr   starlette.middleware.corsr   starlette.requestsr	   starlette.responsesr
   starlette.routingr   r   starlette.typesr   "mcp.server.auth.handlers.authorizer   rz   r   !mcp.server.auth.handlers.registerr   mcp.server.auth.handlers.revoker   mcp.server.auth.handlers.tokenr   &mcp.server.auth.middleware.client_authr   mcp.server.auth.providerr   mcp.server.auth.settingsr   r   mcp.server.streamable_httpr   r{   r   r$   r?   r@   rC   rD   listrb   r/   rJ   r=   rq   r~    r%   r#   <module>r      sd   / / / / / / / /       ! ! ! ! ! !       4 4 4 4 4 4 & & & & & & ( ( ( ( ( ( 5 5 5 5 5 5 5 5 # # # # # # C C C C C C = = = = = = A A A A A A = = = = = = 7 7 7 7 7 7 F F F F F F E E E E E E Q Q Q Q Q Q Q Q B B B B B B ) ) ) ) ) )DZ D D D D4 " 
 
wiIh,?!??@
9
 
 
 
 
  48DH37N N.sC}=NN  *D0N ";T!A	N
 *D0N 
%[N N N Nb%%)D0% ";% *	%
 % % % %PpZ pJ p p p p, *. $04, ,,
+, 3i$&, :	,
 '-, 
%[, , , , , ,r%   