o
    ex                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ ddl	m
Z
 ejdZejdZejdZejd	Zd(ddZd)ddZd)ddZG dd dejZddddddddddd d!d"d#d$d#dd%d&d'ZdS )*    )annotationsNRedirectResponse   )	get_spaceOAUTH_CLIENT_IDOAUTH_CLIENT_SECRETOAUTH_SCOPESOPENID_PROVIDER_URLappfastapi.FastAPIc              
   C  sx   zddl m} W n ty } ztd|d }~ww t d ur$t|  nt|  | j|tt	p0d
  ddd d S )Nr   )SessionMiddlewareCannot initialize OAuth to due a missing library. Please run `pip install gradio[oauth]` or add `gradio[oauth]` to your requirements.txt file in order to install the required dependencies. noneT)Z
secret_keyZ	same_siteZ
https_only)Zstarlette.middleware.sessionsr   ImportErrorr   _add_oauth_routes_add_mocked_oauth_routesZadd_middlewarehashlibsha256r   encode	hexdigest)r   r   e r   <D:\Projects\ConvertPro\env\Lib\site-packages\gradio/oauth.pyattach_oauth   s&   
	

r   returnNonec              
     s   zddl m} W n ty } ztd|d}~ww d}tdu r't|dtdu r2t|dtdu r=t|dtdu rHt|d	|   j	d
ttdtitd d | 
dd fdd}| 
dd fdd}| 
dddd}dS )zIAdd OAuth routes to the FastAPI app (login, callback handler and logout).r   )OAuthr   NzOAuth is required but {} environment variable is not set. Make sure you've enabled OAuth in your Space by setting `hf_oauth: true` in the Space metadata.r   r   r	   r
   huggingfacescopez!/.well-known/openid-configuration)nameZ	client_idZclient_secretZclient_kwargsZserver_metadata_url/login/huggingfacerequestfastapi.Requestc                   s8   t | d}d|v r|dd} j| |I dH S )z)Endpoint that redirects to HF OAuth page.oauth_redirect_callbackz	.hf.spacezhttp://zhttps://N)strZurl_forreplacer   Zauthorize_redirect)r#   Zredirect_uriZoauthr   r   oauth_loginR   s
   z&_add_oauth_routes.<locals>.oauth_login/login/callbackr   r   c                   s4    j | I dH }|d | jd< || jd< tdS ))Endpoint that handles the OAuth callback.Nuserinfooauth_profileoauth_token/)r   Zauthorize_access_tokensessionr   )r#   tokenr(   r   r   r%   [   s
   
z2_add_oauth_routes.<locals>.oauth_redirect_callback/logoutc                   &   | j dd | j dd tdS z=Endpoint that logs out the user (e.g. delete cookie session).r-   Nr.   r/   r0   popr   r#   r   r   r   oauth_logoutc      z'_add_oauth_routes.<locals>.oauth_logoutr#   r$   r#   r$   r   r   )Z%authlib.integrations.starlette_clientr   r   r   
ValueErrorformatr   r	   r
   registerget)r   r   r   msgr)   r%   r8   r   r(   r   r   /   sD   	r   c                 C  sJ   t d | dddd}| ddd
d}| dddd}dS )a  Add fake oauth routes if Gradio is run locally and OAuth is enabled.

    Clicking on a gr.LoginButton will have the same behavior as in a Space (i.e. gets redirected in a new tab) but
    instead of authenticating with HF, a mocked user profile is added to the session.
    zGradio does not support OAuth features outside of a Space environment. To help you debug your app locally, the login and logout buttons are mocked with a fake user profile.r"   r#   r$   c                   s
   t dS )z.Fake endpoint that redirects to HF OAuth page.r*   r   r7   r   r   r   r)   w   s   z-_add_mocked_oauth_routes.<locals>.oauth_loginr*   r   r   c                   s"   t d | jd< t | jd< tdS )r+   r,   r-   r.   r/   )MOCKED_OAUTH_TOKENr0   r   r7   r   r   r   r%   |   s   
z9_add_mocked_oauth_routes.<locals>.oauth_redirect_callbackr2   c                   r3   r4   r5   r7   r   r   r   r8      r9   z._add_mocked_oauth_routes.<locals>.oauth_logoutNr:   r;   )warningswarnr?   )r   r)   r%   r8   r   r   r   r   k   s   r   c                   @  s   e Zd ZdZdS )OAuthProfilea
  
    A Gradio OAuthProfile object that can be used to inject the profile of a user in a
    function. If a function expects `OAuthProfile` or `Optional[OAuthProfile]` as input,
    the value will be injected from the FastAPI session if the user is logged in. If the
    user is not logged in and the function expects `OAuthProfile`, an error will be
    raised.

    Example:
        import gradio as gr
        from typing import Optional


        def hello(profile: Optional[gr.OAuthProfile]) -> str:
            if profile is None:
                return "I don't know you."
            return f"Hello {profile.name}"


        with gr.Blocks() as demo:
            gr.LoginButton()
            gr.LogoutButton()
            gr.Markdown().attach_load_event(hello, None)
    N)__name__
__module____qualname____doc__r   r   r   r   rD      s    rD   Z#hf_oauth_AAAAAAAAAAAAAAAAAAAAAAAAAAZbeareri  ZAAAAAAAAAAAAAAAAAAAAAAAAAAzopenid profileidZ11111111111111111111111zFake Gradio UserZFakeGradioUserz%https://huggingface.co/FakeGradioUserzAhttps://huggingface.co/front/assets/huggingface_logo-noborder.svgr   z$00000000-0000-0000-0000-000000000000idZaaaaaaaaaaaaaaaaaaazhttps://huggingface.co)subr!   Zpreferred_usernameZprofileZpictureZwebsiteZaudZ	auth_timenonceZiatexpZiss)Zaccess_token
token_typeZ
expires_inZid_tokenr    Z
expires_atr,   )r   r   )r   r   r   r   )
__future__r   r   ostypingrB   ZfastapiZfastapi.responsesr   utilsr   environr?   r   r   r	   r
   r   r   r   DictrD   rA   r   r   r   r   <module>   sH    


< 
