Spaces:
Sleeping
Sleeping
Commit ·
2f83d3e
1
Parent(s): 68e5370
Wire app.py/secrets/requirements to the new cookie-based token store
Browse filesCompletes the previous commit -- these didn't get staged there because
git add aborted when one of the paths in that batch (the deleted
token_store.py) no longer existed. app.py now imports
cookie_token_store instead of token_store; secrets.toml/bootstrap_secrets.py
use a [cookies] section instead of [supabase]; requirements.txt swaps
psycopg2-binary for extra-streamlit-components.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Files changed (4)
- README.md+12-12
- app.py+6-6
- requirements.txt+1-1
- src/bootstrap_secrets.py+2-3
42 unmodified lines434445464748495051525319 unmodified lines73747576777879808182838485862 unmodified lines8990919242 unmodified lineslogged in, a separate one-time **Connect Google Drive** button appears in thesidebar; clicking it runs its own OAuth exchange (`access_type=offline` +`prompt=consent`) to force Google to hand back a `refresh_token`, which getsencrypted and stored in a Supabase Postgres table keyed by email. From thenon -- even after closing the browser, or logging in from a different device-- the app silently mints a fresh access token from that stored refresh_tokenon every login; the Connect Drive button only reappears if that stored tokenis ever revoked (e.g. you remove the app's access in your Google account).### One-time Google Cloud setup1. Create an OAuth 2.0 **Web application** Client ID; add your app's URL(s)19 unmodified linesclient_secret = "<oauth client secret>"server_metadata_url = "https://accounts.google.com/.well-known/openid-configuration"[supabase]db_url = "postgresql://..." # Supabase connection stringencryption_key = "<Fernet.generate_key() output>"```The `[supabase]` table (`user_refresh_tokens`) is created automatically onfirst use -- no manual migration needed. Rotate the DB password in Supabase'sdashboard if it's ever been pasted somewhere outside a secrets file (e.g.chat, a ticket, a terminal history you don't control).### Hugging Face Space deploy`st.login()` only reads `st.secrets`, so `src/bootstrap_secrets.py` assembles2 unmodified linesimage: `AUTH_CLIENT_ID`, `AUTH_CLIENT_SECRET`, `AUTH_COOKIE_SECRET`,`AUTH_REDIRECT_URI` (your Space's public URL **with `/oauth2callback`appended**, e.g. `https://randomcatlover-boardgames-tracker.hf.space/oauth2callback`),`SUPABASE_DB_URL`, `SUPABASE_ENCRYPTION_KEY`.42 unmodified lines4344454647484950515253545556575819 unmodified lines7879808182838485862 unmodified lines8990919242 unmodified lineslogged in, a separate one-time **Connect Google Drive** button appears in thesidebar; clicking it runs its own OAuth exchange (`access_type=offline` +`prompt=consent`) to force Google to hand back a `refresh_token`, which getsencrypted and stored in a long-lived browser cookie (via`extra-streamlit-components`' `CookieManager`, since Streamlit page code hasno direct access to set real HttpOnly cookies). From then on, on that samebrowser, the app silently mints a fresh access token from the storedrefresh_token -- no repeat consent screen -- until it's revoked (e.g. youremove the app's access in your Google account) or the browser's cookies arecleared. This is per-browser/device, not per-account centrally: a new deviceor browser needs to click Connect Drive once, same as the very first time.The cookie only ever holds the encrypted blob; the decryption key livespurely in server-side secrets and never reaches the browser.### One-time Google Cloud setup1. Create an OAuth 2.0 **Web application** Client ID; add your app's URL(s)19 unmodified linesclient_secret = "<oauth client secret>"server_metadata_url = "https://accounts.google.com/.well-known/openid-configuration"[cookies]encryption_key = "<Fernet.generate_key() output>"```### Hugging Face Space deploy`st.login()` only reads `st.secrets`, so `src/bootstrap_secrets.py` assembles2 unmodified linesimage: `AUTH_CLIENT_ID`, `AUTH_CLIENT_SECRET`, `AUTH_COOKIE_SECRET`,`AUTH_REDIRECT_URI` (your Space's public URL **with `/oauth2callback`appended**, e.g. `https://randomcatlover-boardgames-tracker.hf.space/oauth2callback`),`COOKIE_ENCRYPTION_KEY`.
10 unmodified lines111213141516171845 unmodified lines64656667686970717 unmodified lines79808182838485864 unmodified lines9192939495969710 unmodified linesimport streamlit as stimport google_oauthimport token_storefrom analytics import inject_umamist.set_page_config(page_title="Board Game Tracker", layout="wide")45 unmodified linesreturnrefresh_token = tokens.get("refresh_token")if refresh_token and "supabase" in st.secrets:token_store.save_refresh_token(st.user.email, refresh_token)_store_access_token(tokens)st.query_params.clear()st.rerun()7 unmodified linesif not st.user.is_logged_in or _access_token_valid():returnif "supabase" in st.secrets:refresh_token = token_store.get_refresh_token(st.user.email)if refresh_token:try:tokens = google_oauth.refresh_access_token(4 unmodified lines_store_access_token(tokens)returnexcept Exception:token_store.delete_refresh_token(st.user.email)state = uuid.uuid4().hexst.session_state["drive_oauth_state"] = state10 unmodified lines111213141516171845 unmodified lines64656667686970717 unmodified lines79808182838485864 unmodified lines9192939495969710 unmodified linesimport streamlit as stimport cookie_token_storeimport google_oauthfrom analytics import inject_umamist.set_page_config(page_title="Board Game Tracker", layout="wide")45 unmodified linesreturnrefresh_token = tokens.get("refresh_token")if refresh_token and "cookies" in st.secrets:cookie_token_store.save_refresh_token(st.user.email, refresh_token)_store_access_token(tokens)st.query_params.clear()st.rerun()7 unmodified linesif not st.user.is_logged_in or _access_token_valid():returnif "cookies" in st.secrets:refresh_token = cookie_token_store.get_refresh_token(st.user.email)if refresh_token:try:tokens = google_oauth.refresh_access_token(4 unmodified lines_store_access_token(tokens)returnexcept Exception:cookie_token_store.delete_refresh_token(st.user.email)state = uuid.uuid4().hexst.session_state["drive_oauth_state"] = state
5 unmodified lines678910115 unmodified linesAuthlibhttpxtomli_wpsycopg2-binarycryptographyrequests5 unmodified lines678910115 unmodified linesAuthlibhttpxtomli_wcryptographyrequestsextra-streamlit-components
18 unmodified lines19202122232425262718 unmodified lines"client_secret": os.environ["AUTH_CLIENT_SECRET"],"server_metadata_url": "https://accounts.google.com/.well-known/openid-configuration",},"supabase": {"db_url": os.environ["SUPABASE_DB_URL"],"encryption_key": os.environ["SUPABASE_ENCRYPTION_KEY"],},}_SECRETS_PATH.write_text(tomli_w.dumps(data))18 unmodified lines192021222324252618 unmodified lines"client_secret": os.environ["AUTH_CLIENT_SECRET"],"server_metadata_url": "https://accounts.google.com/.well-known/openid-configuration",},"cookies": {"encryption_key": os.environ["COOKIE_ENCRYPTION_KEY"],},}_SECRETS_PATH.write_text(tomli_w.dumps(data))
README.md+12-12
app.py+6-6
requirements.txt+1-1
src/bootstrap_secrets.py+2-3