Skip to content

Commit d06276a

Browse files
authored
Add PHP 7.2 support for IAP (GoogleCloudPlatform#544)
1 parent 5f849a3 commit d06276a

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

iap/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"require": {
33
"symfony/console": "^2.8",
44
"google/auth":"^1.2",
5-
"lcobucci/jwt": "^3.2",
6-
"mdanter/ecc":"^0.3.2"
5+
"spomky-labs/jose": "^6.1|^7.0"
76
},
87
"autoload": {
98
"psr-4": {

iap/src/validate_jwt.php

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

2626
# Imports OAuth Guzzle HTTP libraries.
27-
use GuzzleHttp\Client;
28-
use Lcobucci\JWT\Parser;
29-
use Lcobucci\JWT\ValidationData;
30-
use Lcobucci\JWT\Signer\Ecdsa\Sha256;
27+
use Jose\Factory\JWKFactory;
28+
use Jose\Loader;
3129

3230
/**
3331
* Validate a JWT passed to your App Engine app by Identity-Aware Proxy.
@@ -76,27 +74,22 @@ function validate_jwt_from_compute_engine($iap_jwt, $cloud_project_number, $back
7674

7775
function validate_jwt($iap_jwt, $expected_audience)
7876
{
79-
// Validate the algorithm and kid headers. Also fetch the public key using the kid.
80-
$token = (new Parser())->parse((string) $iap_jwt); // Parses from a string
81-
$algorithm = $token->getHeader('alg');
82-
assert($algorithm =='ES256');
83-
$kid = $token->getHeader('kid');
84-
$client = new Client(['base_uri' => 'https://www.gstatic.com/']);
85-
$response = $client->request('GET', 'iap/verify/public_key');
86-
$body_content = json_decode((string) $response->getBody());
87-
$public_key = $body_content->$kid;
77+
// Create a JWK Key Set from the gstatic URL
78+
$jwk_set = JWKFactory::createFromJKU('https://www.gstatic.com/iap/verify/public_key-jwk');
8879

89-
// Validate token by checking issuer and audience fields. The JWT library automatically checks the time constraints.
90-
$data = new ValidationData();
91-
$data->setIssuer('https://cloud.google.com/iap');
92-
$data->setAudience($expected_audience);
93-
assert($token->validate($data));
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+
);
9487

95-
// Verify the signature using the JWT library.
96-
$signer = new Sha256();
97-
assert($token->verify($signer, $public_key));
88+
// 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);
9891

9992
// Return the user identity (subject and user email) if JWT verification is successful.
100-
return array('sub' => $token->getClaim('sub'), 'email' => $token->getClaim('email'));
93+
return array('sub' => $jws->getClaim('sub'), 'email' => $jws->getClaim('email'));
10194
}
10295
# [END validate_jwt]

0 commit comments

Comments
 (0)