o
    h                     @   sd   d dl Zd dlmZmZmZmZmZ d dlm	Z	 d dl
mZ G dd deeeZG dd deZdS )	    N)BaseApp	BaseOAuthOAuth2Mixin
OAuthErrorOpenIDMixin)OAuth2Session)TornadoIntegrationc                       sP   e Zd ZeZ fddZ	ddejjfddZ	dejjfddZ
d	d
 Z  ZS )TornadoOAuth2Appc                    s(   t   }d|dg v rd| jd< |S )zGWe enforce S256 code challenge method if it is supported by the server.S256 code_challenge_methods_supportedcode_challenge_method)superload_server_metadatagetclient_kwargs)selfresult	__class__ S/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/web/server/oidc_mixin.pyr   !   s   

z%TornadoOAuth2App.load_server_metadataNrequest_handlerc                 K   s<   | j |fi |}| jdd|i| |j|d dd dS )a(  Create a HTTP Redirect for Authorization Endpoint.

        :param request_handler: HTTP request instance from Tornado.
        :param redirect_uri: Callback or redirect URI for authorization.
        :param kwargs: Extra parameters to include.
        :return: A HTTP redirect response.
        redirect_uriurli.  )statusNr   )create_authorization_url_save_authorize_dataredirect)r   r   r   kwargsauth_contextr   r   r   authorize_redirect(   s   
z#TornadoOAuth2App.authorize_redirectc                 K   s   | dd}|r| dd}t||d| d| dd}| jjdus'J d}|dd}| j||d}| j||d | ||}| j	di ||}	d	|	v rmd
|v rm| j
|	|d
 |d}
i |	d|
i}	|	S )zl
        :param request_handler: HTTP request instance from Tornado.
        :return: A token dict.
        errorNerror_description)r!   descriptioncodestate)r$   r%   claims_optionsid_tokennonce)r(   r&   userinfor   )get_argumentr   	frameworkcachepopget_state_datar   clear_state_data_format_state_paramsfetch_access_tokenparse_id_token)r   r   r   r!   r#   paramssessionr&   
state_datatokenr)   r   r   r   authorize_access_token6   s(   
z'TornadoOAuth2App.authorize_access_tokenc                 K   s@   | dd}|r| jjdusJ d}| j||| dS td)zAuthlib underlying uses the concept of "session" to store state data.
        In Tornado, we don't have a session, so we use the framework's cache option.
        r%   NzMissing state value)r-   r+   r,   set_state_dataRuntimeError)r   r   r%   r4   r   r   r   r   W   s   z%TornadoOAuth2App._save_authorize_data)N)__name__
__module____qualname__r   
client_clsr   tornadowebRequestHandlerr    r7   r   __classcell__r   r   r   r   r	      s    

!r	   c                       s&   e Zd ZeZeZd fdd	Z  ZS )TornadoOAuthNc                    s   t  j|||d || _d S )N)r,   fetch_tokenupdate_token)r   __init__config)r   rF   r,   rC   rD   r   r   r   rE   h   s   
zTornadoOAuth.__init__)NNNN)	r:   r;   r<   r	   oauth2_client_clsr   framework_integration_clsrE   rA   r   r   r   r   rB   d   s    rB   )tornado.webr>    authlib.integrations.base_clientr   r   r   r   r   $authlib.integrations.requests_clientr   0streamlit.web.server.authlib_tornado_integrationr   r	   rB   r   r   r   r   <module>   s   F