
    kKjc                       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	 	 	 d#	 	 	 	 	 	 	 	 	 d$dZe G d d             Ze G d d             Z 	 	 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 	 	 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#d      Z% e"jL                  de# d        ejN                  e%jP                  jR                  de#d d!"       yy)'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_messagec                    |rdnd}d}|r|rt        d| dd      }n|st        | ddd      }dt                d	t        ||
       d	| d}t        t        z   t
        z   }t        |||      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
            
is_successz
            <div class="close-instruction">
                You can safely close this tab now.
            </div>
        </div>
    )contenttitleadditional_styles)r   r   r   r   r   r   r   )messager   r   
server_urlstatus_titledetail_infor   r   s           n/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/client/oauth_callback.pycreate_callback_htmlr#   "   s     (2#7N 
 Kj%ZL)DD
 %dTT

]O "<JGH IM 		G .?BTT+     c                  `    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       Z	d
dZ
y)CallbackResponseN
str | Nonecodestateerrorerror_descriptionc           
          | di |j                         D ci c]  \  }}|| j                  v s|| c}}S c c}}w )N )items__annotations__)clsdatakvs       r"   	from_dictzCallbackResponse.from_dictV   s=    Qtzz|P|tq!qC<O<O7Oad|PQQPs   ::c                p    | j                   j                         D ci c]  \  }}|	|| c}}S c c}}w )N)__dict__r.   )selfr2   r3   s      r"   to_dictzCallbackResponse.to_dictZ   s5    !%!4!4!6H!6A!-1!6HHHs   
22)r1   dict[str, str]returnr&   )r:   r9   )__name__
__module____qualname__r(   r/   r)   r*   r+   classmethodr4   r8   r-   r$   r"   r&   r&   O   sE    D*E:E:$(z(R RIr$   r&   c                  <    e Zd ZU dZdZded<   dZded<   dZded<   y)OAuthCallbackResultzSContainer for OAuth callback results, used with anyio.Event for async coordination.Nr'   r(   r)   Exception | Noner*   )r;   r<   r=   __doc__r(   r/   r)   r*   r-   r$   r"   r@   r@   ^   s$    ]D*E:"E"r$   r@   	127.0.0.1	/callbackc           
         dddd	 	 	 	 	 	 	 d
fddfd}t        t        ||      g      }t        t        ||| 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)
    N)r(   r)   r*   c                z    j                         ry| _        |_        |_        j	                          y)z?Store the first callback result and ignore subsequent requests.N)is_setr(   r)   r*   set)r(   r)   r*   result_containerresult_readys      r"   store_result_oncez7create_oauth_callback_server.<locals>.store_result_once}   sE     #|';|?R?R?T $!&!&r$   c                d  K   t        | j                        }t        j                  |      }|j                  rQ|j
                  xs d}|j                  dk(  rd}nd| } t        |             t        t        |d      d	      S |j                  s,d
} t        |             t        t        |d      d	      S |j                  ,d} t        |             t        t        |d      d	      S  |j                  |j                         t        t        dd            S w)z:Handle OAuth callback requests with proper HTML responses.zUnknown erroraccess_deniedz.Access was denied by the authorization server.zAuthorization failed: )r*   Fr   i  )status_codez3No authorization code was received from the server.z=The OAuth server did not return the expected state parameter.)r(   r)   r   T)r   r   )dictquery_paramsr&   r4   r*   r+   RuntimeErrorr   r#   r(   r)   )requestrP   callback_response
error_descuser_messager   rK   s        r"   callback_handlerz6create_oauth_callback_server.<locals>.callback_handler   sL    G001,66|D""*<<OJ !&&/9O!7
|D L$>?.$ $    !%%PL L$>?.$ $    ""*O 
 L$>?.$ $    	"''#))	

 + L
 	
s   D-D0)routesoffwarningzwebsockets-sansio)apphostportlifespan	log_levelws)r(   r'   r)   r'   r*   rA   r:   None)rR   r   )r   r   r	   r   )	r\   r[   callback_pathr   rI   rJ   rV   rZ   rK   s	      ```  @r"   create_oauth_callback_serverrb   g   s~    0   "&	   	
 
@
D E-1ABC
DC"	
	 	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)r\   r   zhttp://localhost:rY   F)r[   r\   r^   
access_log)TzFastMCP OAuthN)
r   strr   boolr   re   r   r'   r:   re   )rC   rD   NNN)r\   intr[   re   ra   re   r   r'   rI   zOAuthCallbackResult | NonerJ   zanyio.Event | Noner:   r	   )*rB   
__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@   rb   
webbrowserr\   printserveropenrunconfigrZ   r-   r$   r"   <module>ry      s   # !  , & # " 6 0	 	 	 
H	
  !	*** * 	*
 	*Z I I I # # # $!37'+r
r
r r 	r
 1r %r rj z3 D	
+,	
	(.N
OP	
&tf,ij 
(i
89	
%&	G *GF
 JOO'v-MNO GKK5 r$   