Skip to content

Commit

Permalink
Merge branch 'master' into ae
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Dec 31, 2020
2 parents 45e5b25 + ec74870 commit c7565c7
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 62 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/formatting.yaml

This file was deleted.

18 changes: 9 additions & 9 deletions oauth2_http/java/com/google/auth/oauth2/ClientId.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public static ClientId of(String clientId, String clientSecret) {
/**
* Constructs a Client ID from JSON from a downloaded file.
*
* @param json The JSON from the downloaded file.
* @return the ClientId instance based on the JSON.
* @throws IOException The JSON could not be parsed.
* @param json the JSON from the downloaded file
* @return the ClientId instance based on the JSON
* @throws IOException the JSON could not be parsed
*/
public static ClientId fromJson(Map<String, Object> json) throws IOException {
Object rawDetail = null;
Expand Down Expand Up @@ -105,9 +105,9 @@ public static ClientId fromJson(Map<String, Object> json) throws IOException {
/**
* Constructs a Client ID from JSON file stored as a resource.
*
* @param relativeClass A class in the same namespace as the resource.
* @param resourceName The name of the resource
* @return The constructed ClientID instance based on the JSON in the resource.
* @param relativeClass a class in the same namespace as the resource
* @param resourceName the name of the resource
* @return the constructed ClientID instance based on the JSON in the resource
* @throws IOException The JSON could not be loaded or parsed.
*/
public static ClientId fromResource(Class<?> relativeClass, String resourceName)
Expand All @@ -119,9 +119,9 @@ public static ClientId fromResource(Class<?> relativeClass, String resourceName)
/**
* Constructs a Client ID from JSON file stream.
*
* @param stream Stream of the downloaded JSON file.
* @return The constructed ClientID instance based on the JSON in the stream.
* @throws IOException The JSON could not be read or parsed.
* @param stream the downloaded JSON file
* @return the constructed ClientID instance based on the JSON in the stream
* @throws IOException the JSON could not be read or parsed
*/
public static ClientId fromStream(InputStream stream) throws IOException {
Preconditions.checkNotNull(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class ImpersonatedCredentials extends GoogleCredentials
private static final long serialVersionUID = -2133257318957488431L;
private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static final int TWELVE_HOURS_IN_SECONDS = 43200;
private static final int DEFAULT_LIFETIME_IN_SECONDS = 3600;
private static final String CLOUD_PLATFORM_SCOPE =
"https://www.googleapis.com/auth/cloud-platform";
private static final String IAM_ACCESS_TOKEN_ENDPOINT =
Expand Down Expand Up @@ -120,7 +121,8 @@ public class ImpersonatedCredentials extends GoogleCredentials
* value should be at most 3600. However, you can follow <a
* href='https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials#sa-credentials-oauth'>these
* instructions</a> to set up the service account and extend the maximum lifetime to 43200 (12
* hours).
* hours). If the given lifetime is 0, default value 3600 will be used instead when creating
* the credentials.
* @param transportFactory HTTP transport factory that creates the transport used to get access
* tokens
* @return new credentials
Expand Down Expand Up @@ -159,6 +161,8 @@ public static ImpersonatedCredentials create(
* instructions</a> to set up the service account and extend the maximum lifetime to 43200 (12
* hours).
* https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials#sa-credentials-oauth
* If the given lifetime is 0, default value 3600 will be used instead when creating the
* credentials.
* @return new credentials
*/
public static ImpersonatedCredentials create(
Expand Down Expand Up @@ -186,6 +190,10 @@ public String getAccount() {
return this.targetPrincipal;
}

int getLifetime() {
return this.lifetime;
}

/**
* Signs the provided bytes using the private key associated with the impersonated service account
*
Expand Down Expand Up @@ -355,7 +363,7 @@ public static class Builder extends GoogleCredentials.Builder {
private String targetPrincipal;
private List<String> delegates;
private List<String> scopes;
private int lifetime;
private int lifetime = DEFAULT_LIFETIME_IN_SECONDS;
private HttpTransportFactory transportFactory;

protected Builder() {}
Expand Down Expand Up @@ -402,7 +410,7 @@ public List<String> getScopes() {
}

public Builder setLifetime(int lifetime) {
this.lifetime = lifetime;
this.lifetime = lifetime == 0 ? DEFAULT_LIFETIME_IN_SECONDS : lifetime;
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.auth.http.AuthHttpConstants;
import com.google.auth.http.HttpTransportFactory;
import com.google.common.io.ByteStreams;
Expand Down Expand Up @@ -66,7 +66,7 @@ class OAuth2Utils {

static final HttpTransportFactory HTTP_TRANSPORT_FACTORY = new DefaultHttpTransportFactory();

static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

static final Charset UTF_8 = Charset.forName("UTF-8");

Expand Down
4 changes: 2 additions & 2 deletions oauth2_http/javatests/com/google/auth/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.auth.http.AuthHttpConstants;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
Expand All @@ -54,7 +54,7 @@ public class TestUtils {

public static final String UTF_8 = "UTF-8";

private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

public static void assertContainsBearerToken(Map<String, List<String>> metadata, String token) {
assertNotNull(metadata);
Expand Down
10 changes: 2 additions & 8 deletions oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import com.google.api.client.json.GenericJson;
import com.google.auth.TestUtils;
Expand Down Expand Up @@ -156,7 +155,7 @@ public void fromStream() throws IOException {
}

@Test
public void fromStream_invalidJson_throws() {
public void fromStream_invalidJson_doesNotThrow() throws IOException {
String invalidJson =
"{"
+ "\"web\": {"
Expand All @@ -169,12 +168,7 @@ public void fromStream_invalidJson_throws() {
+ "}"; // No closing brace
InputStream stream = TestUtils.stringToInputStream(invalidJson);

try {
ClientId.fromStream(stream);
fail();
} catch (IOException expected) {
// Expected
}
ClientId.fromStream(stream);
}

private GenericJson writeClientIdJson(String type, String clientId, String clientSecret) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonGenerator;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebToken.Payload;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.util.Clock;
Expand Down Expand Up @@ -111,7 +111,7 @@ public class ImpersonatedCredentialsTest extends BaseSerializationTest {
private static final String ACCESS_TOKEN = "1/MkSJoj1xsli0AccessToken_NKPY2";
private static final int VALID_LIFETIME = 300;
private static final int INVALID_LIFETIME = 43210;
private static JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ss'Z'";

Expand Down Expand Up @@ -198,6 +198,15 @@ public void refreshAccessToken_malformedTarget() throws IOException {
}
}

@Test()
public void credential_with_zero_lifetime() throws IOException, IllegalStateException {
GoogleCredentials sourceCredentials = getSourceCredentials();
ImpersonatedCredentials targetCredentials =
ImpersonatedCredentials.create(
sourceCredentials, IMPERSONATED_CLIENT_EMAIL, null, SCOPES, 0);
assertEquals(3600, targetCredentials.getLifetime());
}

@Test()
public void credential_with_invalid_lifetime() throws IOException, IllegalStateException {

Expand Down Expand Up @@ -693,7 +702,7 @@ private String generateErrorJson(
int errorCode, String errorMessage, String errorDomain, String errorReason)
throws IOException {

JsonFactory factory = new JacksonFactory();
JsonFactory factory = new GsonFactory();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
JsonGenerator generator = factory.createJsonGenerator(bout, Charset.defaultCharset());
generator.enablePrettyPrint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import static org.junit.Assert.fail;

import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.util.Clock;
import com.google.auth.http.AuthHttpConstants;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class JwtCredentialsTest extends BaseSerializationTest {
+ "==\n-----END PRIVATE KEY-----\n";
private static final String JWT_ACCESS_PREFIX =
ServiceAccountJwtAccessCredentials.JWT_ACCESS_PREFIX;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

static PrivateKey getPrivateKey() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
Expand All @@ -54,7 +54,7 @@
public class MockTokenServerTransport extends MockHttpTransport {

static final String EXPECTED_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
static final JsonFactory JSON_FACTORY = new JacksonFactory();
static final JsonFactory JSON_FACTORY = new GsonFactory();
int buildRequestCount;
final Map<String, String> clients = new HashMap<String, String>();
final Map<String, String> refreshTokens = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import static org.junit.Assert.fail;

import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.util.Clock;
import com.google.auth.Credentials;
Expand Down Expand Up @@ -92,7 +92,7 @@ public class ServiceAccountJwtAccessCredentialsTest extends BaseSerializationTes
private static final String JWT_ACCESS_PREFIX =
ServiceAccountJwtAccessCredentials.JWT_ACCESS_PREFIX;
private static final URI CALL_URI = URI.create("http://googleapis.com/testapi/v1/foo");
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String QUOTA_PROJECT = "sample-quota-project-id";

@Test
Expand Down
2 changes: 1 addition & 1 deletion oauth2_http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<artifactId>google-http-client-gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
5 changes: 2 additions & 3 deletions synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"git": {
"name": ".",
"remote": "https://github.com/googleapis/google-auth-library-java.git",
"sha": "5a1d5c0f56c62311b646ffbdc2e39affa55d5c0c"
"sha": "5804ff03a531268831ac797ab262638a3119c14f"
}
},
{
"git": {
"name": "synthtool",
"remote": "https://github.com/googleapis/synthtool.git",
"sha": "3f67ceece7e797a5736a25488aae35405649b90b"
"sha": "6133907dbb3ddab204a17a15d5c53ec0aae9b033"
}
}
],
Expand All @@ -29,7 +29,6 @@
".github/trusted-contribution.yml",
".github/workflows/auto-release.yaml",
".github/workflows/ci.yaml",
".github/workflows/formatting.yaml",
".kokoro/build.bat",
".kokoro/build.sh",
".kokoro/coerce_logs.sh",
Expand Down

0 comments on commit c7565c7

Please sign in to comment.