mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
db: enroll() accepts github profile; no longer returns edit_token
This commit is contained in:
+24
-22
@@ -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:
|
||||
|
||||
+14
-13
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user