Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions solid/lib/BaseServerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
60 changes: 43 additions & 17 deletions solid/lib/Controller/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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";
}
Expand Down
55 changes: 55 additions & 0 deletions solid/tests/Unit/Controller/ServerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down