o
    ;i|                     @   s8  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 ddlmZ ddl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 ddlmZ ddlmZ G dd dZeeZdddddddee  de!de!dee  dee  f
ddZ"dddddde de dee  de!de!dee  fddZ#dS )    N)AsyncGenerator)Optional)asynccontextmanager)api_pb2   )synchronize_api)open_url)run_temporary_http_server)_Client)DEFAULT_SERVER_URL_lookup_workspace_store_user_configconfigconfig_profilesuser_config_path)	AuthError)OutputManagerc                   @   sr   e Zd ZdefddZe	ddee dee dee	eeef df fdd	Z
	ddededeej fddZdS )
_TokenFlowclientc                 C   s   |j | _ d S N)stub)selfr    r   D/home/ubuntu/.local/lib/python3.10/site-packages/modal/token_flow.py__init__   s   z_TokenFlow.__init__N
utm_sourcenext_urlreturnc              	     s    fdd}t j }|t jd|g t|4 I dH 5}tj||t|	dd d} j
|I dH }|j _|j _|j|j|jfV  W d  I dH  dS 1 I dH sZw   Y  dS )mdmd:hiddenc                    s   ddi}t jj j|dS )NzAccess-Control-Allow-Origin*)textheaders)aiohttpwebResponsetoken_flow_id)requestr!   r   r   r   slash"   s   z_TokenFlow.start.<locals>.slash/N:)r   r   localhost_port)r"   r#   Application
add_routesgetr	   r   TokenFlowCreateRequestintsplitr   TokenFlowCreater%   wait_secretweb_urlcode)r   r   r   r(   appurlreqrespr   r'   r   start   s   	
.z_TokenFlow.start      D@      @timeoutgrpc_extra_timeoutc                    s@   t j| j|| jd}| jj|d|| dI dH }|js|S dS )r   )r%   r>   r4   N)retryr>   )r   TokenFlowWaitRequestr%   r4   r   TokenFlowWaitr>   )r   r>   r?   r9   r:   r   r   r   finish3   s   
z_TokenFlow.finish)NN)r<   r=   )__name__
__module____qualname__r
   r   r   r   strr   tupler;   floatr   TokenFlowWaitResponserC   r   r   r   r   r      s&    r   T)profileactivateverifysourcer   rK   rL   rM   rN   r   c              
      s  t jd| d}t }d }t|4 I d H }t|}	|	||4 I d H }\}
}}|d. t|r:|	d n|	d |	d| d| d |rV|	d	| d
 W d    n1 s`w   Y  |d'}t
 D ]}|	 I d H }|d ur~ n|d|d  d qoW d    n1 sw   Y  W d   I d H  n1 I d H sw   Y  |	d |j}W d   I d H  n1 I d H sw   Y  |d usJ |jr|	d|j d t|j|j| |||dI d H  d S )N
server_urlrK   z-Waiting for authentication in the web browserzThe web browser should have opened for you to authenticate and get an API token.
If it didn't, please copy this URL into your web browser manually:
zd[red]Was not able to launch web browser[/red]
Please go to this URL manually and complete the flow:
z[link=]z[/link]
zEnter this code: [yellow]z
[/yellow]
z%Waiting for token flow to complete...z/Waiting for token flow to complete... (attempt    )z8[green]Web authentication finished successfully![/green]z*[green]Token is connected to the [magenta]z[/magenta] workspace.[/green]rK   rL   rM   rO   )r   r/   r   r
   	anonymousr   r;   statusr   print	itertoolscountrC   updaterO   workspace_username
_set_tokentoken_idtoken_secret)rK   rL   rM   rN   r   rO   outputresultr   
token_flow_r5   r6   rV   attemptr   r   r   
_new_tokenE   sP   (
(rd   rT   r]   r^   rO   c             
      s  t jd|d}t }|r'|d| d t|| |fI d H  |d |d u r\dtjv r6tjd }n&zt|| |I d H }W n t	yX } z|sRd}	t	|	||d }~ww |j
}| |d}
|d urm|tkrm||
d< |srt st|nd }|d	 t|
||d
 W d    n1 sw   Y  |dt d| d dd dD }dd |D }d|}|rt|dkrdnd}t|dkrdnd}|d| d| d| d d S d S )NrO   rP   zVerifying token against [blue]z[/blue]z+[green]Token verified successfully![/green]MODAL_PROFILEzSNo profile name given, but could not authenticate client to look up workspace name.)r]   r^   zStoring token)rK   active_profilez![green]Token written to [magenta]z[/magenta] in profile [magenta]z[/magenta].[/green]c                 S   s    g | ]}t j|r|nd qS r   )osenvironr/   .0varr   r   r   
<listcomp>   s     z_set_token.<locals>.<listcomp>)MODAL_TOKEN_IDMODAL_TOKEN_SECRETc                 S   s   g | ]}|d ur|qS r   r   ri   r   r   r   rl      s    z / r   s areisz[yellow]Warning: The z environment variable z7 set; this will override your new credentials.[/yellow])r   r/   r   rW   r
   rM   rg   rh   r   r   usernamer   r   rV   r   r   joinlen)r]   r^   rK   rL   rM   rO   r_   	workspaceexcmsgconfig_datarf   env_varsenv_vars_usedenv_vars_strro   verbr   r   r   r\   |   sT   





r\   )$rX   rg   collections.abcr   typingr   aiohttp.webr"   synchronicity.async_wrapr   modal_protor   _utils.async_utilsr   _utils.browser_utilsr   _utils.http_utilsr	   r   r
   r   r   r   r   r   r   	exceptionr   r_   r   r   	TokenFlowrG   boolrd   r\   r   r   r   r   <module>   sb    -
;