Amazon Ergo 1.0 · Heavy

implement-access-token-context-encoding-framework-keycloak

Keycloak · Java · L · 300–799 lines · 6 files

The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Opus 5.5.

Gold bugs found
4 of 5
Comments published
10
Comments matching a bug
4
Comments not matching
6
Cost of this PR
$5.651

Published comments (10)

highbugserver-spi-private/src/main/java/org/keycloak/protocol/oidc/grants/OAuth2GrantTypeFactory.java:29-33

Abstract getShortcut() breaks third-party grant factories at startup — Third-party `OAuth2GrantTypeFactory` implementations compiled against the previous SPI fail at startup with `AbstractMethodError` when `DefaultTokenContextEncoderProviderFactory.postInit()` calls `getShortcut()`. Make `getShortcut()` a default method returning `getId()` to preserve compatibility.

mediumbugservices/src/main/java/org/keycloak/protocol/oidc/grants/OAuth2GrantTypeBase.java:106-110

GRANT_TYPE unset for client-credentials and token-exchange grants — Client-credentials and legacy/V1 token-exchange access tokens encode `na` instead of `cc`/`te`, losing their grant context. Set `Constants.GRANT_TYPE` where `ClientCredentialsGrantType` and `AbstractTokenExchangeProvider` call `TokenManager.responseBuilder`, or derive it centrally during token creation.

mediumbugservices/src/test/java/org/keycloak/protocol/oidc/encode/DefaultTokenContextEncoderProviderTest.java:77-84

testIncorrectGrantType accepts unrelated RuntimeException failures — Use `Assert.assertThrows` with `IllegalArgumentException.class` around `getTokenContextFromTokenId` so only the expected failure passes.

mediumbugservices/src/main/java/org/keycloak/protocol/oidc/encode/AccessTokenContext.java:72-73

Null rawTokenId check validates grantType instead — The duplicated `grantType` check lets a null `rawTokenId` through, encoding it as `null` in the token ID or causing downstream failures. Change the second `Objects.requireNonNull` call to validate `rawTokenId`.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:482-483

Grant shortcut is read from the wrong token ID indices — `substring(3, 5)` reads `ta` from `onrtac:...` instead of the grant shortcut `ac`, so assertions would still fail after fixing the inverted condition. Use `substring(4, 6)` for the two-character shortcut at indices 4-5.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:476-492

Access-token matcher reads the grant shortcut at the wrong offset — `substring(3, 5)` extracts the last token-type character plus the first grant character, so `isAccessTokenId` never compares the expected two-character grant shortcut. Update the comment and use `substring(4, 6)` to match `DefaultTokenContextEncoderProvider.encodeTokenId`.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:482-483

Access token grant shortcut is read from the wrong offset — `substring(3, 5)` reads one token-type character and one grant character, yielding `ta` instead of `ac` for `onrtac:<uuid>`. Use `substring(4, 6)` with a length check so malformed IDs return false instead of risking an out-of-bounds exception.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:482-483

Grant shortcut check uses wrong offset and inverted match — `substring(3, 5)` captures one token-type character and one grant character, so the comparison can never match a two-character shortcut such as `ac`, even after fixing the inverted condition. Read indices 4-5 with `substring(4, 6)`, matching `DefaultTokenContextEncoderProvider.encodeTokenId`.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:482-484

Access token matcher misreads and inverts the grant shortcut check — The expected grant shortcut is never actually compared, so `isAccessTokenId` silently accepts any grant whose raw ID is a UUID. Read it with `substring(4, 6)` and negate the `expectedGrantShortcut` comparison so mismatches return false, matching `DefaultTokenContextEncoderProvider.encodeTokenId`.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:483

isAccessTokenId rejects matching grant shortcuts — `isAccessTokenId` accepts any token with a UUID raw ID regardless of grant, disabling grant verification for its callers. Negate the `expectedGrantShortcut` comparison so a mismatch, not a match, returns false.

Gold bugs found (4)

mediumThe AccessTokenContext constructor checks grantType twice instead of rawTokenId, so a null rawTokenId passes validation and is encoded as ':null'.
mediumisAccessTokenId reads the grant shortcut at the wrong offset (3,5 instead of 4,6) and inverts the comparison, so the event assertions never verify the token's grant type.
mediumtestIncorrectGrantType catches any RuntimeException, so it passes on a NullPointerException from the unset sessionFactory rather than verifying the unknown-grant IllegalArgumentException.
mediumClient-credentials (and UMA permission) token issuance never sets the GRANT_TYPE attribute, so their access token ids are encoded with the unknown grant 'na' instead of their shortcut.

Gold bugs missed (1)

mediumThe legacy token-exchange path in AbstractTokenExchangeProvider never sets Constants.GRANT_TYPE on the client session context, so tokens it issues are encoded with grant shortcut 'na' instead of the token-exchange shortcut.