
    k7i8                         S r SSKJr  SSKrSSKJr  SSKJr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  \" \5      r " S S\5      r " S S\5      rg)a  OIDC Proxy Provider for FastMCP.

This provider acts as a transparent proxy to an upstream OIDC compliant Authorization
Server. It leverages the OAuthProxy class to handle Dynamic Client Registration and
forwarding of all OAuth flows.

This implementation is based on:
    OpenID Connect Discovery 1.0 - https://openid.net/specs/openid-connect-discovery-1_0.html
    OAuth 2.0 Authorization Server Metadata - https://datatracker.ietf.org/doc/html/rfc8414
    )SequenceN)AsyncKeyValue)
AnyHttpUrl	BaseModelmodel_validator)Self)TokenVerifier)
OAuthProxy)JWTVerifier)
get_loggerc            	          \ rS rSr% SrSr\\S'   Sr\	\
-  S-  \S'   Sr\	\
-  S-  \S'   Sr\	\
-  S-  \S'   Sr\	\
-  S-  \S	'   Sr\	\
-  S-  \S
'   Sr\	\
-  S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr\\
   S-  \S'   Sr \\
   S-  \S'   Sr!\\
   S-  \S'   Sr"\\
   S-  \S'   Sr#\\
   S-  \S'   Sr$\\
   S-  \S'   Sr%\	\
-  S-  \S '   Sr&\\
   S-  \S!'   Sr'\\
   S-  \S"'   Sr(\S-  \S#'   Sr)\S-  \S$'   Sr*\S-  \S%'   Sr+\S-  \S&'   Sr,\	\
-  S-  \S''   Sr-\	\
-  S-  \S('   Sr.\	\
-  S-  \S)'   Sr/\\
   S-  \S*'   Sr0\\
   S-  \S+'   Sr1\	\
-  S-  \S,'   Sr2\\
   S-  \S-'   Sr3\\
   S-  \S.'   Sr4\\
   S-  \S/'   Sr5\
S-  \S0'   \6" S1S29S3\74S4 j5       r8\9S5\	S\S-  S6\:S-  S3\74S7 j5       r;S8r<g)9OIDCConfiguration   zOIDC Configuration.

See:
    https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata
    https://datatracker.ietf.org/doc/html/rfc8414#section-2
