mirror of
https://github.com/gradio-app/gradio.git
synced 2025-04-06 12:30:29 +08:00
[HF OAuth] Logout user if oauth token has expired (#8093)
* Logout user if oauth token has expired * add changeset * add changeset * mark flaky test as flaky * fixes --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
parent
b50a67defe
commit
ac30e07f92
5
.changeset/purple-radios-marry.md
Normal file
5
.changeset/purple-radios-marry.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"gradio": patch
|
||||
---
|
||||
|
||||
feat:[HF OAuth] Logout user if oauth token has expired
|
@ -1,7 +1,9 @@
|
||||
"""Predefined button to sign in with Hugging Face in a Gradio Space."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import time
|
||||
import warnings
|
||||
from typing import Literal
|
||||
|
||||
@ -87,12 +89,21 @@ class LoginButton(Button):
|
||||
session = getattr(request, "session", None) or getattr(
|
||||
request.request, "session", None
|
||||
)
|
||||
|
||||
if session is None or "oauth_info" not in session:
|
||||
return LoginButton(value=self.value, interactive=True) # type: ignore
|
||||
else:
|
||||
username = session["oauth_info"]["userinfo"]["preferred_username"]
|
||||
logout_text = self.logout_value.format(username)
|
||||
return LoginButton(logout_text, interactive=True)
|
||||
# Cookie set but user not logged in
|
||||
return LoginButton(self.value, interactive=True)
|
||||
|
||||
oauth_info = session["oauth_info"]
|
||||
expires_at = oauth_info.get("expires_at")
|
||||
if expires_at is not None and expires_at < time.time():
|
||||
# User is logged in but token has expired => logout
|
||||
session.pop("oauth_info", None)
|
||||
return LoginButton(self.value, interactive=True)
|
||||
|
||||
# User is correctly logged in
|
||||
username = oauth_info["userinfo"]["preferred_username"]
|
||||
return LoginButton(self.logout_value.format(username), interactive=True)
|
||||
|
||||
|
||||
# JS code to redirects to /login/huggingface if user is not logged in.
|
||||
|
@ -347,7 +347,7 @@ def move_files_to_cache(
|
||||
postprocess: bool = False,
|
||||
check_in_upload_folder=False,
|
||||
keep_in_cache=False,
|
||||
) -> dict:
|
||||
):
|
||||
"""Move any files in `data` to cache and (optionally), adds URL prefixes (/file=...) needed to access the cached file.
|
||||
Also handles the case where the file is on an external Gradio app (/proxy=...).
|
||||
|
||||
|
@ -67,6 +67,7 @@ def test_interface_in_blocks():
|
||||
demo.close()
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
def test_transformers_load_from_pipeline():
|
||||
from transformers import pipeline
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user