From 41a7ebd8624f97945ad0a4cbc1adf6bf9a99c1a5 Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Thu, 5 Nov 2026 01:53:53 +0300 Subject: [PATCH 1/2] Fix NPE in device authorization grant when the client session has expired Signed-off-by: Inf1mum --- .../oidc/grants/device/DeviceGrantType.java | 9 ++++ .../OAuth2DeviceAuthorizationGrantTest.java | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/services/src/main/java/org/keycloak/protocol/oidc/grants/device/DeviceGrantType.java b/services/src/main/java/org/keycloak/protocol/oidc/grants/device/DeviceGrantType.java index 38112199a1b..d4b0f0a7b6f 100644 --- a/services/src/main/java/org/keycloak/protocol/oidc/grants/device/DeviceGrantType.java +++ b/services/src/main/java/org/keycloak/protocol/oidc/grants/device/DeviceGrantType.java @@ -327,6 +327,15 @@ public Response process(Context context) { } AuthenticatedClientSessionModel clientSession = userSession.getAuthenticatedClientSessionByClient(client.getId()); + if (clientSession == null) { + // The client session is gone (e.g. its idle timeout is shorter than the user session's), + // so the client has to restart the device flow. + String errorMessage = "Session not active"; + event.detail(Details.REASON, errorMessage); + event.error(Errors.SESSION_EXPIRED); + throw new CorsErrorResponseException(cors, OAuthErrorException.INVALID_GRANT, errorMessage, + Response.Status.BAD_REQUEST); + } if (!client.getClientId().equals(clientSession.getClient().getClientId())) { event.error(Errors.INVALID_CODE); throw new CorsErrorResponseException(cors, OAuthErrorException.INVALID_GRANT, "Auth error", diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/OAuth2DeviceAuthorizationGrantTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/OAuth2DeviceAuthorizationGrantTest.java index 4df4159ea9f..f34fface04e 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/OAuth2DeviceAuthorizationGrantTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/OAuth2DeviceAuthorizationGrantTest.java @@ -867,6 +867,52 @@ public void testDeviceCodeLifespanPerClient() throws Exception { client.update(clientRepresentation); } + @Test + public void testExpiredClientSession() throws Exception { + getTestingClient().testing().setTestingInfinispanTimeService(); + ClientResource client = AdminApiUtil.findClientByClientId(adminClient.realm(REALM_NAME), DEVICE_APP_PUBLIC); + ClientRepresentation clientRepresentation = client.toRepresentation(); + + // Device Authorization Request from device + oauth.realm(REALM_NAME); + oauth.client(DEVICE_APP_PUBLIC); + DeviceAuthorizationResponse response = oauth.device().doDeviceAuthorizationRequest(); + + Assertions.assertEquals(200, response.getStatusCode()); + assertNotNull(response.getDeviceCode()); + assertNotNull(response.getUserCode()); + assertNotNull(response.getVerificationUriComplete()); + + // Make the client session idle timeout shorter than the user session timeout + clientRepresentation.getAttributes().put(OIDCConfigAttributes.CLIENT_SESSION_IDLE_TIMEOUT, "5"); + client.update(clientRepresentation); + + openVerificationPage(response.getVerificationUriComplete()); + + // Do Login + oauth.fillLoginForm("device-login", "password"); + + // Consent + grantPage.accept(); + + try { + // Wait until the client session is expired while the user session is still alive + timeOffSet.set(10); + + // Token request from device + AccessTokenResponse tokenResponse = oauth.device().doDeviceTokenRequest(response.getDeviceCode()); + + Assertions.assertEquals(400, tokenResponse.getStatusCode()); + Assertions.assertEquals("invalid_grant", tokenResponse.getError()); + Assertions.assertEquals("Session not active", tokenResponse.getErrorDescription()); + } finally { + timeOffSet.set(0); + clientRepresentation.getAttributes().remove(OIDCConfigAttributes.CLIENT_SESSION_IDLE_TIMEOUT); + client.update(clientRepresentation); + getTestingClient().testing().revertTestingInfinispanTimeService(); + } + } + @Test public void testDevicePollingIntervalPerClient() throws Exception { getTestingClient().testing().setTestingInfinispanTimeService(); From 890e692afe7b75a5d3ade697e712abbc8da5b8ba Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Sun, 15 Nov 2026 23:41:22 +0300 Subject: [PATCH 2/2] chore(ci): re-trigger the Admin UI E2E suite Signed-off-by: Inf1mum