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 *