
    kKj                    d   d Z ddlmZ ddlZddlZddl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mZmZ ddlmZ  ed	      Z e       Z e
j*                  d
d      Zej/                  d      ddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zej/                  d      dd	 	 	 	 	 dd       Zy)z0CIMD (Client ID Metadata Document) CLI commands.    )annotationsN)Path)	Annotated)Console)CIMDFetcherCIMDFetchErrorCIMDValidationError)
get_loggerzcli.cimdcimdzFCIMD (Client ID Metadata Document) utilities for OAuth authentication.)namehelpcreate)r   T)	client_id
client_urilogo_uriscopeoutputprettyc                r   |xs d| |ddgdgd}|r||d<   |r||d<   |r||d<   |rt        j                  |d	
      nt        j                  |      }	|rt        |      j                         j	                         }
|
j
                  j                  dd       t        |
d      5 }|j                  |	       |j                  d       ddd       t        j                  d|        |st        j                  d       yyt        |	       |st        d      }|j                  d       yy# 1 sw Y   fxY w)a  Generate a CIMD document for hosting.

    Create a Client ID Metadata Document that you can host at an HTTPS URL.
    The URL where you host this document becomes your client_id.

    Example:
        fastmcp cimd create --name "My App" -r "http://localhost:*/callback"

    After creating the document, host it at an HTTPS URL with a non-root path,
    for example: https://myapp.example.com/oauth/client.json
    z+https://YOUR-DOMAIN.com/path/to/client.jsonnoneauthorization_codecode)r   client_nameredirect_uristoken_endpoint_auth_methodgrant_typesresponse_typesr   r   r      )indentT)parentsexist_okw
Nu,   [green]✓[/green] CIMD document written to z
[yellow]Important:[/yellow] client_id is a placeholder. Update it to the URL where you will host this document, or re-run with --client-id.)stderr)jsondumpsr   
expanduserresolveparentmkdiropenwriteconsoleprintr   )r   redirect_urir   r   r   r   r   r   docjson_outputoutput_pathfstderr_consoles                a/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/cli/cimd.pycreate_commandr6      s7   J O"O%&,,-!(C &L"JG 06$**S+4::c?K 6l--/779   =+s#qGGK GGDM $ 	DVHMNMM ` 
 	k$D1N  /  $#s   %#D--D6validateg      $@timeoutc               z     d fd}t        j                   |             }|st        j                  d       yy)a\  Validate a hosted CIMD document.

    Fetches the document from the given URL and validates:
    - URL is valid CIMD URL (HTTPS, non-root path)
    - Document is valid JSON
    - Document conforms to CIMD schema
    - client_id in document matches the URL

    Example:
        fastmcp cimd validate https://myapp.example.com/oauth/client.json
    c                   K   t              } | j                        slt        j                  d        t        j                          t        j                  d       t        j                  d       t        j                  d       yt        j                  d d       	 | j	                         d {   }t        j                  d       t        j                          t        j                  d       t        j                  d|j                          t        j                  d|j                  xs d        t        j                  d|j                          |j                  r?t        j                  d       |j                  D ]  }t        j                  d|         nt        j                  d       |j                  r"t        j                  d|j                          |j                  r"t        j                  d|j                          y7 j# t
        $ r"}t        j                  d	|        Y d }~yd }~wt        $ r"}t        j                  d
|        Y d }~yd }~ww xY ww)Nr8   u!   [red]✗[/red] Invalid CIMD URL: zCIMD URLs must:u     • Use HTTPS (not HTTP)u;     • Have a non-root path (e.g., /client.json, not just /)Fu   [blue]→[/blue] Fetching z...u)   [red]✗[/red] Failed to fetch document: u!   [red]✗[/red] Validation error: u&   [green]✓[/green] Valid CIMD documentz[bold]Document details:[/bold]z  client_id: z  client_name: z	(not set)z  token_endpoint_auth_method: z  redirect_uris:u       • z  redirect_uris: (none)z	  scope: z  client_uri: T)r   is_cimd_client_idr-   r.   fetchr   r	   r   r   r   r   r   r   )fetcherr0   eurir9   urls       r5   	_validatez#validate_command.<locals>._validate   s    g. ((-MM=cUCDMMOMM+,MM67MMWX23%s;<	c**C 	>?67cmm_56(F;'GHI6s7U7U6VWXMM,-((./ ) MM3499MMIcii[12>>MMN3>>*:;<; + 	MMEaSIJ" 	MM=aSAB	sU   B#I?'H( ;H%<H(  E%I?%H( (	I<1I	I?I<I72I?7I<<I?   N)returnbool)asynciorunsysexit)rA   r9   rB   successs   ``  r5   validate_commandrK      s.    6,\ kk)+&G     )r   zXAnnotated[str, cyclopts.Parameter(help='Human-readable name of the client application')]r/   z|Annotated[list[str], cyclopts.Parameter(name=['--redirect-uri', '-r'], help='Allowed redirect URIs (can specify multiple)')]r   zAnnotated[str | None, cyclopts.Parameter(name='--client-id', help='The URL where this document will be hosted (sets client_id directly)')]r   zdAnnotated[str | None, cyclopts.Parameter(name='--client-uri', help="URL of the client's home page")]r   zcAnnotated[str | None, cyclopts.Parameter(name='--logo-uri', help="URL of the client's logo image")]r   zwAnnotated[str | None, cyclopts.Parameter(name='--scope', help='Space-separated list of scopes the client may request')]r   zmAnnotated[str | None, cyclopts.Parameter(name=['--output', '-o'], help='Output file path (default: stdout)')]r   zDAnnotated[bool, cyclopts.Parameter(help='Pretty-print JSON output')]rD   None)rA   zOAnnotated[str, cyclopts.Parameter(help='URL of the CIMD document to validate')]r9   zfAnnotated[float, cyclopts.Parameter(name=['--timeout', '-t'], help='HTTP request timeout in seconds')]rD   rM   )__doc__
__future__r   rF   r%   rH   pathlibr   typingr   cycloptsrich.consoler   fastmcp.server.auth.cimdr   r   r	   fastmcp.utilities.loggingr
   loggerr-   Appcimd_appcommandr6   rK    rL   r5   <module>r[      sQ   6 "   
      
 1	J	
) 8<<		Q 
x ( 	 	 	 	 	 	kllll()l67lDElRSl`all 
ml !l^ 
z" 	J
JJ 
J #JrL   