a
    dx                     @  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dZd
ddddZd
d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_URLzfastapi.FastAPI)appc              
   C  s   zddl m} W n. ty> } ztd|W Y d }~n
d }~0 0 t d urTt|  nt|  | j|tt	pld
  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   U/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio/oauth.pyattach_oauth   s"    

r   None)r   returnc              
     s  zddl m} W n. ty> } ztd|W Y d}~n
d}~0 0 d}tdu rZt|dtdu rpt|dtdu rt|dtdu rt|d	|   j	d
ttdtitd d | 
ddd fdd}| 
dddd fdd}| 
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fastapi.Requestrequestc                   s6   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$   r   c                   s2    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                   s$   | j dd | j dd tdS z=Endpoint that logs out the user (e.g. delete cookie session).r.   Nr/   r0   r1   popr   r#   r   r   r   oauth_logoutc   s    z'_add_oauth_routes.<locals>.oauth_logout)Z%authlib.integrations.starlette_clientr   r   r   
ValueErrorformatr   r	   r
   registerget)r   r   r   msgr)   r%   r7   r   r(   r   r   /   s@    	r   c                 C  sZ   t d | dddd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   r#   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/   r0   )MOCKED_OAUTH_TOKENr1   r   r#   r   r   r   r%   |   s    
z9_add_mocked_oauth_routes.<locals>.oauth_redirect_callbackr3   c                   s$   | j dd | j dd tdS r4   r5   r#   r   r   r   r7      s    z._add_mocked_oauth_routes.<locals>.oauth_logoutN)warningswarnr;   )r   r)   r%   r7   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   r@      s   r@   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-   )
__future__r   r   ostypingr>   ZfastapiZfastapi.responsesr   utilsr   environr;   r   r   r	   r
   r   r   r   Dictr@   r=   r   r   r   r   <module>   sF   < 