Skip to content
Merged
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 @@ -119,6 +119,8 @@ public class ServiceAccountCredentials extends GoogleCredentials

private transient HttpTransportFactory transportFactory;

private transient JwtCredentials selfSignedJwtCredentialsWithScope = null;

/**
* Internal constructor
*
Expand Down Expand Up @@ -731,6 +733,11 @@ public boolean getUseJwtAccessWithScope() {
return useJwtAccessWithScope;
}

@VisibleForTesting
JwtCredentials getSelfSignedJwtCredentialsWithScope() {
return selfSignedJwtCredentialsWithScope;
}

@Override
public String getAccount() {
return getClientEmail();
Expand Down Expand Up @@ -971,8 +978,11 @@ public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException
// Otherwise, use self signed JWT with uri as the audience.
JwtCredentials jwtCredentials;
if (!createScopedRequired() && useJwtAccessWithScope) {
// Create JWT credentials with the scopes.
jwtCredentials = createSelfSignedJwtCredentials(null);
// Create selfSignedJwtCredentialsWithScope when needed and reuse it for better performance.
if (selfSignedJwtCredentialsWithScope == null) {
selfSignedJwtCredentialsWithScope = createSelfSignedJwtCredentials(null);
}
jwtCredentials = selfSignedJwtCredentialsWithScope;
} else {
// Create JWT credentials with the uri as audience.
jwtCredentials = createSelfSignedJwtCredentials(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ void getRequestMetadata_selfSignedJWT_withScopes() throws IOException {
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
assertNotNull(((ServiceAccountCredentials) credentials).getSelfSignedJwtCredentialsWithScope());
verifyJwtAccess(metadata, "dummy.scope");
}

Expand Down Expand Up @@ -1472,6 +1473,7 @@ void getRequestMetadata_selfSignedJWT_withAudience() throws IOException {
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
assertNull(((ServiceAccountCredentials) credentials).getSelfSignedJwtCredentialsWithScope());
verifyJwtAccess(metadata, null);
}

Expand Down