Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public AccessToken refreshAccessToken() throws IOException {
OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX);
int expiresInSeconds =
OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX);
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000;
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000L;

return new AccessToken(accessToken, new Date(expiresAtMilliseconds));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public AccessToken refreshAccessToken() throws IOException {
OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX);
int expiresInSeconds =
OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX);
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000;
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000L;
String scopes =
OAuth2Utils.validateOptionalString(
responseData, OAuth2Utils.TOKEN_RESPONSE_SCOPE, PARSE_ERROR_PREFIX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,34 @@ void getRequestMetadata_shouldInvalidateAccessTokenWhenScoped_newAccessTokenFrom
TestUtils.assertNotContainsBearerToken(metadataForCopiedCredentials, ACCESS_TOKEN);
}

@Test
void getRequestMetadata_multipleCalls_usesCachedToken() throws IOException {
final int[] requestCount = new int[1];
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
transportFactory.transport =
new MockMetadataServerTransport(SCOPE_TO_ACCESS_TOKEN_MAP) {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
if (url.startsWith(ComputeEngineCredentials.getTokenServerEncodedUrl())) {
requestCount[0]++;
}
return super.buildRequest(method, url);
}
};
transportFactory.transport.setServiceAccountEmail(SA_CLIENT_EMAIL);

ComputeEngineCredentials credentials =
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();

Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
assertEquals(1, requestCount[0]);

Map<String, List<String>> metadata2 = credentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata2, ACCESS_TOKEN);
assertEquals(1, requestCount[0]);
}
Comment on lines +423 to +448

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a raw int[] array to track request counts across anonymous class boundaries is not thread-safe and can lead to race conditions if the transport is accessed concurrently. It is highly recommended to use AtomicInteger instead, which provides thread-safe atomic operations and cleaner syntax.

  void getRequestMetadata_multipleCalls_usesCachedToken() throws IOException {
    final java.util.concurrent.atomic.AtomicInteger requestCount = new java.util.concurrent.atomic.AtomicInteger(0);
    MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
    transportFactory.transport =
        new MockMetadataServerTransport(SCOPE_TO_ACCESS_TOKEN_MAP) {
          @Override
          public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            if (url.startsWith(ComputeEngineCredentials.getTokenServerEncodedUrl())) {
              requestCount.incrementAndGet();
            }
            return super.buildRequest(method, url);
          }
        };
    transportFactory.transport.setServiceAccountEmail(SA_CLIENT_EMAIL);

    ComputeEngineCredentials credentials =
        ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();

    Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
    TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
    assertEquals(1, requestCount.get());

    Map<String, List<String>> metadata2 = credentials.getRequestMetadata(CALL_URI);
    TestUtils.assertContainsBearerToken(metadata2, ACCESS_TOKEN);
    assertEquals(1, requestCount.get());
  }


@Test
void getRequestMetadata_missingServiceAccount_throws() {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public LowLevelHttpResponse execute() throws IOException {
refreshContents.put(
"access_token", scopesToAccessToken.get("[" + urlParsed.get(1) + "]"));
}
refreshContents.put("expires_in", 3600000);
refreshContents.put("expires_in", 3600);
refreshContents.put("token_type", "Bearer");
String refreshText = refreshContents.toPrettyString();

Expand Down
Loading