From 22483aea199affae65db0a2e32c07d4439e269a9 Mon Sep 17 00:00:00 2001 From: shay Date: Sun, 19 Apr 2026 18:45:26 -0400 Subject: [PATCH] db: enroll() accepts github profile; no longer returns edit_token --- generator/src/db.py | 46 ++++++++++++++++++++------------------ generator/tests/test_db.py | 27 +++++++++++----------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/generator/src/db.py b/generator/src/db.py index 5ce8969..4436c59 100644 --- a/generator/src/db.py +++ b/generator/src/db.py @@ -152,44 +152,46 @@ def settings_hash(settings: dict) -> str: # ---- settings / enrollment ---- -def enroll(username: str, defaults: dict) -> dict: - """Insert a settings row + increment daily counter + enqueue build. +def enroll(username: str, defaults: dict, + github_id: int | None = None, + github_avatar_url: str | None = None) -> dict: + """Insert a settings row if missing, bump the daily counter, enqueue a build. - Returns {"job_id", "edit_token"}. The edit_token is the bearer token the - caller must present on future settings mutations for this user. We only - store its SHA-256 hash, so the plaintext here is the one and only chance - to surface it to the client. Subsequent enroll() calls for an existing - user return `edit_token=None` to avoid leaking tokens issued to earlier - registrants — settings cannot be taken over by re-enrolling. + Idempotent: calling again for an existing user refreshes github_avatar_url + (so login keeps the avatar fresh) and enqueues a rebuild. Always returns + {"job_id": int}. No token is returned — auth is handled by the signed + session cookie. """ sh = settings_hash(defaults) now = _now() - token = secrets.token_urlsafe(32) - token_hash = _hash_edit_token(token) with _settings_conn() as c: cur_ins = c.execute( """INSERT INTO users(username, settings_json, settings_hash, enrolled_at, - last_requested_at, edit_token_hash) - VALUES (?, ?, ?, ?, ?, ?) + last_requested_at, github_id, github_avatar_url) + VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(username) DO NOTHING""", - (username, json.dumps(defaults), sh, now, now, token_hash), + (username, json.dumps(defaults), sh, now, now, github_id, github_avatar_url), ) inserted = cur_ins.rowcount == 1 - c.execute( - """INSERT INTO enrollments_daily(day, count) VALUES (?, 1) - ON CONFLICT(day) DO UPDATE SET count = count + 1""", - (_today(),), - ) + if not inserted and (github_id is not None or github_avatar_url is not None): + c.execute( + "UPDATE users SET github_id=COALESCE(?, github_id), " + "github_avatar_url=COALESCE(?, github_avatar_url) WHERE username=?", + (github_id, github_avatar_url, username), + ) + if inserted: + c.execute( + """INSERT INTO enrollments_daily(day, count) VALUES (?, 1) + ON CONFLICT(day) DO UPDATE SET count = count + 1""", + (_today(),), + ) cur = c.execute( """INSERT INTO jobs(kind, username, status, created_at, updated_at) VALUES ('build', ?, 'pending', ?, ?)""", (username, now, now), ) c.commit() - return { - "job_id": cur.lastrowid, - "edit_token": token if inserted else None, - } + return {"job_id": cur.lastrowid} def enrollments_today() -> int: diff --git a/generator/tests/test_db.py b/generator/tests/test_db.py index 36ee1e7..6063899 100644 --- a/generator/tests/test_db.py +++ b/generator/tests/test_db.py @@ -19,23 +19,24 @@ def test_settings_hash_is_deterministic(): assert h1 == h2 -def test_enroll_and_get_settings(tmp_dbs): - defaults = {"theme": "dark", "widgets": ["grade"]} - result = dbmod.enroll("alice", defaults) - assert result["job_id"] > 0 - assert result["edit_token"] # first enrollment surfaces a token - s = dbmod.get_settings("alice") - assert s["settings"] == defaults - assert s["manual_refresh_used"] == 0 +def test_enroll_returns_job_id(tmp_dbs): + r = dbmod.enroll("alice", {"theme": "dark"}) + assert r["job_id"] > 0 + assert "edit_token" not in r -def test_enroll_twice_does_not_reissue_token(tmp_dbs): +def test_enroll_twice_noops(tmp_dbs): r1 = dbmod.enroll("alice", {"theme": "dark"}) r2 = dbmod.enroll("alice", {"theme": "dark"}) - assert r1["edit_token"] - assert r2["edit_token"] is None - assert dbmod.verify_edit_token("alice", r1["edit_token"]) is True - assert dbmod.verify_edit_token("alice", "wrong") is False + assert r1["job_id"] > 0 and r2["job_id"] > 0 + + +def test_enroll_with_github_profile(tmp_dbs): + dbmod.enroll("alice", {"theme": "dark"}, github_id=1, github_avatar_url="https://x/a.png") + with dbmod._settings_conn() as c: + row = c.execute("SELECT github_id, github_avatar_url FROM users WHERE username='alice'").fetchone() + assert row["github_id"] == 1 + assert row["github_avatar_url"] == "https://x/a.png" def test_enrollments_daily_counter(tmp_dbs):