
    l0jx                       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Zd&dZd&dZ G 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                `   	 ddl m} n"# t          $ r}t          d          |d }~ww xY wt                      t	          |            nt          |            |                     |t          j        t          p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)
secret_key	same_site
https_only)starlette.middleware.sessionsr   ImportErrorr   _add_oauth_routes_add_mocked_oauth_routesadd_middlewarehashlibsha256r	   encode	hexdigest)r   r   es      O/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/gradio/oauth.pyattach_oauthr!      s    CCCCCCC   k
 
 	 {{# %%%
 >#6#<""D"D"F"FGGQQSS	      s   	 
(#(returnNonec                   	 ddl m} n"# t          $ r}t          d          |d}~ww xY wd}t          "t	          |                    d                    t          "t	          |                    d                    t          "t	          |                    d                    t          "t	          |                    d	                     |            	                    d
t          t          dt          it          dz              | 
                    d          dfd            }| 
                    d          df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)name	client_idclient_secretclient_kwargsserver_metadata_url/login/huggingfacerequestfastapi.Requestc                   K   t          |                     d                    }d|v r|                    dd          }j                            | |           d{V S )z)Endpoint that redirects to HF OAuth page.oauth_redirect_callbackz	.hf.spacezhttp://zhttps://N)strurl_forreplacer&   authorize_redirect)r.   redirect_urioauths     r    oauth_loginz&_add_oauth_routes.<locals>.oauth_loginR   sn       7??+DEEFF,&&'//	:FFL&99'<PPPPPPPPP    /login/callbackr"   r   c                   K   j                             |            d{V }|d         | j        d<   || j        d<   t          d          S ))Endpoint that handles the OAuth callback.Nuserinfooauth_profileoauth_token/)r&   authorize_access_tokensessionr   )r.   tokenr7   s     r    r1   z2_add_oauth_routes.<locals>.oauth_redirect_callback[   s\       '>>wGGGGGGGG+0+<().&$$$r9   /logoutc                   K   | j                             dd           | j                             dd           t          d          S z=Endpoint that logs out the user (e.g. delete cookie session).r>   Nr?   r@   rB   popr   r.   s    r    oauth_logoutz'_add_oauth_routes.<locals>.oauth_logoutc   F       	OT222M4000$$$r9   r.   r/   r.   r/   r"   r   )%authlib.integrations.starlette_clientr%   r   r   
ValueErrorformatr	   r
   r   registerget)r   r%   r   msgr8   r1   rJ   r7   s          @r    r   r   /   s   ???????   k
 
 		;  $566777"$9::;;;N33444"$9::;;; EGGE	NN!)-/2UU     	WW!""Q Q Q Q Q #"Q 	WW% % % % %  % 	WWY% % % % % %s   
 
)$)c                    t          j        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                $   K   t          d          S )z.Fake endpoint that redirects to HF OAuth page.r:   r   rI   s    r    r8   z-_add_mocked_oauth_routes.<locals>.oauth_loginw   s         1222r9   r:   r"   r   c                l   K   t           d         | j        d<   t           | j        d<   t          d          S )r<   r=   r>   r?   r@   )MOCKED_OAUTH_TOKENrB   r   rI   s    r    r1   z9_add_mocked_oauth_routes.<locals>.oauth_redirect_callback|   s3       ,>j+I();&$$$r9   rD   c                   K   | j                             dd           | j                             dd           t          d          S rF   rG   rI   s    r    rJ   z._add_mocked_oauth_routes.<locals>.oauth_logout   rK   r9   NrL   rM   )warningswarnrR   )r   r8   r1   rJ   s       r    r   r   k   s     M	p   	WW!""3 3 3 #"3 	WW% % %  % 	WWY% % % % % %r9   c                      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__ r9   r    r\   r\      s           r9   r\   #hf_oauth_AAAAAAAAAAAAAAAAAAAAAAAAAAbeareri  AAAAAAAAAAAAAAAAAAAAAAAAAAzopenid profileid11111111111111111111111zFake Gradio UserFakeGradioUserz%https://huggingface.co/FakeGradioUserzAhttps://huggingface.co/front/assets/huggingface_logo-noborder.svgr   z$00000000-0000-0000-0000-000000000000idaaaaaaaaaaaaaaaaaaazhttps://huggingface.co)subr(   preferred_usernameprofilepicturewebsiteaud	auth_timenonceiatexpiss)access_token
token_type
expires_inid_tokenr'   
expires_atr=   )r   r   )r   r   r"   r#   )
__future__r   r   ostypingrY   fastapifastapi.responsesr   utilsr   environrR   r   r	   r
   r   r!   r   r   Dictr\   rW   ra   r9   r    <module>r      sx   " " " " " "  				    . . . . . .      *..!233jnn%:;; z~~n--jnn%:;;    89% 9% 9% 9%x% % % %@    6;   6 :,(".:V5&'     r9   