o
    ,&]i                     @  s   d dl mZ d dlmZmZmZ d dlmZmZm	Z	m
Z
mZ d dlmZ d dlmZ er<d dlmZ d dlZd dlmZ G d	d
 d
e	eeZG dd deZdS )    )annotations)TYPE_CHECKINGAnycast)BaseApp	BaseOAuthOAuth2Mixin
OAuthErrorOpenIDMixin)OAuth2Session)TornadoIntegration)CallableN)	AuthCachec                      sD   e Zd ZeZd fddZ	ddddZdddZdddZ  Z	S )TornadoOAuth2Appreturndict[str, Any]c                   s.   t dt  }d|dg v rd| jd< |S )zGWe enforce S256 code challenge method if it is supported by the server.r   S256 code_challenge_methods_supportedcode_challenge_method)r   superload_server_metadatagetclient_kwargs)selfresult	__class__ ]/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/streamlit/web/server/oidc_mixin.pyr   -   s   
z%TornadoOAuth2App.load_server_metadataNrequest_handlertornado.web.RequestHandlerredirect_urir   kwargsNonec                 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.
        r!   urli.  )statusNr   )create_authorization_url_save_authorize_dataredirect)r   r   r!   r"   auth_contextr   r   r   authorize_redirect4   s   z#TornadoOAuth2App.authorize_redirectc                 K  s   | dd}|r| dd}t||d| d| dd}i }|dd}| j||d}| j||d |sCtd	d
d| ||}| jdi ||}	d|	v rmd|v rm| j	|	|d |d}
i |	d|
i}	t
d|	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invalid_statez>OAuth state not found or expired. Please try logging in again.id_tokennonce)r3   r0   userinfor   r   )get_argumentr	   pop	frameworkget_state_datar   clear_state_data_format_state_paramsfetch_access_tokenparse_id_tokenr   )r   r   r"   r+   r-   paramssessionr0   
state_datatokenr4   r   r   r   authorize_access_tokenE   s0   

z'TornadoOAuth2App.authorize_access_tokenc                 K  s0   | dd}|ri }| j||| dS td)a\  Authlib underlying uses the concept of "session" to store state data.
        In Tornado, we don't have a session, so we use an empty dict as a placeholder.
        We also override state access to use the cache instead of session in `TornadoIntegration`.
        Authlib 1.6.6+ always writes state to session even when cache is available.
        r/   NzMissing state value)r6   r7   set_state_dataRuntimeError)r   r"   r/   r>   r   r   r   r'   n   s
   z%TornadoOAuth2App._save_authorize_data)r   r   )N)r   r    r!   r   r"   r   r   r#   )r   r    r"   r   r   r   )r"   r   r   r#   )
__name__
__module____qualname__r   
client_clsr   r*   rA   r'   __classcell__r   r   r   r   r   *   s    

)r   c                      s0   e Zd ZeZeZ				dd fd	d
Z  ZS )TornadoOAuthNconfigdict[str, Any] | NonecacheAuthCache | Nonefetch_token1Callable[[dict[str, Any]], dict[str, Any]] | Noneupdate_tokenc                   s   t  j|||d || _d S )N)rL   rN   rP   )r   __init__rJ   )r   rJ   rL   rN   rP   r   r   r   rQ      s   
zTornadoOAuth.__init__)NNNN)rJ   rK   rL   rM   rN   rO   rP   rO   )	rD   rE   rF   r   oauth2_client_clsr   framework_integration_clsrQ   rH   r   r   r   r   rI   |   s    rI   )
__future__r   typingr   r   r    authlib.integrations.base_clientr   r   r   r	   r
   $authlib.integrations.requests_clientr   0streamlit.web.server.authlib_tornado_integrationr   collections.abcr   tornado.webtornadostreamlit.auth_utilr   r   rI   r   r   r   r   <module>   s   R