
    k7iN                        S r SSKJr  SSKrSSKJrJr  SSKJr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Jr  \" \5      r " S S\5      r " S S\5      rg)a  Supabase authentication provider for FastMCP.

This module provides SupabaseProvider - a complete authentication solution that integrates
with Supabase Auth's JWT verification, supporting Dynamic Client Registration (DCR)
for seamless MCP client authentication.
    )annotationsN)
AnyHttpUrlfield_validator)BaseSettingsSettingsConfigDict)JSONResponse)Route)RemoteAuthProviderTokenVerifier)JWTVerifier)ENV_FILEparse_scopes)
get_logger)NotSetNotSetTc                  l    \ rS rSr% \" S\SS9rS\S'   S\S'   SrS	\S
'   \	" S
SS9\
S 5       5       rSrg)SupabaseProviderSettings   FASTMCP_SERVER_AUTH_SUPABASE_ignore)
env_prefixenv_fileextrar   project_urlbase_urlNzlist[str] | Nonerequired_scopesbefore)modec                    [        U5      $ Nr   )clsvs     `/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/server/auth/providers/supabase.py_parse_scopes&SupabaseProviderSettings._parse_scopes%   s     A     )__name__
__module____qualname____firstlineno__r   r   model_config__annotations__r   r   classmethodr%   __static_attributes__r(   r'   r$   r   r      sP    %2L (,O%,&X6  7r'   r   c                  h   ^  \ rS rSrSr\\\SS.       SU 4S jjjr S	   S
U 4S jjjrSrU =r	$ )SupabaseProvider+   aO  Supabase metadata provider for DCR (Dynamic Client Registration).

This provider implements Supabase Auth integration using metadata forwarding.
This approach allows Supabase to handle the OAuth flow directly while FastMCP acts
as a resource server, verifying JWTs issued by Supabase Auth.

IMPORTANT SETUP REQUIREMENTS:

1. Supabase Project Setup:
   - Create a Supabase project at https://supabase.com
   - Note your project URL (e.g., "https://abc123.supabase.co")
   - For projects created after May 1st, 2025, asymmetric RS256 keys are used by default
   - For older projects, consider migrating to asymmetric keys for better security

2. JWT Verification:
   - FastMCP verifies JWTs using the JWKS endpoint at {project_url}/auth/v1/.well-known/jwks.json
   - JWTs are issued by {project_url}/auth/v1
   - Tokens are cached for up to 10 minutes by Supabase's edge servers

For detailed setup instructions, see:
https://supabase.com/docs/guides/auth/jwts

Example:
    ```python
    from fastmcp.server.auth.providers.supabase import SupabaseProvider

    # Create Supabase metadata provider (JWT verifier created automatically)
    supabase_auth = SupabaseProvider(
        project_url="https://abc123.supabase.co",
        base_url="https://your-fastmcp-server.com",
    )

    # Use with FastMCP
    mcp = FastMCP("My App", auth=supabase_auth)
    ```
N)r   r   r   token_verifierc               
  > [         R                  UUUS.R                  5        VVs0 s H  u  pVU[        Ld  M  XV_M     snn5      n[	        UR
                  5      R                  S5      U l        [	        UR                  5      R                  S5      U l        Uc0  [        U R
                   S3U R
                   S3SUR                  S9n[        TU ]-  U[        U R
                   S35      /U R                  S9  gs  snnf )	aO  Initialize Supabase metadata provider.

Args:
    project_url: Your Supabase project URL (e.g., "https://abc123.supabase.co")
    base_url: Public URL of this FastMCP server
    required_scopes: Optional list of scopes to require for all requests
    token_verifier: Optional token verifier. If None, creates JWT verifier for Supabase
)r   r   r   /Nz/auth/v1/.well-known/jwks.jsonz/auth/v1ES256)jwks_uriissuer	algorithmr   )r4   authorization_serversr   )r   model_validateitemsr   strr   rstripr   r   r   super__init__r   )	selfr   r   r   r4   kr#   settings	__class__s	           r$   rA   SupabaseProvider.__init__Q   s     ,:: $/ ('6 %'	DA
 F? 

 x334;;C@H--.55c: !( ,,--KL**+84! ( 8 8	N 	)#-1A1A0B(.K#L"M]] 	 	
1s
   C?
C?
c                j   >^  [         TT ]  U5      nU 4S jnUR                  [        SUS/S95        U$ )au  Get OAuth routes including Supabase authorization server metadata forwarding.

This returns the standard protected resource routes plus an authorization server
metadata endpoint that forwards Supabase's OAuth metadata to clients.

Args:
    mcp_path: The path where the MCP endpoint is mounted (e.g., "/mcp")
        This is used to advertise the resource URL in metadata.
c                  >#     [         R                  " 5        ISh  vN nUR                  TR                   S35      I Sh  vN nUR	                  5         UR                  5       n[        U5      sSSS5      ISh  vN   $  Nf NB N	! , ISh  vN  (       d  f       g= f! [         a  n[        SSU 3S.SS9s SnA$ SnAff = f7f)zQForward Supabase OAuth authorization server metadata with FastMCP customizations.Nz//auth/v1/.well-known/oauth-authorization-serverserver_errorz#Failed to fetch Supabase metadata: )errorerror_descriptioni  )status_code)httpxAsyncClientgetr   raise_for_statusjsonr   	Exception)requestclientresponsemetadataerB   s        r$   #oauth_authorization_server_metadataHSupabaseProvider.get_routes.<locals>.oauth_authorization_server_metadata   s      ,,..&%+ZZ++,,[\&  H --/'}}H'1 /..  /...  #!//RSTRU-V !$ s   CB% BB% "BB.B3B% ?B	 B% CB% B	B% B"BB"B% !C"B% %
C	/C>C	?CC		Cz'/.well-known/oauth-authorization-serverGET)endpointmethods)r@   
get_routesappendr	   )rB   mcp_pathroutesrX   rE   s   `   r$   r]   SupabaseProvider.get_routes   sA     #H-	( 	9<	
 r'   )r   r   )r   AnyHttpUrl | str | NotSetTr   rb   r   zlist[str] | NotSetT | Noner4   zTokenVerifier | Noner!   )r_   z
str | Nonereturnzlist[Route])
r)   r*   r+   r,   __doc__r   rA   r]   r0   __classcell__)rE   s   @r$   r2   r2   +   sk    #P 39/56</3-
 0-
 -	-

 4-
 --
 -
b  $,, 
, ,r'   r2   ) rd   
__future__r   rM   pydanticr   r   pydantic_settingsr   r   starlette.responsesr   starlette.routingr	   fastmcp.server.authr
   r   !fastmcp.server.auth.providers.jwtr   fastmcp.settingsr   fastmcp.utilities.authr   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r)   loggerr   r2   r(   r'   r$   <module>rr      sU    #  0 > , # A 9 % / 0 3	H	| "A) Ar'   