[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:
Lucain 2024-04-22 17:18:04 +02:00 committed by GitHub
parent b50a67defe
commit ac30e07f92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
feat:[HF OAuth] Logout user if oauth token has expired

View File

@ -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.

View File

@ -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=...).

View File

@ -67,6 +67,7 @@ def test_interface_in_blocks():
demo.close()
@pytest.mark.flaky
def test_transformers_load_from_pipeline():
from transformers import pipeline