o
    h1                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlZd dl	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ e
 ZdddZG dd dejjZG dd deejjZG dd deejjZG dd deejjZdS )    )annotationsN)Any)urlparse)	AuthCachedecode_provider_token!generate_default_provider_sectionget_secrets_auth_section)StreamlitAuthError)make_url_path)TornadoOAuthTornadoOAuth2App)AUTH_COOKIE_NAMEproviderstrreturntuple[TornadoOAuth2App, str]c                 C  s   t  }|r|dd}| }ni }d}|| i }|s(| dkr(t|}||d< |di }d|vr6d|d< d|vr>d	|d< t|td
}||  || |fS )zRCreate an OAuth client for the given provider based on secrets.toml configuration.redirect_uriN/defaultclient_kwargsscopezopenid email profilepromptselect_account)cache)	r   getto_dict
setdefaultr   r   
auth_cacheregistercreate_client)r   auth_sectionr   configprovider_sectionprovider_client_kwargsoauth r%   ]/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/web/server/oauth_authlib_routes.pycreate_oauth_client$   s$   

r'   c                   @  s8   e Zd ZdZdddZddd	ZdddZdddZdS )AuthHandlerMixinzNMixin for handling auth cookies. Added for compatibility with Tornado < 6.3.0.base_urlr   r   Nonec                 C  s
   || _ d S N)r)   )selfr)   r%   r%   r&   
initializeB   s   
zAuthHandlerMixin.initializec                 C  s   |  t| jd d S )Nr   )redirectr
   r)   r,   r%   r%   r&   redirect_to_baseE   s   z!AuthHandlerMixin.redirect_to_base	user_infodict[str, Any]c                 C  sF   t |}z| jt|dd W d S  ty"   | jt|dd Y d S w )NT)httpOnly)httponly)jsondumpsset_signed_cookier   AttributeErrorset_secure_cookie)r,   r1   serialized_cookie_valuer%   r%   r&   set_auth_cookieH   s   
z AuthHandlerMixin.set_auth_cookiec                 C  s   |  t d S r+   )clear_cookier   r/   r%   r%   r&   clear_auth_cookieZ   s   z"AuthHandlerMixin.clear_auth_cookieN)r)   r   r   r*   )r   r*   )r1   r2   r   r*   )__name__
__module____qualname____doc__r-   r0   r;   r=   r%   r%   r%   r&   r(   ?   s    


r(   c                   @  s   e Zd Zdd ZdddZdS )	AuthLoginHandlerc              
     sv   |   }|du r|   dS t|\}}z	|| | W dS  ty: } z| jdt|d W Y d}~dS d}~ww )z*Redirect to the OAuth provider login page.Ni  )reason)_parse_provider_tokenr0   r'   authorize_redirect	Exception
send_errorr   )r,   r   clientr   er%   r%   r&   r   _   s    zAuthLoginHandler.getr   
str | Nonec                 C  sD   |  dd }z|d u rtdt|}W |d S  ty!   Y d S w )Nr   zMissing provider token)get_argumentr	   r   )r,   provider_tokenpayloadr%   r%   r&   rD   l   s   
z&AuthLoginHandler._parse_provider_tokenNr   rJ   )r>   r?   r@   r   rD   r%   r%   r%   r&   rB   ^   s    rB   c                   @  s   e Zd Zdd ZdS )AuthLogoutHandlerc                 C  s   |    |   d S r+   )r=   r0   r/   r%   r%   r&   r   y   s   zAuthLogoutHandler.getN)r>   r?   r@   r   r%   r%   r%   r&   rO   x   s    rO   c                   @  s(   e Zd Zdd Zd
ddZd
ddZd	S )AuthCallbackHandlerc           	        s   |   }|  }|d u r|   d S | dd }|r!|   d S |d u r+|   d S t|\}}|| }|d}t||dd}|rI| | |   d S )NerroruserinfoT)originis_logged_in)	_get_provider_by_state_get_origin_from_secretsr0   rK   r'   authorize_access_tokenr   dictr;   )	r,   r   rS   rQ   rH   _tokenusercookie_valuer%   r%   r&   r      s(   


zAuthCallbackHandler.getr   rJ   c           	      C  sR   |  d}tt  }i }|D ]}|d\}}}}|||< q||d }|S )NstaterY   )rK   listr   get_dictkeyssplitr   )	r,   state_code_from_urlcurrent_cache_keysstate_provider_mappingkeyrY   recorded_providercoder   r%   r%   r&   rU      s   

z*AuthCallbackHandler._get_provider_by_statec                 C  s>   d }t  }|r|dd }|sd S t|}|jd |j }|S )Nr   z://)r   r   r   schemenetloc)r,   r   r    redirect_uri_parsedorigin_from_redirect_urir%   r%   r&   rV      s   z,AuthCallbackHandler._get_origin_from_secretsNrN   )r>   r?   r@   r   rU   rV   r%   r%   r%   r&   rP   ~   s    
rP   )r   r   r   r   )
__future__r   r5   typingr   urllib.parser   tornado.webtornadostreamlit.auth_utilr   r   r   r   streamlit.errorsr	   streamlit.url_utilr
   streamlit.web.server.oidc_mixinr   r    streamlit.web.server.server_utilr   r   r'   webRequestHandlerr(   rB   rO   rP   r%   r%   r%   r&   <module>   s    
