
    Ch"                        S SK JrJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJrJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SK J!r!  S SK"J#r#J$r$  S SK%J&r&  S SK'J(r(  S\4S jr)Sr*Sr+Sr,Sr-S\\/\\\   -  4   S\.\/   S\4S jr0   S.S\!\\\4   S \S!\S-  S"\#S-  S#\$S-  S\.\   4S$ jjr1S \S!\S-  S"\#S#\$S\(4
S% jr2S&\S\4S' jr3   S.S(\S)\.\   S*\.\/   S-  S+\/S-  S,\S-  S\.\   4S- jjr4g)/    )	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                 $   U R                   S:w  aH  U R                  S:w  a8  U R                  b+  U R                  R                  S5      (       d  [        S5      eU R                  (       a  [        S5      eU R
                  (       a  [        S5      eg)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    P/home/james-whalen/.local/lib/python3.13/site-packages/mcp/server/auth/routes.pyvalidate_issuer_urlr$      ss     	

gHH#XX!#((*=*=k*J*J344 ||>??
yyBCC     z
/authorizez/tokenz	/registerz/revokehandlerallow_methodsreturnc                 :    [        [        U 5      SU[        /S9nU$ )N*)appallow_originsr'   allow_headers)r   r   r   )r&   r'   cors_apps      r#   cors_middlewarer/   8   s)     W%#23	H Or%   Nprovider
issuer_urlservice_documentation_urlclient_registration_optionsrevocation_optionsc                    [        U5        U=(       d
    [        5       nU=(       d
    [        5       n[        UUUU5      n[	        U 5      n[        S[        [        U5      R                  SS/5      SS/S9[        [        [        U 5      R                  SS/S9[        [        [        [        X5      R                  SS/5      SS/S9/nUR                  (       a@  [        U US9nUR                  [        [         [        UR                  SS/5      SS/S95        UR                  (       aA  [#        X5      n	UR                  [        [$        [        U	R                  SS/5      SS/S95        U$ )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\+B/@/B!#	H /x8 	5$)00	" I&	
 	 *(3::FO	
 	$X<CC# Y'	
!F4 #**2/ 
 	!((//Y'  +		
 !!.xN(&--Y'  +		
 Mr%   c                 
   [        [        U 5      R                  S5      [        -   5      n[        [        U 5      R                  S5      [        -   5      n[        U UUUR                  S/S SS/S/S US S S S S/S9nUR                  (       a/  [        [        U 5      R                  S5      [        -   5      Ul	        UR                  (       a7  [        [        U 5      R                  S5      [        -   5      Ul        S/Ul        U$ )N/codeauthorization_coderefresh_tokenclient_secret_post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3z?11#6CDI 0 4AA"(!%3_E/C.D9=7!#*0H& #**)3C
O4J4J34ORc4c)d& !!'1#j/2H2H2MP_2_'`$?S>T;Or%   resource_server_urlc                     [        [        U 5      5      nUR                  S:w  a  UR                  OSn[        UR                   SUR
                   SU 35      $ )ue  
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   ra   pathr   r   netloc)ri   parsedresource_paths      r#   build_resource_metadata_urlrp      sQ     c-./F#);;##5FKK2Ms6==/9^_l^mnoor%   resource_urlauthorization_serversrU   resource_nameresource_documentationc                     SSK Jn  SSKJn  U" U UUUUS9nU" U5      n[	        U 5      n	[        [        U	5      5      n
U
R                  n[        U[        UR                  SS/5      SS/S9/$ )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)resourcerr   rU   rs   rt   r6   r7   r8   )!mcp.server.auth.handlers.metadatarv   mcp.shared.authrw   rp   r   ra   rl   r   r/   r>   )rq   rr   rU   rs   rt   rv   rw   rE   r&   metadata_urlrn   well_known_paths               r#    create_protected_resource_routesr}      s    $ S9(3)#5H /x8G /|<Lc,'(FkkO 	$W^^eY5G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   ry   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   rz   r   r$   r?   r@   rC   rD   listra   r/   rJ   r=   rp   r}    r%   r#   <module>r      s   /  !  4 & ( 5 # C = A = 7 F E Q B )DZ D4 " 
 
wiIh,?!??@
9
 
  48DH37N.sC}=NN  *D0N ";T!A	N
 *D0N 
%[Nb%%)D0% ";% *	%
 %PpZ pJ p, *. $04,,
+, 3i$&, :	,
 '-, 
%[,r%   