From 07a9cb649fbd42d4bab91e98f9d5faf2f9205458 Mon Sep 17 00:00:00 2001 From: Lumina Date: Sun, 15 Nov 2026 16:13:19 -0500 Subject: [PATCH] Make login work for clients with a Client Identifier Document When a client identifies itself with a URL (a Solid-OIDC Client Identifier Document, as Penny and most current Solid apps do), login could not complete: - authorize saved a new registration with a random client ID on every request, so the user's approval never applied to the next request; - after approval the user was sent to the client's redirect URI instead of back to the authorization request, so the app got no code; - the authorization server was configured with an empty client for the URL client_id, so even an approved request failed with 401 because no redirect URI was registered for it. Derive one registration from the client_id URL and the redirect URI, use it for the approval (which is then bound to that redirect URI) and for the authorization server's client, keep the URL as the client identifier the code is issued to, and return to the authorization request after approval, as for other clients. The Client Identifier Document itself is still not fetched; that remains the existing FIXME. Co-Authored-By: Claude Opus 5.5 --- solid/lib/BaseServerConfig.php | 30 ++++++++++ solid/lib/Controller/ServerController.php | 60 +++++++++++++------ .../Unit/Controller/ServerControllerTest.php | 55 +++++++++++++++++ 3 files changed, 128 insertions(+), 17 deletions(-) diff --git a/solid/lib/BaseServerConfig.php b/solid/lib/BaseServerConfig.php index 5e788202..d3d6ea31 100644 --- a/solid/lib/BaseServerConfig.php +++ b/solid/lib/BaseServerConfig.php @@ -189,6 +189,36 @@ public function saveClientRegistration($clientData) return $clientData; } + /** + * Returns the registration for a client that identifies itself with a + * Client Identifier Document URL (Solid-OIDC), creating it when needed. + * + * The registration ID is derived from the client_id URL and the redirect + * URI, so the same client gets the same registration on every + * authorization request. A user's approval then sticks, and it is bound + * to that exact redirect URI. + */ + public function getClientIdDocumentRegistration(string $clientIdUrl, string $redirectUri, string $origin): array + { + $clientId = substr(hash('sha256', $clientIdUrl . "\n" . $redirectUri), 0, 32); + + $registration = $this->getClientRegistration($clientId); + if (($registration['client_id'] ?? null) === $clientId) { + return $registration; + } + + $registration = [ + 'client_id' => $clientId, + 'client_id_issued_at' => time(), + 'client_name' => $clientIdUrl, + 'origin' => $origin, + 'redirect_uris' => [$redirectUri], + ]; + $this->config->setAppValue('solid', "client-" . $clientId, json_encode($registration)); + + return $registration; + } + public function removeClientRegistration($clientId) { $this->config->deleteAppValue('solid', "client-" . $clientId); } diff --git a/solid/lib/Controller/ServerController.php b/solid/lib/Controller/ServerController.php index deeff6cd..620e4903 100644 --- a/solid/lib/Controller/ServerController.php +++ b/solid/lib/Controller/ServerController.php @@ -118,7 +118,29 @@ private function createAuthServerConfig() { $clientId = $_POST['client_id']; } } - $client = $this->getClient($clientId); + if ( + $clientId !== null + && preg_match("/^http(s)?:/", $clientId) + && isset($_GET['redirect_uri']) + && is_string($_GET['redirect_uri']) + ) { + // A Client Identifier Document has no registration under its URL. + // Use the registration for this client and redirect URI, but keep + // the URL as the client identifier the authorization code is issued to. + $registration = $this->config->getClientIdDocumentRegistration( + $clientId, + $_GET['redirect_uri'], + $this->getOrigin($_GET['redirect_uri']) + ); + $client = new \Pdsinterop\Solid\Auth\Config\Client( + $clientId, + '', + $registration['redirect_uris'], + $registration['client_name'] + ); + } else { + $client = $this->getClient($clientId); + } $keys = $this->getKeys(); try { return (new \Pdsinterop\Solid\Auth\Factory\ConfigFactory( @@ -200,23 +222,18 @@ public function authorize() { } if (preg_match("/^http(s)?:/", $getVars['client_id'])) { - $parsedOrigin = parse_url($getVars['redirect_uri']); - $origin = $parsedOrigin['scheme'] . '://' . $parsedOrigin['host']; - if (isset($parsedOrigin['port'])) { - $origin .= ":" . $parsedOrigin['port']; - } - $clientData = array( - "client_id_issued_at" => time(), - "client_name" => $getVars['client_id'], - "origin" => $origin, - "redirect_uris" => array( - $getVars['redirect_uri'] - ) + $origin = $this->getOrigin($getVars['redirect_uri']); + // Use a stable registration for this client and redirect URI. A new + // registration on every request meant the user's approval never + // applied to the next request, and returning to the redirect URI + // after approval sent the user back to the app without a code. + $clientRegistration = $this->config->getClientIdDocumentRegistration( + $getVars['client_id'], + $getVars['redirect_uri'], + $origin ); - - $clientId = $this->config->saveClientRegistration($clientData)['client_id']; - $returnUrl = $getVars['redirect_uri']; - $clientRegistration = $this->config->getClientRegistration($clientId); + $clientId = $clientRegistration['client_id']; + $returnUrl = $_SERVER['REQUEST_URI']; // @FIXME: Implement $clientRegistration = $this->fetchRemoteClientDocument($getVars['client_id']) // to replace this section of this `if` statement. } else { @@ -310,6 +327,15 @@ private function checkApproval($clientId, $clientRegistration) return $approved; } + private function getOrigin(string $uri): string { + $parsedOrigin = parse_url($uri); + $origin = ($parsedOrigin['scheme'] ?? '') . '://' . ($parsedOrigin['host'] ?? ''); + if (isset($parsedOrigin['port'])) { + $origin .= ":" . $parsedOrigin['port']; + } + return $origin; + } + private function getProfilePage() { return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.profile.handleGet", array("userId" => $this->userId, "path" => "/card"))) . "#me"; } diff --git a/solid/tests/Unit/Controller/ServerControllerTest.php b/solid/tests/Unit/Controller/ServerControllerTest.php index a6768aa7..b7d41556 100644 --- a/solid/tests/Unit/Controller/ServerControllerTest.php +++ b/solid/tests/Unit/Controller/ServerControllerTest.php @@ -160,6 +160,61 @@ public function testAuthorizeWithoutValidToken() $this->assertEquals($expected, $actual); } + /** + * @testdox ServerController should use the same registration for a Client Identifier Document on every request, and return to the authorization request after approval + * + * @covers ::authorize + * @uses \OCA\Solid\BaseServerConfig::getClientIdDocumentRegistration + */ + public function testAuthorizeWithClientIdDocumentUsesStableRegistration() + { + $_GET = [ + 'client_id' => 'https://app.example/id', + 'redirect_uri' => 'https://app.example/callback', + 'response_type' => 'code', + ]; + $_SERVER['REQUEST_URI'] = '/apps/solid/authorize?client_id=https%3A%2F%2Fapp.example%2Fid'; + + $appValues = []; + $approvalUrls = []; + + for ($i = 0; $i < 2; $i++) { + $parameters = $this->createMockConstructorParameters(); + + $config = $this->createMock(IConfig::class); + $config->method('getAppValue')->willReturnCallback( + function ($app, $key, $default = '') use (&$appValues) { + $fixed = ['encryptionKey' => 'mock encryption key', 'privateKey' => self::$privateKey]; + return $fixed[$key] ?? $appValues[$key] ?? $default; + } + ); + $config->method('setAppValue')->willReturnCallback( + function ($app, $key, $value) use (&$appValues) { $appValues[$key] = $value; } + ); + $config->method('getUserValue')->willReturnArgument(3); + $parameters[self::provideConstructorParameterIndex()['config'][0]] = $config; + $this->mockUserManager->method('userExists')->willReturn(true); + $this->mockURLGenerator->method('linkToRoute')->willReturnCallback( + fn ($route, $params = []) => $route . '?' . http_build_query($params) + ); + $this->mockURLGenerator->method('getAbsoluteURL')->willReturnArgument(0); + + $controller = new ServerController(...$parameters); + $response = $controller->authorize(); + + $this->assertEquals(Http::STATUS_FOUND, $response->getStatus()); + $approvalUrls[] = $response->getHeaders()['Location']; + } + + $_GET = []; + + $this->assertCount(1, $appValues, 'Only one registration should be created'); + $this->assertSame($approvalUrls[0], $approvalUrls[1]); + + parse_str(parse_url($approvalUrls[0], PHP_URL_QUERY), $query); + $this->assertSame($_SERVER['REQUEST_URI'], $query['returnUrl']); + } + /** * @testdox ServerController should return a 302 redirect when asked to authorize client that has not been approved *