Skip to content

Commit aaed057

Browse files
authored
fix: updates IAP samples for new IAP service URL (GoogleCloudPlatform#1109)
1 parent 20b907a commit aaed057

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

.kokoro/secrets.sh.enc

15 Bytes
Binary file not shown.

iap/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ To run the Cloud Identity Aware Proxy Samples:
4444

4545
Available commands:
4646
request Make a request to an IAP-protected resource using a service account.
47-
validate Make a request to an IAP-protected resource using a service account and then validate the JWT.
47+
validate Validates the JWT in the X-Goog-Iap-Jwt-Assertion header of an IAP-protected resource.
4848

4949
### Run Request
5050

@@ -56,7 +56,7 @@ To run the Request sample:
5656

5757
To run the Analyze Sentiment sample:
5858

59-
$ php iap.php validate [YOUR_CLOUD_IAP_URL] [YOUR_CLIENT_ID] [PATH_TO_YOUR_SERVICE_ACCOUNT] [YOUR_PROJECT_NUMBER] [YOUR_PROJECT_ID]
59+
$ php iap.php validate [YOUR_IAP_JWT] [YOUR_PROJECT_NUMBER] [YOUR_PROJECT_ID]
6060

6161
[iap]: http://cloud.google.com/iap
6262
[iap-quickstart]: https://cloud.google.com/iap/docs/app-engine-quickstart

iap/iap.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,19 @@
5151

5252
// Create a validate Command.
5353
$application->add((new Command('validate'))
54-
->addArgument('url', InputArgument::REQUIRED, 'The Identity-Aware Proxy-protected URL to fetch.')
55-
->addArgument('clientId', InputArgument::REQUIRED, 'The client ID used by Identity-Aware Proxy.')
56-
->addArgument('serviceAccountPath', InputArgument::REQUIRED, 'Path for the service account you want to use.')
54+
->addArgument('jwt', InputArgument::REQUIRED, 'A JWT from the X-Goog-Iap-Jwt-Assertion header')
5755
->addArgument('projectNumber', InputArgument::REQUIRED, 'The project *number* for your Google Cloud project. This is returned by gcloud projects describe $PROJECT_ID or in the Project Info card in Cloud Console.')
5856
->addArgument('projectId', InputArgument::REQUIRED, 'The project ID for your Google Cloud Platform project.')
59-
->setDescription('Makes a request to an IAP-protected resource using a service account and then validates the JWT.')
57+
->setDescription('Validates the JWT in the X-Goog-Iap-Jwt-Assertion header of an IAP-protected resource.')
6058
->setHelp(<<
6159
The %command.name% command makes a request to an IAP-protected resource and then validates the JWT.
6260
php %command.full_name%
6361
6462
EOF
6563
)
6664
->setCode(function ($input, $output) {
67-
$response = make_iap_request(
68-
$input->getArgument('url'),
69-
$input->getArgument('clientId'),
70-
$input->getArgument('serviceAccountPath'));
71-
$response_body = (string)$response->getBody();
72-
$iap_jwt = explode(': ', $response_body)[1];
7365
$user_identity = validate_jwt_from_app_engine(
74-
$iap_jwt,
66+
$input->getArgument('jwt'),
7567
$input->getArgument('projectNumber'),
7668
$input->getArgument('projectId'));
7769
print('Printing user identity information from ID token payload:');

iap/test/iapTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,33 @@ class iapTest extends TestCase
2929

3030
private static $commandFile = __DIR__ . '/../iap.php';
3131

32-
public function testRequest()
32+
public function testRequestAndValidate()
3333
{
34+
// Make a request to our IAP URL, which returns the IAP's JWT Assertion.
3435
$output = $this->runCommand('request', [
3536
'url' => $this->requireEnv('IAP_URL'),
3637
'clientId' => $this->requireEnv('IAP_CLIENT_ID'),
3738
'serviceAccountPath' => $this->requireEnv('GOOGLE_APPLICATION_CREDENTIALS'),
3839
]);
39-
$this->assertContains('x-goog-authenticated-user-jwt:', $output);
40-
}
4140

42-
public function testInvalidJwt()
43-
{
44-
validate_jwt('fake_jwt', 'fake_expected_audience');
45-
$this->expectOutputRegex('/Failed to validate JWT:/');
46-
}
41+
// Verify an ID token was returned
42+
$this->assertContains('Printing out response body:', $output);
43+
list($_, $iapJwt) = explode(':', $output);
4744

48-
public function testValidate()
49-
{
45+
// Now validate the JWT using the validation command
5046
$output = $this->runCommand('validate', [
51-
'url' => $this->requireEnv('IAP_URL'),
52-
'clientId' => $this->requireEnv('IAP_CLIENT_ID'),
53-
'serviceAccountPath' => $this->requireEnv('GOOGLE_APPLICATION_CREDENTIALS'),
47+
'jwt' => $iapJwt,
5448
'projectNumber' => $this->requireEnv('IAP_PROJECT_NUMBER'),
5549
'projectId' => $this->requireEnv('IAP_PROJECT_ID'),
5650
]);
5751
$this->assertContains('Printing user identity information from ID token payload:', $output);
5852
$this->assertContains('sub: accounts.google.com', $output);
5953
$this->assertContains('email:', $output);
6054
}
55+
56+
public function testInvalidJwt()
57+
{
58+
validate_jwt('fake_jwt', 'fake_expected_audience');
59+
$this->expectOutputRegex('/Failed to validate JWT:/');
60+
}
6161
}

0 commit comments

Comments
 (0)