Skip to content

Commit 3908a3e

Browse files
committed
chore: Create CLOUD_PLATFORM_SCOPE constant in Oauth2Utils
1 parent 7705a7b commit 3908a3e

11 files changed

+40
-37
lines changed

oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
import java.net.URI;
4949
import java.nio.charset.StandardCharsets;
5050
import java.util.ArrayList;
51-
import java.util.Arrays;
5251
import java.util.Collection;
52+
import java.util.Collections;
5353
import java.util.HashMap;
5454
import java.util.List;
5555
import java.util.Locale;
@@ -68,9 +68,6 @@ public abstract class ExternalAccountCredentials extends GoogleCredentials {
6868

6969
private static final long serialVersionUID = 8049126194174465023L;
7070

71-
private static final String CLOUD_PLATFORM_SCOPE =
72-
"https://www.googleapis.com/auth/cloud-platform";
73-
7471
static final String EXECUTABLE_SOURCE_KEY = "executable";
7572

7673
static final String DEFAULT_TOKEN_URL = "https://sts.{UNIVERSE_DOMAIN}/v1/token";
@@ -200,7 +197,9 @@ protected ExternalAccountCredentials(
200197
this.clientId = clientId;
201198
this.clientSecret = clientSecret;
202199
this.scopes =
203-
(scopes == null || scopes.isEmpty()) ? Arrays.asList(CLOUD_PLATFORM_SCOPE) : scopes;
200+
(scopes == null || scopes.isEmpty())
201+
? Collections.singletonList(OAuth2Utils.CLOUD_PLATFORM_SCOPE)
202+
: scopes;
204203
this.environmentProvider =
205204
environmentProvider == null ? SystemEnvironmentProvider.getInstance() : environmentProvider;
206205
this.workforcePoolUserProject = null;
@@ -245,7 +244,7 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)
245244

246245
this.scopes =
247246
(builder.scopes == null || builder.scopes.isEmpty())
248-
? Arrays.asList(CLOUD_PLATFORM_SCOPE)
247+
? Collections.singletonList(OAuth2Utils.CLOUD_PLATFORM_SCOPE)
249248
: builder.scopes;
250249
this.environmentProvider =
251250
builder.environmentProvider == null

oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,12 @@ public class ImpersonatedCredentials extends GoogleCredentials
101101
private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ssX";
102102
private static final int TWELVE_HOURS_IN_SECONDS = 43200;
103103
private static final int DEFAULT_LIFETIME_IN_SECONDS = 3600;
104-
private static final String CLOUD_PLATFORM_SCOPE =
105-
"https://www.googleapis.com/auth/cloud-platform";
106104
private GoogleCredentials sourceCredentials;
107-
private String targetPrincipal;
105+
private final String targetPrincipal;
108106
private List<String> delegates;
109107
private final List<String> scopes;
110-
private int lifetime;
111-
private String iamEndpointOverride;
108+
private final int lifetime;
109+
private final String iamEndpointOverride;
112110
private final String transportFactoryClassName;
113111
private static final LoggerProvider LOGGER_PROVIDER =
114112
LoggerProvider.forClazz(ImpersonatedCredentials.class);
@@ -395,7 +393,7 @@ static ImpersonatedCredentials fromJson(
395393
// This applies to the scopes applied for the impersonated token and not the
396394
// underlying source credential. Default to empty list to keep the existing
397395
// behavior (when json file did not populate a scopes field).
398-
List<String> scopes = new ArrayList<>();
396+
List<String> scopes = ImmutableList.of();
399397
try {
400398
serviceAccountImpersonationUrl = (String) json.get("service_account_impersonation_url");
401399
if (json.containsKey("delegates")) {
@@ -406,7 +404,7 @@ static ImpersonatedCredentials fromJson(
406404
quotaProjectId = (String) json.get("quota_project_id");
407405
targetPrincipal = extractTargetPrincipal(serviceAccountImpersonationUrl);
408406
if (json.containsKey("scopes")) {
409-
scopes = (List<String>) json.get("scopes");
407+
scopes = ImmutableList.copyOf((List<String>) json.get("scopes"));
410408
}
411409
} catch (ClassCastException | NullPointerException | IllegalArgumentException e) {
412410
throw new CredentialFormatException("An invalid input stream was provided.", e);
@@ -415,7 +413,9 @@ static ImpersonatedCredentials fromJson(
415413
GoogleCredentials sourceCredentials;
416414
if (GoogleCredentialsInfo.USER_CREDENTIALS.getFileType().equals(sourceCredentialsType)) {
417415
sourceCredentials = UserCredentials.fromJson(sourceCredentialsJson, transportFactory);
418-
} else if (GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS.getFileType().equals(sourceCredentialsType)) {
416+
} else if (GoogleCredentialsInfo.SERVICE_ACCOUNT_CREDENTIALS
417+
.getFileType()
418+
.equals(sourceCredentialsType)) {
419419
sourceCredentials =
420420
ServiceAccountCredentials.fromJson(sourceCredentialsJson, transportFactory);
421421
} else {
@@ -443,7 +443,7 @@ public boolean createScopedRequired() {
443443

444444
@Override
445445
public GoogleCredentials createScoped(Collection<String> scopes) {
446-
return toBuilder().setScopes(new ArrayList<>(scopes)).setAccessToken(null).build();
446+
return toBuilder().setScopes(ImmutableList.copyOf(scopes)).setAccessToken(null).build();
447447
}
448448

449449
@Override
@@ -520,8 +520,10 @@ public String getUniverseDomain() throws IOException {
520520
@Override
521521
public AccessToken refreshAccessToken() throws IOException {
522522
if (this.sourceCredentials.getAccessToken() == null) {
523+
// Apply the `CLOUD_PLATFORM_SCOPE` to access the iamcredentials endpoint
523524
this.sourceCredentials =
524-
this.sourceCredentials.createScoped(Collections.singletonList(CLOUD_PLATFORM_SCOPE));
525+
this.sourceCredentials.createScoped(
526+
Collections.singletonList(OAuth2Utils.CLOUD_PLATFORM_SCOPE));
525527
}
526528

527529
// skip for SA with SSJ flow because it uses self-signed JWT
@@ -555,7 +557,7 @@ public AccessToken refreshAccessToken() throws IOException {
555557
GenericUrl url = new GenericUrl(endpointUrl);
556558

557559
Map<String, Object> body =
558-
ImmutableMap.<String, Object>of(
560+
ImmutableMap.of(
559561
"delegates", this.delegates, "scope", this.scopes, "lifetime", this.lifetime + "s");
560562

561563
HttpContent requestContent = new JsonHttpContent(parser.getJsonFactory(), body);

oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public class OAuth2Utils {
9595
static final URI TOKEN_REVOKE_URI = URI.create("https://oauth2.googleapis.com/revoke");
9696
static final URI USER_AUTH_URI = URI.create("https://accounts.google.com/o/oauth2/auth");
9797

98+
public static final String CLOUD_PLATFORM_SCOPE =
99+
"https://www.googleapis.com/auth/cloud-platform";
100+
98101
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
99102

100103
public static final HttpTransportFactory HTTP_TRANSPORT_FACTORY =

oauth2_http/javatests/com/google/auth/oauth2/DownscopedCredentialsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void builder_noTransport_defaults() throws IOException {
236236
.build();
237237

238238
GoogleCredentials scopedSourceCredentials =
239-
sourceCredentials.createScoped("https://www.googleapis.com/auth/cloud-platform");
239+
sourceCredentials.createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
240240
assertEquals(scopedSourceCredentials, credentials.getSourceCredentials());
241241
assertEquals(CREDENTIAL_ACCESS_BOUNDARY, credentials.getCredentialAccessBoundary());
242242
assertEquals(OAuth2Utils.HTTP_TRANSPORT_FACTORY, credentials.getTransportFactory());
@@ -254,7 +254,7 @@ public void builder_noUniverseDomain_defaults() throws IOException {
254254
.build();
255255

256256
GoogleCredentials scopedSourceCredentials =
257-
sourceCredentials.createScoped("https://www.googleapis.com/auth/cloud-platform");
257+
sourceCredentials.createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
258258
assertEquals(OAuth2Utils.HTTP_TRANSPORT_FACTORY, credentials.getTransportFactory());
259259
assertEquals(scopedSourceCredentials, credentials.getSourceCredentials());
260260
assertEquals(CREDENTIAL_ACCESS_BOUNDARY, credentials.getCredentialAccessBoundary());
@@ -320,7 +320,7 @@ private static GoogleCredentials getServiceAccountSourceCredentials(boolean canR
320320
transportFactory.transport.setError(new IOException());
321321
}
322322

323-
return sourceCredentials.createScoped("https://www.googleapis.com/auth/cloud-platform");
323+
return sourceCredentials.createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
324324
}
325325

326326
private static GoogleCredentials getUserSourceCredentials() {

oauth2_http/javatests/com/google/auth/oauth2/ITDownscopingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public AccessToken refreshAccessToken() throws IOException {
102102
ServiceAccountCredentials credentials =
103103
(ServiceAccountCredentials)
104104
GoogleCredentials.getApplicationDefault()
105-
.createScoped("https://www.googleapis.com/auth/cloud-platform");
105+
.createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
106106

107107
DownscopedCredentials downscopedCredentials =
108108
DownscopedCredentials.newBuilder()

oauth2_http/javatests/com/google/auth/oauth2/ITWorkloadIdentityFederationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ private void callGcs(GoogleCredentials credentials) throws IOException {
408408
*/
409409
private String generateGoogleIdToken(String audience) throws IOException {
410410
GoogleCredentials googleCredentials =
411-
GoogleCredentials.getApplicationDefault()
412-
.createScoped("https://www.googleapis.com/auth/cloud-platform");
411+
GoogleCredentials.getApplicationDefault().createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
413412

414413
HttpCredentialsAdapter credentialsAdapter = new HttpCredentialsAdapter(googleCredentials);
415414
HttpRequestFactory requestFactory =

oauth2_http/javatests/com/google/auth/oauth2/MockExternalAccountCredentialsTransport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public class MockExternalAccountCredentialsTransport extends MockHttpTransport {
6262

6363
private static final String EXPECTED_GRANT_TYPE =
6464
"urn:ietf:params:oauth:grant-type:token-exchange";
65-
private static final String CLOUD_PLATFORM_SCOPE =
66-
"https://www.googleapis.com/auth/cloud-platform";
65+
private static final String CLOUD_PLATFORM_SCOPE = OAuth2Utils.CLOUD_PLATFORM_SCOPE;
6766
private static final String ISSUED_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token";
6867
private static final String AWS_CREDENTIALS_URL = "https://169.254.169.254";
6968
private static final String AWS_REGION_URL = "https://169.254.169.254/region";

oauth2_http/javatests/com/google/auth/oauth2/StsRequestHandlerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public final class StsRequestHandlerTest {
5656

5757
private static final String TOKEN_EXCHANGE_GRANT_TYPE =
5858
"urn:ietf:params:oauth:grant-type:token-exchange";
59-
private static final String CLOUD_PLATFORM_SCOPE =
60-
"https://www.googleapis.com/auth/cloud-platform";
59+
private static final String CLOUD_PLATFORM_SCOPE = OAuth2Utils.CLOUD_PLATFORM_SCOPE;
6160
private static final String DEFAULT_REQUESTED_TOKEN_TYPE =
6261
"urn:ietf:params:oauth:token-type:access_token";
6362
private static final String TOKEN_URL = "https://sts.googleapis.com/v1/token";

oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public void testGetTokenResponseFromAuthCodeExchange_workforceIdentityFederation
313313
UserAuthorizer authorizer =
314314
UserAuthorizer.newBuilder()
315315
.setClientId(CLIENT_ID)
316-
.setScopes(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"))
316+
.setScopes(Collections.singletonList(OAuth2Utils.CLOUD_PLATFORM_SCOPE))
317317
.setTokenServerUri(WORKFORCE_IDENTITY_FEDERATION_TOKEN_SERVER_URI)
318318
.setUserAuthUri(WORKFORCE_IDENTITY_FEDERATION_AUTH_URI)
319319
.setClientAuthenticationType(ClientAuthenticationType.CLIENT_SECRET_BASIC)
@@ -354,7 +354,7 @@ public void testGetTokenResponseFromAuthCodeExchange_workforceIdentityFederation
354354
UserAuthorizer authorizer =
355355
UserAuthorizer.newBuilder()
356356
.setClientId(CLIENT_ID)
357-
.setScopes(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"))
357+
.setScopes(Collections.singletonList(OAuth2Utils.CLOUD_PLATFORM_SCOPE))
358358
.setTokenServerUri(WORKFORCE_IDENTITY_FEDERATION_TOKEN_SERVER_URI)
359359
.setUserAuthUri(WORKFORCE_IDENTITY_FEDERATION_AUTH_URI)
360360
.setClientAuthenticationType(ClientAuthenticationType.NONE)

oauth2_http/javatests/com/google/auth/oauth2/functional/FTComputeEngineCredentialsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
import com.google.auth.oauth2.IdToken;
4545
import com.google.auth.oauth2.IdTokenCredentials;
4646
import com.google.auth.oauth2.IdTokenProvider;
47+
import com.google.auth.oauth2.OAuth2Utils;
4748
import org.junit.Test;
4849

4950
public final class FTComputeEngineCredentialsTest {
5051
private final String computeUrl =
5152
"https://compute.googleapis.com/compute/v1/projects/gcloud-devel/zones/us-central1-a/instances";
52-
private final String cloudPlatformScope = "https://www.googleapis.com/auth/cloud-platform";
5353

5454
@Test
5555
public void RefreshCredentials() throws Exception {
@@ -64,7 +64,7 @@ public void RefreshCredentials() throws Exception {
6464
@Test
6565
public void DefaultCredentials() throws Exception {
6666
final GoogleCredentials defaultCredential =
67-
GoogleCredentials.getApplicationDefault().createScoped(cloudPlatformScope);
67+
GoogleCredentials.getApplicationDefault().createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
6868

6969
AccessToken accessToken = defaultCredential.refreshAccessToken();
7070
assertNotNull(accessToken);

0 commit comments

Comments
 (0)