TstrictNissuerauthorization_endpointtoken_endpointuserinfo_endpointjwks_uriregistration_endpointscopes_supportedresponse_types_supportedresponse_modes_supportedgrant_types_supportedacr_values_supportedsubject_types_supported%id_token_signing_alg_values_supported(id_token_encryption_alg_values_supported(id_token_encryption_enc_values_supported%userinfo_signing_alg_values_supported(userinfo_encryption_alg_values_supported(userinfo_encryption_enc_values_supported+request_object_signing_alg_values_supported.request_object_encryption_alg_values_supported.request_object_encryption_enc_values_supported%token_endpoint_auth_methods_supported0token_endpoint_auth_signing_alg_values_supporteddisplay_values_supportedclaim_types_supportedclaims_supportedservice_documentationclaims_locales_supportedui_locales_supportedclaims_parameter_supportedrequest_parameter_supportedrequest_uri_parameter_supported require_request_uri_registrationop_policy_uri
op_tos_urirevocation_endpoint*revocation_endpoint_auth_methods_supported5revocation_endpoint_auth_signing_alg_values_supportedintrospection_endpoint-introspection_endpoint_auth_methods_supported8introspection_endpoint_auth_signing_alg_values_supported code_challenge_methods_supportedsigned_metadataafter)modereturnc                    ^  T R                   (       d  T $ SS[        S[        SS4U 4S jjjnU" SS5        U" SS5        U" S	S5        U" S
S5        U" S5        U" S5        U" S5        T $ )zEnforce strict rules.attris_urlr>   Nc                 :  > [        TU S 5      nU(       d%  SU  3n[        R                  U5        [        U5      eU(       a  [	        U[
        5      (       a  g  [        U5        g ! [         a+  nSU  3n[        R                  U5        [        U5      UeS nAff = f)Nz)Missing required configuration metadata: z(Invalid URL for configuration metadata: )getattrloggererror
ValueError
isinstancer   	Exception)r@   rA   valuemessageeselfs        X/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/server/auth/oidc_proxy.pyenforce2OIDCConfiguration._enforce_strict.<locals>.enforcer   s    D$-EEdVLW% ))Zz::15! 1DTFKW% )q01s   A% %
B/&BBr   Tr   r   r   r   r   r   )F)r   strbool)rL   rN   s   ` rM   _enforce_strict!OIDCConfiguration._enforce_strictl   s|     {{K	1# 	1t 	1 	1 	1" 	$($/ $'
D!*+)*78    
config_urltimeout_secondsc                   0 nUb  X4S'    [         R                  " [        U5      40 UD6nUR                  5         UR	                  5       nUb  X&S'   U R                  U5      $ ! [         a    [        R                  SU 35        e f = f)zGet the OIDC configuration for the specified config URL.

Args:
    config_url: The OIDC config URL
    strict: The strict flag for the configuration
    timeout_seconds: HTTP request timeout in seconds
timeoutr   z1Unable to get OIDC configuration for config url: )	httpxgetrP   raise_for_statusjsonmodel_validaterH   rD   	exception)clsrU   r   rV   
get_kwargsresponseconfig_datas          rM   get_oidc_configuration(OIDCConfiguration.get_oidc_configuration   s     
&$3y!	yyZ?J?H%%'"--/K!(.H%%%k22 	CJ<P 		s   AA# #$B )=__name__
__module____qualname____firstlineno____doc__r   rQ   __annotations__r   r   rP   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r   r   rR   classmethodintrc   __static_attributes__re   rT   rM   r   r      s    FD '+FJt#*6:J,t3:.2NJ$t+215zC'$.5(,Hj3%,59:+d29-1hsmd*159hsmd2959hsmd29268C=4/615(3-$.548Xc]T18BF)8C=4+?FEI,hsmd.BIEI,hsmd.BIBF)8C=4+?FEI,hsmd.BIEI,hsmd.BIHL/#1ELKO2HSMD4HOKO2HSMD4HOBF)8C=4+?FMQ4hsmd6JQ59hsmd29268C=4/6-1hsmd*159:+d2959hsmd2915(3-$.5.2t2/3337#TD[748$dTk8-1M:#d*1*.J
S 4'. 48c)D07GK.0DKRV98C=4;OV6:J,t3:JN18C=43GN =hsmd>R  >B$hsmd&:A"&OS4Z&'"  #@ #04tNQTXj	 rT   r   c            #       t  ^  \ rS rSr% Sr\\S'   SSSSSSSSSSSSS.S\\-  S\	S-  S	\S
\S\S-  S\
S-  S\S-  S\\   S-  S\\-  S\\-  S-  S\S-  S\\   S-  S\S-  S\\-  S-  S\S-  S\	SS4"U 4S jjjrS\S\	S-  S\
S-  S\4S jrSSSSS.S\S-  S\S-  S\\   S-  S\
S-  S\4
S jjrSrU =r$ )	OIDCProxy   ah  OAuth provider that wraps OAuthProxy to provide configuration via an OIDC configuration URL.

This provider makes it easier to add OAuth protection for any upstream provider
that is OIDC compliant.

Example:
    ```python
    from fastmcp import FastMCP
    from fastmcp.server.auth.oidc_proxy import OIDCProxy

    # Simple OIDC based protection
    auth = OIDCProxy(
        config_url="https://oidc.config.url",
        client_id="your-oidc-client-id",
        client_secret="your-oidc-client-secret",
        base_url="https://your.server.url",
    )

    mcp = FastMCP("My Protected Server", auth=auth)
    ```
oidc_configNT)r   audiencerV   	algorithmrequired_scopes
issuer_urlredirect_pathallowed_client_redirect_urisclient_storagejwt_signing_keytoken_endpoint_auth_methodrequire_authorization_consentrU   r   	client_idclient_secretrs   rV   rt   ru   base_urlrv   rw   rx   ry   rz   r{   r|   r>   c                  > U(       d  [        S5      eU(       d  [        S5      eU(       d  [        S5      eU	(       d  [        S5      e[        U[        5      (       a  [        U5      nU R	                  XU5      U l        U R
                  R                  (       a  U R
                  R                  (       d-  [        R                  SU R
                   35        [        S5      eU R
                  R                  (       a  [        U R
                  R                  5      OSnU R                  UUUUS9n[        U R
                  R                  5      [        U R
                  R                  5      UUUUU	U
=(       d    U	U R
                  R                  UUUUUS	.nU(       a  UUS
'   U(       a  SU0nUUS'   UUS'   [        TU ]8  " S0 UD6  g)ai	  Initialize the OIDC proxy provider.

Args:
    config_url: URL of upstream configuration
    strict: Optional strict flag for the configuration
    client_id: Client ID registered with upstream server
    client_secret: Client secret for upstream server
    audience: Audience for upstream server
    timeout_seconds: HTTP request timeout in seconds
    algorithm: Token verifier algorithm
    required_scopes: Required OAuth scopes
    base_url: Public URL where OAuth endpoints will be accessible (includes any mount path)
    issuer_url: Issuer URL for OAuth metadata (defaults to base_url). Use root-level URL
        to avoid 404s during discovery when mounting under a path.
    redirect_path: Redirect path configured in upstream OAuth app (defaults to "/auth/callback")
    allowed_client_redirect_uris: List of allowed redirect URI patterns for MCP clients.
        Patterns support wildcards (e.g., "http://localhost:*", "https://*.example.com/*").
        If None (default), only localhost redirect URIs are allowed.
        If empty list, all redirect URIs are allowed (not recommended for production).
        These are for MCP clients performing loopback redirects, NOT for the upstream OAuth app.
    client_storage: Storage backend for OAuth state (client registrations, encrypted tokens).
        If None, a DiskStore will be created in the data directory (derived from `platformdirs`). The
        disk store will be encrypted using a key derived from the JWT Signing Key.
    jwt_signing_key: Secret for signing FastMCP JWT tokens (any string or bytes). If bytes are provided,
        they will be used as is. If a string is provided, it will be derived into a 32-byte key. If not
        provided, the upstream client secret will be used to derive a 32-byte key using PBKDF2.
    token_endpoint_auth_method: Token endpoint authentication method for upstream server.
        Common values: "client_secret_basic", "client_secret_post", "none".
        If None, authlib will use its default (typically "client_secret_basic").
    require_authorization_consent: Whether to require user consent before authorizing clients (default True).
        When True, users see a consent screen before being redirected to the upstream IdP.
        When False, authorization proceeds directly without user confirmation.
        SECURITY WARNING: Only disable for local development or testing environments.
zMissing required config URLzMissing required client idzMissing required client secretzMissing required base URLzInvalid OIDC Configuration: zMissing required OIDC endpointsNrt   rs   ru   rV   )upstream_authorization_endpointupstream_token_endpointupstream_client_idupstream_client_secretupstream_revocation_endpointtoken_verifierr   rv   service_documentation_urlrx   ry   rz   r{   r|   rw   rs   extra_authorize_paramsextra_token_paramsre   )rF   rG   rP   r   rc   rr   r   r   rD   debugr4   get_token_verifierr+   super__init__)rL   rU   r   r}   r~   rs   rV   rt   ru   r   rv   rw   rx   ry   rz   r{   r|   r4   r   init_kwargsextra_params	__class__s                        rM   r   OIDCProxy.__init__   s   | :;;9::=>>899j#&&#J/J66
   77##22LL78H8H7IJK>?? 33   445 	 00++	 1 
 03  770 (+4+;+;+J+J'K"+&3,?, $0)-)9)9)O)O,H,.*D-J!
& +8K(&1L4@K010<K,-';'rT   c                 *    [         R                  XUS9$ )zGets the OIDC configuration for the specified configuration URL.

Args:
    config_url: The OIDC configuration URL
    strict: The strict flag for the configuration
    timeout_seconds: HTTP request timeout in seconds
)r   rV   )r   rc   )rL   rU   r   rV   s       rM   rc    OIDCProxy.get_oidc_configurationF  s!     !77 8 
 	
rT   r   c                    [        [        U R                  R                  5      [        U R                  R                  5      UUUS9$ )a(  Creates the token verifier for the specified OIDC configuration and arguments.

Args:
    algorithm: Optional token verifier algorithm
    audience: Optional token verifier audience
    required_scopes: Optional token verifier required_scopes
    timeout_seconds: HTTP request timeout in seconds
)r   r   rt   rs   ru   )r   rP   rr   r   r   )rL   rt   rs   ru   rV   s        rM   r   OIDCProxy.get_token_verifierW  sB      ))223t''../+
 	
rT   )rr   )rf   rg   rh   ri   rj   r   rk   r   rP   rQ   rm   listr   bytesr   rc   r	   r   rn   __classcell__)r   s   @rM   rp   rp      s   , #" #  $&* $,0 /3$(9=/3.215.25( $	(
 t( ( ( *( t( :( cT)( s"(  $t+!(" Tz#(& '+3i$&6'(( &,)(, ut+-(0 %($J1(4 (,5(6 
7( (B

 t
 t	

 

( !%#,0&*
 :
 *	

 cT)
 t
 

 
rT   rp   )rj   collections.abcr   rY   key_value.aio.protocolsr   pydanticr   r   r   typing_extensionsr   fastmcp.server.authr	   fastmcp.server.auth.oauth_proxyr
   !fastmcp.server.auth.providers.jwtr   fastmcp.utilities.loggingr   rf   rD   r   rp   re   rT   rM   <module>r      sQ   	 %  1 ; ; " - 6 9 0	H	N	 NbA

 A
rT   