Amazon Ergo 1.0 · Balanced

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

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

The normal review, with one generalist agent. Judge: Claude Opus 5.5.

Gold bugs found
4 of 5
Comments published
4
Comments matching a bug
4
Comments not matching
0
Cost of this PR
$4.392

Published comments (4)

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

isAccessTokenId extracts the grant shortcut from the wrong offsets — Valid token IDs are rejected because the matcher reads an overlapping range and treats a match as failure. Use `items[0].substring(4, 6)` for indexes 4-5, and reject the item only when the shortcut differs from `expectedGrantShortcut`.

mediumbugservices/src/main/java/org/keycloak/protocol/oidc/encode/AccessTokenContext.java:69-77

Constructor validates grantType twice, skipping the rawTokenId null check — A null `rawTokenId` is accepted and can produce an encoded ID ending in `null` instead of failing fast. Replace the duplicate `grantType` check so `Objects.requireNonNull` validates `rawTokenId` with the existing error message.

mediumbugservices/src/main/java/org/keycloak/protocol/oidc/tokenexchange/StandardTokenExchangeProvider.java:234-239

V1 and subject-impersonation token exchange omit the 'te' grant type — Access tokens from `V1TokenExchangeProvider` and `SubjectImpersonationTokenExchangeProvider` carry `na` instead of `te`, so their encoded IDs do not identify token exchange. Set `Constants.GRANT_TYPE` in `AbstractTokenExchangeProvider` or `TokenExchangeGrantType.process` so every exchange flow inherits it.

mediumbugservices/src/main/java/org/keycloak/protocol/oidc/grants/PermissionGrantTypeFactory.java:38-41

PermissionGrantType.process leaves grant type null, encoding RPT ID as 'na' — UMA RPT IDs are encoded with `na` instead of `pg`, misidentifying permission-grant tokens. Set `Constants.GRANT_TYPE` in `PermissionGrantType.process` before calling `AuthorizationTokenService.authorize` so the authorization-token path inherits it.

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.
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.
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.

Gold bugs missed (1)

mediumtestIncorrectGrantType catches any RuntimeException, so it passes on a NullPointerException from the unset sessionFactory rather than verifying the unknown-grant IllegalArgumentException.