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 lines43444546474849504647484950515253545556575819 unmodified lines7879807677818283808182838485862 unmodified lines899091929242 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).encrypted 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"
[supabase]db_url = "postgresql://..." # Supabase connection string[cookies]encryption_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`.`COOKIE_ENCRYPTION_KEY`.10 unmodified lines11121314151516171845 unmodified lines646566676867686970717 unmodified lines798081828382838485864 unmodified lines919293949495969710 unmodified lines
import streamlit as st
import cookie_token_storeimport google_oauthimport token_storefrom analytics import inject_umami
st.set_page_config(page_title="Board Game Tracker", layout="wide")45 unmodified lines return
refresh_token = tokens.get("refresh_token") if refresh_token and "supabase" in st.secrets: token_store.save_refresh_token(st.user.email, 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 lines if not st.user.is_logged_in or _access_token_valid(): return
if "supabase" in st.secrets: refresh_token = token_store.get_refresh_token(st.user.email) if "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) return except Exception: token_store.delete_refresh_token(st.user.email) cookie_token_store.delete_refresh_token(st.user.email)
state = uuid.uuid4().hex st.session_state["drive_oauth_state"] = state5 unmodified lines6789910115 unmodified linesAuthlibhttpxtomli_wpsycopg2-binarycryptographyrequestsextra-streamlit-components18 unmodified lines192021222324222324252618 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"], "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