Skip to content

fix(email): change and verify email without a Keycloak session refresh - #1536

Draft
neubig wants to merge 2 commits into
mainfrom
fix/1532-email-change-v2-session
Draft

neubig wants to merge 2 commits into
mainfrom
fix/1532-email-change-v2-session

Conversation

@neubig

@neubig neubig commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

This PR was created by an AI agent (OpenHands) on behalf of @neubig.

HUMAN:

  • A human has tested these changes.

AGENT:


Why

For an OAuth v2 cookie session, POST /api/email changed the address in Keycloak and in the users row. It then called SaasUserAuth.refresh(), which decodes the Keycloak refresh token. A v2 session has no Keycloak refresh token, so the call raised jwt.exceptions.DecodeError and the route returned 500 "An error occurred while updating the email". At that point the address had already changed, but no verification email had been sent. GET /api/email/verified had the same refresh() call and also crashed.

Summary

server/routes/email.py:

  • update_email sends the verification email right after the two address writes, before any session work.
  • OAuth v2 sessions skip the Keycloak refresh and cookie re-issue. The v2 cookie has no email in it; the email comes from the users row.
  • Legacy Keycloak sessions still refresh and re-set the cookie. If that fails, the route logs a warning and returns 200 {"message": "Email changed", "reauth_required": true} instead of a 500 for a change that already happened.
  • verified_email marks the address verified only if Keycloak reports emailVerified. Then it skips the refresh for v2, or does a best-effort refresh for legacy. It always redirects to /settings/user. The check is needed because the route is a plain GET. Before this change, any signed-in session could call it to mark its own address verified (v2 sessions only avoided this because they crashed). Keycloak sets emailVerified when the user completes the verify-email action, before it redirects here.
  • The shared cookie code is moved into _set_keycloak_cookie.

Issue Number

Fixes #1532

How to Test

uv sync --all-groups
uv run pytest tests/unit/server/routes/test_email_routes_session_refresh.py -v

The new tests call the real routes through httpx.ASGITransport, with a real SaasUserAuth built from a signed openhands_auth cookie and the real UserStore on the PostgreSQL test database. Only the Keycloak admin client is an in-memory fake (the external boundary).

  • On main: 4 failed (500 / DecodeError: Not enough segments).
  • On this branch: 4 passed.

The existing test_email_routes.py tests were updated for the Keycloak check (15 passed). test_service_account_guard.py, test_auth_middleware.py and test_auth_routes.py also pass: 137 passed.

Video/Screenshots

BEFORE (main 0a0b475 with only the new test file added):

Before: email change and verified callback fail on main

AFTER (this branch):

After: all four tests pass on the fix branch

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • .pr/1532/ holds only the two screenshots. It is removed automatically when the PR is approved.
  • The frontend does not read reauth_required yet. It shows the normal success toast, which is now correct: the address changed and the verification email was sent. A follow-up can use the flag to prompt a fresh sign-in.
  • SaasUserAuth.refresh() itself still assumes a Keycloak session. No other route calls it for v2 sessions.

Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-3c5752f

For OAuth v2 cookie sessions, POST /api/email and GET /api/email/verified
called SaasUserAuth.refresh(), which decodes the (empty) Keycloak refresh
token and raised DecodeError after the address had already changed.

- The verification email is sent right after both address writes.
- OAuth v2 sessions skip the Keycloak refresh; the cookie carries no email.
- A failed legacy refresh returns 200 with reauth_required instead of 500.
- /verified marks the address verified only when Keycloak reports it.

Fixes #1532

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions github-actions Bot added the type: fix A bug fix label Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📁 PR Artifacts Notice

This PR contains a .pr/ directory with PR-specific documents. This directory will be automatically removed when the PR is approved.

For fork PRs: Manual removal is required before merging.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  server/routes
  email.py 193-194, 202
Project Total  

This report was generated by python-coverage-comment-action

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Changing email with an OAuth v2 session fails after the address is updated (refresh() DecodeError), and no verification email is sent

2 participants