diff --git a/.travis.yml b/.travis.yml index f69b4a881..28bc9310a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,6 @@ matrix: include: - os: linux jdk: oraclejdk8 - - os: linux - jdk: oraclejdk7 - os: linux jdk: openjdk7 - os: osx diff --git a/README.md b/README.md index 46a3eb7dd..62deb1701 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Open source authentication client library for Java. [![Build Status](https://travis-ci.org/google/google-auth-library-java.svg?branch=master)](https://travis-ci.org/google/google-auth-library-java.svg) [![Maven](https://img.shields.io/maven-central/v/com.google.auth/google-auth-library-credentials.svg)](https://img.shields.io/maven-central/v/com.google.auth/google-auth-library-credentials.svg) -- [API Documentation] (https://google.github.io/google-auth-library-java/releases/0.6.0/apidocs) +- [API Documentation] (https://google.github.io/google-auth-library-java/releases/0.7.1/apidocs) This project consists of 3 artifacts: @@ -30,16 +30,16 @@ If you are using Maven, add this to your pom.xml file (notice that you can repla com.google.auth google-auth-library-oauth2-http - 0.6.0 + 0.7.1 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.auth:google-auth-library-oauth2-http:0.6.0' +compile 'com.google.auth:google-auth-library-oauth2-http:0.7.1' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.auth" % "google-auth-library-oauth2-http" % "0.6.0" +libraryDependencies += "com.google.auth" % "google-auth-library-oauth2-http" % "0.7.1" ``` google-auth-library-credentials diff --git a/RELEASE.md b/RELEASE.md index a13153f64..fafb73c63 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -66,19 +66,21 @@ Versioning Minor changes should be a point increase (0.6.0 -> 0.6.1). Additions to API or breaking changes should be a major release. (0.6.0 -> 0.7.0) -Deploy to Sonatype ------------------- -* Update all ```pom.xml``` files in the package to the release version you want. Submit a pull request, get it reviewed, and submit +Prepare release +--------------- +* Update all ```pom.xml``` files in the package to the release version you want. +* Update version numbers appearing in `README.md`. +* Submit a pull request, get it reviewed, and submit. * ```mvn clean install deploy -DperformRelease=true``` -* Verify the result [here](https://oss.sonatype.org/#nexus-search;quick~com.google.auth) - * If there is a problem, undo by ```mvn nexus-staging:drop``` +* Verify the result [here](https://oss.sonatype.org/#nexus-search;quick~com.google.auth). + * If there is a problem, undo by ```mvn nexus-staging:drop```. * ```mvn nexus-staging:release -DperformRelease=true``` * On the [releases](https://github.com/google/google-auth-library-java/releases) page, create a corresponding Git tag (e.g., "v0.7.0") on the release commit, and summarize the commits since the last release. Follow the style of previous release notes. -* Update all ```pom.xml``` files to the new snapshot version (unless it's a bugfix release, we -update from 0.4.0 to 0.5.0-SNAPSHOT) +* Update Javadoc on Github using `update_javadoc.sh`. +* Update all ```pom.xml``` files to the new snapshot version (increment patch version number, e.g., from 0.4.0 to 0.4.1-SNAPSHOT). -Publish the release -------------------- +Publish release +--------------- * Go to [Sonatype](https://oss.sonatype.org/) and log in * Click on *Staging Repositories* on the left * Filter down to the repository by typing the package's groupId without periods in the search box diff --git a/appengine/java/com/google/auth/appengine/AppEngineCredentials.java b/appengine/java/com/google/auth/appengine/AppEngineCredentials.java index 548a028b1..5a83ed4d7 100644 --- a/appengine/java/com/google/auth/appengine/AppEngineCredentials.java +++ b/appengine/java/com/google/auth/appengine/AppEngineCredentials.java @@ -62,10 +62,12 @@ public class AppEngineCredentials extends GoogleCredentials implements ServiceAc private transient AppIdentityService appIdentityService; + @Deprecated public AppEngineCredentials(Collection scopes) { this(scopes, null); } + @Deprecated public AppEngineCredentials(Collection scopes, AppIdentityService appIdentityService) { this.scopes = scopes == null ? ImmutableSet.of() : ImmutableList.copyOf(scopes); this.appIdentityService = appIdentityService != null ? appIdentityService @@ -137,4 +139,47 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou input.defaultReadObject(); appIdentityService = newInstance(appIdentityServiceClassName); } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static class Builder extends GoogleCredentials.Builder { + + private Collection scopes; + private AppIdentityService appIdentityService; + + protected Builder() {} + + protected Builder(AppEngineCredentials credentials) { + this.scopes = credentials.scopes; + this.appIdentityService = credentials.appIdentityService; + } + + public Builder setScopes(Collection scopes) { + this.scopes = scopes; + return this; + } + + public Builder setAppIdentityService(AppIdentityService appIdentityService) { + this.appIdentityService = appIdentityService; + return this; + } + + public Collection getScopes() { + return scopes; + } + + public AppIdentityService getAppIdentityService() { + return appIdentityService; + } + + public AppEngineCredentials build() { + return new AppEngineCredentials(scopes, appIdentityService); + } + } } diff --git a/appengine/javatests/com/google/auth/appengine/AppEngineCredentialsTest.java b/appengine/javatests/com/google/auth/appengine/AppEngineCredentialsTest.java index 79147b730..52e2518dd 100644 --- a/appengine/javatests/com/google/auth/appengine/AppEngineCredentialsTest.java +++ b/appengine/javatests/com/google/auth/appengine/AppEngineCredentialsTest.java @@ -89,7 +89,10 @@ public void refreshAccessToken_sameAs() throws IOException { MockAppIdentityService appIdentity = new MockAppIdentityService(); appIdentity.setAccessTokenText(expectedAccessToken); appIdentity.setExpiration(new Date(System.currentTimeMillis() + 60L * 60L * 100L)); - AppEngineCredentials credentials = new AppEngineCredentials(SCOPES, appIdentity); + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(SCOPES) + .setAppIdentityService(appIdentity) + .build(); AccessToken accessToken = credentials.refreshAccessToken(); assertEquals(appIdentity.getAccessTokenText(), accessToken.getTokenValue()); assertEquals(appIdentity.getExpiration(), accessToken.getExpirationTime()); @@ -99,7 +102,10 @@ public void refreshAccessToken_sameAs() throws IOException { public void getAccount_sameAs() throws IOException { MockAppIdentityService appIdentity = new MockAppIdentityService(); appIdentity.setServiceAccountName(EXPECTED_ACCOUNT); - AppEngineCredentials credentials = new AppEngineCredentials(SCOPES, appIdentity); + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(SCOPES) + .setAppIdentityService(appIdentity) + .build(); assertEquals(EXPECTED_ACCOUNT, credentials.getAccount()); } @@ -108,7 +114,10 @@ public void sign_sameAs() throws IOException { byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD}; MockAppIdentityService appIdentity = new MockAppIdentityService(); appIdentity.setSignature(expectedSignature); - AppEngineCredentials credentials = new AppEngineCredentials(SCOPES, appIdentity); + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(SCOPES) + .setAppIdentityService(appIdentity) + .build(); assertArrayEquals(expectedSignature, credentials.sign(expectedSignature)); } @@ -120,8 +129,10 @@ public void createScoped_clonesWithScopes() throws IOException { MockAppIdentityService appIdentity = new MockAppIdentityService(); appIdentity.setAccessTokenText(expectedAccessToken); - GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity); - + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(emptyScopes) + .setAppIdentityService(appIdentity) + .build(); assertTrue(credentials.createScopedRequired()); try { credentials.getRequestMetadata(CALL_URI); @@ -143,8 +154,15 @@ public void createScoped_clonesWithScopes() throws IOException { public void equals_true() throws IOException { final Collection emptyScopes = Collections.emptyList(); MockAppIdentityService appIdentity = new MockAppIdentityService(); - GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity); - GoogleCredentials otherCredentials = new AppEngineCredentials(emptyScopes, appIdentity); + + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(emptyScopes) + .setAppIdentityService(appIdentity) + .build(); + AppEngineCredentials otherCredentials = AppEngineCredentials.newBuilder() + .setScopes(emptyScopes) + .setAppIdentityService(appIdentity) + .build(); assertTrue(credentials.equals(credentials)); assertTrue(credentials.equals(otherCredentials)); assertTrue(otherCredentials.equals(credentials)); @@ -155,8 +173,15 @@ public void equals_false_scopes() throws IOException { final Collection emptyScopes = Collections.emptyList(); final Collection scopes = Collections.singleton("SomeScope"); MockAppIdentityService appIdentity = new MockAppIdentityService(); - GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity); - GoogleCredentials otherCredentials = new AppEngineCredentials(scopes, appIdentity); + + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(emptyScopes) + .setAppIdentityService(appIdentity) + .build(); + AppEngineCredentials otherCredentials = AppEngineCredentials.newBuilder() + .setScopes(scopes) + .setAppIdentityService(appIdentity) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -170,7 +195,12 @@ public void toString_containsFields() throws IOException { MockAppIdentityService.class.getName()); final Collection scopes = Collections.singleton("SomeScope"); MockAppIdentityService appIdentity = new MockAppIdentityService(); - GoogleCredentials credentials = new AppEngineCredentials(scopes, appIdentity); + + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(scopes) + .setAppIdentityService(appIdentity) + .build(); + assertEquals(expectedToString, credentials.toString()); } @@ -178,8 +208,14 @@ public void toString_containsFields() throws IOException { public void hashCode_equals() throws IOException { final Collection emptyScopes = Collections.emptyList(); MockAppIdentityService appIdentity = new MockAppIdentityService(); - GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity); - GoogleCredentials otherCredentials = new AppEngineCredentials(emptyScopes, appIdentity); + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(emptyScopes) + .setAppIdentityService(appIdentity) + .build(); + AppEngineCredentials otherCredentials = AppEngineCredentials.newBuilder() + .setScopes(emptyScopes) + .setAppIdentityService(appIdentity) + .build(); assertEquals(credentials.hashCode(), otherCredentials.hashCode()); } @@ -187,7 +223,10 @@ public void hashCode_equals() throws IOException { public void serialize() throws IOException, ClassNotFoundException { final Collection scopes = Collections.singleton("SomeScope"); MockAppIdentityService appIdentity = new MockAppIdentityService(); - GoogleCredentials credentials = new AppEngineCredentials(scopes, appIdentity); + AppEngineCredentials credentials = AppEngineCredentials.newBuilder() + .setScopes(scopes) + .setAppIdentityService(appIdentity) + .build(); GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); diff --git a/appengine/pom.xml b/appengine/pom.xml index cb09231e9..fa033e1f6 100644 --- a/appengine/pom.xml +++ b/appengine/pom.xml @@ -5,7 +5,7 @@ com.google.auth google-auth-library-parent - 0.7.1 + 0.7.2-SNAPSHOT ../pom.xml diff --git a/credentials/pom.xml b/credentials/pom.xml index b71918678..281fca200 100644 --- a/credentials/pom.xml +++ b/credentials/pom.xml @@ -5,7 +5,7 @@ com.google.auth google-auth-library-parent - 0.7.1 + 0.7.2-SNAPSHOT ../pom.xml diff --git a/oauth2_http/java/com/google/auth/oauth2/ClientId.java b/oauth2_http/java/com/google/auth/oauth2/ClientId.java index 12e9fb2af..6d5913de6 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ClientId.java +++ b/oauth2_http/java/com/google/auth/oauth2/ClientId.java @@ -123,6 +123,7 @@ public static ClientId fromStream(InputStream stream) throws IOException { * @param clientId Text identifier of the Client ID. * @param clientSecret Secret to associated with the Client ID. */ + @Deprecated public ClientId(String clientId, String clientSecret) { this.clientId = Preconditions.checkNotNull(clientId); this.clientSecret = clientSecret; @@ -141,4 +142,44 @@ public final String getClientId() { public final String getClientSecret() { return clientSecret; } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static class Builder { + + private String clientId; + + private String clientSecret; + + protected Builder() {} + + protected Builder(ClientId clientId) { + this.clientId = clientId.getClientId(); + this.clientSecret = clientId.getClientSecret(); + } + + public Builder setClientId(String clientId) { + this.clientId = clientId; + return this; + } + + public Builder setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + return this; + } + + public String getClientSecret() { + return clientSecret; + } + + public ClientId build() { + return new ClientId(clientId, clientSecret); + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java b/oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java index 58bde6050..c90ca5237 100644 --- a/oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java @@ -63,6 +63,11 @@ public class CloudShellCredentials extends GoogleCredentials { private final int authPort; + public static CloudShellCredentials of(int authPort) { + return CloudShellCredentials.newBuilder().setAuthPort(authPort).build(); + } + + @Deprecated public CloudShellCredentials(int authPort) { this.authPort = authPort; } @@ -112,4 +117,35 @@ public boolean equals(Object obj) { CloudShellCredentials other = (CloudShellCredentials) obj; return this.authPort == other.authPort; } + + public Builder toBuilder() { + return new Builder(this); + } + + public static Builder newBuilder() { + return new Builder(); + } + + public static class Builder extends GoogleCredentials.Builder { + private int authPort; + + protected Builder() {} + + protected Builder(CloudShellCredentials credentials) { + this.authPort = credentials.authPort; + } + + public Builder setAuthPort(int authPort) { + this.authPort = authPort; + return this; + } + + public int getAuthPort() { + return authPort; + } + + public CloudShellCredentials build() { + return new CloudShellCredentials(authPort); + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java index 16e0fcc04..2a87c6912 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java @@ -82,9 +82,20 @@ public class ComputeEngineCredentials extends GoogleCredentials { private transient HttpTransportFactory transportFactory; + /** + * Returns a credentials instance from the given transport factory + * + * @param transportFactory The Http transport factory + * @return the credential instance + */ + public static ComputeEngineCredentials of(HttpTransportFactory transportFactory) { + return ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build(); + } + /** * Constructor with minimum information and default behavior. */ + @Deprecated public ComputeEngineCredentials() { this(null); } @@ -95,6 +106,7 @@ public ComputeEngineCredentials() { * @param transportFactory HTTP transport factory, creates the transport used to get access * tokens. */ + @Deprecated public ComputeEngineCredentials(HttpTransportFactory transportFactory) { this.transportFactory = firstNonNull(transportFactory, getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY)); @@ -225,4 +237,35 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou input.defaultReadObject(); transportFactory = newInstance(transportFactoryClassName); } + + public Builder toBuilder() { + return new Builder(this); + } + + public static Builder newBuilder() { + return new Builder(); + } + + public static class Builder extends GoogleCredentials.Builder { + private HttpTransportFactory transportFactory; + + protected Builder() {} + + protected Builder(ComputeEngineCredentials credentials) { + this.transportFactory = credentials.transportFactory; + } + + public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) { + this.transportFactory = transportFactory; + return this; + } + + public HttpTransportFactory getHttpTransportFactory() { + return transportFactory; + } + + public ComputeEngineCredentials build() { + return new ComputeEngineCredentials(transportFactory); + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java b/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java index a09cf56df..b4baa694b 100644 --- a/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java +++ b/oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java @@ -69,6 +69,9 @@ class DefaultCredentialsProvider { static final String CLOUD_SHELL_ENV_VAR = "DEVSHELL_CLIENT_PORT"; static final String SKIP_APP_ENGINE_ENV_VAR = "GOOGLE_APPLICATION_CREDENTIALS_SKIP_APP_ENGINE"; + static final String SPECIFICATION_VERSION = System.getProperty("java.specification.version"); + static final String GAE_RUNTIME_VERSION = System.getProperty("com.google.appengine.runtime.version"); + static final String RUNTIME_JETTY_LOGGER = System.getProperty("org.eclipse.jetty.util.log.class"); static final String NO_GCE_CHECK_ENV_VAR = "NO_GCE_CHECK"; static final String GCE_METADATA_HOST_ENV_VAR = "GCE_METADATA_HOST"; @@ -175,8 +178,8 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized( } } - // Then try App Engine - if (credentials == null && !skipAppEngineCredentialsCheck()) { + // Then try GAE 7 standard environment + if (credentials == null && isOnGAEStandard7() && !skipAppEngineCredentialsCheck()) { credentials = tryGetAppEngineCredential(); } @@ -186,7 +189,7 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized( credentials = tryGetCloudShellCredentials(); } - // Then try Compute Engine + // Then try Compute Engine and GAE 8 standard environment if (credentials == null) { credentials = tryGetComputeCredentials(transportFactory); } @@ -283,6 +286,11 @@ private boolean skipAppEngineCredentialsCheck() { return skip; } + protected boolean isOnGAEStandard7() { + return GAE_RUNTIME_VERSION != null + && (SPECIFICATION_VERSION.equals("1.7") || RUNTIME_JETTY_LOGGER == null); + } + /* * Start of methods to allow overriding in the test code to isolate from the environment. */ diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index caa927c07..bde04ba12 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -53,6 +53,16 @@ public class GoogleCredentials extends OAuth2Credentials { private static final DefaultCredentialsProvider defaultCredentialsProvider = new DefaultCredentialsProvider(); + /** + * Returns the credentials instance from the given access token. + * + * @param accessToken the access token + * @return the credentials instance + */ + public static GoogleCredentials of(AccessToken accessToken) { + return GoogleCredentials.newBuilder().setAccessToken(accessToken).build(); + } + /** * Returns the Application Default Credentials. * @@ -167,10 +177,19 @@ protected GoogleCredentials() { * * @param accessToken Initial or temporary access token. **/ + @Deprecated public GoogleCredentials(AccessToken accessToken) { super(accessToken); } + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + /** * Indicates whether the credentials require scopes to be specified via a call to * {link GoogleCredentials#createScoped} before use. @@ -195,4 +214,22 @@ public GoogleCredentials createScoped(Collection scopes) { public GoogleCredentials createDelegated(String user) { return this; } + + public static class Builder extends OAuth2Credentials.Builder { + protected Builder() {} + + protected Builder(GoogleCredentials credentials) { + setAccessToken(credentials.getAccessToken()); + } + + public GoogleCredentials build() { + return new GoogleCredentials(getAccessToken()); + } + + @Override + public Builder setAccessToken(AccessToken token) { + super.setAccessToken(token); + return this; + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java b/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java index 2a22bfdb4..f4c9c3910 100644 --- a/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java @@ -72,6 +72,16 @@ public class OAuth2Credentials extends Credentials { @VisibleForTesting transient Clock clock = Clock.SYSTEM; + /** + * Returns the credentials instance from the given access token. + * + * @param accessToken the access token + * @return the credentials instance + */ + public static OAuth2Credentials of(AccessToken accessToken) { + return OAuth2Credentials.newBuilder().setAccessToken(accessToken).build(); + } + /** * Default constructor. **/ @@ -84,6 +94,7 @@ protected OAuth2Credentials() { * * @param accessToken Initial or temporary access token. **/ + @Deprecated public OAuth2Credentials(AccessToken accessToken) { if (accessToken != null) { useAccessToken(accessToken); @@ -279,4 +290,36 @@ protected static T newInstance(String className) throws IOException, ClassNo protected static T getFromServiceLoader(Class clazz, T defaultInstance) { return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance); } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static class Builder { + + private AccessToken accessToken; + + protected Builder() {} + + protected Builder(OAuth2Credentials credentials) { + this.accessToken = credentials.getAccessToken(); + } + + public Builder setAccessToken(AccessToken token) { + this.accessToken = token; + return this; + } + + public AccessToken getAccessToken() { + return accessToken; + } + + public OAuth2Credentials build() { + return new OAuth2Credentials(accessToken); + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java index 7691586ae..90d6f20c8 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java @@ -93,6 +93,7 @@ public class ServiceAccountCredentials extends GoogleCredentials implements Serv private final PrivateKey privateKey; private final String privateKeyId; private final String serviceAccountUser; + private final String projectId; private final String transportFactoryClassName; private final URI tokenServerUri; private final Collection scopes; @@ -109,10 +110,11 @@ public class ServiceAccountCredentials extends GoogleCredentials implements Serv * @param scopes Scope strings for the APIs to be called. May be null or an empty collection, * which results in a credential that must have createScoped called before use. */ + @Deprecated public ServiceAccountCredentials( String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId, Collection scopes) { - this(clientId, clientEmail, privateKey, privateKeyId, scopes, null, null, null); + this(clientId, clientEmail, privateKey, privateKeyId, scopes, null, null, null, null); } /** @@ -128,10 +130,11 @@ public ServiceAccountCredentials( * tokens. * @param tokenServerUri URI of the end point that provides tokens. */ + @Deprecated public ServiceAccountCredentials( String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId, Collection scopes, HttpTransportFactory transportFactory, URI tokenServerUri) { - this(clientId, clientEmail, privateKey, privateKeyId, scopes, transportFactory, tokenServerUri, null); + this(clientId, clientEmail, privateKey, privateKeyId, scopes, transportFactory, tokenServerUri, null, null); } /** @@ -152,7 +155,7 @@ public ServiceAccountCredentials( ServiceAccountCredentials( String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId, Collection scopes, HttpTransportFactory transportFactory, URI tokenServerUri, - String serviceAccountUser) { + String serviceAccountUser, String projectId) { this.clientId = clientId; this.clientEmail = Preconditions.checkNotNull(clientEmail); this.privateKey = Preconditions.checkNotNull(privateKey); @@ -163,10 +166,11 @@ public ServiceAccountCredentials( this.transportFactoryClassName = this.transportFactory.getClass().getName(); this.tokenServerUri = (tokenServerUri == null) ? OAuth2Utils.TOKEN_SERVER_URI : tokenServerUri; this.serviceAccountUser = serviceAccountUser; + this.projectId = projectId; } /** - * Returns service account crentials defined by JSON using the format supported by the Google + * Returns service account credentials defined by JSON using the format supported by the Google * Developers Console. * * @param json a map from the JSON representing the credentials. @@ -181,6 +185,7 @@ static ServiceAccountCredentials fromJson( String clientEmail = (String) json.get("client_email"); String privateKeyPkcs8 = (String) json.get("private_key"); String privateKeyId = (String) json.get("private_key_id"); + String projectId = (String) json.get("project_id"); if (clientId == null || clientEmail == null || privateKeyPkcs8 == null || privateKeyId == null) { throw new IOException("Error reading service account credential from JSON, " @@ -188,11 +193,11 @@ static ServiceAccountCredentials fromJson( } return fromPkcs8(clientId, clientEmail, privateKeyPkcs8, privateKeyId, null, transportFactory, - null); + null, null, projectId); } /** - * Factory with miniumum identifying information using PKCS#8 for the private key. + * Factory with minimum identifying information using PKCS#8 for the private key. * * @param clientId Client ID of the service account from the console. May be null. * @param clientEmail Client email address of the service account from the console. @@ -208,7 +213,7 @@ public static ServiceAccountCredentials fromPkcs8( } /** - * Factory with miniumum identifying information and custom transport using PKCS#8 for the + * Factory with minimum identifying information and custom transport using PKCS#8 for the * private key. * * @param clientId Client ID of the service account from the console. May be null. @@ -229,7 +234,7 @@ public static ServiceAccountCredentials fromPkcs8( } /** - * Factory with miniumum identifying information and custom transport using PKCS#8 for the + * Factory with minimum identifying information and custom transport using PKCS#8 for the * private key. * * @param clientId Client ID of the service account from the console. May be null. @@ -249,9 +254,17 @@ public static ServiceAccountCredentials fromPkcs8( Collection scopes, HttpTransportFactory transportFactory, URI tokenServerUri, String serviceAccountUser) throws IOException { + return fromPkcs8(clientId, clientEmail, privateKeyPkcs8, privateKeyId, scopes, transportFactory, tokenServerUri, serviceAccountUser, null); + } + + static ServiceAccountCredentials fromPkcs8( + String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, + Collection scopes, HttpTransportFactory transportFactory, URI tokenServerUri, + String serviceAccountUser, String projectId) + throws IOException { PrivateKey privateKey = privateKeyFromPkcs8(privateKeyPkcs8); return new ServiceAccountCredentials( - clientId, clientEmail, privateKey, privateKeyId, scopes, transportFactory, tokenServerUri, serviceAccountUser); + clientId, clientEmail, privateKey, privateKeyId, scopes, transportFactory, tokenServerUri, serviceAccountUser, projectId); } /** @@ -372,7 +385,7 @@ public boolean isRequired(HttpResponse response) { responseData, "access_token", PARSE_ERROR_PREFIX); int expiresInSeconds = OAuth2Utils.validateInt32( responseData, "expires_in", PARSE_ERROR_PREFIX); - long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000; + long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000L; return new AccessToken(accessToken, new Date(expiresAtMilliseconds)); } @@ -392,13 +405,13 @@ public boolean createScopedRequired() { @Override public GoogleCredentials createScoped(Collection newScopes) { return new ServiceAccountCredentials(clientId, clientEmail, privateKey, privateKeyId, newScopes, - transportFactory, tokenServerUri, serviceAccountUser); + transportFactory, tokenServerUri, serviceAccountUser, projectId); } @Override public GoogleCredentials createDelegated(String user) { return new ServiceAccountCredentials(clientId, clientEmail, privateKey, privateKeyId, scopes, - transportFactory, tokenServerUri, user); + transportFactory, tokenServerUri, user, projectId); } public final String getClientId() { @@ -425,6 +438,10 @@ public final String getServiceAccountUser() { return serviceAccountUser; } + public final String getProjectId() { + return projectId; + } + @Override public String getAccount() { return getClientEmail(); @@ -500,4 +517,126 @@ String createAssertion(JsonFactory jsonFactory, long currentTime) throws IOExcep } return assertion; } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static class Builder extends GoogleCredentials.Builder { + + private String clientId; + private String clientEmail; + private PrivateKey privateKey; + private String privateKeyId; + private String serviceAccountUser; + private String projectId; + private URI tokenServerUri; + private Collection scopes; + private HttpTransportFactory transportFactory; + + protected Builder() {} + + protected Builder(ServiceAccountCredentials credentials) { + this.clientId = credentials.clientId; + this.clientEmail = credentials.clientEmail; + this.privateKey = credentials.privateKey; + this.privateKeyId = credentials.privateKeyId; + this.scopes = credentials.scopes; + this.transportFactory = credentials.transportFactory; + this.tokenServerUri = credentials.tokenServerUri; + this.serviceAccountUser = credentials.serviceAccountUser; + this.projectId = credentials.projectId; + } + + public Builder setClientId(String clientId) { + this.clientId = clientId; + return this; + } + + public Builder setClientEmail(String clientEmail) { + this.clientEmail = clientEmail; + return this; + } + + public Builder setPrivateKey(PrivateKey privateKey) { + this.privateKey = privateKey; + return this; + } + + public Builder setPrivateKeyId(String privateKeyId) { + this.privateKeyId = privateKeyId; + return this; + } + + public Builder setScopes(Collection scopes) { + this.scopes = scopes; + return this; + } + + public Builder setServiceAccountUser(String serviceAccountUser) { + this.serviceAccountUser = serviceAccountUser; + return this; + } + + public Builder setProjectId(String projectId) { + this.projectId = projectId; + return this; + } + + public Builder setTokenServerUri(URI tokenServerUri) { + this.tokenServerUri = tokenServerUri; + return this; + } + + public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) { + this.transportFactory = transportFactory; + return this; + } + + public String getClientId() { + return clientId; + } + + public String getClientEmail() { + return clientEmail; + } + + public PrivateKey getPrivateKey() { + return privateKey; + } + + public String getPrivateKeyId() { + return privateKeyId; + } + + public Collection getScopes() { + return scopes; + } + + public String getServiceAccountUser() { + return serviceAccountUser; + } + + public String getProjectId() { + return projectId; + } + + public URI getTokenServerUri() { + return tokenServerUri; + } + + public HttpTransportFactory getHttpTransportFactory() { + return transportFactory; + } + + public ServiceAccountCredentials build() { + return new ServiceAccountCredentials( + clientId, clientEmail, privateKey, privateKeyId, scopes, + transportFactory, tokenServerUri, serviceAccountUser, projectId); + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountJwtAccessCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountJwtAccessCredentials.java index bfe897ee1..b2c67ee5a 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ServiceAccountJwtAccessCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ServiceAccountJwtAccessCredentials.java @@ -92,6 +92,7 @@ public class ServiceAccountJwtAccessCredentials extends Credentials * @param privateKey RSA private key object for the service account. * @param privateKeyId Private key identifier for the service account. May be null. */ + @Deprecated public ServiceAccountJwtAccessCredentials( String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId) { this(clientId, clientEmail, privateKey, privateKeyId, null); @@ -106,6 +107,7 @@ public ServiceAccountJwtAccessCredentials( * @param privateKeyId Private key identifier for the service account. May be null. * @param defaultAudience Audience to use if not provided by transport. May be null. */ + @Deprecated public ServiceAccountJwtAccessCredentials(String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId, URI defaultAudience) { this.clientId = clientId; @@ -364,4 +366,76 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou input.defaultReadObject(); clock = Clock.SYSTEM; } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static class Builder { + + private String clientId; + private String clientEmail; + private PrivateKey privateKey; + private String privateKeyId; + private URI defaultAudience; + + protected Builder() {} + + protected Builder(ServiceAccountJwtAccessCredentials credentials) { + this.clientId = credentials.clientId; + this.clientEmail = credentials.clientEmail; + this.privateKey = credentials.privateKey; + this.privateKeyId = credentials.privateKeyId; + this.defaultAudience = credentials.defaultAudience; + } + + public Builder setClientId(String clientId) { + this.clientId = clientId; + return this; + } + + public Builder setClientEmail(String clientEmail) { + this.clientEmail = clientEmail; + return this; + } + + public Builder setPrivateKey(PrivateKey privateKey) { + this.privateKey = privateKey; + return this; + } + + public Builder setPrivateKeyId(String privateKeyId) { + this.privateKeyId = privateKeyId; + return this; + } + + public String getClientId() { + return clientId; + } + + public String getClientEmail() { + return clientEmail; + } + + public PrivateKey getPrivateKey() { + return privateKey; + } + + public String getPrivateKeyId() { + return privateKeyId; + } + + public URI getDefaultAudience() { + return defaultAudience; + } + + public ServiceAccountJwtAccessCredentials build() { + return new ServiceAccountJwtAccessCredentials( + clientId, clientEmail, privateKey, privateKeyId, defaultAudience); + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java index f1e495b5e..71aca49af 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java @@ -76,6 +76,7 @@ public class UserAuthorizer { * @param scopes OAUth2 scopes defining the user consent. * @param tokenStore Implementation of component for long term storage of tokens. */ + @Deprecated public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore tokenStore) { this(clientId, scopes, tokenStore, null, null, null, null); } @@ -88,8 +89,8 @@ public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore t * @param tokenStore Implementation of component for long term storage of tokens. * @param callbackUri URI for implementation of the OAuth2 web callback. */ - public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore tokenStore, - URI callbackUri) { + @Deprecated + public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore tokenStore, URI callbackUri) { this(clientId, scopes, tokenStore, callbackUri, null, null, null); } @@ -105,8 +106,9 @@ public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore t * @param tokenServerUri URI of the end point that provides tokens. * @param userAuthUri URI of the Web UI for user consent. */ + @Deprecated public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore tokenStore, - URI callbackUri, HttpTransportFactory transportFactory, URI tokenServerUri, URI userAuthUri) { + URI callbackUri, HttpTransportFactory transportFactory, URI tokenServerUri, URI userAuthUri) { this.clientId = Preconditions.checkNotNull(clientId); this.scopes = ImmutableList.copyOf(Preconditions.checkNotNull(scopes)); this.callbackUri = (callbackUri == null) ? DEFAULT_CALLBACK_URI : callbackUri; @@ -117,6 +119,7 @@ public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore t this.tokenStore = tokenStore; } + /** * Returns the Client ID user to identify the OAuth2 consent prompt. */ @@ -389,4 +392,103 @@ public void onChanged(OAuth2Credentials credentials) throws IOException { storeCredentials(userId, userCredentials); } } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static class Builder { + + private ClientId clientId; + private TokenStore tokenStore; + private URI callbackUri; + private URI tokenServerUri; + private URI userAuthUri; + private Collection scopes; + private HttpTransportFactory transportFactory; + + protected Builder() {} + + protected Builder(UserAuthorizer authorizer) { + this.clientId = authorizer.clientId; + this.scopes = authorizer.scopes; + this.transportFactory = authorizer.transportFactory; + this.tokenServerUri = authorizer.tokenServerUri; + this.tokenStore = authorizer.tokenStore; + this.callbackUri = authorizer.callbackUri; + this.userAuthUri = authorizer.userAuthUri; + } + + public Builder setClientId(ClientId clientId) { + this.clientId = clientId; + return this; + } + + public Builder setTokenStore(TokenStore tokenStore) { + this.tokenStore = tokenStore; + return this; + } + + public Builder setScopes(Collection scopes) { + this.scopes = scopes; + return this; + } + + public Builder setTokenServerUri(URI tokenServerUri) { + this.tokenServerUri = tokenServerUri; + return this; + } + + public Builder setCallbackUri(URI callbackUri) { + this.callbackUri = callbackUri; + return this; + } + + public Builder setUserAuthUri(URI userAuthUri) { + this.userAuthUri = userAuthUri; + return this; + } + + public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) { + this.transportFactory = transportFactory; + return this; + } + + public ClientId getClientId() { + return clientId; + } + + public TokenStore getTokenStore() { + return tokenStore; + } + + public Collection getScopes() { + return scopes; + } + + public URI getTokenServerUri() { + return tokenServerUri; + } + + public URI getCallbackUri() { + return callbackUri; + } + + public URI getUserAuthUri() { + return userAuthUri; + } + + public HttpTransportFactory getHttpTransportFactory() { + return transportFactory; + } + + public UserAuthorizer build() { + return new UserAuthorizer(clientId, scopes, tokenStore, + callbackUri, transportFactory, tokenServerUri, userAuthUri); + } + } } diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index 7293d4c70..851ddb8e7 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -77,6 +77,7 @@ public class UserCredentials extends GoogleCredentials { * @param clientSecret Client ID of the credential from the console. * @param refreshToken A refresh token resulting from a OAuth2 consent flow. */ + @Deprecated public UserCredentials(String clientId, String clientSecret, String refreshToken) { this(clientId, clientSecret, refreshToken, null, null, null); } @@ -89,6 +90,7 @@ public UserCredentials(String clientId, String clientSecret, String refreshToken * @param refreshToken A refresh token resulting from a OAuth2 consent flow. * @param accessToken Initial or temporary access token. */ + @Deprecated public UserCredentials( String clientId, String clientSecret, String refreshToken, AccessToken accessToken) { this(clientId, clientSecret, refreshToken, accessToken, null, null); @@ -106,8 +108,9 @@ public UserCredentials( * tokens. * @param tokenServerUri URI of the end point that provides tokens. */ + @Deprecated public UserCredentials(String clientId, String clientSecret, String refreshToken, - AccessToken accessToken, HttpTransportFactory transportFactory, URI tokenServerUri) { + AccessToken accessToken, HttpTransportFactory transportFactory, URI tokenServerUri) { super(accessToken); this.clientId = Preconditions.checkNotNull(clientId); this.clientSecret = Preconditions.checkNotNull(clientSecret); @@ -275,4 +278,86 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou input.defaultReadObject(); transportFactory = newInstance(transportFactoryClassName); } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static class Builder extends GoogleCredentials.Builder { + + private String clientId; + private String clientSecret; + private String refreshToken; + private URI tokenServerUri; + private HttpTransportFactory transportFactory; + + protected Builder() {} + + protected Builder(UserCredentials credentials) { + this.clientId = credentials.clientId; + this.clientSecret = credentials.clientSecret; + this.refreshToken = credentials.refreshToken; + this.transportFactory = credentials.transportFactory; + this.tokenServerUri = credentials.tokenServerUri; + } + + public Builder setClientId(String clientId) { + this.clientId = clientId; + return this; + } + + public Builder setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + return this; + } + + public Builder setRefreshToken(String refreshToken) { + this.refreshToken = refreshToken; + return this; + } + + public Builder setTokenServerUri(URI tokenServerUri) { + this.tokenServerUri = tokenServerUri; + return this; + } + + public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) { + this.transportFactory = transportFactory; + return this; + } + + public Builder setAccessToken(AccessToken token) { + super.setAccessToken(token); + return this; + } + + public String getClientId() { + return clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public String getRefreshToken() { + return refreshToken; + } + + public URI getTokenServerUri() { + return tokenServerUri; + } + + public HttpTransportFactory getHttpTransportFactory() { + return transportFactory; + } + + public UserCredentials build() { + return new UserCredentials( + clientId, clientSecret, refreshToken, getAccessToken(), transportFactory,tokenServerUri); + } + } } diff --git a/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java b/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java index 38c465280..f4bbeaee3 100644 --- a/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java +++ b/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java @@ -67,8 +67,14 @@ public void initialize_populatesOAuth2Credentials() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, null); + + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .build(); + HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials); HttpRequestFactory requestFactory = transportFactory.transport.createRequestFactory(); HttpRequest request = requestFactory.buildGetRequest(new GenericUrl("http://foo")); @@ -90,8 +96,13 @@ public void initialize_populatesOAuth2Credentials_handle401() throws IOException tokenServerTransportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); tokenServerTransportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, tokenServerTransportFactory, null); + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(tokenServerTransportFactory) + .build(); + credentials.refresh(); HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials); @@ -120,8 +131,14 @@ public void initialize_noURI() throws IOException { new MockTokenServerTransportFactory(); tokenServerTransportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); tokenServerTransportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, tokenServerTransportFactory, null); + + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(tokenServerTransportFactory) + .build(); + HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials); HttpRequestFactory requestFactory = tokenServerTransportFactory.transport.createRequestFactory(); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java index 48031a007..5576f9f02 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java @@ -55,7 +55,10 @@ public class ClientIdTest { @Test public void constructor() { - ClientId clientId = new ClientId(CLIENT_ID, CLIENT_SECRET); + ClientId clientId = ClientId.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .build(); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); @@ -63,13 +66,16 @@ public void constructor() { @Test(expected = NullPointerException.class) public void constructor_nullClientId_throws() { - new ClientId(null, CLIENT_SECRET); + ClientId clientId = ClientId.newBuilder() + .setClientSecret(CLIENT_SECRET) + .build(); } @Test public void constructor_nullClientSecret() { - ClientId clientId = new ClientId(CLIENT_ID, null); - + ClientId clientId = ClientId.newBuilder() + .setClientId(CLIENT_ID) + .build(); assertEquals(CLIENT_ID, clientId.getClientId()); assertNull(clientId.getClientSecret()); } diff --git a/oauth2_http/javatests/com/google/auth/oauth2/CloudShellCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/CloudShellCredentialsTest.java index a527afe1a..3ea2102d7 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/CloudShellCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/CloudShellCredentialsTest.java @@ -80,8 +80,10 @@ public void run() { }; Thread serverThread = new Thread(serverTask); serverThread.start(); - - GoogleCredentials creds = new CloudShellCredentials(authSocket.getLocalPort()); + + GoogleCredentials creds = CloudShellCredentials.newBuilder() + .setAuthPort(authSocket.getLocalPort()) + .build(); assertEquals("token", creds.refreshAccessToken().getTokenValue()); } finally { authSocket.close(); @@ -90,16 +92,24 @@ public void run() { @Test public void equals_true() throws IOException { - GoogleCredentials credentials = new CloudShellCredentials(42); - GoogleCredentials otherCredentials = new CloudShellCredentials(42); + GoogleCredentials credentials = CloudShellCredentials.newBuilder() + .setAuthPort(42) + .build(); + GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder() + .setAuthPort(42) + .build(); assertTrue(credentials.equals(otherCredentials)); assertTrue(otherCredentials.equals(credentials)); } @Test public void equals_false_authPort() throws IOException { - GoogleCredentials credentials = new CloudShellCredentials(42); - GoogleCredentials otherCredentials = new CloudShellCredentials(43); + GoogleCredentials credentials = CloudShellCredentials.newBuilder() + .setAuthPort(42) + .build(); + GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder() + .setAuthPort(43) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -107,20 +117,28 @@ public void equals_false_authPort() throws IOException { @Test public void toString_containsFields() throws IOException { String expectedToString = String.format("CloudShellCredentials{authPort=%d}", 42); - GoogleCredentials credentials = new CloudShellCredentials(42); + GoogleCredentials credentials = CloudShellCredentials.newBuilder() + .setAuthPort(42) + .build(); assertEquals(expectedToString, credentials.toString()); } @Test public void hashCode_equals() throws IOException { - GoogleCredentials credentials = new CloudShellCredentials(42); - GoogleCredentials otherCredentials = new CloudShellCredentials(42); + GoogleCredentials credentials = CloudShellCredentials.newBuilder() + .setAuthPort(42) + .build(); + GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder() + .setAuthPort(42) + .build(); assertEquals(credentials.hashCode(), otherCredentials.hashCode()); } @Test public void serialize() throws IOException, ClassNotFoundException { - GoogleCredentials credentials = new CloudShellCredentials(42); + GoogleCredentials credentials = CloudShellCredentials.newBuilder() + .setAuthPort(42) + .build(); GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java index 3a320395d..3b27a146c 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java @@ -76,8 +76,8 @@ public void getRequestMetadata_hasAccessToken() throws IOException { final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2"; MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory(); transportFactory.transport.setAccessToken(accessToken); - ComputeEngineCredentials credentials = new ComputeEngineCredentials(transportFactory); - + ComputeEngineCredentials credentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build(); Map> metadata = credentials.getRequestMetadata(CALL_URI); TestUtils.assertContainsBearerToken(metadata, accessToken); @@ -89,8 +89,8 @@ public void getRequestMetadata_missingServiceAccount_throws() { MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory(); transportFactory.transport.setAccessToken(accessToken); transportFactory.transport.setTokenRequestStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND); - ComputeEngineCredentials credentials = new ComputeEngineCredentials(transportFactory); - + ComputeEngineCredentials credentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build(); try { credentials.getRequestMetadata(CALL_URI); fail("Expected error refreshing token."); @@ -107,8 +107,8 @@ public void getRequestMetadata_serverError_throws() { MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory(); transportFactory.transport.setAccessToken(accessToken); transportFactory.transport.setTokenRequestStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND); - ComputeEngineCredentials credentials = new ComputeEngineCredentials(transportFactory); - + ComputeEngineCredentials credentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build(); try { credentials.getRequestMetadata(CALL_URI); fail("Expected error refreshing token."); @@ -133,8 +133,10 @@ public void equals_false_transportFactory() throws IOException { MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory(); MockMetadataServerTransportFactory serverTransportFactory = new MockMetadataServerTransportFactory(); - ComputeEngineCredentials credentials = new ComputeEngineCredentials(serverTransportFactory); - ComputeEngineCredentials otherCredentials = new ComputeEngineCredentials(httpTransportFactory); + ComputeEngineCredentials credentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(serverTransportFactory).build(); + ComputeEngineCredentials otherCredentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(httpTransportFactory).build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -146,7 +148,8 @@ public void toString_containsFields() throws IOException { String expectedToString = String.format("ComputeEngineCredentials{transportFactoryClassName=%s}", MockMetadataServerTransportFactory.class.getName()); - ComputeEngineCredentials credentials = new ComputeEngineCredentials(serverTransportFactory); + ComputeEngineCredentials credentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(serverTransportFactory).build(); assertEquals(expectedToString, credentials.toString()); } @@ -154,8 +157,8 @@ public void toString_containsFields() throws IOException { public void hashCode_equals() throws IOException { MockMetadataServerTransportFactory serverTransportFactory = new MockMetadataServerTransportFactory(); - ComputeEngineCredentials credentials = new ComputeEngineCredentials(serverTransportFactory); - ComputeEngineCredentials otherCredentials = + ComputeEngineCredentials credentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(serverTransportFactory).build(); ComputeEngineCredentials otherCredentials = new ComputeEngineCredentials(serverTransportFactory); assertEquals(credentials.hashCode(), otherCredentials.hashCode()); } @@ -164,13 +167,14 @@ public void hashCode_equals() throws IOException { public void serialize() throws IOException, ClassNotFoundException { MockMetadataServerTransportFactory serverTransportFactory = new MockMetadataServerTransportFactory(); - ComputeEngineCredentials credentials = new ComputeEngineCredentials(serverTransportFactory); + ComputeEngineCredentials credentials = + ComputeEngineCredentials.newBuilder().setHttpTransportFactory(serverTransportFactory).build(); GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); assertEquals(credentials.toString(), deserializedCredentials.toString()); assertSame(deserializedCredentials.clock, Clock.SYSTEM); - credentials = new ComputeEngineCredentials(); + credentials = ComputeEngineCredentials.newBuilder().build(); deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java b/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java index 35d0ed29c..cb8e5ebce 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java @@ -187,6 +187,7 @@ public void getDefaultCredentials_appEngineClassWithoutRuntime_NotFoundError() { TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider(); testProvider.addType(DefaultCredentialsProvider.APP_ENGINE_SIGNAL_CLASS, MockOffAppEngineSystemProperty.class); + testProvider.setProperty("isOnGAEStandard7", "true"); try { testProvider.getDefaultCredentials(transportFactory); @@ -203,6 +204,7 @@ public void getDefaultCredentials_appEngineRuntimeWithoutClass_throwsHelpfulLoad TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider(); testProvider.addType(DefaultCredentialsProvider.APP_ENGINE_SIGNAL_CLASS, MockAppEngineSystemProperty.class); + testProvider.setProperty("isOnGAEStandard7", "true"); try { testProvider.getDefaultCredentials(transportFactory); @@ -223,6 +225,7 @@ public void getDefaultCredentials_appEngineSkipWorks_retrievesCloudShellCredenti MockOffAppEngineSystemProperty.class); testProvider.setEnv(DefaultCredentialsProvider.CLOUD_SHELL_ENV_VAR,"9090"); testProvider.setEnv(DefaultCredentialsProvider.SKIP_APP_ENGINE_ENV_VAR, "true"); + testProvider.setProperty("isOnGAEStanadard7", "true"); GoogleCredentials credentials = testProvider.getDefaultCredentials(transportFactory); assertNotNull(credentials); assertTrue(credentials instanceof CloudShellCredentials); @@ -578,6 +581,11 @@ Class forName(String className) throws ClassNotFoundException { throw new ClassNotFoundException("TestDefaultCredentialProvider: Class not found."); } + @Override + protected boolean isOnGAEStandard7() { + return getProperty("isOnGAEStandard7", "false").equals("true"); + } + int getForNameCallCount() { return forNameCallCount; } diff --git a/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java b/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java index 7be949be7..b9b3594aa 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java @@ -65,6 +65,7 @@ public class MockTokenServerTransport extends MockHttpTransport { private IOException error; private Queue responseErrorSequence = new ArrayDeque(); private Queue responseSequence = new ArrayDeque(); + private int expiresInSeconds = 3600; public MockTokenServerTransport() {} @@ -113,6 +114,10 @@ public void addResponseSequence(LowLevelHttpResponse... responses) { } } + public void setExpiresInSeconds(int expiresInSeconds) { + this.expiresInSeconds = expiresInSeconds; + } + @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { buildRequestCount++; @@ -187,7 +192,7 @@ public LowLevelHttpResponse execute() throws IOException { GenericJson refreshContents = new GenericJson(); refreshContents.setFactory(JSON_FACTORY); refreshContents.put("access_token", accessToken); - refreshContents.put("expires_in", 3600); + refreshContents.put("expires_in", expiresInSeconds); refreshContents.put("token_type", "Bearer"); if (refreshToken != null) { refreshContents.put("refresh_token", refreshToken); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java index e1ab01528..ee8b0aac9 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java @@ -70,26 +70,39 @@ public class OAuth2CredentialsTest extends BaseSerializationTest { @Test public void constructor_storesAccessToken() { - OAuth2Credentials credentials = new OAuth2Credentials(new AccessToken(ACCESS_TOKEN, null)); - + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(ACCESS_TOKEN, null)) + .build(); assertEquals(credentials.getAccessToken().getTokenValue(), ACCESS_TOKEN); } @Test public void getAuthenticationType_returnsOAuth2() { - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); assertEquals(credentials.getAuthenticationType(), "OAuth2"); } @Test public void hasRequestMetadata_returnsTrue() { - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); assertTrue(credentials.hasRequestMetadata()); } @Test public void hasRequestMetadataOnly_returnsTrue() { - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); assertTrue(credentials.hasRequestMetadata()); } @@ -100,8 +113,12 @@ public void addChangeListener_notifiesOnRefresh() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1); - OAuth2Credentials userCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, null); + OAuth2Credentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .build(); // Use a fixed clock so tokens don't expire userCredentials.clock = new TestClock(); TestChangeListener listener = new TestChangeListener(); @@ -134,8 +151,12 @@ public void getRequestMetadata_blocking_cachesExpiringToken() throws IOException transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1); TestClock clock = new TestClock(); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, null); + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .build(); credentials.clock = clock; // Verify getting the first token @@ -183,8 +204,12 @@ public void getRequestMetadata_async() throws IOException { transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1); TestClock clock = new TestClock(); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, null); + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .build(); credentials.clock = clock; MockExecutor executor = new MockExecutor(); @@ -247,8 +272,12 @@ public void getRequestMetadata_async_refreshRace() throws IOException { transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1); TestClock clock = new TestClock(); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, null); + OAuth2Credentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .build(); credentials.clock = clock; MockExecutor executor = new MockExecutor(); @@ -272,7 +301,9 @@ public void getRequestMetadata_async_refreshRace() throws IOException { @Test public void getRequestMetadata_temporaryToken_hasToken() throws IOException { - OAuth2Credentials credentials = new OAuth2Credentials(new AccessToken(ACCESS_TOKEN, null)); + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(ACCESS_TOKEN, null)) + .build(); // Verify getting the first token Map> metadata = credentials.getRequestMetadata(CALL_URI); @@ -286,8 +317,12 @@ public void refresh_refreshesToken() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1); - OAuth2Credentials userCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, null); + OAuth2Credentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .build(); // Use a fixed clock so tokens don't exire userCredentials.clock = new TestClock(); @@ -312,15 +347,21 @@ public void refresh_refreshesToken() throws IOException { @Test(expected = IllegalStateException.class) public void refresh_temporaryToken_throws() throws IOException { - OAuth2Credentials credentials = new OAuth2Credentials(new AccessToken(ACCESS_TOKEN, null)); + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(ACCESS_TOKEN, null)) + .build(); credentials.refresh(); } @Test public void equals_true() throws IOException { final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2"; - OAuth2Credentials credentials = new OAuth2Credentials(new AccessToken(accessToken1, null)); - OAuth2Credentials otherCredentials = new OAuth2Credentials(new AccessToken(accessToken1, null)); + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(accessToken1, null)) + .build(); + OAuth2Credentials otherCredentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(accessToken1, null)) + .build(); assertTrue(credentials.equals(otherCredentials)); assertTrue(otherCredentials.equals(credentials)); } @@ -329,8 +370,12 @@ public void equals_true() throws IOException { public void equals_false_accessToken() throws IOException { final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2"; final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2"; - OAuth2Credentials credentials = new OAuth2Credentials(new AccessToken(accessToken1, null)); - OAuth2Credentials otherCredentials = new OAuth2Credentials(new AccessToken(accessToken2, null)); + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(accessToken1, null)) + .build(); + OAuth2Credentials otherCredentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(accessToken2, null)) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -338,7 +383,9 @@ public void equals_false_accessToken() throws IOException { @Test public void toString_containsFields() throws IOException { AccessToken accessToken = new AccessToken("1/MkSJoj1xsli0AccessToken_NKPY2", null); - OAuth2Credentials credentials = new OAuth2Credentials(accessToken); + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(accessToken) + .build(); String expectedToString = String.format("OAuth2Credentials{requestMetadata=%s, temporaryAccess=%s}", ImmutableMap.of(AuthHttpConstants.AUTHORIZATION, @@ -350,7 +397,9 @@ public void toString_containsFields() throws IOException { @Test public void hashCode_equals() throws IOException { final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2"; - OAuth2Credentials credentials = new OAuth2Credentials(new AccessToken(accessToken, null)); + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(accessToken, null)) + .build(); OAuth2Credentials otherCredentials = new OAuth2Credentials(new AccessToken(accessToken, null)); assertEquals(credentials.hashCode(), otherCredentials.hashCode()); } @@ -358,8 +407,9 @@ public void hashCode_equals() throws IOException { @Test public void serialize() throws IOException, ClassNotFoundException { final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2"; - OAuth2Credentials credentials = new OAuth2Credentials(new AccessToken(accessToken, null)); - OAuth2Credentials deserializedCredentials = serializeAndDeserialize(credentials); + OAuth2Credentials credentials = OAuth2Credentials.newBuilder() + .setAccessToken(new AccessToken(accessToken, null)) + .build(); OAuth2Credentials deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); assertEquals(credentials.toString(), deserializedCredentials.toString()); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java index 3400bf258..7058adde9 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java @@ -35,6 +35,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -43,6 +44,7 @@ import com.google.api.client.json.JsonFactory; import com.google.api.client.json.webtoken.JsonWebSignature; import com.google.api.client.json.webtoken.JsonWebToken; +import com.google.api.client.testing.http.FixedClock; import com.google.api.client.testing.http.MockLowLevelHttpResponse; import com.google.api.client.util.Clock; import com.google.api.client.util.Joiner; @@ -98,6 +100,7 @@ public class ServiceAccountCredentialsTest extends BaseSerializationTest { private static final String ACCESS_TOKEN = "1/MkSJoj1xsli0AccessToken_NKPY2"; private static final Collection SCOPES = Collections.singletonList("dummy.scope"); private static final String SERVICE_ACCOUNT_USER = "user@example.com"; + private static final String PROJECT_ID = "project-id"; private static final Collection EMPTY_SCOPES = Collections.emptyList(); private static final URI CALL_URI = URI.create("http://googleapis.com/testapi/v1/foo"); private static final HttpTransportFactory DUMMY_TRANSPORT_FACTORY = @@ -106,8 +109,15 @@ public class ServiceAccountCredentialsTest extends BaseSerializationTest { @Test public void createdScoped_clones() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - GoogleCredentials credentials = new ServiceAccountCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID, SCOPES, null, null, SERVICE_ACCOUNT_USER); + GoogleCredentials credentials = ServiceAccountCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .setScopes(SCOPES) + .setServiceAccountUser(SERVICE_ACCOUNT_USER) + .setProjectId(PROJECT_ID) + .build(); List newScopes = Arrays.asList("scope1", "scope2"); ServiceAccountCredentials newCredentials = @@ -119,6 +129,7 @@ public void createdScoped_clones() throws IOException { assertEquals(SA_PRIVATE_KEY_ID, newCredentials.getPrivateKeyId()); assertArrayEquals(newScopes.toArray(), newCredentials.getScopes().toArray()); assertEquals(SERVICE_ACCOUNT_USER, newCredentials.getServiceAccountUser()); + assertEquals(PROJECT_ID, newCredentials.getProjectId()); assertArrayEquals(SCOPES.toArray(), ((ServiceAccountCredentials)credentials).getScopes().toArray()); } @@ -126,8 +137,15 @@ public void createdScoped_clones() throws IOException { @Test public void createdDelegated_clones() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - GoogleCredentials credentials = new ServiceAccountCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID, SCOPES, null, null, SERVICE_ACCOUNT_USER); + ServiceAccountCredentials credentials = ServiceAccountCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .setScopes(SCOPES) + .setServiceAccountUser(SERVICE_ACCOUNT_USER) + .setProjectId(PROJECT_ID) + .build(); String newServiceAccountUser = "stranger@other.org"; ServiceAccountCredentials newCredentials = @@ -139,6 +157,7 @@ public void createdDelegated_clones() throws IOException { assertEquals(SA_PRIVATE_KEY_ID, newCredentials.getPrivateKeyId()); assertArrayEquals(SCOPES.toArray(), newCredentials.getScopes().toArray()); assertEquals(newServiceAccountUser, newCredentials.getServiceAccountUser()); + assertEquals(PROJECT_ID, newCredentials.getProjectId()); assertEquals(SERVICE_ACCOUNT_USER, ((ServiceAccountCredentials)credentials).getServiceAccountUser()); } @@ -147,8 +166,15 @@ public void createdDelegated_clones() throws IOException { public void createAssertion_correct() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); List scopes = Arrays.asList("scope1", "scope2"); - ServiceAccountCredentials credentials = new ServiceAccountCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID, scopes, null, null, SERVICE_ACCOUNT_USER); + ServiceAccountCredentials credentials = ServiceAccountCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .setScopes(scopes) + .setServiceAccountUser(SERVICE_ACCOUNT_USER) + .setProjectId(PROJECT_ID) + .build(); JsonFactory jsonFactory = OAuth2Utils.JSON_FACTORY; long currentTimeMillis = Clock.SYSTEM.currentTimeMillis(); @@ -200,12 +226,34 @@ public void createScopedRequired_nonEmptyScopes_false() throws IOException { assertFalse(credentials.createScopedRequired()); } + @Test + public void fromJSON_getProjectId() throws IOException { + MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); + transportFactory.transport.addServiceAccount(SA_CLIENT_EMAIL, ACCESS_TOKEN); + GenericJson json = writeServiceAccountJson( + SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, PROJECT_ID); + + ServiceAccountCredentials credentials = ServiceAccountCredentials.fromJson(json, transportFactory); + assertEquals(PROJECT_ID, credentials.getProjectId()); + } + + @Test + public void fromJSON_getProjectIdNull() throws IOException { + MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); + transportFactory.transport.addServiceAccount(SA_CLIENT_EMAIL, ACCESS_TOKEN); + GenericJson json = writeServiceAccountJson( + SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, null); + + ServiceAccountCredentials credentials = ServiceAccountCredentials.fromJson(json, transportFactory); + assertNull(credentials.getProjectId()); + } + @Test public void fromJSON_hasAccessToken() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addServiceAccount(SA_CLIENT_EMAIL, ACCESS_TOKEN); GenericJson json = writeServiceAccountJson( - SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID); + SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, PROJECT_ID); GoogleCredentials credentials = ServiceAccountCredentials.fromJson(json, transportFactory); @@ -265,6 +313,34 @@ public void refreshAccessToken_refreshesToken() throws IOException { TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken2); } + @Test + public void refreshAccessToken_tokenExpiry() throws IOException { + final String tokenString = "1/MkSJoj1xsli0AccessToken_NKPY2"; + MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); + MockTokenServerTransport transport = transportFactory.transport; + ServiceAccountCredentials credentials = + ServiceAccountCredentials.fromPkcs8( + SA_CLIENT_ID, + SA_CLIENT_EMAIL, + SA_PRIVATE_KEY_PKCS8, + SA_PRIVATE_KEY_ID, + SCOPES, + transportFactory, + null); + credentials.clock = new FixedClock(0L); + + transport.addServiceAccount(SA_CLIENT_EMAIL, tokenString); + AccessToken accessToken = credentials.refreshAccessToken(); + assertEquals(tokenString, accessToken.getTokenValue()); + assertEquals(3600 * 1000L, accessToken.getExpirationTimeMillis().longValue()); + + // Test for large expires_in values (should not overflow). + transport.setExpiresInSeconds(3600 * 1000); + accessToken = credentials.refreshAccessToken(); + assertEquals(tokenString, accessToken.getTokenValue()); + assertEquals(3600 * 1000 * 1000L, accessToken.getExpirationTimeMillis().longValue()); + } + @Test public void refreshAccessToken_retriesIOException() throws IOException { final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2"; @@ -618,7 +694,7 @@ public void fromStream_noPrivateKeyId_throws() throws IOException { } static GenericJson writeServiceAccountJson( - String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId) { + String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, String projectId) { GenericJson json = new GenericJson(); if (clientId != null) { json.put("client_id", clientId); @@ -632,6 +708,9 @@ static GenericJson writeServiceAccountJson( if (privateKeyId != null) { json.put("private_key_id", privateKeyId); } + if (projectId != null) { + json.put("project_id", projectId); + } json.put("type", GoogleCredentials.SERVICE_ACCOUNT_FILE_TYPE); return json; } @@ -639,7 +718,7 @@ static GenericJson writeServiceAccountJson( static InputStream writeServiceAccountStream(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId) throws IOException { GenericJson json = - writeServiceAccountJson(clientId, clientEmail, privateKeyPkcs8, privateKeyId); + writeServiceAccountJson(clientId, clientEmail, privateKeyPkcs8, privateKeyId, null); return TestUtils.jsonToInputStream(json); } diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java index 785cfbd2a..41df1dfa3 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java @@ -95,8 +95,12 @@ public class ServiceAccountJwtAccessCredentialsTest extends BaseSerializationTes @Test public void constructor_allParameters_constructs() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - ServiceAccountJwtAccessCredentials credentials = new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); assertEquals(SA_CLIENT_ID, credentials.getClientId()); assertEquals(SA_CLIENT_EMAIL, credentials.getClientEmail()); @@ -107,21 +111,30 @@ public void constructor_allParameters_constructs() throws IOException { @Test public void constructor_noClientId_constructs() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - new ServiceAccountJwtAccessCredentials(null, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); - } + ServiceAccountJwtAccessCredentials.newBuilder() + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); } @Test public void constructor_noPrivateKeyId_constructs() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - new ServiceAccountJwtAccessCredentials(SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, null); - } + ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .build(); } @Test public void constructor_noEmail_throws() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); try { - new ServiceAccountJwtAccessCredentials(SA_CLIENT_ID, null, privateKey, SA_PRIVATE_KEY_ID); - fail("exception expected"); + ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); fail("exception expected"); } catch (NullPointerException e) { // Expected } @@ -130,8 +143,11 @@ public void constructor_noEmail_throws() throws IOException { @Test public void constructor_noPrivateKey_throws() { try { - new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, null , SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); fail("exception expected"); } catch (NullPointerException e) { // Expected @@ -162,8 +178,12 @@ public void hasRequestMetadataOnly_returnsTrue() throws IOException { @Test public void getRequestMetadata_blocking_hasJwtAccess() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - Credentials credentials = new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); Map> metadata = credentials.getRequestMetadata(CALL_URI); @@ -184,8 +204,12 @@ public void getRequestMetadata_blocking_defaultURI_hasJwtAccess() throws IOExcep @Test public void getRequestMetadata_blocking_noURI_throws() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - Credentials credentials = new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); try { credentials.getRequestMetadata(); @@ -198,8 +222,12 @@ public void getRequestMetadata_blocking_noURI_throws() throws IOException { @Test public void getRequestMetadata_async_hasJwtAccess() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - Credentials credentials = new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); MockExecutor executor = new MockExecutor(); MockRequestMetadataCallback callback = new MockRequestMetadataCallback(); @@ -226,8 +254,12 @@ public void getRequestMetadata_async_defaultURI_hasJwtAccess() throws IOExceptio @Test public void getRequestMetadata_async_noURI_exception() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - Credentials credentials = new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); MockExecutor executor = new MockExecutor(); MockRequestMetadataCallback callback = new MockRequestMetadataCallback(); @@ -239,8 +271,12 @@ public void getRequestMetadata_async_noURI_exception() throws IOException { @Test public void getAccount_sameAs() throws IOException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); - ServiceAccountJwtAccessCredentials credentials = new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); assertEquals(SA_CLIENT_EMAIL, credentials.getAccount()); } @@ -249,8 +285,12 @@ public void sign_sameAs() throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException { PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); byte[] toSign = {0xD, 0xE, 0xA, 0xD}; - ServiceAccountJwtAccessCredentials credentials = new ServiceAccountJwtAccessCredentials( - SA_CLIENT_ID, SA_CLIENT_EMAIL, privateKey, SA_PRIVATE_KEY_ID); + ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder() + .setClientId(SA_CLIENT_ID) + .setClientEmail(SA_CLIENT_EMAIL) + .setPrivateKey(privateKey) + .setPrivateKeyId(SA_PRIVATE_KEY_ID) + .build(); byte[] signedBytes = credentials.sign(toSign); Signature signature = Signature.getInstance(OAuth2Utils.SIGNATURE_ALGORITHM); signature.initSign(credentials.getPrivateKey()); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java index 36ed90899..333076465 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java @@ -77,7 +77,11 @@ public class UserAuthorizerTest { public void constructorMinimum() { TestTokenStore store = new TestTokenStore(); - UserAuthorizer authorizer = new UserAuthorizer(CLIENT_ID, SCOPES, store); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(store) + .build(); assertSame(CLIENT_ID, authorizer.getClientId()); assertSame(store, authorizer.getTokenStore()); @@ -89,7 +93,12 @@ public void constructorMinimum() { public void constructorCommon() { TestTokenStore store = new TestTokenStore(); - UserAuthorizer authorizer = new UserAuthorizer(CLIENT_ID, SCOPES, store, CALLBACK_URI); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(store) + .setCallbackUri(CALLBACK_URI) + .build(); assertSame(CLIENT_ID, authorizer.getClientId()); assertSame(store, authorizer.getTokenStore()); @@ -99,19 +108,28 @@ public void constructorCommon() { @Test(expected = NullPointerException.class) public void constructorCommon_nullClientId_throws() { - new UserAuthorizer(null, SCOPES, null); + UserAuthorizer.newBuilder() + .setScopes(SCOPES) + .setCallbackUri(CALLBACK_URI) + .build(); } @Test(expected = NullPointerException.class) public void constructorCommon_nullScopes_throws() { - new UserAuthorizer(CLIENT_ID, null, null); + UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .build(); } @Test public void getCallbackUri_relativeToBase() { final URI callbackURI = URI.create("/bar"); final URI expectedCallbackURI = URI.create("http://example.com/bar"); - UserAuthorizer authorizer = new UserAuthorizer(CLIENT_ID, SCOPES, null, callbackURI); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setCallbackUri(callbackURI) + .build(); URI absoluteCallbackURI = authorizer.getCallbackUri(BASE_URI); @@ -126,8 +144,12 @@ public void getAuthorizationUrl() throws IOException { final String PATH = "/o/o/oauth2/auth"; final URI AUTH_URI = URI.create(PROTOCOL + "://" + HOST + PATH); final String EXPECTED_CALLBACK = "http://example.com" + CALLBACK_URI.toString(); - UserAuthorizer authorizer = new UserAuthorizer( - CLIENT_ID, SCOPES, null, CALLBACK_URI, null, null, AUTH_URI); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setCallbackUri(CALLBACK_URI) + .setUserAuthUri(AUTH_URI) + .build(); URL authorizationUrl = authorizer.getAuthorizationUrl(USER_ID, CUSTOM_STATE, BASE_URI); @@ -147,7 +169,11 @@ public void getAuthorizationUrl() throws IOException { @Test public void getCredentials_noCredentials_returnsNull() throws IOException { - UserAuthorizer authorizer = new UserAuthorizer(CLIENT_ID, SCOPES, new TestTokenStore()); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(new TestTokenStore()) + .build(); UserCredentials credentials = authorizer.getCredentials(USER_ID); @@ -157,9 +183,19 @@ public void getCredentials_noCredentials_returnsNull() throws IOException { @Test public void getCredentials_storedCredentials_returnsStored() throws IOException { TestTokenStore tokenStore = new TestTokenStore(); - UserCredentials initialCredentials = - new UserCredentials(CLIENT_ID_VALUE, CLIENT_SECRET, REFRESH_TOKEN, ACCESS_TOKEN); - UserAuthorizer authorizer = new UserAuthorizer(CLIENT_ID, SCOPES, tokenStore); + + UserCredentials initialCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID_VALUE) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(ACCESS_TOKEN) + .build(); + + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(tokenStore) + .build(); authorizer.storeCredentials(USER_ID, initialCredentials); UserCredentials credentials = authorizer.getCredentials(USER_ID); @@ -172,14 +208,21 @@ public void getCredentials_storedCredentials_returnsStored() throws IOException @Test(expected = NullPointerException.class) public void getCredentials_nullUserId_throws() throws IOException { TestTokenStore tokenStore = new TestTokenStore(); - UserAuthorizer authorizer = new UserAuthorizer(CLIENT_ID, SCOPES, tokenStore); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(tokenStore) + .build(); authorizer.getCredentials(null); } @Test(expected = IllegalStateException.class) public void getCredentials_nullTokenStore_throws() throws IOException { - UserAuthorizer authorizer = new UserAuthorizer(CLIENT_ID, SCOPES, null); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .build(); authorizer.getCredentials(USER_ID); } @@ -188,16 +231,27 @@ public void getCredentials_nullTokenStore_throws() throws IOException { public void getCredentials_refreshedToken_stored() throws IOException { final String accessTokenValue1 = "1/MkSJoj1xsli0AccessToken_NKPY2"; final String accessTokenValue2 = "2/MkSJoj1xsli0AccessToken_NKPY2"; - AccessToken acessToken1 = + AccessToken accessToken1 = new AccessToken(accessTokenValue1, new Date(EXPIRATION_TIME)); MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessTokenValue2); TestTokenStore tokenStore = new TestTokenStore(); - UserAuthorizer authorizer = - new UserAuthorizer(CLIENT_ID, SCOPES, tokenStore, null, transportFactory, null, null); - UserCredentials originalCredentials = new UserCredentials( - CLIENT_ID_VALUE, CLIENT_SECRET, REFRESH_TOKEN, acessToken1, transportFactory, null); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(tokenStore) + .setHttpTransportFactory(transportFactory) + .build(); + + UserCredentials originalCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID_VALUE) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken1) + .setHttpTransportFactory(transportFactory) + .build(); + authorizer.storeCredentials(USER_ID, originalCredentials); UserCredentials credentials1 = authorizer.getCredentials(USER_ID); @@ -224,8 +278,12 @@ public void getCredentialsFromCode_conevertsCodeToTokens() throws IOException { transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addAuthorizationCode(CODE, REFRESH_TOKEN, ACCESS_TOKEN_VALUE); TestTokenStore tokenStore = new TestTokenStore(); - UserAuthorizer authorizer = - new UserAuthorizer(CLIENT_ID, SCOPES, tokenStore, null, transportFactory, null, null); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(tokenStore) + .setHttpTransportFactory(transportFactory) + .build(); UserCredentials credentials = authorizer.getCredentialsFromCode(CODE, BASE_URI); @@ -235,8 +293,11 @@ public void getCredentialsFromCode_conevertsCodeToTokens() throws IOException { @Test(expected = NullPointerException.class) public void getCredentialsFromCode_nullCode_throws() throws IOException { - UserAuthorizer authorizer = - new UserAuthorizer(CLIENT_ID, SCOPES, new TestTokenStore()); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(new TestTokenStore()) + .build(); authorizer.getCredentialsFromCode(null, BASE_URI); } @@ -249,8 +310,12 @@ public void getAndStoreCredentialsFromCode_getAndStoresCredentials() throws IOEx transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addAuthorizationCode(CODE, REFRESH_TOKEN, accessTokenValue1); TestTokenStore tokenStore = new TestTokenStore(); - UserAuthorizer authorizer = - new UserAuthorizer(CLIENT_ID, SCOPES, tokenStore, null, transportFactory, null, null); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(tokenStore) + .setHttpTransportFactory(transportFactory) + .build(); UserCredentials credentials1 = authorizer.getAndStoreCredentialsFromCode(USER_ID, CODE, BASE_URI); @@ -274,16 +339,22 @@ public void getAndStoreCredentialsFromCode_getAndStoresCredentials() throws IOEx @Test(expected = NullPointerException.class) public void getAndStoreCredentialsFromCode_nullCode_throws() throws IOException { - UserAuthorizer authorizer = - new UserAuthorizer(CLIENT_ID, SCOPES, new TestTokenStore()); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(new TestTokenStore()) + .build(); authorizer.getAndStoreCredentialsFromCode(USER_ID, null, BASE_URI); } @Test(expected = NullPointerException.class) public void getAndStoreCredentialsFromCode_nullUserId_throws() throws IOException { - UserAuthorizer authorizer = - new UserAuthorizer(CLIENT_ID, SCOPES, new TestTokenStore()); + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(new TestTokenStore()) + .build(); authorizer.getAndStoreCredentialsFromCode(null, CODE, BASE_URI); } @@ -294,10 +365,20 @@ public void revokeAuthorization_revokesAndClears() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN_VALUE); - UserCredentials initialCredentials = - new UserCredentials(CLIENT_ID_VALUE, CLIENT_SECRET, REFRESH_TOKEN, ACCESS_TOKEN); - UserAuthorizer authorizer = - new UserAuthorizer(CLIENT_ID, SCOPES, tokenStore, null, transportFactory, null, null); + UserCredentials initialCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID_VALUE) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(ACCESS_TOKEN) + .build(); + + UserAuthorizer authorizer = UserAuthorizer.newBuilder() + .setClientId(CLIENT_ID) + .setScopes(SCOPES) + .setTokenStore(tokenStore) + .setHttpTransportFactory(transportFactory) + .build(); + authorizer.storeCredentials(USER_ID, initialCredentials); UserCredentials credentials1 = authorizer.getCredentials(USER_ID); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java index 805247bee..17b0882ec 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java @@ -75,25 +75,39 @@ public class UserCredentialsTest extends BaseSerializationTest { @Test(expected = IllegalStateException.class) public void constructor_accessAndRefreshTokenNull_throws() { - new UserCredentials(CLIENT_ID, CLIENT_SECRET, null, null); + UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .build(); } @Test public void constructor_storesRefreshToken() { - UserCredentials credentials = - new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); assertEquals(REFRESH_TOKEN, credentials.getRefreshToken()); } @Test public void createScoped_same() { - UserCredentials userCredentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); assertSame(userCredentials, userCredentials.createScoped(SCOPES)); } @Test public void createScopedRequired_false() { - UserCredentials userCredentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); assertFalse(userCredentials.createScopedRequired()); } @@ -115,8 +129,12 @@ public void getRequestMetadata_initialToken_hasAccessToken() throws IOException MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); - OAuth2Credentials userCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, null, accessToken, transportFactory, null); + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .build(); Map> metadata = userCredentials.getRequestMetadata(CALL_URI); @@ -128,8 +146,12 @@ public void getRequestMetadata_initialTokenRefreshed_throws() throws IOException MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); - OAuth2Credentials userCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, null, accessToken, transportFactory, null); + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .build(); try { userCredentials.refresh(); @@ -144,8 +166,12 @@ public void getRequestMetadata_fromRefreshToken_hasAccessToken() throws IOExcept MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN); - OAuth2Credentials userCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, null); + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .build(); Map> metadata = userCredentials.getRequestMetadata(CALL_URI); @@ -159,8 +185,13 @@ public void getRequestMetadata_customTokenServer_hasAccessToken() throws IOExcep transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN); transportFactory.transport.setTokenServerUri(TOKEN_SERVER); - OAuth2Credentials userCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, null, transportFactory, TOKEN_SERVER); + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setHttpTransportFactory(transportFactory) + .setTokenServerUri(TOKEN_SERVER) + .build(); Map> metadata = userCredentials.getRequestMetadata(CALL_URI); @@ -172,10 +203,22 @@ public void equals_true() throws IOException { final URI tokenServer = URI.create("https://foo.com/bar"); MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, accessToken, transportFactory, tokenServer); - OAuth2Credentials otherCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, accessToken, transportFactory, tokenServer); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .setTokenServerUri(tokenServer) + .build(); + UserCredentials otherCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .setTokenServerUri(tokenServer) + .build(); assertTrue(credentials.equals(otherCredentials)); assertTrue(otherCredentials.equals(credentials)); } @@ -185,10 +228,22 @@ public void equals_false_clientId() throws IOException { final URI tokenServer1 = URI.create("https://foo1.com/bar"); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory(); - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, - accessToken, httpTransportFactory, tokenServer1); - OAuth2Credentials otherCredentials = new UserCredentials("otherClientId", CLIENT_SECRET, - REFRESH_TOKEN, accessToken, httpTransportFactory, tokenServer1); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); + UserCredentials otherCredentials = UserCredentials.newBuilder() + .setClientId("other client id") + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -198,10 +253,22 @@ public void equals_false_clientSecret() throws IOException { final URI tokenServer1 = URI.create("https://foo1.com/bar"); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory(); - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, - accessToken, httpTransportFactory, tokenServer1); - OAuth2Credentials otherCredentials = new UserCredentials(CLIENT_ID, "otherClientSecret", - REFRESH_TOKEN, accessToken, httpTransportFactory, tokenServer1); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); + UserCredentials otherCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret("other client secret") + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -225,10 +292,22 @@ public void equals_false_accessToken() throws IOException { AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); AccessToken otherAccessToken = new AccessToken("otherAccessToken", null); MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory(); - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, - accessToken, httpTransportFactory, tokenServer1); - OAuth2Credentials otherCredentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, - REFRESH_TOKEN, otherAccessToken, httpTransportFactory, tokenServer1); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); + UserCredentials otherCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(otherAccessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -239,10 +318,22 @@ public void equals_false_transportFactory() throws IOException { AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory(); MockTokenServerTransportFactory serverTransportFactory = new MockTokenServerTransportFactory(); - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, - accessToken, httpTransportFactory, tokenServer1); - OAuth2Credentials otherCredentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, - REFRESH_TOKEN, accessToken, serverTransportFactory, tokenServer1); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); + UserCredentials otherCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(serverTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -253,10 +344,22 @@ public void equals_false_tokenServer() throws IOException { final URI tokenServer2 = URI.create("https://foo2.com/bar"); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory(); - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, - accessToken, httpTransportFactory, tokenServer1); - OAuth2Credentials otherCredentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, - REFRESH_TOKEN, accessToken, httpTransportFactory, tokenServer2); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer1) + .build(); + UserCredentials otherCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(httpTransportFactory) + .setTokenServerUri(tokenServer2) + .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); } @@ -266,8 +369,15 @@ public void toString_containsFields() throws IOException { AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); final URI tokenServer = URI.create("https://foo.com/bar"); MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); - OAuth2Credentials credentials = new UserCredentials(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, - accessToken, transportFactory, tokenServer); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .setTokenServerUri(tokenServer) + .build(); + String expectedToString = String.format( "UserCredentials{requestMetadata=%s, temporaryAccess=%s, clientId=%s, refreshToken=%s, " + "tokenServerUri=%s, transportFactoryClassName=%s}", @@ -286,10 +396,22 @@ public void hashCode_equals() throws IOException { final URI tokenServer = URI.create("https://foo.com/bar"); MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); - OAuth2Credentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, accessToken, transportFactory, tokenServer); - OAuth2Credentials otherCredentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, accessToken, transportFactory, tokenServer); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .setTokenServerUri(tokenServer) + .build(); + UserCredentials otherCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .setTokenServerUri(tokenServer) + .build(); assertEquals(credentials.hashCode(), otherCredentials.hashCode()); } @@ -298,8 +420,14 @@ public void serialize() throws IOException, ClassNotFoundException { final URI tokenServer = URI.create("https://foo.com/bar"); MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null); - UserCredentials credentials = new UserCredentials( - CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, accessToken, transportFactory, tokenServer); + UserCredentials credentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setAccessToken(accessToken) + .setHttpTransportFactory(transportFactory) + .setTokenServerUri(tokenServer) + .build(); UserCredentials deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); diff --git a/oauth2_http/pom.xml b/oauth2_http/pom.xml index 97517260c..ed6882866 100644 --- a/oauth2_http/pom.xml +++ b/oauth2_http/pom.xml @@ -5,7 +5,7 @@ com.google.auth google-auth-library-parent - 0.7.1 + 0.7.2-SNAPSHOT ../pom.xml diff --git a/pom.xml b/pom.xml index b0a4e6773..d9f471a68 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.google.auth google-auth-library-parent - 0.7.1 + 0.7.2-SNAPSHOT pom Google Auth Library for Java Client libraries providing authentication and diff --git a/update_javadoc.sh b/update_javadoc.sh new file mode 100644 index 000000000..8ed8d5a2f --- /dev/null +++ b/update_javadoc.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Copyright 2017, Google Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +set -e + +VERSION=$(mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -Ev '(^\[|\w+:)') + +if [ -z "$VERSION" ]; then + echo "Error updating Javadoc: could not obtain version number from maven-help-plugin." + exit 1 +fi + +git clone --branch gh-pages --single-branch https://github.com/google/google-auth-library-java/ tmp_gh-pages +mkdir -p tmp_gh-pages/releases/$VERSION + +mvn javadoc:aggregate + +pushd tmp_gh-pages/ +cp -r ../target/site/* releases/$VERSION/ +git add releases/$VERSION + +echo "" > index.html +git add index.html + +git commit --quiet -m "Add version $VERSION and update root redirect [ci skip]" +git push + +popd +rm -rf tmp_gh-pages