Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate state before ID Token request #447

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2024-09-13
[unreleased]

### Added
- Add unit tests for verifyJWTClaims and different aud claims. #443

## Changed
- Validate state before ID Token request. #447

### Fixed
- Fix protected responseContentType to allow overloading of fetchUrl function. #446
- Fix TypeError in verifyJWTClaims. #442


## [1.0.2] - 2024-09-13

### Added
- Add unit test for SERVER_PORT type cast. #438

### Fixed
- Cast `$_SERVER['SERVER_PORT']` to integer to prevent adding 80 or 443 port to redirect URL. #437
- Fix protected $responseCode to allow proper overloading of fetchURL(). #433
- Fix bring back #404. #437

## [1.0.1] - 2024-09-05

Expand Down
18 changes: 9 additions & 9 deletions src/OpenIDConnectClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,15 @@ public function authenticate(): bool

// If we have an authorization code then proceed to request a token
if (isset($_REQUEST['code'])) {
// Do an OpenID Connect session check
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
throw new OpenIDConnectClientException('Unable to determine state');
}

// Cleanup state
$this->unsetState();

// Request ID Token
$code = $_REQUEST['code'];
$token_json = $this->requestTokens($code);

Expand All @@ -318,14 +326,6 @@ public function authenticate(): bool
throw new OpenIDConnectClientException('Got response: ' . $token_json->error);
}

// Do an OpenID Connect session check
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
throw new OpenIDConnectClientException('Unable to determine state');
}

// Cleanup state
$this->unsetState();

if (!property_exists($token_json, 'id_token')) {
throw new OpenIDConnectClientException('User did not authorize openid scope.');
}
Expand Down Expand Up @@ -379,7 +379,7 @@ public function authenticate(): bool
$accessToken = $_REQUEST['access_token'] ?? null;

// Do an OpenID Connect session check
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
if (!isset($_REQUEST['state']) || ($_REQUEST['state'] !== $this->getState())) {
throw new OpenIDConnectClientException('Unable to determine state');
}

Expand Down
66 changes: 66 additions & 0 deletions tests/OpenIDConnectClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,72 @@ public function testAuthenticateDoesNotThrowExceptionIfClaimsIsMissingNonce()
}
}

public function testAuthenticateWithCodeThrowsExceptionIfStateDoesNotMatch()
{
$_REQUEST['code'] = 'some-code';
$_REQUEST['state'] = "incorrect-state-from-user";
$_SESSION['openid_connect_state'] = "random-generated-state";

$client = new OpenIDConnectClient();

try {
$client->authenticate();
} catch ( OpenIDConnectClientException $e ) {
$this->assertEquals('Unable to determine state', $e->getMessage());
return;
}

$this->fail('OpenIDConnectClientException was not thrown when it should have been.');
}

public function testAuthenticateWithCodeMockedVerify()
{
$mockCode = 'some-code';
$mockState = 'some-code';

$_REQUEST['code'] = $mockCode;
$_REQUEST['state'] = $mockState;

$mockClaims = (object)['email' => 'test@example.com'];
$mockIdToken = implode('.', [base64_encode('{}'), base64_encode(json_encode($mockClaims)), '']);
$mockAccessToken = 'some-access-token';
$mockRefreshToken = 'some-access-token';

$mockTokenResponse = (object)[
'id_token' => $mockIdToken,
'access_token' => $mockAccessToken,
'refresh_token' => $mockRefreshToken,
];

$client = $this->getMockBuilder(OpenIDConnectClient::class)
->setMethods(['requestTokens', 'verifySignatures', 'verifyJWTClaims', 'getState'])
->getMock();
$client->method('getState')
->willReturn($mockState);
$client->method('requestTokens')
->with($mockCode)
->willReturn($mockTokenResponse);
$client->method('verifySignatures')
->with($mockIdToken);
$client->method('verifyJWTClaims')
->with($mockClaims, $mockAccessToken)
->willReturn(true);

try {
// In this mocked case we should be authenticated
// because we are not actually verifying the JWT
$authenticated = $client->authenticate();
$this->assertTrue($authenticated);
$this->assertEquals($mockIdToken, $client->getIdToken());
$this->assertEquals($mockAccessToken, $client->getAccessToken());
$this->assertEquals($mockTokenResponse, $client->getTokenResponse());
$this->assertEquals($mockClaims, $client->getVerifiedClaims());
$this->assertEquals($mockRefreshToken, $client->getRefreshToken());
} catch ( OpenIDConnectClientException $e ) {
$this->fail('OpenIDConnectClientException was thrown when it should not have been. Received exception: ' . $e->getMessage());
}
}

public function testSerialize()
{
$client = new OpenIDConnectClient('https://example.com', 'foo', 'bar', 'baz');
Expand Down