Spaces:
Runtime error
Runtime error
russkiyximik commited on
Commit ·
5a8848f
1
Parent(s): 028d45f
changed oauth link to redirect to _self instead of _blank
Browse files- mycoder/app.py +8 -6
mycoder/app.py
CHANGED
|
@@ -23,14 +23,15 @@ OPENID_PROVIDER_URL = os.getenv("OPENID_PROVIDER_URL", "https://huggingface.co")
|
|
| 23 |
SCOPES = os.getenv("OAUTH_SCOPES", "openid profile") # spaces fills this env var
|
| 24 |
|
| 25 |
def oauth_login_url():
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
query = urlencode({
|
| 29 |
"client_id": OAUTH_CLIENT_ID,
|
| 30 |
"redirect_uri": REDIRECT_URI,
|
| 31 |
"response_type": "code",
|
| 32 |
"scope": SCOPES,
|
| 33 |
-
"state":
|
| 34 |
})
|
| 35 |
return f"{AUTH_URL}?{query}"
|
| 36 |
|
|
@@ -76,14 +77,15 @@ if code and "user" not in st.session_state:
|
|
| 76 |
user = get_userinfo(tokens["access_token"])
|
| 77 |
st.session_state["tokens"] = tokens
|
| 78 |
st.session_state["user"] = user
|
|
|
|
| 79 |
st.query_params.clear() # remove code/state from URL
|
| 80 |
st.rerun()
|
| 81 |
|
| 82 |
# UI
|
| 83 |
if "user" not in st.session_state:
|
| 84 |
login_url = oauth_login_url()
|
| 85 |
-
#
|
| 86 |
-
st.markdown(f'<a href="{login_url}" target="
|
| 87 |
st.stop()
|
| 88 |
|
| 89 |
st.success(f"Signed in as @{st.session_state['user'].get('preferred_username', 'unknown')}")
|
|
@@ -122,4 +124,4 @@ if st.button("Run Query2Plot"):
|
|
| 122 |
if os.path.exists("output/plot.png"):
|
| 123 |
st.image("output/plot.png", caption="Generated Figure")
|
| 124 |
|
| 125 |
-
|
|
|
|
| 23 |
SCOPES = os.getenv("OAUTH_SCOPES", "openid profile") # spaces fills this env var
|
| 24 |
|
| 25 |
def oauth_login_url():
|
| 26 |
+
if "oauth_state" not in st.session_state or not st.session_state["oauth_state"]:
|
| 27 |
+
st.session_state["oauth_state"] = secrets.token_urlsafe(24)
|
| 28 |
+
|
| 29 |
query = urlencode({
|
| 30 |
"client_id": OAUTH_CLIENT_ID,
|
| 31 |
"redirect_uri": REDIRECT_URI,
|
| 32 |
"response_type": "code",
|
| 33 |
"scope": SCOPES,
|
| 34 |
+
"state": st.session_state["oauth_state"],
|
| 35 |
})
|
| 36 |
return f"{AUTH_URL}?{query}"
|
| 37 |
|
|
|
|
| 77 |
user = get_userinfo(tokens["access_token"])
|
| 78 |
st.session_state["tokens"] = tokens
|
| 79 |
st.session_state["user"] = user
|
| 80 |
+
st.session_state["oauth_state"] = None
|
| 81 |
st.query_params.clear() # remove code/state from URL
|
| 82 |
st.rerun()
|
| 83 |
|
| 84 |
# UI
|
| 85 |
if "user" not in st.session_state:
|
| 86 |
login_url = oauth_login_url()
|
| 87 |
+
# Open in the same tab so Streamlit session_state survives the callback.
|
| 88 |
+
st.markdown(f'<a href="{login_url}" target="_self">Sign in with Hugging Face</a>', unsafe_allow_html=True)
|
| 89 |
st.stop()
|
| 90 |
|
| 91 |
st.success(f"Signed in as @{st.session_state['user'].get('preferred_username', 'unknown')}")
|
|
|
|
| 124 |
if os.path.exists("output/plot.png"):
|
| 125 |
st.image("output/plot.png", caption="Generated Figure")
|
| 126 |
|
| 127 |
+
|