Repository navigation
Description
Before reporting an issue
- I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.
Area
authentication
Bug Description
In 26.8.2, AuthenticationProcessor.attachSession refuses an SSO authentication with DIFFERENT_USER_AUTHENTICATED when no user session exists for the browser's root authentication session id. In 26.8.1 a user session was created in that case.
When a user confirms an email change (UPDATE_EMAIL with verify email, via the verify-new-email action token) in the same browser, that browser gets a new AUTH_SESSION_ID. Its KEYCLOAK_IDENTITY still points at the original user session, whose id does not change.
From then on, every application-initiated action finished in that browser (kc_action=UPDATE_PASSWORD, kc_action=CONFIGURE_TOTP, …) ends on the error page "You are already authenticated as different user '' in this session. Please sign out first." The user named is the same user. This lasts until the user logs out. Plain SSO logins (no kc_action) keep working. If the confirmation link is opened in a different browser, the problem does not occur.
We traced it to 666eede ("Make sure SSO_AUTH checks map the user authenticating", #54950, part of #54688). That commit is in the 26.8.1...26.8.2 compare and is still on main and release/26.8. We found no existing issue for this; the nearest are #54688 and #54344.
Version
26.8.2
Regression
- The issue is a regression
Expected behavior
The authenticated user and the session's user are the same user, so the required action completes and the browser returns to the client, as in 26.8.1. Alternatively, consuming the action token keeps the browser's auth session consistent with its user session.
Actual behavior
The required action ends on "You are already authenticated as different user '' in this session. Please sign out first.", with event LOGIN_ERROR error="different_user_authenticated". It happens on every later kc_action in that browser until logout.
How to Reproduce?
With stock quay.io/keycloak/keycloak:26.8.2, start-dev:
- Create a realm with verify email on and the
UPDATE_EMAILrequired action enabled, a public client with a redirect URI, and one user with a verified email and a password. - In browser A, log in as that user through the client.
- In browser A, start
kc_action=UPDATE_EMAIL, submit a new address, and open the confirmation link from the email in browser A. - In browser A, start
kc_action=UPDATE_PASSWORDand submit a new password. - Expected: back at the client. Actual: the error page above.
The script below runs these steps with curl against a stock container and Mailpit (./repro.sh 26.8.2 fails at step 6; ./repro.sh 26.8.1 reaches CALLBACK). It needs docker, curl, jq and openssl.
repro.sh
#!/usr/bin/env bash
# Stock Keycloak, plain realm: does a same-browser email-change
# confirmation break the next kc_action in that browser? usage: stock-repro.sh <keycloak tag>
set -uo pipefail
TAG="${1:?keycloak tag}"; ID="stock-$(echo "$TAG" | tr . -)-$$"; NET="$ID-net"; W="$(mktemp -d)"; P="$W/p"
cleanup() { docker rm -f "$ID-kc" "$ID-mail" >/dev/null 2>&1; docker network rm "$NET" >/dev/null 2>&1; rm -rf "$W"; }
trap cleanup EXIT
docker network create "$NET" >/dev/null
docker run -d --name "$ID-mail" --network "$NET" --network-alias mail -p 127.0.0.1::8025 axllent/mailpit:v1.33.0 >/dev/null
ADMINPW="local-$(openssl rand -hex 6)"
docker run -d --name "$ID-kc" --network "$NET" -p 127.0.0.1::8080 \
-e KC_BOOTSTRAP_ADMIN_USERNAME=admin -e KC_BOOTSTRAP_ADMIN_PASSWORD="$ADMINPW" \
"quay.io/keycloak/keycloak:$TAG" start-dev >/dev/null
B="http://127.0.0.1:$(docker port "$ID-kc" 8080 | head -1 | sed 's/.*://')"; M="http://127.0.0.1:$(docker port "$ID-mail" 8025 | head -1 | sed 's/.*://')"
for _ in $(seq 1 120); do curl -sf "$B/realms/master" >/dev/null && break; sleep 1; done
T() { curl -sf "$B/realms/master/protocol/openid-connect/token" -d grant_type=password -d client_id=admin-cli -d username=admin --data-urlencode "password=$ADMINPW" | jq -r .access_token; }
api() { curl -s -o /dev/null -w '%{http_code}' -X "$1" -H "Authorization: Bearer $(T)" -H 'Content-Type: application/json' "$B/admin/realms$2" ${3:+--data "$3"}; }
apiget() { curl -s -H "Authorization: Bearer $(T)" "$B/admin/realms$1"; }
echo "== Keycloak $TAG, stock image, start-dev, plain realm 'stock'"
echo "realm: $(api POST "" '{"realm":"stock","enabled":true,"verifyEmail":true,"smtpServer":{"host":"mail","port":"1025","from":"noreply@example.test"}}')"
echo "client: $(api POST /stock/clients '{"clientId":"app","publicClient":true,"standardFlowEnabled":true,"redirectUris":["http://localhost:9/*"]}')"
UA="$(apiget /stock/authentication/required-actions/UPDATE_EMAIL)"; echo "UPDATE_EMAIL required action: $(jq -c '{enabled,config}' <<<"$UA")"
api PUT /stock/authentication/required-actions/UPDATE_EMAIL "$(jq -c '.enabled=true | .config.verifyEmail="true"' <<<"$UA")" >/dev/null
PW="Xx-$(openssl rand -hex 8)"; E="sam-$(openssl rand -hex 3)@example.test"; N="new-$(openssl rand -hex 3)@example.test"
echo "user: $(api POST /stock/users "$(jq -nc --arg e "$E" --arg p "$PW" '{username:"sam",email:$e,emailVerified:true,enabled:true,firstName:"Sam",lastName:"Test",credentials:[{type:"password",value:$p,temporary:false}]}')")"
AUTH="$B/realms/stock/protocol/openid-connect/auth?client_id=app&response_type=code&scope=openid&redirect_uri=http%3A%2F%2Flocalhost%3A9%2Fcb"
J="$W/a"
go() { local url="$1"; shift; local args=() out; for d in "$@"; do args+=(--data-urlencode "$d"); done
while :; do case "$url" in http://localhost:9/*) echo CALLBACK > "$P"; return 0;; esac
out="$(curl -s -b "$J" -c "$J" -o "$P" -w '%{http_code} %{redirect_url}' ${args[@]+"${args[@]}"} "$url")"; args=()
case "${out%% *}" in 30?) url="${out#* }";; *) return 0;; esac; done; }
fa() { tr '\n' ' ' < "$P" | grep -o '<form[^>]*action="[^"]*"' | sed 's/.*action="//; s/"$//; s/&/\&/g' | grep -m1 login-actions || true; }
page() { tr '\n' ' ' < "$P" | sed -e 's/<script[^>]*>[^<]*<\/script>//g; s/<[^>]*>/ /g; s/'/'"'"'/g; s/ */ /g' | grep -oE "(already authenticated as different user[^.]*|CALLBACK|Mobile Authenticator Setup|Update password|Sign in to your account|Your email address has been updated[^.]*|confirm[^.]*email[^.]*)" | head -1; }
cookies() { awk '$6=="AUTH_SESSION_ID"||$6=="KEYCLOAK_IDENTITY"{print $1, $3, $6, substr($7,1,36)}' "$J" | sed 's/^#HttpOnly_//'; }
sessions() { apiget "/stock/users/$UID_/sessions" | jq -c '[.[].id]'; }
go "$AUTH"; go "$(fa)" "username=sam" "password=$PW"; echo "1 sign-in: $(page)"
UID_="$(apiget "/stock/users?username=sam&exact=true" | jq -r '.[0].id')"
echo " user sessions: $(sessions)"; cookies | sed 's/^/ cookie: /'
go "$AUTH&kc_action=UPDATE_EMAIL"; go "$(fa)" "email=$N"; echo "2 update-email submitted: $(page)"
L=""; for _ in $(seq 1 20); do mid="$(curl -sf "$M/api/v1/search?query=$(jq -rn --arg q "to:$N" '$q|@uri')" | jq -r '.messages[0].ID // empty')"
[ -n "$mid" ] && { L="$(curl -sf "$M/api/v1/message/$mid" | jq -r .Text | tr -d '\r' | grep -oE 'https?://[^ )>"]+' | grep -m1 action-token)"; break; }; sleep 1; done
echo " link host: $(sed -E 's#(https?://[^/]+).*#\1#' <<<"$L") (browser host: $B)"
go "$L"; echo "3 link opened in the same browser: $(page)"
echo " email now: $(apiget "/stock/users/$UID_" | jq -r .email); user sessions: $(sessions)"; cookies | sed 's/^/ cookie: /'
go "$AUTH"; echo "4 plain SSO sign-in: $(page)"
go "$AUTH&kc_action=UPDATE_PASSWORD"; echo "5 kc_action=UPDATE_PASSWORD page: $(page)"
go "$(fa)" "password-new=$PW-2" "password-confirm=$PW-2"; echo "6 after submitting the new password: $(page)"
echo " events: $(apiget "/stock/events?max=5" | jq -c '[.[]|{type,error}]' 2>/dev/null)"
docker logs "$ID-kc" 2>&1 | grep -i 'different_user\|DIFFERENT_USER' | tail -2 | cut -c1-300Output with 26.8.2 (abridged):
3 link opened in the same browser:
user sessions: unchanged; AUTH_SESSION_ID replaced
4 plain SSO sign-in: CALLBACK
5 kc_action=UPDATE_PASSWORD page: Update password
6 after submitting the new password: already authenticated as different user 'sam' in this session
type="LOGIN_ERROR", realmName="stock", clientId="app", error="different_user_authenticated"
With 26.8.1, step 6 is CALLBACK.
Anything else?
The user session id is the same before and after the confirmation; only the browser's AUTH_SESSION_ID changes when the action token is consumed. The link host and the browser host are the same, and the cookie domain and path are unchanged. 26.8.1 replaces AUTH_SESSION_ID in the same way, but then accepts it. We found no option to turn the new check off.
Metadata
Metadata
Assignees
Labels
Type
Fields
Projects
- StatusShow more project fieldsDone
Activity
ig-suyas commentedon Nov 16, 2026
Hi, I'd like to work on this. I'll start from the reporter's repro and the commit they point to (666eede), and I'll share what I find before opening a PR. Could you please assign it to me? Thank you!
rmartinc commentedon Nov 17, 2026
This is beacuse issue #54688. I'm working on it.
Recover correct user session when auth session expired
Recover correct user session when auth session expired
Recover correct user session when auth session expired