|
31 | 31 |
|
32 | 32 | import java.io.IOException;
|
33 | 33 | import java.io.StringReader;
|
34 |
| -import java.net.URL; |
35 | 34 | import java.security.Key;
|
36 | 35 | import java.security.KeyFactory;
|
37 | 36 | import java.security.NoSuchAlgorithmException;
|
@@ -100,33 +99,43 @@ private Key resolveSigningKey(JwsHeader header) {
|
100 | 99 | }
|
101 | 100 | };
|
102 | 101 |
|
103 |
| - private static String getBaseUrl(URL url) throws Exception { |
104 |
| - String urlFilePath = url.getFile(); |
105 |
| - int pathDelim = urlFilePath.lastIndexOf('/'); |
106 |
| - String path = (pathDelim > 0) ? urlFilePath.substring(0, pathDelim) : ""; |
107 |
| - return (url.getProtocol() + "://" + url.getHost() + path).trim(); |
| 102 | + // Verify jwt tokens addressed to IAP protected resources on App Engine. |
| 103 | + // The project *number* for your Google Cloud project available via 'gcloud projects describe $PROJECT_ID' |
| 104 | + // or in the Project Info card in Cloud Console. |
| 105 | + // projectId is The project *ID* for your Google Cloud Project. |
| 106 | + Jwt verifyJWTTokenForAppEngine(HttpRequest request, long projectNumber, String projectId) throws Exception { |
| 107 | + // Check for iap jwt header in incoming request |
| 108 | + String jwtToken = |
| 109 | + request.getHeaders().getFirstHeaderStringValue("x-goog-iap-jwt-assertion"); |
| 110 | + if (jwtToken == null) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + return verifyJWTToken(jwtToken, String.format("/projects/%s/apps/%s", |
| 114 | + Long.toUnsignedString(projectNumber), |
| 115 | + projectId)); |
108 | 116 | }
|
109 | 117 |
|
110 |
| - Jwt verifyJWTToken(HttpRequest request) throws Exception { |
| 118 | + Jwt verifyJWTTokenForComputeEngine(HttpRequest request, long projectNumber, long backendServiceId) throws Exception { |
111 | 119 | // Check for iap jwt header in incoming request
|
112 | 120 | String jwtToken =
|
113 |
| - request.getHeaders().getFirstHeaderStringValue("x-goog-authenticated-user-jwt"); |
| 121 | + request.getHeaders().getFirstHeaderStringValue("x-goog-iap-jwt-assertion"); |
114 | 122 | if (jwtToken == null) {
|
115 | 123 | return null;
|
116 | 124 | }
|
117 |
| - String baseUrl = getBaseUrl(request.getUrl().toURL()); |
118 |
| - return verifyJWTToken(jwtToken, baseUrl); |
| 125 | + return verifyJWTToken(jwtToken, String.format("/projects/%s/global/backendServices/%s", |
| 126 | + Long.toUnsignedString(projectNumber), |
| 127 | + Long.toUnsignedString(backendServiceId))); |
119 | 128 | }
|
120 |
| - |
121 |
| - Jwt verifyJWTToken(String jwtToken, String baseUrl) throws Exception { |
| 129 | + |
| 130 | + Jwt verifyJWTToken(String jwtToken, String expectedAudience) throws Exception { |
122 | 131 | // Time constraints are automatically checked, use setAllowedClockSkewSeconds
|
123 | 132 | // to specify a leeway window
|
124 | 133 | // The token was issued in a past date "iat" < TODAY
|
125 | 134 | // The token hasn't expired yet "exp" > TODAY
|
126 | 135 | Jwt jwt =
|
127 | 136 | Jwts.parser()
|
128 | 137 | .setSigningKeyResolver(resolver)
|
129 |
| - .requireAudience(baseUrl) |
| 138 | + .requireAudience(expectedAudience) |
130 | 139 | .requireIssuer(IAP_ISSUER_URL)
|
131 | 140 | .parse(jwtToken);
|
132 | 141 | DefaultClaims claims = (DefaultClaims) jwt.getBody();
|
|
0 commit comments