Skip to content

Commit eb37091

Browse files
authored
updating audience from baseUrl to clientId (GoogleCloudPlatform#700)
1 parent 1336be3 commit eb37091

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

iap/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ It will be used to test both the authorization of an incoming request to an IAP
2626
- [Enable](https://cloud.google.com/iap/docs/app-engine-quickstart) Identity-Aware Proxy on the App Engine app.
2727
- Add the service account email to the Identity-Aware Proxy access list for the project.
2828
- Set the environment variable `IAP_PROTECTED_URL` to point to `https://your-project-id.appspot.com`
29+
- Set the environment variable `IAP_CLIENT_ID` to point to the [OAuth 2.0 Client ID](https://console.cloud.google.com/apis/credentials) of your IAP protected App Engine Application.
2930
- Run the integration test:
3031
```
3132
mvn -Dtest=com.example.iap.BuildAndVerifyIapRequestIT verify

iap/src/main/java/com/example/iap/BuildIapRequest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ public class BuildIapRequest {
5050

5151
private BuildIapRequest() {}
5252

53-
private static String getBaseUrl(URL url) throws Exception {
54-
String urlFilePath = url.getFile();
55-
int pathDelim = urlFilePath.lastIndexOf('/');
56-
String path = (pathDelim > 0) ? urlFilePath.substring(0, pathDelim) : "";
57-
return (url.getProtocol() + "://" + url.getHost() + path).trim();
58-
}
59-
6053
private static ServiceAccountCredentials getCredentials() throws Exception {
6154
GoogleCredentials credentials =
6255
GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(IAM_SCOPE));
@@ -67,7 +60,7 @@ private static ServiceAccountCredentials getCredentials() throws Exception {
6760
return (ServiceAccountCredentials) credentials;
6861
}
6962

70-
private static String getSignedJWToken(ServiceAccountCredentials credentials, String baseUrl)
63+
private static String getSignedJWToken(ServiceAccountCredentials credentials, String iapClientId)
7164
throws IOException {
7265
Instant now = Instant.now(clock);
7366
long expirationTime = now.getEpochSecond() + EXPIRATION_TIME_IN_SECONDS;
@@ -80,7 +73,7 @@ private static String getSignedJWToken(ServiceAccountCredentials credentials, St
8073
.setSubject(credentials.getClientEmail())
8174
.setIssuedAt(Date.from(now))
8275
.setExpiration(Date.from(Instant.ofEpochSecond(expirationTime)))
83-
.claim("target_audience", baseUrl)
76+
.claim("target_audience", iapClientId)
8477
.signWith(SignatureAlgorithm.RS256, credentials.getPrivateKey())
8578
.compact();
8679
}
@@ -105,16 +98,22 @@ private static String getGoogleIdToken(String jwt) throws Exception {
10598
return idToken;
10699
}
107100

108-
public static HttpRequest buildIAPRequest(HttpRequest request) throws Exception {
101+
/**
102+
* Clone request and add an IAP Bearer Authorization header with signed JWT token.
103+
* @param request Request to add authorization header
104+
* @param iapClientId OAuth 2.0 client ID for IAP protected resource
105+
* @return Clone of request with Bearer style authorization header with signed jwt token.
106+
* @throws Exception
107+
*/
108+
public static HttpRequest buildIAPRequest(HttpRequest request, String iapClientId) throws Exception {
109109
// get service account credentials
110110
ServiceAccountCredentials credentials = getCredentials();
111111
// get the base url of the request URL
112-
String baseUrl = getBaseUrl(request.getUrl().toURL());
113-
String jwt = getSignedJWToken(credentials, baseUrl);
112+
String jwt = getSignedJWToken(credentials, iapClientId);
114113
if (jwt == null) {
115114
throw new Exception(
116115
"Unable to create a signed jwt token for : "
117-
+ baseUrl
116+
+ iapClientId
118117
+ "with issuer : "
119118
+ credentials.getClientEmail());
120119
}

iap/src/test/java/com/example/iap/BuildAndVerifyIapRequestIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
public class BuildAndVerifyIapRequestIT {
3535

3636
private String iapProtectedUrl = System.getenv("IAP_PROTECTED_URL");
37+
private String iapClientId = System.getenv("IAP_CLIENT_ID");
3738
private HttpTransport httpTransport = new NetHttpTransport();
3839
private VerifyIapRequestHeader verifyIapRequestHeader = new VerifyIapRequestHeader();
3940

4041
@Before
4142
public void setUp() {
4243
assertNotNull(iapProtectedUrl);
44+
assertNotNull(iapClientId);
4345
}
4446

4547
// Access an IAP protected url without signed jwt authorization header
@@ -59,7 +61,7 @@ public void accessIapProtectedResourceFailsWithoutJwtHeader() throws Exception {
5961
public void testGenerateAndVerifyIapRequestIsSuccessful() throws Exception {
6062
HttpRequest request =
6163
httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(iapProtectedUrl));
62-
HttpRequest iapRequest = buildIAPRequest(request);
64+
HttpRequest iapRequest = buildIAPRequest(request, iapClientId);
6365
HttpResponse response = iapRequest.execute();
6466
assertEquals(response.getStatusCode(), HttpStatus.SC_OK);
6567
String headerWithtoken = response.parseAsString();

0 commit comments

Comments
 (0)