Skip to content

Commit a9337dd

Browse files
authored
fixes IAP validation (GoogleCloudPlatform#900)
1 parent 9c2305c commit a9337dd

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

iap/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"require": {
33
"symfony/console": "^2.8",
44
"google/auth":"^1.2",
5-
"spomky-labs/jose": "^6.1|^7.0"
5+
"guzzlehttp/guzzle": "^6.3",
6+
"kelvinmo/simplejwt": "^0.2.4"
67
},
78
"autoload": {
89
"psr-4": {

iap/src/validate_jwt.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
namespace Google\Cloud\Samples\Iap;
2525

2626
# Imports OAuth Guzzle HTTP libraries.
27-
use Jose\Factory\JWKFactory;
28-
use Jose\Loader;
27+
use GuzzleHttp\Client;
28+
# Imports libraries for JWK validation
29+
use SimpleJWT\JWT;
30+
use SimpleJWT\Keys\KeySet;
31+
use SimpleJWT\InvalidTokenException;
2932

3033
/**
3134
* Validate a JWT passed to your App Engine app by Identity-Aware Proxy.
@@ -74,22 +77,26 @@ function validate_jwt_from_compute_engine($iap_jwt, $cloud_project_number, $back
7477

7578
function validate_jwt($iap_jwt, $expected_audience)
7679
{
80+
// get the public key JWK Set object (RFC7517)
81+
$httpclient = new Client();
82+
$response = $httpclient->request('GET', 'https://www.gstatic.com/iap/verify/public_key-jwk', []);
83+
7784
// Create a JWK Key Set from the gstatic URL
78-
$jwk_set = JWKFactory::createFromJKU('https://www.gstatic.com/iap/verify/public_key-jwk');
85+
$jwkset = new KeySet();
86+
$jwkset->load((string) $response->getBody());
7987

80-
// Validate the signature using the key set and ES256 algorithm.
81-
$loader = new Loader();
82-
$jws = $loader->loadAndVerifySignatureUsingKeySet(
83-
$iap_jwt,
84-
$jwk_set,
85-
['ES256']
86-
);
8788

89+
// Validate the signature using the key set and ES256 algorithm.
90+
try {
91+
$jwt = JWT::decode($iap_jwt, $jwkset, 'ES256');
92+
} catch (InvalidTokenException $e) {
93+
return print("Failed to validate JWT: " . $e->getMessage() . PHP_EOL);
94+
}
8895
// Validate token by checking issuer and audience fields.
89-
assert($jws->getClaim('iss') == 'https://cloud.google.com/iap');
90-
assert($jws->getClaim('aud') == $expected_audience);
96+
assert($jwt->getClaim('iss') == 'https://cloud.google.com/iap');
97+
assert($jwt->getClaim('aud') == $expected_audience);
9198

9299
// Return the user identity (subject and user email) if JWT verification is successful.
93-
return array('sub' => $jws->getClaim('sub'), 'email' => $jws->getClaim('email'));
100+
return array('sub' => $jwt->getClaim('sub'), 'email' => $jwt->getClaim('email'));
94101
}
95102
# [END iap_validate_jwt]

iap/test/iapTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
namespace Google\Cloud\Samples\Iap\Tests;
17+
namespace Google\Cloud\Samples\Iap;
1818

1919
use Symfony\Component\Console\Tester\CommandTester;
2020

@@ -44,6 +44,12 @@ public function testRequest()
4444
$this->assertContains('x-goog-authenticated-user-jwt:', $output);
4545
}
4646

47+
public function testInvalidJwt()
48+
{
49+
validate_jwt('fake_jwt', 'fake_expected_audience');
50+
$this->expectOutputRegex('/Failed to validate JWT:/');
51+
}
52+
4753
public function testValidate()
4854
{
4955
if (version_compare(PHP_VERSION, '7.2.0') === 1) {

0 commit comments

Comments
 (0)