From bcf9304c23765477f676d7b31425f16dfd932912 Mon Sep 17 00:00:00 2001 From: arithmetic1728 Date: Thu, 8 Jul 2021 15:30:26 -0700 Subject: [PATCH] address comments --- .../auth/oauth2/ServiceAccountCredentials.java | 13 +++++-------- .../auth/oauth2/ServiceAccountCredentialsTest.java | 6 +++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java index e9a997811..573b4753a 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java @@ -985,14 +985,11 @@ public void getRequestMetadata( /** Provide the request metadata by putting an access JWT directly in the metadata. */ @Override public Map> getRequestMetadata(URI uri) throws IOException { - if (createScopedRequired()) { - if (!useJWTAccessWithScope) { - throw new IOException( - "Scopes are not configured for service account. Specify the scopes" - + " by calling createScoped or passing scopes to constructor."); - } else if (uri == null) { - throw new IOException("Scopes and uri are not configured for service account."); - } + if (scopes.isEmpty() && defaultScopes.isEmpty() && uri == null) { + throw new IOException( + "Scopes and uri are not configured for service account. Either pass uri" + + " to getRequestMetadata, or specify the scopes" + + " by calling createScoped or passing scopes to constructor."); } if (useJWTAccessWithScope) { JwtCredentials jwtCredentials = createSelfSignedJwtCredentials(uri); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java index 9438ca25f..c00977130 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java @@ -419,12 +419,12 @@ public void createdScoped_enablesAccessTokens() throws IOException { null); try { - credentials.getRequestMetadata(CALL_URI); - fail("Should not be able to get token without scopes"); + credentials.getRequestMetadata(null); + fail("Should not be able to get token without scopes, defaultScopes and uri"); } catch (IOException e) { assertTrue( "expected to fail with exception", - e.getMessage().contains("Scopes are not configured for service account")); + e.getMessage().contains("Scopes and uri are not configured for service account")); } GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);