o
    i                     @  s  d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZmZmZmZ eeZ			d7d8ddZeG dd dZeG dd dZ 				d9d:d%d&Z!ed'kr	 ddl"Z"ddlZe Z#e$d( e$d) e$d*e# d+ e$d,e# d- e$d.e# d e$d/ e$  e!e#d0d1Z%e"&d2e# d+ ej'e%j(j)d3e#d4d5d6 dS dS );z
OAuth callback server for handling authorization code flows.

This module provides a reusable callback server that can handle OAuth redirects
and display styled responses to users.
    )annotations)	dataclassN)	Starlette)Request)Route)ConfigServer)find_available_port)
get_logger)HELPER_TEXT_STYLESINFO_BOX_STYLESSTATUS_MESSAGE_STYLEScreate_info_boxcreate_logocreate_pagecreate_secure_html_responsecreate_status_messageTFastMCP OAuthmessagestr
is_successbooltitle
server_url
str | Nonereturnc                 C  s~   |rdnd}d}|r|rt d| ddd}n
|s!t | dddd}dt  d	t||d
 d	| d}tt t }t|||dS )z2Create a styled HTML response for OAuth callbacks.zAuthentication successfulzAuthentication failed zConnected to: T)centered	monospace)is_errorr   r   z-
        <div class="container">
            z
            r   z
            <div class="close-instruction">
                You can safely close this tab now.
            </div>
        </div>
    )contentr   additional_styles)r   r   r   r   r   r   r   )r   r   r   r   status_titledetail_infor!   r"    r%   c/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/client/oauth_callback.pycreate_callback_html"   s0   
	
r'   c                   @  sV   e Zd ZU dZded< dZded< dZded< dZded< edd
dZ	dddZ
dS )CallbackResponseNr   codestateerrorerror_descriptiondatadict[str, str]r   c                   s     di  fdd|  D S )Nc                   s    i | ]\}}| j v r||qS r%   )__annotations__.0kvclsr%   r&   
<dictcomp>X   s     z.CallbackResponse.from_dict.<locals>.<dictcomp>r%   )items)r5   r-   r%   r4   r&   	from_dictV   s    zCallbackResponse.from_dictc                 C  s   dd | j  D S )Nc                 S  s   i | ]\}}|d ur||qS )Nr%   r0   r%   r%   r&   r6   [   s    z,CallbackResponse.to_dict.<locals>.<dictcomp>)__dict__r7   )selfr%   r%   r&   to_dictZ   s   zCallbackResponse.to_dict)r-   r.   r   r(   )r   r.   )__name__
__module____qualname__r)   r/   r*   r+   r,   classmethodr8   r;   r%   r%   r%   r&   r(   O   s   
 r(   c                   @  s6   e Zd ZU dZdZded< dZded< dZded< dS )OAuthCallbackResultzSContainer for OAuth callback results, used with anyio.Event for async coordination.Nr   r)   r*   zException | Noner+   )r<   r=   r>   __doc__r)   r/   r*   r+   r%   r%   r%   r&   r@   ^   s
   
 r@   	/callbackportintcallback_pathresult_containerOAuthCallbackResult | Noneresult_readyanyio.Event | Noner   c              	     s<   d fdd}t t||gd}tt|d| ddd	d
S )a  
    Create an OAuth callback server.

    Args:
        port: The port to run the server on
        callback_path: The path to listen for OAuth redirects on
        server_url: Optional server URL to display in success messages
        result_container: Optional container to store callback results
        result_ready: Optional event to signal when callback is received

    Returns:
        Configured uvicorn Server instance (not yet running)
    requestr   c                   s4  t | j}t|}|jr;|jpd}|jdkrd}nd| } dur1dur1t| _  tt	|dddd	S |j
s[d
} durQdurQt| _  tt	|dddd	S |jdu r}d} dursdurst| _  tt	|dddd	S  durdur|j
 _
|j _  tt	dddS )z:Handle OAuth callback requests with proper HTML responses.zUnknown erroraccess_deniedz.Access was denied by the authorization server.zAuthorization failed: NFr    i  )status_codez3No authorization code was received from the server.z=The OAuth server did not return the expected state parameter.r   T)r   r   )dictquery_paramsr(   r8   r+   r,   RuntimeErrorsetr   r'   r)   r*   )rJ   rN   callback_response
error_descuser_messagerF   rH   r   r%   r&   callback_handler|   sd   







	
	z6create_oauth_callback_server.<locals>.callback_handler)routes	127.0.0.1offwarningzwebsockets-sansio)apphostrC   lifespan	log_levelwsN)rJ   r   )r   r   r   r   )rC   rE   r   rF   rH   rU   rZ   r%   rT   r&   create_oauth_callback_serverg   s   Hr_   __main__u   🎭 OAuth Callback Test Serveru   📍 Test URLs:z  Success: http://localhost:z /callback?code=test123&state=xyzz  Error:   http://localhost:z=/callback?error=access_denied&error_description=User%20deniedz  Missing: http://localhost:u   🛑 Press Ctrl+C to stopz'https://fastmcp-test-server.example.com)rC   r   zhttp://localhost:rW   rY   F)r[   rC   r]   
access_log)Tr   N)
r   r   r   r   r   r   r   r   r   r   )rB   NNN)rC   rD   rE   r   r   r   rF   rG   rH   rI   r   r   )*rA   
__future__r   dataclassesr   anyiostarlette.applicationsr   starlette.requestsr   starlette.routingr   uvicornr   r   fastmcp.utilities.httpr	   fastmcp.utilities.loggingr
   fastmcp.utilities.uir   r   r   r   r   r   r   r   r<   loggerr'   r(   r@   r_   
webbrowserrC   printserveropenrunconfigrZ   r%   r%   r%   r&   <module>   sf    (-
k

