From 9c7bba5f6368faf1d057419acffc3fab4514fc51 Mon Sep 17 00:00:00 2001 From: Lumina Date: Sun, 15 Nov 2026 15:10:41 -0500 Subject: [PATCH] Accept client_secret_basic at the token endpoint The discovery document advertises client_secret_basic, but the token endpoint only looked for client_id in the request body. A confidential client that authenticates with an HTTP Basic Authorization header (the default for openid-client, which @inrupt/solid-client-authn-node uses) therefore got an ID token with "aud": null, which the client rejects, and its registration was never loaded, so the client_credentials grant always failed with 401. Read the client_id from the Basic header (form-urlencoded per RFC 6749 section 2.3.1) when it is not in the body, both for loading the client registration and for the audience of the issued tokens. Co-Authored-By: Claude Opus 5.5 --- solid/lib/Controller/ServerController.php | 33 ++++++++++++- .../Unit/Controller/ServerControllerTest.php | 47 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/solid/lib/Controller/ServerController.php b/solid/lib/Controller/ServerController.php index deeff6cd..7f7e60de 100644 --- a/solid/lib/Controller/ServerController.php +++ b/solid/lib/Controller/ServerController.php @@ -118,6 +118,12 @@ private function createAuthServerConfig() { $clientId = $_POST['client_id']; } } + + if ($clientId === null) { + // A confidential client may authenticate with HTTP Basic (client_secret_basic), + // in which case the client_id is not in the request body. + $clientId = $this->getBasicAuthClientId(); + } $client = $this->getClient($clientId); $keys = $this->getKeys(); try { @@ -310,6 +316,31 @@ private function checkApproval($clientId, $clientRegistration) return $approved; } + /** + * Returns the client_id from an HTTP Basic Authorization header, if present. + * + * Per RFC 6749 section 2.3.1 the client_id and client_secret are + * form-urlencoded before they are joined and base64 encoded. + */ + private function getBasicAuthClientId(): ?string { + $header = $_SERVER['HTTP_AUTHORIZATION'] ?? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ?? ''; + + if (stripos($header, 'Basic ') !== 0) { + return null; + } + + $decoded = base64_decode(substr($header, 6), true); + + if ($decoded === false || strpos($decoded, ':') === false) { + return null; + } + + [$clientId] = explode(':', $decoded, 2); + $clientId = urldecode($clientId); + + return $clientId === '' ? null : $clientId; + } + private function getProfilePage() { return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.profile.handleGet", array("userId" => $this->userId, "path" => "/card"))) . "#me"; } @@ -366,7 +397,7 @@ public function token() { break; } - $clientId = $request->getParsedBody()['client_id'] ?? null; + $clientId = $request->getParsedBody()['client_id'] ?? $this->getBasicAuthClientId(); $httpDpop = $request->getServerParams()['HTTP_DPOP'] ?? null; diff --git a/solid/tests/Unit/Controller/ServerControllerTest.php b/solid/tests/Unit/Controller/ServerControllerTest.php index a6768aa7..7d24b9ff 100644 --- a/solid/tests/Unit/Controller/ServerControllerTest.php +++ b/solid/tests/Unit/Controller/ServerControllerTest.php @@ -520,6 +520,53 @@ public function testToken() $this->assertEquals($expected, $actual); } + /** + * @testdox ServerController should read the client_id from an HTTP Basic Authorization header when generating a token + * + * @covers ::token + */ + public function testTokenWithClientSecretBasic() + { + unset($_POST['client_id']); + $_POST['code'] = ''; + $_POST['grant_type'] = 'authorization_code'; + $_SERVER['HTTP_AUTHORIZATION'] = 'Basic ' . base64_encode(urlencode(self::MOCK_CLIENT_ID) . ':mock secret'); + $_SERVER['HTTP_DPOP'] = 'mock dpop'; + $_SESSION['nonce'] = 'mock nonce'; + + $parameters = $this->createMockConstructorParameters(); + + $mockTokenGenerator = $this->createMock(\Pdsinterop\Solid\Auth\TokenGenerator::class); + $mockTokenGenerator->method('getCodeInfo')->willReturn(['user_id' => self::MOCK_USER_ID]); + $mockTokenGenerator->expects($this->once()) + ->method('addIdTokenToResponse') + ->with( + $this->isInstanceOf(Response::class), + self::MOCK_CLIENT_ID, + self::MOCK_USER_ID, + $_SESSION['nonce'], + self::$privateKey, + $_SERVER['HTTP_DPOP'], + ) + ->willReturn(new Response('php://memory', Http::STATUS_IM_A_TEAPOT, [ + 'Content-Type' => 'mock application type' + ])); + + try { + $controller = new ServerController(...$parameters); + + $reflectionObject = new \ReflectionObject($controller); + $reflectionProperty = $reflectionObject->getProperty('tokenGenerator'); + $reflectionProperty->setValue($controller, $mockTokenGenerator); + + $tokenResponse = $controller->token(); + } finally { + unset($_SERVER['HTTP_AUTHORIZATION']); + } + + $this->assertEquals(Http::STATUS_IM_A_TEAPOT, $tokenResponse->getStatus()); + } + /** * @testdox ServerController should return an OK when asked to logout *