From f30d059a88f45751141706988ca10f3a64791367 Mon Sep 17 00:00:00 2001 From: Lumina Date: Sun, 15 Nov 2026 15:34:01 -0500 Subject: [PATCH] Do not put an ID token in the authorization code redirect For response_type=code the authorize endpoint inserted an id_token query parameter into the redirect to the client. That is not part of the code flow (the token endpoint already returns the ID token), and it breaks spec-following clients: they pass the URL they were called back on, minus code, state and iss, as redirect_uri to the token endpoint, which then no longer matches and fails with "Invalid redirect URI". This is what @inrupt/solid-client-authn-node does. Only add the ID token to the redirect for the implicit and hybrid response types. Co-Authored-By: Claude Opus 5.5 --- solid/lib/Controller/ServerController.php | 22 ++++++--- .../Unit/Controller/ServerControllerTest.php | 47 +++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/solid/lib/Controller/ServerController.php b/solid/lib/Controller/ServerController.php index deeff6cd..3e2a67b9 100644 --- a/solid/lib/Controller/ServerController.php +++ b/solid/lib/Controller/ServerController.php @@ -280,13 +280,21 @@ public function authorize() { $server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response); $response = $server->respondToAuthorizationRequest($request, $user, $approval); - $response = $this->tokenGenerator->addIdTokenToResponse( - $response, - $clientId, - $this->getProfilePage(), - $this->session->get("nonce"), - $this->config->getPrivateKey() - ); + + // In the authorization code flow the ID token is issued by the token + // endpoint, not in the redirect. Clients pass the redirect URI they + // were called back on (minus code, state and iss) to the token + // endpoint, so an extra `id_token` query parameter makes that URI + // differ from the registered one and the code exchange fails. + if ($getVars['response_type'] !== 'code') { + $response = $this->tokenGenerator->addIdTokenToResponse( + $response, + $clientId, + $this->getProfilePage(), + $this->session->get("nonce"), + $this->config->getPrivateKey() + ); + } return $this->respond($response); // ->addHeader('Access-Control-Allow-Origin', '*'); } diff --git a/solid/tests/Unit/Controller/ServerControllerTest.php b/solid/tests/Unit/Controller/ServerControllerTest.php index a6768aa7..506776d5 100644 --- a/solid/tests/Unit/Controller/ServerControllerTest.php +++ b/solid/tests/Unit/Controller/ServerControllerTest.php @@ -289,6 +289,53 @@ public function testAuthorizeWithInvalidRedirectUri() $this->assertEquals($expected, $actual); } + /** + * @testdox ServerController should not add an ID token to the redirect URI in the authorization code flow + * + * @covers ::authorize + */ + public function testAuthorizeWithCodeResponseTypeOmitsIdToken() + { + $_GET = [ + 'client_id' => self::MOCK_CLIENT_ID, + 'code_challenge' => 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM', + 'code_challenge_method' => 'S256', + 'nonce' => 'mock-nonce', + 'redirect_uri' => 'https://mock.client/redirect', + 'response_type' => 'code', + 'state' => 'mock-state', + ]; + $_SERVER['REQUEST_URI'] = '/apps/solid/authorize'; + + $clientData = json_encode(['client_name' => 'Mock Client', 'redirect_uris' => ['https://mock.client/redirect']]); + + $parameters = $this->createMockConstructorParameters($clientData); + + $this->mockConfig->method('getUserValue') + ->with(self::MOCK_USER_ID, Application::APP_ID, 'allowedClients', '[]') + ->willReturn(json_encode([self::MOCK_CLIENT_ID])); + + $this->mockUserManager->method('userExists')->willReturn(true); + + $controller = new ServerController(...$parameters); + + try { + $response = $controller->authorize(); + } finally { + $_GET = []; + } + + $this->assertEquals(Http::STATUS_FOUND, $response->getStatus()); + + $location = parse_url($response->getHeaders()['Location'] ?? ''); + parse_str($location['query'] ?? '', $query); + + $this->assertEquals('https://mock.client/redirect', "{$location['scheme']}://{$location['host']}{$location['path']}"); + $this->assertArrayHasKey('code', $query); + $this->assertEquals('mock-state', $query['state']); + $this->assertArrayNotHasKey('id_token', $query); + } + /** * @testdox ServerController should return a 302 redirect when asked to authorize client that has been approved *