
    k7i                        S r SSKJr  SSK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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  Scalekit authentication provider for FastMCP.

This module provides ScalekitProvider - a complete authentication solution that integrates
with Scalekit's OAuth 2.1 and OpenID Connect services, supporting Resource Server
authentication for seamless MCP client authentication.
    )annotationsN)
AnyHttpUrl)BaseSettingsSettingsConfigDict)JSONResponse)Route)RemoteAuthProviderTokenVerifier)JWTVerifier)ENV_FILE)
get_logger)NotSetNotSetTc                  N    \ rS rSr% \" S\SS9rS\S'   S\S'   S\S	'   S\S
'   Srg)ScalekitProviderSettings   %FASTMCP_SERVER_AUTH_SCALEKITPROVIDER_ignore)
env_prefixenv_fileextrar   environment_urlstr	client_idresource_idmcp_url N)	__name__
__module____qualname____firstlineno__r   r   model_config__annotations____static_attributes__r       `/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/server/auth/providers/scalekit.pyr   r      s.    %:L  Nr%   r   c                  n   ^  \ 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	$ )ScalekitProvider&   aw  Scalekit resource server provider for OAuth 2.1 authentication.

This provider implements Scalekit integration using resource server pattern.
FastMCP acts as a protected resource server that validates access tokens issued
by Scalekit's authorization server.

IMPORTANT SETUP REQUIREMENTS:

1. Create an MCP Server in Scalekit Dashboard:
   - Go to your [Scalekit Dashboard](https://app.scalekit.com/)
   - Navigate to MCP Servers section
   - Register a new MCP Server with appropriate scopes
   - Ensure the Resource Identifier matches exactly what you configure as MCP URL
   - Note the Resource ID

2. Environment Configuration:
   - Set SCALEKIT_ENVIRONMENT_URL (e.g., https://your-env.scalekit.com)
   - Set SCALEKIT_CLIENT_ID from your OAuth application
   - Set SCALEKIT_RESOURCE_ID from your created resource
   - Set MCP_URL to your FastMCP server's public URL

For detailed setup instructions, see:
https://docs.scalekit.com/mcp/overview/

Example:
    ```python
    from fastmcp.server.auth.providers.scalekit import ScalekitProvider

    # Create Scalekit resource server provider
    scalekit_auth = ScalekitProvider(
        environment_url="https://your-env.scalekit.com",
        client_id="sk_client_...",
        resource_id="sk_resource_...",
        mcp_url="https://your-fastmcp-server.com",
    )

    # Use with FastMCP
    mcp = FastMCP("My App", auth=scalekit_auth)
    ```
N)r   r   r   r   token_verifierc               D  > [         R                  UUUUS.R                  5        VVs0 s H  u  pgU[        Ld  M  Xg_M     snn5      n[	        UR
                  5      R                  S5      U l        UR                  U l        UR                  U l        [	        UR                  5      U l	        Uc-  [        U R
                   S3U R
                  SU R                  S9n[        T	U ]1  U[        U R
                   SU R                   35      /U R                  S9  gs  snnf )	a  Initialize Scalekit resource server provider.

Args:
    environment_url: Your Scalekit environment URL (e.g., "https://your-env.scalekit.com")
    client_id: Your Scalekit OAuth client ID
    resource_id: Your Scalekit resource ID
    mcp_url: Public URL of this FastMCP server (used as audience)
    token_verifier: Optional token verifier. If None, creates JWT verifier for Scalekit
)r   r   r   r   /Nz/keysRS256)jwks_uriissuer	algorithmaudiencez/resources/)r*   authorization_serversbase_url)r   model_validateitemsr   r   r   rstripr   r   r   r   super__init__r   )
selfr   r   r   r   r*   kvsettings	__class__s
            r&   r8   ScalekitProvider.__init__P   s/   $ ,:: (7!*#.&	
 %'	DA F? 	
  #8#;#;<CCCH!++#//8++, !( 0017++!	N 	)d223;t?O?O>PQR# \\ 	 	
7	s
   D
D
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 Scalekit authorization server metadata forwarding.

This returns the standard protected resource routes plus an authorization server
metadata endpoint that forwards Scalekit'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TR                   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   $  Nr NB N	! , ISh  vN  (       d  f       g= f! [         a2  n[        R                  SU 35        [        SSU 3S.SS9s SnA$ SnAff = f7f)zQForward Scalekit OAuth authorization server metadata with FastMCP customizations.Nz2/.well-known/oauth-authorization-server/resources/z#Failed to fetch Scalekit metadata: server_error)errorerror_descriptioni  )status_code)httpxAsyncClientgetr   r   raise_for_statusjsonr   	ExceptionloggerrB   )requestclientresponsemetadataer9   s        r&   #oauth_authorization_server_metadataHScalekitProvider.get_routes.<locals>.oauth_authorization_server_metadata   s      ,,..&%+ZZ//00bcgcscsbtu&  H --/'}}H'1 /..  /...  B1#FG#!//RSTRU-V !$ s   C0B1 BB1 .BB.B?B1 BB1 C0B1 BB1 B.B B.*B1 -C0.B1 1
C-;'C("C-#C0(C--C0z'/.well-known/oauth-authorization-serverGET)endpointmethods)r7   
get_routesappendr   )r9   mcp_pathroutesrQ   r=   s   `   r&   rV   ScalekitProvider.get_routes   sA     #H-	* 	9<	
 r%   )r   r   r   r   )
r   AnyHttpUrl | str | NotSetTr   str | NotSetTr   r\   r   r[   r*   zTokenVerifier | None)N)rX   z
str | Nonereturnzlist[Route])
r   r   r    r!   __doc__r   r8   rV   r$   __classcell__)r=   s   @r&   r(   r(   &   sx    'X 7=#)%+.4/34
 44
 !	4

 #4
 ,4
 -4
 4
p  $-- 
- -r%   r(   )r^   
__future__r   rE   pydanticr   pydantic_settingsr   r   starlette.responsesr   starlette.routingr   fastmcp.server.authr	   r
   !fastmcp.server.auth.providers.jwtr   fastmcp.settingsr   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r   rK   r   r(   r   r%   r&   <module>rj      sR    #   > , # A 9 % 0 3	H	
| 
M) Mr%   