Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let JS client work with Gradio apps with auth #2266

Closed
leszekhanusz opened this issue Sep 15, 2022 · 6 comments · Fixed by #8299
Closed

Let JS client work with Gradio apps with auth #2266

leszekhanusz opened this issue Sep 15, 2022 · 6 comments · Fixed by #8299
Assignees
Labels
enhancement New feature or request gradio_client Related to the one of the gradio client libraries svelte Frontend-related issue (JS)

Comments

@leszekhanusz
Copy link

leszekhanusz commented Sep 15, 2022

I am trying to connect to a gradio backend which uses authentication (auth argument with login and password is set in the launch method of my gradio interface).

It works fine when I use my browser to login but I'm now trying to login through another page using javascript and I don't know how to proceed. The documentation about sharing your app explains how to activate this option but there is no explanation on how it works.

By looking at the network tab on Chrome, I was able to infer that you need to post formdata with username and password to the /login endpoint but if I do programmatically that I receive an html response.

Here is my javascript code right now:

const formData = new FormData();                                                                                    
formData.append("username", "admin");                                                                               
formData.append("password", "1234");

const login_response = await fetch("http://127.0.0.1:7860/login/", {
  method: "POST",
  body: formData,
});                      
                      
console.log(login_response);                                                               
                    
for (var pair of login_response.headers.entries()) { 
  console.log(pair[0]+ ': '+ pair[1]);                                                                        
}
// Only headers received are content-length and content-type (text/html)

const login_response_text = await login_response.text();
console.log(login_response_text);
// response here is an html page without any token or session hash

EDIT

By looking at the /login/ path in gradio/routes.py we have:

response = RedirectResponse(url="/", status_code=status.HTTP_302_FOUND)                                 
response.set_cookie(key="access-token", value=token, httponly=True)                                     

So accessing /login redirects to / and will set a http cookie with key access-token containing the login token.
But we can see that the httponly=True is there, which will tell the browser not to provide the cookie when using fetch, in order to follow the CORS policy.

So the question is now, is it possible to login to gradio through another origin when we use the auth parameter?

@abidlabs
Copy link
Member

Thanks for creating this issue @leszekhanusz and for providing the detailed JS code. @aliabid94 can you look at this, and is it possibly related to #1259?

@abidlabs abidlabs changed the title How does the login process works Logging in via javascript Sep 15, 2022
@abidlabs abidlabs added question svelte Frontend-related issue (JS) labels Sep 15, 2022
@abidlabs abidlabs changed the title Logging in via javascript Login via javascript Sep 15, 2022
@aliabid94 aliabid94 self-assigned this Sep 16, 2022
@aliabid94
Copy link
Collaborator

will take a look!

@leszekhanusz
Copy link
Author

In the meantime, as a workaround, I simply added an access code in the gradio inputs.

@Fondson
Copy link

Fondson commented Feb 13, 2023

I was stuck on this problem too and I found this thread https://github.com/gradio-app/gradio/discussions/444#discussioncomment-1993832. It looks like the recommended flow is to login through the UI, call /token to get the user-specific token, and use that token in all API requests moving forward.

If this is true, then I wonder if this token ever expires? And if it does, then can we programmatically get another token?

edit: I looked at the endpoints in gradio/routes.py. It seems like the token from /token can't be used as a sort of API key. Count me in as someone who's waiting for a solution to access gradio backends using authn

@chhatra-thapa
Copy link

chhatra-thapa commented Sep 22, 2023

Hello @aliabid94 & @Fondson
Is there any update regarding this issue?
I could not find any solution regarding this issue.

My gardio client javascript app has been stucked on login process too.

@abidlabs
Copy link
Member

Not at the moment sorry, we haven't had a chance to look into it. Contributions are welcome!

@abidlabs abidlabs added enhancement New feature or request and removed bug Something isn't working question labels Dec 20, 2023
@abidlabs abidlabs added the gradio_client Related to the one of the gradio client libraries label Jan 4, 2024
@abidlabs abidlabs changed the title Login via javascript Let JS client work with Gradio apps with auth Jan 4, 2024
@abidlabs abidlabs added this to the Clients 1.0 📡 milestone Feb 27, 2024
@hannahblair hannahblair self-assigned this May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request gradio_client Related to the one of the gradio client libraries svelte Frontend-related issue (JS)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants