From 4fa1b0da518c260c9a7a0eeacff077c2d5566989 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 11:17:18 +0100 Subject: [PATCH 01/17] Created MemoryCredentialsStorage and implemented added opportunity to save current credentials in UserCredentials.java --- .../google/auth/oauth2/GoogleCredentials.java | 14 +++- .../google/auth/oauth2/UserAuthorizer.java | 1 + .../google/auth/oauth2/UserCredentials.java | 35 ++++++++-- .../storage/MemoryCredentialsStorage.java | 29 ++++++++ .../auth/oauth2/storage/TokenStore.java | 66 +++++++++++++++++++ .../auth/oauth2/UserAuthorizerTest.java | 1 + 6 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java create mode 100644 oauth2_http/java/com/google/auth/oauth2/storage/TokenStore.java diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index bde04ba12..f7c9c9237 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -37,8 +37,7 @@ import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.util.Collection; /** @@ -165,6 +164,17 @@ public static GoogleCredentials fromStream(InputStream credentialsStream, fileType, USER_FILE_TYPE, SERVICE_ACCOUNT_FILE_TYPE)); } + public void saveCredentialsToFile(InputStream credentials, String credentialsFileName) throws IOException { + byte[] buffer = new byte[credentials.available()]; + credentials.read(buffer); + + String userDirectory = System.getProperty("user.dir"); + File file = new File(userDirectory, credentialsFileName); + OutputStream fileOutputStream = new FileOutputStream(file); + fileOutputStream.write(buffer); + fileOutputStream.close(); + } + /** * Default constructor. **/ diff --git a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java index 71aca49af..60da27816 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java @@ -42,6 +42,7 @@ import com.google.api.client.util.Joiner; import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; +import com.google.auth.oauth2.storage.TokenStore; import com.google.common.collect.ImmutableList; import java.io.IOException; diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index 851ddb8e7..d011dc8d4 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -31,6 +31,8 @@ package com.google.auth.oauth2; +import static com.google.auth.oauth2.OAuth2Utils.JSON_FACTORY; +import static com.google.auth.oauth2.OAuth2Utils.UTF_8; import static com.google.common.base.MoreObjects.firstNonNull; import com.google.api.client.http.GenericUrl; @@ -45,9 +47,7 @@ import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; +import java.io.*; import java.net.URI; import java.util.Date; import java.util.Map; @@ -60,6 +60,7 @@ public class UserCredentials extends GoogleCredentials { private static final String GRANT_TYPE = "refresh_token"; private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. "; + private static final String USER_CREDENTIALS_FILE_NAME = "GOOGLE_AUTH_USER_CREDENTIALS_FILE_NAME"; private static final long serialVersionUID = -4800758775038679176L; private final String clientId; @@ -170,7 +171,7 @@ public static UserCredentials fromStream(InputStream credentialsStream, Preconditions.checkNotNull(credentialsStream); Preconditions.checkNotNull(transportFactory); - JsonFactory jsonFactory = OAuth2Utils.JSON_FACTORY; + JsonFactory jsonFactory = JSON_FACTORY; JsonObjectParser parser = new JsonObjectParser(jsonFactory); GenericJson fileContents = parser.parseAndClose( credentialsStream, OAuth2Utils.UTF_8, GenericJson.class); @@ -206,7 +207,7 @@ public AccessToken refreshAccessToken() throws IOException { HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory(); HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(tokenServerUri), content); - request.setParser(new JsonObjectParser(OAuth2Utils.JSON_FACTORY)); + request.setParser(new JsonObjectParser(JSON_FACTORY)); HttpResponse response = request.execute(); GenericData responseData = response.parseAs(GenericData.class); String accessToken = @@ -244,6 +245,30 @@ public final String getRefreshToken() { return refreshToken; } + private InputStream getUserCredentialsStream() throws IOException { + GenericJson json = new GenericJson(); + json.put("type", GoogleCredentials.USER_FILE_TYPE); + if (refreshToken != null) { + json.put("refresh_token", refreshToken); + } + if (tokenServerUri != null) { + json.put("token_server_uri", tokenServerUri); + } + if (clientId != null) { + json.put("client_id", clientId); + } + if (clientSecret != null) { + json.put("client_secret", clientSecret); + } + json.setFactory(JSON_FACTORY); + String text = json.toPrettyString(); + return new ByteArrayInputStream(text.getBytes(UTF_8)); + } + + public void saveUserCredentials() throws IOException { + super.saveCredentialsToFile(getUserCredentialsStream(), USER_CREDENTIALS_FILE_NAME); + } + @Override public int hashCode() { return Objects.hash(super.hashCode(), clientId, clientSecret, refreshToken, tokenServerUri, diff --git a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java b/oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java new file mode 100644 index 000000000..ccd3d0fb4 --- /dev/null +++ b/oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java @@ -0,0 +1,29 @@ +package com.google.auth.oauth2.storage; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class MemoryCredentialsStorage implements TokenStore { + + private Map tokensStorage; + + public MemoryCredentialsStorage() { + tokensStorage = new HashMap<>(); + } + + @Override + public String load(String id) throws IOException { + return tokensStorage.get(id); + } + + @Override + public void store(String id, String tokens) throws IOException { + tokensStorage.put(id, tokens); + } + + @Override + public void delete(String id) throws IOException { + tokensStorage.remove(id); + } +} diff --git a/oauth2_http/java/com/google/auth/oauth2/storage/TokenStore.java b/oauth2_http/java/com/google/auth/oauth2/storage/TokenStore.java new file mode 100644 index 000000000..db58c1512 --- /dev/null +++ b/oauth2_http/java/com/google/auth/oauth2/storage/TokenStore.java @@ -0,0 +1,66 @@ +/* + * Copyright 2015, 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. + */ + +package com.google.auth.oauth2.storage; + +import java.io.IOException; + +/** + * Interface for long term storage of tokens + */ +public interface TokenStore { + + /** + * Load the token data from storage for the given ID. + * + * @param id ID of token data to load. + * @return The loaded token data. + * @throws IOException An error loading the token data from storage. + */ + String load(String id) throws IOException; + + /** + * Put the token data into storage for the given ID. + * + * @param id ID of token data to store. + * @param tokens The token data to store. + * @throws IOException An error storing the token data. + */ + void store(String id, String tokens) throws IOException; + + /** + * Remove the token data from storage for the given ID. + * + * @param id ID of token data to store. + * @throws IOException An error storing the token data. + */ + void delete(String id) throws IOException; +} diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java index 333076465..9a2bccbea 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java @@ -40,6 +40,7 @@ import com.google.auth.TestUtils; import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; +import com.google.auth.oauth2.storage.TokenStore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; From 9cfc14ded9064886c449006675f56322e380db80 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 11:17:40 +0100 Subject: [PATCH 02/17] Moved TokenStore.java to another directory --- .../com/google/auth/oauth2/TokenStore.java | 66 ------------------- 1 file changed, 66 deletions(-) delete mode 100644 oauth2_http/java/com/google/auth/oauth2/TokenStore.java diff --git a/oauth2_http/java/com/google/auth/oauth2/TokenStore.java b/oauth2_http/java/com/google/auth/oauth2/TokenStore.java deleted file mode 100644 index c0fc2dfee..000000000 --- a/oauth2_http/java/com/google/auth/oauth2/TokenStore.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2015, 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. - */ - -package com.google.auth.oauth2; - -import java.io.IOException; - -/** - * Interface for long term storage of tokens - */ -public interface TokenStore { - - /** - * Load the token data from storage for the given ID. - * - * @param id ID of token data to load. - * @return The loaded token data. - * @throws IOException An error loading the token data from storage. - */ - String load(String id) throws IOException; - - /** - * Put the token data into storage for the given ID. - * - * @param id ID of token data to store. - * @param tokens The token data to store. - * @throws IOException An error storing the token data. - */ - void store(String id, String tokens) throws IOException; - - /** - * Remove the token data from storage for the given ID. - * - * @param id ID of token data to store. - * @throws IOException An error storing the token data. - */ - void delete(String id) throws IOException; -} From 48ff1677efb661d40caad41b2a53b8845aea6d6f Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 11:33:23 +0100 Subject: [PATCH 03/17] Comments added --- .../com/google/auth/oauth2/GoogleCredentials.java | 7 +++++++ .../java/com/google/auth/oauth2/UserCredentials.java | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index f7c9c9237..032db4d53 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -164,6 +164,13 @@ public static GoogleCredentials fromStream(InputStream credentialsStream, fileType, USER_FILE_TYPE, SERVICE_ACCOUNT_FILE_TYPE)); } + /** + * Saves the end user credentials in specified file + * + * @param credentials InputStream containing user credentials in JSON format + * @param credentialsFileName Name of file where to store the credentials + * @throws IOException An error saving the credentials. + */ public void saveCredentialsToFile(InputStream credentials, String credentialsFileName) throws IOException { byte[] buffer = new byte[credentials.available()]; credentials.read(buffer); diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index d011dc8d4..04a91c509 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -245,6 +245,12 @@ public final String getRefreshToken() { return refreshToken; } + + /** + * Returns the instance of InputStream containing user credentials in JSON format + * + * @return user credentials stream + */ private InputStream getUserCredentialsStream() throws IOException { GenericJson json = new GenericJson(); json.put("type", GoogleCredentials.USER_FILE_TYPE); @@ -265,6 +271,11 @@ private InputStream getUserCredentialsStream() throws IOException { return new ByteArrayInputStream(text.getBytes(UTF_8)); } + /** + * Saves the end user credentials in file + * + * @throws IOException An error storing the credentials. + */ public void saveUserCredentials() throws IOException { super.saveCredentialsToFile(getUserCredentialsStream(), USER_CREDENTIALS_FILE_NAME); } From dd03f9d7ed1fb27bfc8fe3df6efb2f04a5b3c0a3 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 11:39:10 +0100 Subject: [PATCH 04/17] Tests for UserAuthorizer updated --- .../oauth2/storage/MemoryTokensStorage.java | 25 ++++++++++ .../auth/oauth2/UserAuthorizerTest.java | 46 ++++++------------- 2 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java diff --git a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java b/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java new file mode 100644 index 000000000..750dfdf05 --- /dev/null +++ b/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java @@ -0,0 +1,25 @@ +package com.google.auth.oauth2.storage; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class MemoryTokensStorage implements TokenStore { + + private final Map tokensStorage = new HashMap<>(); + + @Override + public String load(String id) throws IOException { + return tokensStorage.get(id); + } + + @Override + public void store(String id, String tokens) throws IOException { + tokensStorage.put(id, tokens); + } + + @Override + public void delete(String id) throws IOException { + tokensStorage.remove(id); + } +} diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java index 9a2bccbea..fe3b65f5a 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java @@ -40,10 +40,12 @@ import com.google.auth.TestUtils; import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; +import com.google.auth.oauth2.storage.MemoryTokensStorage; import com.google.auth.oauth2.storage.TokenStore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import sun.tools.jstat.Token; import java.io.IOException; import java.net.URI; @@ -76,7 +78,7 @@ public class UserAuthorizerTest { @Test public void constructorMinimum() { - TestTokenStore store = new TestTokenStore(); + TokenStore store = new MemoryTokensStorage(); UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) @@ -92,7 +94,7 @@ public void constructorMinimum() { @Test public void constructorCommon() { - TestTokenStore store = new TestTokenStore(); + TokenStore store = new MemoryTokensStorage(); UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) @@ -173,7 +175,7 @@ public void getCredentials_noCredentials_returnsNull() throws IOException { UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) - .setTokenStore(new TestTokenStore()) + .setTokenStore(new MemoryTokensStorage()) .build(); UserCredentials credentials = authorizer.getCredentials(USER_ID); @@ -183,7 +185,7 @@ public void getCredentials_noCredentials_returnsNull() throws IOException { @Test public void getCredentials_storedCredentials_returnsStored() throws IOException { - TestTokenStore tokenStore = new TestTokenStore(); + TokenStore tokenStore = new MemoryTokensStorage(); UserCredentials initialCredentials = UserCredentials.newBuilder() .setClientId(CLIENT_ID_VALUE) @@ -208,7 +210,7 @@ public void getCredentials_storedCredentials_returnsStored() throws IOException @Test(expected = NullPointerException.class) public void getCredentials_nullUserId_throws() throws IOException { - TestTokenStore tokenStore = new TestTokenStore(); + TokenStore tokenStore = new MemoryTokensStorage(); UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) @@ -237,7 +239,7 @@ public void getCredentials_refreshedToken_stored() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessTokenValue2); - TestTokenStore tokenStore = new TestTokenStore(); + TokenStore tokenStore = new MemoryTokensStorage(); UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) @@ -278,7 +280,7 @@ public void getCredentialsFromCode_conevertsCodeToTokens() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addAuthorizationCode(CODE, REFRESH_TOKEN, ACCESS_TOKEN_VALUE); - TestTokenStore tokenStore = new TestTokenStore(); + TokenStore tokenStore = new MemoryTokensStorage(); UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) @@ -297,7 +299,7 @@ public void getCredentialsFromCode_nullCode_throws() throws IOException { UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) - .setTokenStore(new TestTokenStore()) + .setTokenStore(new MemoryTokensStorage()) .build(); authorizer.getCredentialsFromCode(null, BASE_URI); @@ -310,7 +312,7 @@ public void getAndStoreCredentialsFromCode_getAndStoresCredentials() throws IOEx MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addAuthorizationCode(CODE, REFRESH_TOKEN, accessTokenValue1); - TestTokenStore tokenStore = new TestTokenStore(); + TokenStore tokenStore = new MemoryTokensStorage(); UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) @@ -343,7 +345,7 @@ public void getAndStoreCredentialsFromCode_nullCode_throws() throws IOException UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) - .setTokenStore(new TestTokenStore()) + .setTokenStore(new MemoryTokensStorage()) .build(); authorizer.getAndStoreCredentialsFromCode(USER_ID, null, BASE_URI); @@ -354,7 +356,7 @@ public void getAndStoreCredentialsFromCode_nullUserId_throws() throws IOExceptio UserAuthorizer authorizer = UserAuthorizer.newBuilder() .setClientId(CLIENT_ID) .setScopes(SCOPES) - .setTokenStore(new TestTokenStore()) + .setTokenStore(new MemoryTokensStorage()) .build(); authorizer.getAndStoreCredentialsFromCode(null, CODE, BASE_URI); @@ -362,7 +364,7 @@ public void getAndStoreCredentialsFromCode_nullUserId_throws() throws IOExceptio @Test public void revokeAuthorization_revokesAndClears() throws IOException { - TestTokenStore tokenStore = new TestTokenStore(); + TokenStore tokenStore = new MemoryTokensStorage(); MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN_VALUE); @@ -399,24 +401,4 @@ public void revokeAuthorization_revokesAndClears() throws IOException { UserCredentials credentials2 = authorizer.getCredentials(USER_ID); assertNull(credentials2); } - - private static class TestTokenStore implements TokenStore { - - private final Map map = new HashMap<>(); - - @Override - public String load(String id) throws IOException { - return map.get(id); - } - - @Override - public void store(String id, String tokens) throws IOException { - map.put(id, tokens); - } - - @Override - public void delete(String id) throws IOException { - map.remove(id); - } - } } From 71adc52c6e5b0cb272cb26e5dffe157c06df8881 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 11:39:35 +0100 Subject: [PATCH 05/17] Renamed MemoryCredentialsStorage to MemoryTokensStorage --- .../storage/MemoryCredentialsStorage.java | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java diff --git a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java b/oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java deleted file mode 100644 index ccd3d0fb4..000000000 --- a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryCredentialsStorage.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.google.auth.oauth2.storage; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -public class MemoryCredentialsStorage implements TokenStore { - - private Map tokensStorage; - - public MemoryCredentialsStorage() { - tokensStorage = new HashMap<>(); - } - - @Override - public String load(String id) throws IOException { - return tokensStorage.get(id); - } - - @Override - public void store(String id, String tokens) throws IOException { - tokensStorage.put(id, tokens); - } - - @Override - public void delete(String id) throws IOException { - tokensStorage.remove(id); - } -} From 820826304ec79d4e990fbd7820075b59620bdb2f Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 11:57:01 +0100 Subject: [PATCH 06/17] Tests for UserCredentials updated --- .../google/auth/oauth2/UserCredentials.java | 3 +- .../auth/oauth2/UserCredentialsTest.java | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index 04a91c509..455b43d02 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -58,9 +58,10 @@ */ public class UserCredentials extends GoogleCredentials { + public static final String USER_CREDENTIALS_FILE_NAME = "GOOGLE_AUTH_USER_CREDENTIALS_FILE_NAME"; + private static final String GRANT_TYPE = "refresh_token"; private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. "; - private static final String USER_CREDENTIALS_FILE_NAME = "GOOGLE_AUTH_USER_CREDENTIALS_FILE_NAME"; private static final long serialVersionUID = -4800758775038679176L; private final String clientId; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java index 17b0882ec..91d470edb 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java @@ -51,9 +51,7 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.URI; import java.util.Collection; import java.util.Collections; @@ -492,6 +490,36 @@ public void fromStream_userNoRefreshToken_throws() throws IOException { testFromStreamException(userStream, "refresh_token"); } + @Test + public void saveUserCredentials_saved_throws() throws IOException { + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); + userCredentials.saveUserCredentials(); + } + + @Test + public void saveAndRestoreUserCredential_saveAndRestored_throws() throws IOException { + UserCredentials userCredentials = UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .build(); + userCredentials.saveUserCredentials(); + + String userDirectory = System.getProperty("user.dir"); + System.out.println(userDirectory); + FileInputStream inputStream = new FileInputStream(new File(userDirectory, UserCredentials.USER_CREDENTIALS_FILE_NAME)); + + UserCredentials restoredCredentials = UserCredentials.fromStream(inputStream); + + assertEquals(userCredentials.getClientId(), restoredCredentials.getClientId()); + assertEquals(userCredentials.getClientSecret(), restoredCredentials.getClientSecret()); + assertEquals(userCredentials.getRefreshToken(), restoredCredentials.getRefreshToken()); + } + static GenericJson writeUserJson(String clientId, String clientSecret, String refreshToken) { GenericJson json = new GenericJson(); if (clientId != null) { From b44d07d8a2999988929b5766c2728cbb064359c0 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 14:09:58 +0100 Subject: [PATCH 07/17] Java header added --- .../oauth2/storage/MemoryTokensStorage.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java b/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java index 750dfdf05..7e72c28f4 100644 --- a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java +++ b/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java @@ -1,3 +1,34 @@ +/* + * 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. + */ + package com.google.auth.oauth2.storage; import java.io.IOException; From 8adc97af66a693ecfdc6b410388292454a3b8bd6 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 18:08:39 +0100 Subject: [PATCH 08/17] small fixes --- .../java/com/google/auth/oauth2/GoogleCredentials.java | 7 ++++++- .../java/com/google/auth/oauth2/UserCredentials.java | 7 +++++-- .../com/google/auth/oauth2/UserAuthorizerTest.java | 2 -- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index 032db4d53..ceda409dc 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -37,7 +37,12 @@ import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.FileOutputStream; + import java.util.Collection; /** diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index 455b43d02..25f92b3bb 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -47,7 +47,10 @@ import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; import java.net.URI; import java.util.Date; import java.util.Map; @@ -58,7 +61,7 @@ */ public class UserCredentials extends GoogleCredentials { - public static final String USER_CREDENTIALS_FILE_NAME = "GOOGLE_AUTH_USER_CREDENTIALS_FILE_NAME"; + public static final String USER_CREDENTIALS_FILE_NAME = "GOOGLE_APPLICATION_CREDENTIALS"; private static final String GRANT_TYPE = "refresh_token"; private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. "; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java index fe3b65f5a..38d244282 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java @@ -45,7 +45,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import sun.tools.jstat.Token; import java.io.IOException; import java.net.URI; @@ -53,7 +52,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.Map; /** From 431453dee84999425743360193ec0a6b85860b92 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 18:15:04 +0100 Subject: [PATCH 09/17] Used MemoryTokenStorage as default for UserAuthorizer --- oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java index 60da27816..69f254eea 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java @@ -42,6 +42,7 @@ import com.google.api.client.util.Joiner; import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; +import com.google.auth.oauth2.storage.MemoryTokensStorage; import com.google.auth.oauth2.storage.TokenStore; import com.google.common.collect.ImmutableList; @@ -117,7 +118,7 @@ public UserAuthorizer(ClientId clientId, Collection scopes, TokenStore t (transportFactory == null) ? OAuth2Utils.HTTP_TRANSPORT_FACTORY : transportFactory; this.tokenServerUri = (tokenServerUri == null) ? OAuth2Utils.TOKEN_SERVER_URI : tokenServerUri; this.userAuthUri = (userAuthUri == null) ? OAuth2Utils.USER_AUTH_URI : userAuthUri; - this.tokenStore = tokenStore; + this.tokenStore = (tokenStore == null) ? new MemoryTokensStorage() : tokenStore; } From c3feabca4db09f2b57a0a3f662c0c1c7d596418a Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 14 Sep 2017 18:23:28 +0100 Subject: [PATCH 10/17] Comments regrading credentials --- .../java/com/google/auth/oauth2/UserCredentials.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index 25f92b3bb..1aea19380 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -251,7 +251,11 @@ public final String getRefreshToken() { /** - * Returns the instance of InputStream containing user credentials in JSON format + * Returns the instance of InputStream containing the following user credentials in JSON format: + * - RefreshToken + * - ClientId + * - ClientSecret + * - ServerTokenUri * * @return user credentials stream */ From 5fcddeb6154fedacaa45545b4929cc4ccf7c8a34 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Fri, 15 Sep 2017 10:42:47 +0100 Subject: [PATCH 11/17] Removed unnecessary test Removed test, since there is always TokenStore exist by default. --- .../com/google/auth/oauth2/UserAuthorizerTest.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java index 38d244282..8fbc02189 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java @@ -218,16 +218,6 @@ public void getCredentials_nullUserId_throws() throws IOException { authorizer.getCredentials(null); } - @Test(expected = IllegalStateException.class) - public void getCredentials_nullTokenStore_throws() throws IOException { - UserAuthorizer authorizer = UserAuthorizer.newBuilder() - .setClientId(CLIENT_ID) - .setScopes(SCOPES) - .build(); - - authorizer.getCredentials(USER_ID); - } - @Test public void getCredentials_refreshedToken_stored() throws IOException { final String accessTokenValue1 = "1/MkSJoj1xsli0AccessToken_NKPY2"; From 7bcfeab27a90350151ce180e5a2f3bd923fcb79c Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 2 Nov 2017 22:57:15 +0300 Subject: [PATCH 12/17] Add apache.commons.io dependency and update the implementation of 'saveCredentialsToFile'. --- .../java/com/google/auth/oauth2/GoogleCredentials.java | 9 ++------- .../com/google/auth/oauth2/UserCredentialsTest.java | 6 +++++- oauth2_http/pom.xml | 9 +++++++++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index ceda409dc..159a3f4d6 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -36,6 +36,7 @@ import com.google.api.client.json.JsonObjectParser; import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; +import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; @@ -177,14 +178,8 @@ public static GoogleCredentials fromStream(InputStream credentialsStream, * @throws IOException An error saving the credentials. */ public void saveCredentialsToFile(InputStream credentials, String credentialsFileName) throws IOException { - byte[] buffer = new byte[credentials.available()]; - credentials.read(buffer); - String userDirectory = System.getProperty("user.dir"); - File file = new File(userDirectory, credentialsFileName); - OutputStream fileOutputStream = new FileOutputStream(file); - fileOutputStream.write(buffer); - fileOutputStream.close(); + FileUtils.copyInputStreamToFile(credentials, new File(userDirectory, credentialsFileName)); } /** diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java index 91d470edb..c06b434df 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java @@ -51,7 +51,11 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ByteArrayInputStream; import java.net.URI; import java.util.Collection; import java.util.Collections; diff --git a/oauth2_http/pom.xml b/oauth2_http/pom.xml index ed6882866..1fb78dc96 100644 --- a/oauth2_http/pom.xml +++ b/oauth2_http/pom.xml @@ -65,6 +65,15 @@ junit test + + com.google.guava + guava + + + commons-io + commons-io + 2.5 + From 46ea6afbba301ed4ea06d2eec81d255f409e4140 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 2 Nov 2017 23:41:53 +0300 Subject: [PATCH 13/17] Returned pom.xml to its previous state, used Guava 'ByteStreams.copy' to copy the inputStream to fileOutputStream. --- .../java/com/google/auth/oauth2/GoogleCredentials.java | 8 +++++++- oauth2_http/pom.xml | 9 --------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index 159a3f4d6..5013b7c55 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -36,6 +36,7 @@ import com.google.api.client.json.JsonObjectParser; import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; +import com.google.common.io.ByteStreams; import org.apache.commons.io.FileUtils; import java.io.File; @@ -179,7 +180,12 @@ public static GoogleCredentials fromStream(InputStream credentialsStream, */ public void saveCredentialsToFile(InputStream credentials, String credentialsFileName) throws IOException { String userDirectory = System.getProperty("user.dir"); - FileUtils.copyInputStreamToFile(credentials, new File(userDirectory, credentialsFileName)); + final OutputStream outputStream = new FileOutputStream(new File(userDirectory, credentialsFileName)); + try { + ByteStreams.copy(credentials, outputStream); + } finally { + outputStream.close(); + } } /** diff --git a/oauth2_http/pom.xml b/oauth2_http/pom.xml index ff37a7cc3..d6e4e3f21 100644 --- a/oauth2_http/pom.xml +++ b/oauth2_http/pom.xml @@ -65,15 +65,6 @@ junit test - - com.google.guava - guava - - - commons-io - commons-io - 2.5 - From 7845198d23e74eb398ea65401bd9710041767f3f Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Thu, 2 Nov 2017 23:43:24 +0300 Subject: [PATCH 14/17] Move storage implementation to its original package. --- oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java | 1 - .../google/auth/oauth2/{storage => }/MemoryTokensStorage.java | 2 +- .../java/com/google/auth/oauth2/{storage => }/TokenStore.java | 2 +- oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java | 2 -- .../javatests/com/google/auth/oauth2/UserAuthorizerTest.java | 2 -- 5 files changed, 2 insertions(+), 7 deletions(-) rename oauth2_http/java/com/google/auth/oauth2/{storage => }/MemoryTokensStorage.java (98%) rename oauth2_http/java/com/google/auth/oauth2/{storage => }/TokenStore.java (98%) diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index 5013b7c55..7c1240fef 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -37,7 +37,6 @@ import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; import com.google.common.io.ByteStreams; -import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; diff --git a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java b/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java similarity index 98% rename from oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java rename to oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java index 7e72c28f4..602c4be26 100644 --- a/oauth2_http/java/com/google/auth/oauth2/storage/MemoryTokensStorage.java +++ b/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java @@ -29,7 +29,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package com.google.auth.oauth2.storage; +package com.google.auth.oauth2; import java.io.IOException; import java.util.HashMap; diff --git a/oauth2_http/java/com/google/auth/oauth2/storage/TokenStore.java b/oauth2_http/java/com/google/auth/oauth2/TokenStore.java similarity index 98% rename from oauth2_http/java/com/google/auth/oauth2/storage/TokenStore.java rename to oauth2_http/java/com/google/auth/oauth2/TokenStore.java index db58c1512..c0fc2dfee 100644 --- a/oauth2_http/java/com/google/auth/oauth2/storage/TokenStore.java +++ b/oauth2_http/java/com/google/auth/oauth2/TokenStore.java @@ -29,7 +29,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package com.google.auth.oauth2.storage; +package com.google.auth.oauth2; import java.io.IOException; diff --git a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java index 69f254eea..43e0c8edc 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java @@ -42,8 +42,6 @@ import com.google.api.client.util.Joiner; import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; -import com.google.auth.oauth2.storage.MemoryTokensStorage; -import com.google.auth.oauth2.storage.TokenStore; import com.google.common.collect.ImmutableList; import java.io.IOException; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java index 8fbc02189..25b6c8fa2 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java @@ -40,8 +40,6 @@ import com.google.auth.TestUtils; import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; -import com.google.auth.oauth2.storage.MemoryTokensStorage; -import com.google.auth.oauth2.storage.TokenStore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; From de94bccfad4796f0bee12c8f252a831d5a7fcd8e Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Fri, 3 Nov 2017 00:03:30 +0300 Subject: [PATCH 15/17] Update the implementation of saving user credentials, fix issues with packages and indentation. --- .../google/auth/oauth2/GoogleCredentials.java | 7 +++-- .../auth/oauth2/MemoryTokensStorage.java | 26 +++++++++---------- .../google/auth/oauth2/UserCredentials.java | 8 +++--- .../auth/oauth2/UserCredentialsTest.java | 13 ++++++---- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index 7c1240fef..ef9593801 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -174,12 +174,11 @@ public static GoogleCredentials fromStream(InputStream credentialsStream, * Saves the end user credentials in specified file * * @param credentials InputStream containing user credentials in JSON format - * @param credentialsFileName Name of file where to store the credentials + * @param filePath Path to file where to store the credentials * @throws IOException An error saving the credentials. */ - public void saveCredentialsToFile(InputStream credentials, String credentialsFileName) throws IOException { - String userDirectory = System.getProperty("user.dir"); - final OutputStream outputStream = new FileOutputStream(new File(userDirectory, credentialsFileName)); + public void saveCredentialsToFile(InputStream credentials, String filePath) throws IOException { + final OutputStream outputStream = new FileOutputStream(new File(filePath)); try { ByteStreams.copy(credentials, outputStream); } finally { diff --git a/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java b/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java index 602c4be26..b87e951ad 100644 --- a/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java +++ b/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java @@ -36,21 +36,21 @@ import java.util.Map; public class MemoryTokensStorage implements TokenStore { + private final Map tokensStorage = new HashMap<>(); - private final Map tokensStorage = new HashMap<>(); + @Override + public String load(String id) throws IOException { + return tokensStorage.get(id); + } - @Override - public String load(String id) throws IOException { - return tokensStorage.get(id); - } + @Override + public void store(String id, String tokens) throws IOException { + tokensStorage.put(id, tokens); + } - @Override - public void store(String id, String tokens) throws IOException { - tokensStorage.put(id, tokens); - } + @Override + public void delete(String id) throws IOException { + tokensStorage.remove(id); + } - @Override - public void delete(String id) throws IOException { - tokensStorage.remove(id); - } } diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index 1aea19380..93b97fd17 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -61,8 +61,6 @@ */ public class UserCredentials extends GoogleCredentials { - public static final String USER_CREDENTIALS_FILE_NAME = "GOOGLE_APPLICATION_CREDENTIALS"; - private static final String GRANT_TYPE = "refresh_token"; private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. "; private static final long serialVersionUID = -4800758775038679176L; @@ -282,10 +280,12 @@ private InputStream getUserCredentialsStream() throws IOException { /** * Saves the end user credentials in file * + * @param filePath Path to file where to store the credentials + * * @throws IOException An error storing the credentials. */ - public void saveUserCredentials() throws IOException { - super.saveCredentialsToFile(getUserCredentialsStream(), USER_CREDENTIALS_FILE_NAME); + public void save(String filePath) throws IOException { + super.saveCredentialsToFile(getUserCredentialsStream(), filePath); } @Override diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java index c06b434df..7dcfba52d 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java @@ -501,7 +501,8 @@ public void saveUserCredentials_saved_throws() throws IOException { .setClientSecret(CLIENT_SECRET) .setRefreshToken(REFRESH_TOKEN) .build(); - userCredentials.saveUserCredentials(); + String filePath = System.getProperty("user.dir") + File.separator + "GOOGLE_APPLICATION_CREDENTIALS"; + userCredentials.save(filePath); } @Test @@ -511,11 +512,13 @@ public void saveAndRestoreUserCredential_saveAndRestored_throws() throws IOExcep .setClientSecret(CLIENT_SECRET) .setRefreshToken(REFRESH_TOKEN) .build(); - userCredentials.saveUserCredentials(); - String userDirectory = System.getProperty("user.dir"); - System.out.println(userDirectory); - FileInputStream inputStream = new FileInputStream(new File(userDirectory, UserCredentials.USER_CREDENTIALS_FILE_NAME)); + String filePath = System.getProperty("user.dir") + File.separator + "GOOGLE_APPLICATION_CREDENTIALS"; + + userCredentials.save(filePath); + + + FileInputStream inputStream = new FileInputStream(new File(filePath)); UserCredentials restoredCredentials = UserCredentials.fromStream(inputStream); From 4026b3a063e2c458cb9021118c0c266ca6965cf7 Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Fri, 3 Nov 2017 00:44:57 +0300 Subject: [PATCH 16/17] Use temporary files for tests, move generic implementation to 'OAuth2Utils', --- .../google/auth/oauth2/GoogleCredentials.java | 20 ------------------- .../auth/oauth2/MemoryTokensStorage.java | 3 +++ .../com/google/auth/oauth2/OAuth2Utils.java | 20 +++++++++++++++++++ .../google/auth/oauth2/UserCredentials.java | 4 ++-- .../auth/oauth2/UserCredentialsTest.java | 11 +++++++--- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java index aecbdf892..b0f47c4e4 100644 --- a/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java @@ -36,13 +36,9 @@ import com.google.api.client.json.JsonObjectParser; import com.google.api.client.util.Preconditions; import com.google.auth.http.HttpTransportFactory; -import com.google.common.io.ByteStreams; -import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.io.FileOutputStream; import java.util.Collection; @@ -183,22 +179,6 @@ public static GoogleCredentials fromStream(InputStream credentialsStream, fileType, USER_FILE_TYPE, SERVICE_ACCOUNT_FILE_TYPE)); } - /** - * Saves the end user credentials in specified file - * - * @param credentials InputStream containing user credentials in JSON format - * @param filePath Path to file where to store the credentials - * @throws IOException An error saving the credentials. - */ - public void saveCredentialsToFile(InputStream credentials, String filePath) throws IOException { - final OutputStream outputStream = new FileOutputStream(new File(filePath)); - try { - ByteStreams.copy(credentials, outputStream); - } finally { - outputStream.close(); - } - } - /** * Default constructor. **/ diff --git a/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java b/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java index b87e951ad..6940a6350 100644 --- a/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java +++ b/oauth2_http/java/com/google/auth/oauth2/MemoryTokensStorage.java @@ -35,6 +35,9 @@ import java.util.HashMap; import java.util.Map; +/** + * Represents an in-memory storage of tokens. + */ public class MemoryTokensStorage implements TokenStore { private final Map tokensStorage = new HashMap<>(); diff --git a/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java b/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java index da32539e6..3ce6ae6c5 100644 --- a/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java +++ b/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java @@ -40,10 +40,14 @@ import com.google.api.client.json.jackson2.JacksonFactory; import com.google.auth.http.AuthHttpConstants; import com.google.auth.http.HttpTransportFactory; +import com.google.common.io.ByteStreams; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.math.BigDecimal; import java.net.URI; import java.nio.charset.Charset; @@ -122,6 +126,22 @@ static String validateString(Map map, String key, String errorPr return (String) value; } + /** + * Saves the end user credentials into the given file path. + * + * @param credentials InputStream containing user credentials in JSON format + * @param filePath Path to file where to store the credentials + * @throws IOException An error saving the credentials. + */ + static void saveCredentialsToFile(InputStream credentials, String filePath) throws IOException { + final OutputStream outputStream = new FileOutputStream(new File(filePath)); + try { + ByteStreams.copy(credentials, outputStream); + } finally { + outputStream.close(); + } + } + /** * Return the specified optional string from JSON or throw a helpful error message. */ diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index 49460d146..ab47f7fb9 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -292,14 +292,14 @@ private InputStream getUserCredentialsStream() throws IOException { } /** - * Saves the end user credentials in file + * Saves the end user credentials into the given file path. * * @param filePath Path to file where to store the credentials * * @throws IOException An error storing the credentials. */ public void save(String filePath) throws IOException { - super.saveCredentialsToFile(getUserCredentialsStream(), filePath); + OAuth2Utils.saveCredentialsToFile(getUserCredentialsStream(), filePath); } @Override diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java index ee5e1a9b4..6d2bbfc7f 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java @@ -513,7 +513,10 @@ public void saveUserCredentials_saved_throws() throws IOException { .setClientSecret(CLIENT_SECRET) .setRefreshToken(REFRESH_TOKEN) .build(); - String filePath = System.getProperty("user.dir") + File.separator + "GOOGLE_APPLICATION_CREDENTIALS"; + File file = File.createTempFile("GOOGLE_APPLICATION_CREDENTIALS", null, null); + file.deleteOnExit(); + + String filePath = file.getAbsolutePath(); userCredentials.save(filePath); } @@ -525,10 +528,12 @@ public void saveAndRestoreUserCredential_saveAndRestored_throws() throws IOExcep .setRefreshToken(REFRESH_TOKEN) .build(); - String filePath = System.getProperty("user.dir") + File.separator + "GOOGLE_APPLICATION_CREDENTIALS"; + File file = File.createTempFile("GOOGLE_APPLICATION_CREDENTIALS", null, null); + file.deleteOnExit(); - userCredentials.save(filePath); + String filePath = file.getAbsolutePath(); + userCredentials.save(filePath); FileInputStream inputStream = new FileInputStream(new File(filePath)); From e3d802078899ae7a3e0225c66851b899321f98dc Mon Sep 17 00:00:00 2001 From: Yury Bandarchuk Date: Fri, 3 Nov 2017 00:47:23 +0300 Subject: [PATCH 17/17] Update method name to 'writeInputStreamToFile'. --- oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java | 2 +- oauth2_http/java/com/google/auth/oauth2/UserCredentials.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java b/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java index 3ce6ae6c5..96f1f877c 100644 --- a/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java +++ b/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java @@ -133,7 +133,7 @@ static String validateString(Map map, String key, String errorPr * @param filePath Path to file where to store the credentials * @throws IOException An error saving the credentials. */ - static void saveCredentialsToFile(InputStream credentials, String filePath) throws IOException { + static void writeInputStreamToFile(InputStream credentials, String filePath) throws IOException { final OutputStream outputStream = new FileOutputStream(new File(filePath)); try { ByteStreams.copy(credentials, outputStream); diff --git a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java index ab47f7fb9..7ae0a8d11 100644 --- a/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/UserCredentials.java @@ -299,7 +299,7 @@ private InputStream getUserCredentialsStream() throws IOException { * @throws IOException An error storing the credentials. */ public void save(String filePath) throws IOException { - OAuth2Utils.saveCredentialsToFile(getUserCredentialsStream(), filePath); + OAuth2Utils.writeInputStreamToFile(getUserCredentialsStream(), filePath); } @Override