Skip to content

Keycloak oauth configuration code not cleared #13861

Description

@kiranchavala

problem

No response

versions

ACS 4.23 RC

The keycloak oauth was introduced with the pr
#13033

The steps to reproduce the bug

Steps to reproduce the behaviour

  1. Run a keycloack service using a docker container
docker run -d --name keycloak -p 8081:8080 \
    -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin \
    quay.io/keycloak/keycloak:latest start-dev
  1. Login to the keycloak ui and create client

Example : create a client name cloudstack

  1. In CloudStack (Settings → OAuth Settings, registering the keycloak provider
Image Image
  1. Create a matching CloudStack user (username == the Keycloak test user's email).

  2. Try login with keycloak provider flow end to end > Login works fine

Issue to reproduce the bug

  1. Cause one failure attempt with keycloak oauth attempt

Rename a CloudStack username so it no longer matches an OAuth-configured account (or use an IdP account with no matching CloudStack user).

  1. Log in via the IdP with valid credentials: the code exchange succeeds (verifyOAuthCodeAndGetUser returns the correct email), but oauthlogin fails downstream because OAuth2UserAuthenticator.authenticate() can't find a matching account (userAccountDao.getUserAccount() returns null) — note this returns false before verifyUser() is ever called, so the cached token is never cleared.

  2. Do not restart the management server.

  3. Fix the mismatch, then perform a genuinely fresh IdP login (new authorization code) and retry.

  4. Issue gets resolved if the management server is restarted

Logs

root@Cloudstack-422-before:/home/ubuntu# cat     /var/log/cloudstack/management/management-server.log |grep -i "logid:84fb4f98"
2026-08-12 08:05:42,795 DEBUG [c.c.a.ApiServlet] (qtp659590237-697:[ctx-95f375f8]) (logid:84fb4f98) ===START===  192.168.55.207 -- GET  provider=keycloak&secretcode=031a66aa-7525-b93e-3757-73350343db82.XbFFhkc0E7t7nZJ_7joLuMwU.5ed14f93-3977-4527-9649-c88da3e7be55&domain=d1&command=verifyOAuthCodeAndGetUser&response=json
2026-08-12 08:05:42,796 DEBUG [c.c.a.ApiServlet] (qtp659590237-697:[ctx-95f375f8]) (logid:84fb4f98) Authentication failure: Unable to verify the code provided
2026-08-12 08:05:42,796 DEBUG [c.c.a.ApiServlet] (qtp659590237-697:[ctx-95f375f8]) (logid:84fb4f98) ===END===  192.168.55.207 -- GET  provider=keycloak&secretcode=031a66aa-7525-b93e-3757-73350343db82.XbFFhkc0E7t7nZJ_7joLuMwU.5ed14f93-3977-4527-9649-c88da3e7be55&domain=d1&command=verifyOAuthCodeAndGetUser&response=json


root@Cloudstack-422-before:/home/ubuntu# cat     /var/log/cloudstack/management/management-server.log |grep -i "logid:2a077ac1"
2026-08-12 08:06:03,408 DEBUG [c.c.a.ApiServlet] (qtp659590237-694:[ctx-9bb94840]) (logid:2a077ac1) ===START===  192.168.55.207 -- GET  provider=keycloak&secretcode=a51ebc47-eac5-a5b2-ccc5-c38f10a6a3c1.XbFFhkc0E7t7nZJ_7joLuMwU.5ed14f93-3977-4527-9649-c88da3e7be55&domain=d1&command=verifyOAuthCodeAndGetUser&response=json
2026-08-12 08:06:03,409 DEBUG [c.c.a.ApiServlet] (qtp659590237-694:[ctx-9bb94840]) (logid:2a077ac1) Authentication failure: Unable to verify the code provided
2026-08-12 08:06:03,409 DEBUG [c.c.a.ApiServlet] (qtp659590237-694:[ctx-9bb94840]) (logid:2a077ac1) ===END===  192.168.55.207 -- GET  provider=keycloak&secretcode=a51ebc47-eac5-a5b2-ccc5-c38f10a6a3c1.XbFFhkc0E7t7nZJ_7joLuMwU.5ed14f93-3977-4527-9649-c88da3e7be55&domain=d1&command=verifyOAuthCodeAndGetUser&response=json

What to do about it?

Expected: The new code is validated against the IdP's token endpoint independently.
Actual: The plugin never makes a second token-exchange call — it serves the result off the token cached in st

Activity

  1. kiranchavala commented on Aug 12, 2026

    @kiranchavala
    MemberAuthor
  2. changed the title [-]Keycloak oauth configuration[/-] [+]Keycloak oauth configuration code not cleared[/+] on Aug 12, 2026
  3. self-assigned this
    on Aug 12, 2026
  4. weizhouapache commented on Aug 21, 2026

    @weizhouapache
    Member

    @Damans227
    will you create a PR ? I notice you have assigned this to yourself

  5. added this to the 4.24.0 milestone on Aug 21, 2026
  6. la-wu commented on Nov 25, 2026

    @la-wu

    just a short hint for all users that might be affected by this issue.
    As a workaround we limited the cloudstack client in keycloak to just the users that are created in cloudstack.

    Below are the steps we did in keycloak:

    • Go to the desired client → roles → create role (give a proper name like cloudstack-access)
    • Go to users → for each cloudstack user → Role mapping → Assign role → client role → select the newly created role (e.g. cloudstack-access)
    • Create an authentication flow that checks the role assignment. If the user is not part of the role keycloak will block access and never forward a token to cloudstack. To do so:
    1. Under Authentication → Flows, open the copy browser-cloudstack.
    2. On the forms sub-flow (menu on the right, plus icon), choose Add sub-flow. Name it e.g. Deny without cloudstack role. Set the requirement of the new sub-flow to Conditional. and move it under the forms sub-flow.
    3. In this sub-flow, choose Add condition and add Condition - User Role. Set its requirement to Required.
    4. Open the condition's configuration via the gear icon:
      • Alias: anything, e.g. has-role.
      • User role: select the role you created at the beginning (e.g. cloudstack-access).
      • Negate output: enable it.
    5. In the same sub-flow, choose Add execution and add Deny access. Set its requirement to Required.
    6. Via the gear icon of Deny access, set an Alias and optionally an Error message. If the field is left empty, the default message access-denied is shown.
    • Bind the flow to the client: Clients → Advanced → Authentication flow overrides → Browser Flow (at the end) → select the newly created flow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions