2929/**
3030 * Validate a JWT passed to your App Engine app by Identity-Aware Proxy.
3131 *
32- * @param string $iap_jwt The contents of the X-Goog-IAP-JWT-Assertion header.
33- * @param string $cloud_project_number The project *number* for your Google
32+ * @param string $iapJwt The contents of the X-Goog-IAP-JWT-Assertion header.
33+ * @param string $cloudProjectNumber The project *number* for your Google
3434 * Cloud project. This is returned by 'gcloud projects describe $PROJECT_ID',
3535 * or in the Project Info card in Cloud Console.
3636 * @param string $cloud_project Your Google Cloud Project ID.
3737 *
3838 * @return (user_id, user_email).
3939 */
40- function validate_jwt_from_app_engine ($ iap_jwt , $ cloud_project_number , $ cloud_project_id )
40+ function validate_jwt_from_app_engine ($ iapJwt , $ cloudProjectNumber , $ cloudProjectId )
4141{
42- $ expected_audience = sprintf (
42+ $ expectedAudience = sprintf (
4343 '/projects/%s/apps/%s ' ,
44- $ cloud_project_number ,
45- $ cloud_project_id
44+ $ cloudProjectNumber ,
45+ $ cloudProjectId
4646 );
47- return validate_jwt ($ iap_jwt , $ expected_audience );
47+ return validate_jwt ($ iapJwt , $ expectedAudience );
4848}
4949
5050/**
5151 * Validate a JWT passed to your Compute / Container Engine app by Identity-Aware Proxy.
5252 *
53- * @param string $iap_jwt The contents of the X-Goog-IAP-JWT-Assertion header.
54- * @param string $cloud_project_number The project *number* for your Google
53+ * @param string $iapJwt The contents of the X-Goog-IAP-JWT-Assertion header.
54+ * @param string $cloudProjectNumber The project *number* for your Google
5555 * Cloud project. This is returned by 'gcloud projects describe $PROJECT_ID',
5656 * or in the Project Info card in Cloud Console.
57- * @param string $backend_service_id The ID of the backend service used to access the
57+ * @param string $backendServiceId The ID of the backend service used to access the
5858 * application. See https://cloud.google.com/iap/docs/signed-headers-howto
5959 * for details on how to get this value.
60- *
61- * @return (user_id, user_email).
6260 */
63- function validate_jwt_from_compute_engine ($ iap_jwt , $ cloud_project_number , $ backend_service_id )
61+ function validate_jwt_from_compute_engine ($ iapJwt , $ cloudProjectNumber , $ backendServiceId )
6462{
65- $ expected_audience = sprintf (
63+ $ expectedAudience = sprintf (
6664 '/projects/%s/global/backendServices/%s ' ,
67- $ cloud_project_number ,
68- $ backend_service_id
65+ $ cloudProjectNumber ,
66+ $ backendServiceId
6967 );
70- return validate_jwt ($ iap_jwt , $ expected_audience );
68+ validate_jwt ($ iapJwt , $ expectedAudience );
7169}
7270
73-
74- function validate_jwt ($ iap_jwt , $ expected_audience )
71+ /**
72+ * Validate a JWT passed to your app by Identity-Aware Proxy.
73+ *
74+ * @param string $iapJwt The contents of the X-Goog-IAP-JWT-Assertion header.
75+ * @param string $expectedAudience The expected audience of the JWT with the following formats:
76+ * App Engine: /projects/{PROJECT_NUMBER}/apps/{PROJECT_ID}
77+ * Compute Engine: /projects/{PROJECT_NUMBER}/global/backendServices/{BACKEND_SERVICE_ID}
78+ */
79+ function validate_jwt ($ iapJwt , $ expectedAudience )
7580{
7681 // Validate the signature using the IAP cert URL.
7782 $ token = new AccessToken ();
78- $ jwt = $ token ->verify ($ iap_jwt , [
83+ $ jwt = $ token ->verify ($ iapJwt , [
7984 'certsLocation ' => AccessToken::IAP_CERT_URL
8085 ]);
8186
@@ -85,9 +90,14 @@ function validate_jwt($iap_jwt, $expected_audience)
8590
8691 // Validate token by checking issuer and audience fields.
8792 assert ($ jwt ['iss ' ] == 'https://cloud.google.com/iap ' );
88- assert ($ jwt ['aud ' ] == $ expected_audience );
93+ assert ($ jwt ['aud ' ] == $ expectedAudience );
8994
90- // Return the user identity (subject and user email) if JWT verification is successful.
91- return array ('sub ' => $ jwt ['sub ' ], 'email ' => $ jwt ['email ' ]);
95+
96+ print ('Printing user identity information from ID token payload: ' );
97+ printf ('sub: %s ' , $ jwt ['sub ' ]);
98+ printf ('email: %s ' , $ jwt ['email ' ]);
9299}
93100# [END iap_validate_jwt]
101+
102+ require_once __DIR__ . '/../../testing/sample_helpers.php ' ;
103+ \Google \Cloud \Samples \execute_sample (__FILE__ , __NAMESPACE__ , $ argv );
0 commit comments