Skip to content

Commit

Permalink
feat: add self signed jwt support (#572)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
arithmetic1728 and bshaffer authored Mar 16, 2021
1 parent 497d4e7 commit efe103a
Show file tree
Hide file tree
Showing 11 changed files with 537 additions and 45 deletions.
30 changes: 25 additions & 5 deletions oauth2_http/java/com/google/auth/oauth2/AppEngineCredentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,32 @@ class AppEngineCredentials extends GoogleCredentials implements ServiceAccountSi
private transient Method getSignature;
private transient String account;

AppEngineCredentials(Collection<String> scopes) throws IOException {
this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes);
AppEngineCredentials(Collection<String> scopes, Collection<String> defaultScopes)
throws IOException {
// Use defaultScopes only when scopes don't exist.
if (scopes == null || scopes.isEmpty()) {
this.scopes =
defaultScopes == null ? ImmutableList.<String>of() : ImmutableList.copyOf(defaultScopes);
} else {
this.scopes = ImmutableList.copyOf(scopes);
}
this.scopesRequired = this.scopes.isEmpty();
init();
}

AppEngineCredentials(Collection<String> scopes, AppEngineCredentials unscoped) {
AppEngineCredentials(
Collection<String> scopes, Collection<String> defaultScopes, AppEngineCredentials unscoped) {
this.appIdentityService = unscoped.appIdentityService;
this.getAccessToken = unscoped.getAccessToken;
this.getAccessTokenResult = unscoped.getAccessTokenResult;
this.getExpirationTime = unscoped.getExpirationTime;
this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes);
// Use defaultScopes only when scopes don't exist.
if (scopes == null || scopes.isEmpty()) {
this.scopes =
defaultScopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(defaultScopes);
} else {
this.scopes = ImmutableList.copyOf(scopes);
}
this.scopesRequired = this.scopes.isEmpty();
}

Expand Down Expand Up @@ -145,7 +159,13 @@ public boolean createScopedRequired() {

@Override
public GoogleCredentials createScoped(Collection<String> scopes) {
return new AppEngineCredentials(scopes, this);
return new AppEngineCredentials(scopes, null, this);
}

@Override
public GoogleCredentials createScoped(
Collection<String> scopes, Collection<String> defaultScopes) {
return new AppEngineCredentials(scopes, defaultScopes, this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,22 @@ public class ComputeEngineCredentials extends GoogleCredentials
* @param transportFactory HTTP transport factory, creates the transport used to get access
* tokens.
* @param scopes scope strings for the APIs to be called. May be null or an empty collection.
* @param defaultScopes default scope strings for the APIs to be called. May be null or an empty
* collection. Default scopes are ignored if scopes are provided.
*/
private ComputeEngineCredentials(
HttpTransportFactory transportFactory, Collection<String> scopes) {
HttpTransportFactory transportFactory,
Collection<String> scopes,
Collection<String> defaultScopes) {
this.transportFactory =
firstNonNull(
transportFactory,
getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY));
this.transportFactoryClassName = this.transportFactory.getClass().getName();
// Use defaultScopes only when scopes don't exist.
if (scopes == null || scopes.isEmpty()) {
scopes = defaultScopes;
}
if (scopes == null) {
this.scopes = ImmutableSet.<String>of();
} else {
Expand All @@ -129,7 +137,14 @@ private ComputeEngineCredentials(
/** Clones the compute engine account with the specified scopes. */
@Override
public GoogleCredentials createScoped(Collection<String> newScopes) {
return new ComputeEngineCredentials(this.transportFactory, newScopes);
return new ComputeEngineCredentials(this.transportFactory, newScopes, null);
}

/** Clones the compute engine account with the specified scopes. */
@Override
public GoogleCredentials createScoped(
Collection<String> newScopes, Collection<String> newDefaultScopes) {
return new ComputeEngineCredentials(this.transportFactory, newScopes, newDefaultScopes);
}

/**
Expand All @@ -138,7 +153,7 @@ public GoogleCredentials createScoped(Collection<String> newScopes) {
* @return new ComputeEngineCredentials
*/
public static ComputeEngineCredentials create() {
return new ComputeEngineCredentials(null, null);
return new ComputeEngineCredentials(null, null, null);
}

public final Collection<String> getScopes() {
Expand Down Expand Up @@ -465,7 +480,7 @@ public Collection<String> getScopes() {
}

public ComputeEngineCredentials build() {
return new ComputeEngineCredentials(transportFactory, scopes);
return new ComputeEngineCredentials(transportFactory, scopes, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ private GoogleCredentials tryGetAppEngineCredential() throws IOException {
if (!onAppEngine) {
return null;
}
return new AppEngineCredentials(Collections.<String>emptyList());
return new AppEngineCredentials(
Collections.<String>emptyList(), Collections.<String>emptyList());
}

private final GoogleCredentials tryGetComputeCredentials(HttpTransportFactory transportFactory) {
Expand Down
14 changes: 14 additions & 0 deletions oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ public GoogleCredentials createScoped(Collection<String> scopes) {
return this;
}

/**
* If the credentials support scopes, creates a copy of the the identity with the specified scopes
* and default scopes; otherwise, returns the same instance. This is mainly used by client
* libraries.
*
* @param scopes Collection of scopes to request.
* @param defaultScopes Collection of default scopes to request.
* @return GoogleCredentials with requested scopes.
*/
public GoogleCredentials createScoped(
Collection<String> scopes, Collection<String> defaultScopes) {
return this;
}

/**
* If the credentials support scopes, creates a copy of the the identity with the specified
* scopes; otherwise, returns the same instance.
Expand Down
Loading

0 comments on commit efe103a

Please sign in to comment.