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
33 changes: 32 additions & 1 deletion solid/lib/Controller/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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;

Expand Down
47 changes: 47 additions & 0 deletions solid/tests/Unit/Controller/ServerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down