Skip to content

Commit 14991f7

Browse files
authored
Merge pull request #280 from jenkoian/check-nonce-isset
Check nonce isset
2 parents 83481eb + 9b04bf4 commit 14991f7

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
### Changed
1010

1111
* signOut() Method parameter $accessToken -> $idToken to prevent confusion about access and id tokens usage. #127
12+
* Fixed issue where missing nonce within the claims was causing an exception. #280
1213

1314
## [0.9.4]
1415

phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
bootstrap="./vendor/autoload.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
verbose="true"
9+
stopOnFailure="false"
10+
processIsolation="false"
11+
backupGlobals="false"
12+
syntaxCheck="true"
13+
>
14+
<testsuites>
15+
<testsuite name="Tests">
16+
<directory>./tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist addUncoveredFilesFromWhitelist="true">
21+
<directory suffix=".php">./src</directory>
22+
<exclude>
23+
<directory>./vendor</directory>
24+
<directory>./tests</directory>
25+
</exclude>
26+
</whitelist>
27+
</filter>
28+
</phpunit>

src/OpenIDConnectClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ protected function verifyJWTclaims($claims, $accessToken = null) {
10261026
}
10271027
return (($this->issuerValidator->__invoke($claims->iss))
10281028
&& (($claims->aud === $this->clientID) || in_array($this->clientID, $claims->aud, true))
1029-
&& ($claims->nonce === $this->getNonce())
1029+
&& (!isset($claims->nonce) || $claims->nonce === $this->getNonce())
10301030
&& ( !isset($claims->exp) || ((gettype($claims->exp) === 'integer') && ($claims->exp >= time() - $this->leeway)))
10311031
&& ( !isset($claims->nbf) || ((gettype($claims->nbf) === 'integer') && ($claims->nbf <= time() + $this->leeway)))
10321032
&& ( !isset($claims->at_hash) || !isset($accessToken) || $claims->at_hash === $expected_at_hash )

tests/OpenIDConnectClientTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Jumbojett\OpenIDConnectClient;
4+
use Jumbojett\OpenIDConnectClientException;
45

56
class OpenIDConnectClientTest extends PHPUnit_Framework_TestCase
67
{
@@ -17,4 +18,39 @@ public function testGetRedirectURL()
1718
$_SERVER['REQUEST_URI'] = '/path/index.php?foo=bar&baz#fragment';
1819
self::assertSame('http://domain.test/path/index.php', $client->getRedirectURL());
1920
}
21+
22+
public function testAuthenticateDoesNotThrowExceptionIfClaimsIsMissingNonce()
23+
{
24+
$fakeClaims = new \StdClass();
25+
$fakeClaims->iss = 'fake-issuer';
26+
$fakeClaims->aud = 'fake-client-id';
27+
$fakeClaims->nonce = null;
28+
29+
$_REQUEST['id_token'] = 'abc.123.xyz';
30+
$_REQUEST['state'] = false;
31+
$_SESSION['openid_connect_state'] = false;
32+
33+
/** @var OpenIDConnectClient | PHPUnit_Framework_MockObject_MockObject $client */
34+
$client = $this->getMockBuilder(OpenIDConnectClient::class)->setMethods(['decodeJWT', 'getProviderConfigValue', 'verifyJWTsignature'])->getMock();
35+
$client->method('decodeJWT')->willReturn($fakeClaims);
36+
$client->method('getProviderConfigValue')->with('jwks_uri')->willReturn(true);
37+
$client->method('verifyJWTsignature')->willReturn(true);
38+
39+
$client->setClientID('fake-client-id');
40+
$client->setIssuer('fake-issuer');
41+
$client->setIssuerValidator(function() {
42+
return true;
43+
});
44+
$client->setAllowImplicitFlow(true);
45+
$client->setProviderURL('https://jwt.io/');
46+
47+
try {
48+
$authenticated = $client->authenticate();
49+
$this->assertTrue($authenticated);
50+
} catch ( OpenIDConnectClientException $e ) {
51+
if ( $e->getMessage() === 'Unable to verify JWT claims' ) {
52+
self::fail( 'OpenIDConnectClientException was thrown when it should not have been.' );
53+
}
54+
}
55+
}
2056
}

0 commit comments

Comments
 (0)