From 8afecd553c55d001ff1e50acfbdd383a28548c37 Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Thu, 29 Oct 2026 15:23:51 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20Callback=20best=C3=A4tigt=20direkt=20sta?= =?UTF-8?q?tt=20Weiterleitung=20(Sitzung=20geh=C3=B6rt=20danach=20dem=20te?= =?UTF-8?q?chnischen=20Konto)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Claude-Session: https://claude.ai/code/session_01SqzF6qT3TQ8ej3wigEuMSu --- app.py | 14 +++++++++++--- index.html | 5 +++-- tests/test_oauth.py | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 7bac0e1..8671eff 100644 --- a/app.py +++ b/app.py @@ -150,14 +150,22 @@ def oidc_callback(): if not OAUTH: abort(404) if request.args.get("error"): - return redirect(url_for("index", error=request.args["error"])) + return render_template("index.html", email_address=EMAIL_USERNAME, oauth=True, connected=False, + error=request.args["error"]), 400 try: OAUTH.finish(request.args.get("state", ""), request.args.get("code", "")) except OAuthError as e: logger.warning("Mailbox connect failed: %s", e) - return redirect(url_for("index", error=str(e))) + return render_template("index.html", email_address=EMAIL_USERNAME, oauth=True, connected=False, + error=str(e)), 400 logger.info("Collection mailbox %s connected via OAuth", EMAIL_USERNAME) - return redirect(url_for("index")) + # the browser session now belongs to the technical account, which forward_auth does not let in: + # confirm here instead of redirecting to the start page + return ( + "Connected" + "

Collection mailbox connected. Close this private window and open the Unsubscriber again " + "in your normal browser.

" + ) @app.route("/disconnect", methods=["POST"]) diff --git a/index.html b/index.html index d8ba050..719dc11 100644 --- a/index.html +++ b/index.html @@ -19,8 +19,9 @@ {% endif %} {% else %} -

The collection mailbox is not connected yet. Open this page in a private window and sign in - as {{ email_address }} (technical account) – not with your own account.

+

The collection mailbox is not connected yet. In a private window: open this page, sign in with + your own account, click “Connect mailbox”, then sign in as + {{ email_address }} (technical account).

diff --git a/tests/test_oauth.py b/tests/test_oauth.py index d9c4298..d00212f 100644 --- a/tests/test_oauth.py +++ b/tests/test_oauth.py @@ -139,3 +139,30 @@ def test_index_shows_connect_when_not_connected(monkeypatch): monkeypatch.setattr(unsub, "OAUTH", FakeOAuth()) html = unsub.app.test_client().get("/").get_data(as_text=True) assert 'action="/connect"' in html and 'action="/result"' not in html + + +def test_callback_confirms_without_redirect(monkeypatch): + import app as unsub + + class FakeOAuth: + def finish(self, state, code): + assert (state, code) == ("s", "c") + + monkeypatch.setattr(unsub, "OAUTH", FakeOAuth()) + r = unsub.app.test_client().get("/oidc/callback?state=s&code=c") + assert r.status_code == 200 and "connected" in r.get_data(as_text=True) + + +def test_callback_error_is_shown(monkeypatch): + import app as unsub + + class FakeOAuth: + def finish(self, state, code): + raise oauth.OAuthError("Signed in as philipp@example.org, expected unsubscribe-bot@example.org") + + def connected(self): + return False + + monkeypatch.setattr(unsub, "OAUTH", FakeOAuth()) + r = unsub.app.test_client().get("/oidc/callback?state=s&code=c") + assert r.status_code == 400 and "expected unsubscribe-bot" in r.get_data(as_text=True)