From a6356d4d1d43e4d4bf7577b66f762ee63d326a3d Mon Sep 17 00:00:00 2001 From: shay Date: Sun, 19 Apr 2026 18:53:12 -0400 Subject: [PATCH] feat(api): add /api/auth/me and /api/auth/logout --- generator/src/api.py | 20 +++++++++++++- generator/tests/test_auth.py | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/generator/src/api.py b/generator/src/api.py index 81ee86f..e1389e5 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -22,7 +22,7 @@ from concurrent.futures import ThreadPoolExecutor from datetime import timedelta from functools import wraps from threading import BoundedSemaphore, Lock -from flask import Flask, jsonify, request, Response, send_from_directory +from flask import Flask, jsonify, request, Response, send_from_directory, session from . import auth, config, db, fetcher_client, placeholder from .themes import THEMES @@ -334,6 +334,24 @@ def spa(path: str): return jsonify({"error": "frontend not built"}), 503 +@app.route("/api/auth/me", methods=["GET"]) +def auth_me(): + login = auth.current_login() + if login is None: + return jsonify({"login": None}) + return jsonify({ + "login": login, + "avatar_url": session.get("gh_avatar_url"), + }) + + +@app.route("/api/auth/logout", methods=["POST"]) +@auth.require_same_origin +def auth_logout(): + session.clear() + return ("", 204) + + @app.route("/api/health", methods=["GET"]) def health(): return jsonify({"status": "ok", "service": "generator"}) diff --git a/generator/tests/test_auth.py b/generator/tests/test_auth.py index 1efa0bb..ac48b67 100644 --- a/generator/tests/test_auth.py +++ b/generator/tests/test_auth.py @@ -67,3 +67,54 @@ def test_require_same_origin_rejects_missing_origin(app): s["gh_login"] = "alice" r = c.post("/protected/alice") assert r.status_code == 403 + + +from src import api as api_module + + +@pytest.fixture +def api_client(monkeypatch, tmp_path): + # Point DBs at tmp + configure origins. + from src import db as dbmod, config as cfg + monkeypatch.setattr(cfg, "SETTINGS_DB_PATH", str(tmp_path / "s.db")) + monkeypatch.setattr(cfg, "WIDGETS_DB_PATH", str(tmp_path / "w.db")) + monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", str(tmp_path / "s.db")) + monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", str(tmp_path / "w.db")) + dbmod.init_dbs() + monkeypatch.setattr(cfg, "ALLOWED_ORIGINS", ("https://gh-stats.com",)) + api_module.app.config["TESTING"] = True + api_module.app.config["SESSION_COOKIE_SECURE"] = False + return api_module.app.test_client() + + +def test_me_unauthed_returns_null(api_client): + r = api_client.get("/api/auth/me") + assert r.status_code == 200 + assert r.get_json() == {"login": None} + + +def test_me_authed_returns_login(api_client): + with api_client.session_transaction() as s: + s["gh_login"] = "alice" + s["gh_avatar_url"] = "https://x/a.png" + r = api_client.get("/api/auth/me") + assert r.status_code == 200 + j = r.get_json() + assert j["login"] == "alice" + assert j["avatar_url"] == "https://x/a.png" + + +def test_logout_clears_session(api_client): + with api_client.session_transaction() as s: + s["gh_login"] = "alice" + r = api_client.post("/api/auth/logout", headers={"Origin": "https://gh-stats.com"}) + assert r.status_code == 204 + r = api_client.get("/api/auth/me") + assert r.get_json()["login"] is None + + +def test_logout_rejects_bad_origin(api_client): + with api_client.session_transaction() as s: + s["gh_login"] = "alice" + r = api_client.post("/api/auth/logout", headers={"Origin": "https://evil.example"}) + assert r.status_code == 403