Skip to content
Permalink
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>
  • Loading branch information
arithmetic1728 and bshaffer committed Mar 16, 2021
1 parent 497d4e7 commit efe103a2e688ca915ec9925a72c49bb2a1b3c3b5
@@ -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();
}

@@ -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
@@ -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 {
@@ -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);
}

/**
@@ -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() {
@@ -465,7 +480,7 @@ public Collection<String> getScopes() {
}

public ComputeEngineCredentials build() {
return new ComputeEngineCredentials(transportFactory, scopes);
return new ComputeEngineCredentials(transportFactory, scopes, null);
}
}
}
@@ -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) {
@@ -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.

0 comments on commit efe103a

Please sign in to comment.