
    Ki                         d Z ddlmZ ddlmZmZ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mZ  ee      Z G d
 de      Z G d de      Zy)a  Auth0 OAuth provider for FastMCP.

This module provides a complete Auth0 integration that's ready to use with
just the configuration URL, client ID, client secret, audience, and base URL.

Example:
    ```python
    from fastmcp import FastMCP
    from fastmcp.server.auth.providers.auth0 import Auth0Provider

    # Simple Auth0 OAuth protection
    auth = Auth0Provider(
        config_url="https://auth0.config.url",
        client_id="your-auth0-client-id",
        client_secret="your-auth0-client-secret",
        audience="your-auth0-api-audience",
        base_url="http://localhost:8000",
    )

    mcp = FastMCP("My Protected Server", auth=auth)
    ```
    )AsyncKeyValue)
AnyHttpUrl	SecretStrfield_validator)BaseSettingsSettingsConfigDict)	OIDCProxy)ENV_FILEparse_scopes)
get_logger)NotSetNotSetTc                   &   e Zd ZU dZ eded      ZdZedz  e	d<   dZ
edz  e	d<   dZedz  e	d<   dZedz  e	d	<   dZedz  e	d
<   dZedz  e	d<   dZedz  e	d<   dZee   dz  e	d<   dZee   dz  e	d<   dZedz  e	d<    edd      ed               Zy)Auth0ProviderSettingsz!Settings for Auth0 OIDC provider.FASTMCP_SERVER_AUTH_AUTH0_ignore)
env_prefixenv_fileextraN
config_url	client_idclient_secretaudiencebase_url
issuer_urlredirect_pathrequired_scopesallowed_client_redirect_urisjwt_signing_keybefore)modec                     t        |      S )Nr   )clsvs     o/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/server/auth/providers/auth0.py_parse_scopesz#Auth0ProviderSettings._parse_scopes9   s     A    )__name__
__module____qualname____doc__r   r
   model_configr   r   __annotations__r   strr   r   r   r   r   r   r   listr   r    r   classmethodr'    r(   r&   r   r   %   s    +%/L %)J
T!( IsTz &*M9t#*HcDj"&Hj4&$(J
T!( $M3:$(,OT#Y%,59 $s)d"29"&OS4Z&&X6  7r(   r   c                        e Zd ZdZeeeeeeeeededddeez  ez  deez  deez  deez  d	eez  ez  d
eez  ez  dee   ez  deez  dee   ez  de	dz  dee
z  ez  deddf fdZ xZS )Auth0Providera  An Auth0 provider implementation for FastMCP.

    This provider is a complete Auth0 integration that's ready to use with
    just the configuration URL, client ID, client secret, audience, and base URL.

    Example:
        ```python
        from fastmcp import FastMCP
        from fastmcp.server.auth.providers.auth0 import Auth0Provider

        # Simple Auth0 OAuth protection
        auth = Auth0Provider(
            config_url="https://auth0.config.url",
            client_id="your-auth0-client-id",
            client_secret="your-auth0-client-secret",
            audience="your-auth0-api-audience",
            base_url="http://localhost:8000",
        )

        mcp = FastMCP("My Protected Server", auth=auth)
        ```
    NT)r   r   r   r   r   r   r   r   r   client_storager    require_authorization_consentr   r   r   r   r   r   r   r   r   r5   r    r6   returnc                   t         j                  |||||||||	|d
j                         D ci c]  \  }}|t        ur|| c}}      }|j                  st        d      |j                  st        d      |j                  st        d      |j                  st        d      |j                  st        d      |j                  xs dg}t        | 1  |j                  |j                  |j                  j                         |j                  |j                  |j                  |j                  ||j                   |
|j"                  |       t$        j'                  d	|j                  |       y
c c}}w )aV  Initialize Auth0 OAuth provider.

        Args:
            config_url: Auth0 config URL
            client_id: Auth0 application client id
            client_secret: Auth0 application client secret
            audience: Auth0 API audience
            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.
            required_scopes: Required Auth0 scopes (defaults to ["openid"])
            redirect_path: Redirect path configured in Auth0 application
            allowed_client_redirect_uris: List of allowed redirect URI patterns for MCP clients.
                If None (default), all URIs are allowed. If empty list, no URIs are allowed.
            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.
            require_authorization_consent: Whether to require user consent before authorizing clients (default True).
                When True, users see a consent screen before being redirected to Auth0.
                When False, authorization proceeds directly without user confirmation.
                SECURITY WARNING: Only disable for local development or testing environments.
        )
r   r   r   r   r   r   r   r   r   r    zRconfig_url is required - set via parameter or FASTMCP_SERVER_AUTH_AUTH0_CONFIG_URLzPclient_id is required - set via parameter or FASTMCP_SERVER_AUTH_AUTH0_CLIENT_IDzXclient_secret is required - set via parameter or FASTMCP_SERVER_AUTH_AUTH0_CLIENT_SECRETzNaudience is required - set via parameter or FASTMCP_SERVER_AUTH_AUTH0_AUDIENCEzNbase_url is required - set via parameter or FASTMCP_SERVER_AUTH_AUTH0_BASE_URLopenid)r   r   r   r   r   r   r   r   r   r5   r    r6   z>Initialized Auth0 OAuth provider for client %s with scopes: %sN)r   model_validateitemsr   r   
ValueErrorr   r   r   r   r   super__init__get_secret_valuer   r   r   r    loggerdebug)selfr   r   r   r   r   r   r   r   r   r5   r    r6   kr%   settingsauth0_required_scopes	__class__s                    r&   r>   zAuth0Provider.__init__W   s   R )77 #-!*%2 ( (",'6%24P'6 %'Aq F? 1
& ""d  !!b  %%j    `    `  !) 8 8 FXJ**(("00AAC&&&&**"001)1)N)N)$44*G 	 	
 	L!	
ys   E5
)r)   r*   r+   r,   r   r   r/   r   r0   r   bytesboolr>   __classcell__)rF   s   @r&   r4   r4   ?   s   4 28#)'-"(/517/5'-<B/317.2j
 $w.j
 =	j

 W}j
 -j
 s"W,j
 $w.j
 cW,j
 W}j
 '+3i'&9j
 &,j
 uw.j
 (,j
 
j
 j
r(   r4   N)r,   key_value.aio.protocolsr   pydanticr   r   r   pydantic_settingsr   r   fastmcp.server.auth.oidc_proxyr	   fastmcp.settingsr
   fastmcp.utilities.authr   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r)   r@   r   r4   r2   r(   r&   <module>rR      sK   . 2 ; ; > 4 % / 0 3	H	L 4B
I B
r(   