Repository navigation
Fix 500 from the Keycloak auth manager when a browser session has no Keycloak tokens - #76353
Merged
dabla merged 5 commits intoNov 17, 2026
Merged
Conversation
…turning None Since Airflow 3.3 the browser JWT no longer carries the Keycloak tokens, and get_user_from_token returned None when they were not supplied from the cookies either. The core get_user dependency passed that None on to the is_authorized_* checks, which failed with "'NoneType' object has no attribute 'access_token'" and answered every request with a 500. That happens as soon as the Keycloak refresh token of a browser session has expired or its access token cookie is missing, and the user stays stuck because the middleware never gets to clear the session cookie. Raise InvalidTokenError instead, as BaseAuthManager.get_user_from_token promises. The API then answers 403 "Invalid JWT token", which sends the UI to the login page. closes: apache#76352
…oken Raising InvalidTokenError from get_user_from_token broke every browser session: the core JWTRefreshMiddleware resolves the Airflow JWT on every request without the Keycloak cookies, and treated the exception as an invalidated session, clearing the cookie after the first request. The None it returned before is what lets that middleware pass the request on, so it stays, now with a comment saying why. The 500 comes from the other caller. When KeycloakJWTMiddleware cannot attach the user (the Keycloak refresh token expired, or the access token cookie is missing), it already ends the session by clearing the Airflow JWT on the response, but the route still received the JWT and get_user resolved it to None for the authorization checks. The middleware now also removes the JWT from the request it passes on, so get_user answers 401 and the UI goes to the login page, while routes that need no user keep working. The new tests run the middlewares as the API server installs them in front of a route that needs the user: a session with its Keycloak tokens keeps its cookie, and an expired or missing Keycloak session answers 401 and clears it.
stephen-bracken
approved these changes
Nov 6, 2026
stephen-bracken
left a comment
Contributor
There was a problem hiding this comment.
LGTM. I will include this with #70707
Contributor
Author
Thx for the review @stephen-bracken. |
stegololz
reviewed
Nov 15, 2026
Once KeycloakJWTMiddleware hid the Airflow JWT from the request, the core logout route found no token to revoke: a user whose Keycloak refresh token had expired and who then logged out kept an Airflow JWT that stayed valid until it expired. The middleware now revokes the JWT itself before hiding it, on every path where it ends the session. A JWT that was rejected itself (expired, invalid or already revoked) is not revoked again: there is nothing to revoke, and the validator would log a warning with a traceback on each such request. To tell the cases apart, _refresh_user chains its 401 and 403 from the JWT error. revoke_token validates the token and writes to the database, so it runs in the thread pool, and it is looked up on the auth manager because it only exists from Airflow 3.2. A rejected JWT now answers 401 instead of 403, as the route no longer sees a credential; a test covers it. The tests of an expired Keycloak session and of a session without Keycloak cookies assert the revocation, and fail without this change.
vincbeck
reviewed
Nov 16, 2026
Revoking the Airflow JWT in KeycloakJWTMiddleware whenever it ended a session logged users out on realms that rotate refresh tokens. Two requests arrive with the same expired access token; the first refreshes and rotates the refresh token, the second sends the old one, fails the same way as on an expired refresh token and revoked the JWT both share. The first then found its JWT revoked and cleared the session instead of setting the refreshed cookies. The middleware no longer revokes anything. On the core logout route it leaves the JWT in the request, so the route revokes it itself; that route needs no user, so the None user the JWT was hidden for cannot occur there. On every other route the JWT is still hidden, without revocation. The tests of an ended session are one parametrized test that also asserts nothing is revoked, and a new one sends the same sessions through the core logout route and asserts the revocation.
vincbeck
approved these changes
Nov 16, 2026
Open
1 task done
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since Airflow 3.3 the browser JWT no longer carries the Keycloak tokens:
KeycloakJWTMiddlewarereads them from their own cookies. When it cannot complete the session (the Keycloak refresh token expired, or the access token cookie is missing), it clears the Airflow JWT on the response, but the request still reaches the route with that JWT.get_userthen falls back toresolve_user_from_token,KeycloakAuthManager.get_user_from_tokenreturnsNonefor a browser token without Keycloak tokens, and theis_authorized_*checks fail onuser.access_token: every UI request answers 500 until the user deletes the cookies.KeycloakJWTMiddlewarenow also removes the Airflow JWT from the request it passes on once it has ended the session.get_userthen answers 401 "Not authenticated", which sends the UI to the login page, and routes that need no user (the login route among them) keep working.The core logout route is the exception: it needs no user and revokes the JWT it receives, so the middleware leaves the JWT in the request there. The middleware itself revokes nothing: a parallel request that fails on a rotated-out refresh token would otherwise revoke the JWT of the request that just refreshed the session. A rejected JWT (expired, invalid or revoked) now answers 401 "Not authenticated" instead of 403 "Invalid JWT token", since the route no longer sees a credential.
get_user_from_tokenkeeps returningNonein that case: the coreJWTRefreshMiddlewareresolves the Airflow JWT on every request without the Keycloak cookies, and an exception there would end every browser session. A comment now says so.Tests: a new class runs the middlewares as
init_middlewaresinstalls them, in front of a route that needs the user. A session with its Keycloak tokens keeps its cookie (fails with the first version of this PR, which raised instead of returningNone); an expired Keycloak refresh token and a missing access token cookie answer 401 and clear the cookie (both fail onmain, where the route receivesNoneas user) without revoking the JWT; the same two sessions sent to the core logout route do revoke it; a rejected JWT answers 401 and clears the cookie.The
Nonepath came with #70550, which moved the Keycloak tokens into their own cookies in Airflow 3.3. #70707 reworks the middleware for Airflow 3.4.1+; this change is independent of it and covers 3.3 to 3.4.0.@stegololz
closes: #76352
related: #70550, #70707
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Opus 5.5) following the guidelines
🤖 Generated with Claude Code