Skip to content

Commit efe103a

Browse files
feat: add self signed jwt support (#572)
* feat: add self signed jwt support * update * chore: add more tests * update * update defaultscopes * update default scopes * update * update ComputeEngineCredentials * improve test coverage * update Co-authored-by: Brent Shaffer <betterbrent@google.com>
1 parent 497d4e7 commit efe103a

11 files changed

+537
-45
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,32 @@ class AppEngineCredentials extends GoogleCredentials implements ServiceAccountSi
7979
private transient Method getSignature;
8080
private transient String account;
8181

82-
AppEngineCredentials(Collection<String> scopes) throws IOException {
83-
this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes);
82+
AppEngineCredentials(Collection<String> scopes, Collection<String> defaultScopes)
83+
throws IOException {
84+
// Use defaultScopes only when scopes don't exist.
85+
if (scopes == null || scopes.isEmpty()) {
86+
this.scopes =
87+
defaultScopes == null ? ImmutableList.<String>of() : ImmutableList.copyOf(defaultScopes);
88+
} else {
89+
this.scopes = ImmutableList.copyOf(scopes);
90+
}
8491
this.scopesRequired = this.scopes.isEmpty();
8592
init();
8693
}
8794

88-
AppEngineCredentials(Collection<String> scopes, AppEngineCredentials unscoped) {
95+
AppEngineCredentials(
96+
Collection<String> scopes, Collection<String> defaultScopes, AppEngineCredentials unscoped) {
8997
this.appIdentityService = unscoped.appIdentityService;
9098
this.getAccessToken = unscoped.getAccessToken;
9199
this.getAccessTokenResult = unscoped.getAccessTokenResult;
92100
this.getExpirationTime = unscoped.getExpirationTime;
93-
this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes);
101+
// Use defaultScopes only when scopes don't exist.
102+
if (scopes == null || scopes.isEmpty()) {
103+
this.scopes =
104+
defaultScopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(defaultScopes);
105+
} else {
106+
this.scopes = ImmutableList.copyOf(scopes);
107+
}
94108
this.scopesRequired = this.scopes.isEmpty();
95109
}
96110

@@ -145,7 +159,13 @@ public boolean createScopedRequired() {
145159

146160
@Override
147161
public GoogleCredentials createScoped(Collection<String> scopes) {
148-
return new AppEngineCredentials(scopes, this);
162+
return new AppEngineCredentials(scopes, null, this);
163+
}
164+
165+
@Override
166+
public GoogleCredentials createScoped(
167+
Collection<String> scopes, Collection<String> defaultScopes) {
168+
return new AppEngineCredentials(scopes, defaultScopes, this);
149169
}
150170

151171
@Override

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,22 @@ public class ComputeEngineCredentials extends GoogleCredentials
109109
* @param transportFactory HTTP transport factory, creates the transport used to get access
110110
* tokens.
111111
* @param scopes scope strings for the APIs to be called. May be null or an empty collection.
112+
* @param defaultScopes default scope strings for the APIs to be called. May be null or an empty
113+
* collection. Default scopes are ignored if scopes are provided.
112114
*/
113115
private ComputeEngineCredentials(
114-
HttpTransportFactory transportFactory, Collection<String> scopes) {
116+
HttpTransportFactory transportFactory,
117+
Collection<String> scopes,
118+
Collection<String> defaultScopes) {
115119
this.transportFactory =
116120
firstNonNull(
117121
transportFactory,
118122
getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY));
119123
this.transportFactoryClassName = this.transportFactory.getClass().getName();
124+
// Use defaultScopes only when scopes don't exist.
125+
if (scopes == null || scopes.isEmpty()) {
126+
scopes = defaultScopes;
127+
}
120128
if (scopes == null) {
121129
this.scopes = ImmutableSet.<String>of();
122130
} else {
@@ -129,7 +137,14 @@ private ComputeEngineCredentials(
129137
/** Clones the compute engine account with the specified scopes. */
130138
@Override
131139
public GoogleCredentials createScoped(Collection<String> newScopes) {
132-
return new ComputeEngineCredentials(this.transportFactory, newScopes);
140+
return new ComputeEngineCredentials(this.transportFactory, newScopes, null);
141+
}
142+
143+
/** Clones the compute engine account with the specified scopes. */
144+
@Override
145+
public GoogleCredentials createScoped(
146+
Collection<String> newScopes, Collection<String> newDefaultScopes) {
147+
return new ComputeEngineCredentials(this.transportFactory, newScopes, newDefaultScopes);
133148
}
134149

135150
/**
@@ -138,7 +153,7 @@ public GoogleCredentials createScoped(Collection<String> newScopes) {
138153
* @return new ComputeEngineCredentials
139154
*/
140155
public static ComputeEngineCredentials create() {
141-
return new ComputeEngineCredentials(null, null);
156+
return new ComputeEngineCredentials(null, null, null);
142157
}
143158

144159
public final Collection<String> getScopes() {
@@ -465,7 +480,7 @@ public Collection<String> getScopes() {
465480
}
466481

467482
public ComputeEngineCredentials build() {
468-
return new ComputeEngineCredentials(transportFactory, scopes);
483+
return new ComputeEngineCredentials(transportFactory, scopes, null);
469484
}
470485
}
471486
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ private GoogleCredentials tryGetAppEngineCredential() throws IOException {
301301
if (!onAppEngine) {
302302
return null;
303303
}
304-
return new AppEngineCredentials(Collections.<String>emptyList());
304+
return new AppEngineCredentials(
305+
Collections.<String>emptyList(), Collections.<String>emptyList());
305306
}
306307

307308
private final GoogleCredentials tryGetComputeCredentials(HttpTransportFactory transportFactory) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ public GoogleCredentials createScoped(Collection<String> scopes) {
235235
return this;
236236
}
237237

238+
/**
239+
* If the credentials support scopes, creates a copy of the the identity with the specified scopes
240+
* and default scopes; otherwise, returns the same instance. This is mainly used by client
241+
* libraries.
242+
*
243+
* @param scopes Collection of scopes to request.
244+
* @param defaultScopes Collection of default scopes to request.
245+
* @return GoogleCredentials with requested scopes.
246+
*/
247+
public GoogleCredentials createScoped(
248+
Collection<String> scopes, Collection<String> defaultScopes) {
249+
return this;
250+
}
251+
238252
/**
239253
* If the credentials support scopes, creates a copy of the the identity with the specified
240254
* scopes; otherwise, returns the same instance.

0 commit comments

Comments
 (0)