Skip to content

Commit be89d07

Browse files
authored
feat: updates IAP using the new auth library (GoogleCloudPlatform#1019)
1 parent ae0acb9 commit be89d07

File tree

3 files changed

+25
-62
lines changed

3 files changed

+25
-62
lines changed

iap/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"require": {
33
"symfony/console": "^2.8",
4-
"google/auth":"^1.2",
5-
"guzzlehttp/guzzle": "^6.3",
4+
"google/auth":"^1.7.1",
5+
"guzzlehttp/guzzle": "~6.3.3",
66
"kelvinmo/simplejwt": "^0.2.4"
77
},
88
"autoload": {

iap/src/make_iap_request.php

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

2626
# Imports Auth libraries and Guzzle HTTP libraries.
27-
use Google\Auth\OAuth2;
28-
use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
27+
use Google\Auth\ApplicationDefaultCredentials;
2928
use GuzzleHttp\Client;
3029
use GuzzleHttp\HandlerStack;
3130

@@ -37,47 +36,20 @@
3736
*
3837
* @return The response body.
3938
*/
40-
function make_iap_request($url, $clientId, $pathToServiceAccount)
39+
function make_iap_request($url, $clientId)
4140
{
42-
$serviceAccountKey = json_decode(file_get_contents($pathToServiceAccount), true);
43-
$oauth_token_uri = 'https://www.googleapis.com/oauth2/v4/token';
44-
$iam_scope = 'https://www.googleapis.com/auth/iam';
45-
46-
# Create an OAuth object using the service account key
47-
$oauth = new OAuth2([
48-
'audience' => $oauth_token_uri,
49-
'issuer' => $serviceAccountKey['client_email'],
50-
'signingAlgorithm' => 'RS256',
51-
'signingKey' => $serviceAccountKey['private_key'],
52-
'tokenCredentialUri' => $oauth_token_uri,
53-
]);
54-
$oauth->setGrantType(OAuth2::JWT_URN);
55-
$oauth->setAdditionalClaims(['target_audience' => $clientId]);
56-
57-
# Obtain an OpenID Connect token, which is a JWT signed by Google.
58-
$token = $oauth->fetchAuthToken();
59-
$idToken = $oauth->getIdToken();
60-
61-
# Construct a ScopedAccessTokenMiddleware with the ID token.
62-
$middleware = new ScopedAccessTokenMiddleware(
63-
function () use ($idToken) {
64-
return $idToken;
65-
},
66-
$iam_scope
67-
);
68-
41+
// create middleware, using the client ID as the target audience for IAP
42+
$middleware = ApplicationDefaultCredentials::getIdTokenMiddleware($clientId);
6943
$stack = HandlerStack::create();
7044
$stack->push($middleware);
7145

72-
# Create an HTTP Client using Guzzle and pass in the credentials.
73-
$http_client = new Client([
74-
'handler' => $stack,
75-
'base_uri' => $url,
76-
'auth' => 'scoped'
46+
// create the HTTP client
47+
$client = new Client([
48+
'handler' => $stack,
49+
'auth' => 'google_auth'
7750
]);
7851

79-
# Make an authenticated HTTP Request
80-
$response = $http_client->request('GET', '/', []);
81-
return $response;
52+
// make the request
53+
return $client->get($url);
8254
}
8355
# [END iap_make_request]

iap/src/validate_jwt.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@
2323
# [START iap_validate_jwt]
2424
namespace Google\Cloud\Samples\Iap;
2525

26-
# Imports OAuth Guzzle HTTP libraries.
27-
use GuzzleHttp\Client;
28-
# Imports libraries for JWK validation
29-
use SimpleJWT\JWT;
30-
use SimpleJWT\Keys\KeySet;
31-
use SimpleJWT\InvalidTokenException;
26+
# Imports Google auth libraries for IAP validation
27+
use Google\Auth\AccessToken;
3228

3329
/**
3430
* Validate a JWT passed to your App Engine app by Identity-Aware Proxy.
@@ -77,26 +73,21 @@ function validate_jwt_from_compute_engine($iap_jwt, $cloud_project_number, $back
7773

7874
function validate_jwt($iap_jwt, $expected_audience)
7975
{
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', []);
76+
// Validate the signature using the IAP cert URL.
77+
$token = new AccessToken();
78+
$jwt = $token->verify($iap_jwt, [
79+
'certsLocation' => AccessToken::IAP_CERT_URL
80+
]);
8381

84-
// Create a JWK Key Set from the gstatic URL
85-
$jwkset = new KeySet();
86-
$jwkset->load((string) $response->getBody());
87-
88-
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);
82+
if (!$jwt) {
83+
return print('Failed to validate JWT: Invalid JWT');
9484
}
85+
9586
// Validate token by checking issuer and audience fields.
96-
assert($jwt->getClaim('iss') == 'https://cloud.google.com/iap');
97-
assert($jwt->getClaim('aud') == $expected_audience);
87+
assert($jwt['iss'] == 'https://cloud.google.com/iap');
88+
assert($jwt['aud'] == $expected_audience);
9889

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

0 commit comments

Comments
 (0)