
    g                     p    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eee      Z G d de      Zy)	    N)BaseApp	BaseOAuthOAuth2Mixin
OAuthErrorOpenIDMixin)OAuth2Session)TornadoIntegrationc                        e Zd ZeZ fdZ	 ddej                  j                  fdZ	dej                  j                  fdZ
d Z xZS )TornadoOAuth2Appc                 j    t         |          }d|j                  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     T/var/www/openai/venv/lib/python3.12/site-packages/streamlit/web/server/oidc_mixin.pyr   z%TornadoOAuth2App.load_server_metadata!   s:    -/VZZ BBGG:@D67    request_handlerc                 ~     | j                   |fi |} | j                  dd|i| |j                  |d   d       y)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.  )statusN )create_authorization_url_save_authorize_dataredirect)r   r   r   kwargsauth_contexts        r   authorize_redirectz#TornadoOAuth2App.authorize_redirect(   sK     5t44\LVL!!!L|L|L  e!4S Ar   c                 P   |j                  dd      }|r|j                  dd      }t        ||      |j                  d      |j                  d      d}| j                  j                  J d}|j	                  dd      }| j                  j                  ||j                  d            }| j                  j                  ||j                  d             | j                  ||      } | j                  di ||}	d	|	v r#d
|v r| j                  |	|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.   s              r   authorize_access_tokenz'TornadoOAuth2App.authorize_access_token6   s<     ,,Wd;)667JDQK5kBB $008$11':

 ~~##///$4d;^^227FJJw<OP
''G1DE**:v>''';&;F;7j#8**Z0 + H 4u3j(3Er   c                     |j                  dd      }|r8| j                  j                  J d}| j                  j                  |||       y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)r2   r0   r1   set_state_dataRuntimeError)r   r"   r*   r9   s       r   r    z%TornadoOAuth2App._save_authorize_dataW   sT     

7D)>>''333GNN))'5&A455r   )N)__name__
__module____qualname__r   
client_clsr   tornadowebRequestHandlerr$   r<   r    __classcell__r   s   @r   r   r      sJ    J IMB&{{99B&{{99B
6r   r   c                   (     e Zd ZeZeZd fd	Z xZS )TornadoOAuthc                 8    t         |   |||       || _        y )N)r1   fetch_tokenupdate_token)r   __init__config)r   rO   r1   rL   rM   r   s        r   rN   zTornadoOAuth.__init__h   s%    [| 	 	
 r   )NNNN)	r@   rA   rB   r   oauth2_client_clsr	   framework_integration_clsrN   rG   rH   s   @r   rJ   rJ   d   s    ( 2 r   rJ   )tornado.webrD    authlib.integrations.base_clientr   r   r   r   r   $authlib.integrations.requests_clientr   0streamlit.web.server.authlib_tornado_integrationr	   r   rJ   r   r   r   <module>rV      s>      PC6{K C6L9 r   