
    Ki                    z   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           i/home/jay/workspace/scripts/.codegraph-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   s9    Qtzz|Ptq!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   s/    !%!4!4!6HA!-1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)   zException | Noner*   )r;   r<   r=   __doc__r(   r/   r)   r*   r-   r$   r"   r@   r@   ^   s$    ]D*E:"E"r$   r@   	/callbackc           
     z    dfd}t        t        ||      g      }t        t        |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)
    c                   K   t        | j                        }t        j                  |      }|j                  rc|j
                  xs d}|j                  dk(  rd}nd| }" t        |      _        j                          t        t        |d      d      S |j                  s>d	}" t        |      _        j                          t        t        |d      d      S |j                  >d
}" t        |      _        j                          t        t        |d      d      S 42|j                  _
        |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: Fr   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&   r4   r*   r+   RuntimeErrorsetr   r#   r(   r)   )requestrH   callback_response
error_descuser_messageresult_containerresult_readyr   s        r"   callback_handlerz6create_oauth_callback_server.<locals>.callback_handler|   s    G001,66|D""*<<OJ !&&/9O!7
|D  +0H)5l)C &  ".$ $    !%%PL  +0H)5l)C &  ".$ $    ""*O 
  +0H)5l)C &  ".$ $    'L,D$5$:$:!%6%<%<"* L
 	
s   E;E>)routes	127.0.0.1offwarningzwebsockets-sansio)apphostportlifespan	log_levelws)rK   r   )r   r   r	   r   )rX   callback_pathr   rO   rP   rQ   rV   s     ```  r"   create_oauth_callback_serverr]   g   sH    *F
P 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)rX   r   zhttp://localhost:rS   rU   F)rW   rX   rZ   
access_log)TzFastMCP OAuthN)
r   strr   boolr   r`   r   r'   r:   r`   )rB   NNN)rX   intr\   r`   r   r'   rO   zOAuthCallbackResult | NonerP   zanyio.Event | Noner:   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]   
webbrowserrX   printserveropenrunconfigrV   r-   r$   r"   <module>rt      s   # !  , & # " 6 0	 	 	 
H	
  !	*** * 	*
 	*Z I I I # # # %!37'+h
hh h 1	h
 %h hV z3 D	
+,	
	(.N
OP	
&tf,ij 
(i
89	
%&	G *GF
 JOO'v-MNO GKK5 r$   