Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented in-memory TokenStore and added opportunity to save user credentials into file #129

Merged

Conversation

YuryBandarchuk16
Copy link
Contributor

Firstly, I implemented in-memory TokenStore by just using HashMap.

After that I added some methods that allows to save UserCredentials to file.

Everything, that might be affected by these changes have been tests in appropriate test files.

@tswast
Copy link
Contributor

tswast commented Sep 14, 2017

/cc @lesv @garrettjonesgoogle

@@ -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.*;

This comment was marked as spam.

This comment was marked as spam.

@@ -166,6 +165,24 @@ public static GoogleCredentials fromStream(InputStream credentialsStream,
}

/**
* Saves the end user credentials in specified file

This comment was marked as spam.

This comment was marked as spam.

@@ -58,6 +58,8 @@
*/
public class UserCredentials extends GoogleCredentials {

public static final String USER_CREDENTIALS_FILE_NAME = "GOOGLE_AUTH_USER_CREDENTIALS_FILE_NAME";

This comment was marked as spam.

Copy link
Contributor

@lesv lesv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TokenStore is only used for testing. I don't see much advantage to this change.

@YuryBandarchuk16
Copy link
Contributor Author

YuryBandarchuk16 commented Sep 14, 2017

@lesv Updated UserAuthorized, so that now it uses MemoryTokensStorage as default one if TokenStore is not specified.

@lesv
Copy link
Contributor

lesv commented Sep 14, 2017

Cool - Good to mention issues in the initial commit as then I can come up to speed on the PR.

I'm going to wait for @garrettjonesgoogle to comment before approving this. But I'm happy with the changes.

I did as a Q, in GoogleCredentials.java, what kind of credentials are you writing?

@YuryBandarchuk16
Copy link
Contributor Author

@lesv In GoogleCredentials.java I am saving the following credentials (updated comment above the method):

  • RefreshToken
  • ClientId
  • ClientSecret
  • ServerTokenUri

As in the issue, the main purpose is to store the refresh token.

Removed test, since there is always TokenStore exist by default.
@YuryBandarchuk16
Copy link
Contributor Author

@lesv In the latest test run there was a failure, because the test checked the following: in case TokenStore is not specified and you tried to get data from it, it will throw an exception. Right now, there is always a default implementation exists for TokenStore. It means that we don't need this test anymore. I removed it in this commit: 5fcddeb

@lesv
Copy link
Contributor

lesv commented Sep 15, 2017

@YuryBandarchuk16 - @garrettjonesgoogle is out until at least next week. Do you need this merged and released soon, or can hold it for a bit?

@YuryBandarchuk16
Copy link
Contributor Author

@lesv Sure, can hold, there is no rush.

@YuryBandarchuk16
Copy link
Contributor Author

@lesv are there any updates?

@lesv
Copy link
Contributor

lesv commented Sep 25, 2017

@garrettjonesgoogle Do you have an opinion? It seems like a reasonable change to me.

@garrettjonesgoogle
Copy link
Member

I will take a look.

* @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 {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;

This comment was marked as spam.

This comment was marked as spam.

@lesv
Copy link
Contributor

lesv commented Nov 2, 2017

@YuryBandarchuk16 Could you address Garrett's asks - we are trying to release this asap.

@YuryBandarchuk16
Copy link
Contributor Author

@lesv Sorry for the delay, thanks for letting me know. I think I have already fixed everything @garrettjonesgoogle asked me to fix.

Copy link
Member

@garrettjonesgoogle garrettjonesgoogle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done a deeper review now.

I don't think I'll try to include this in the release I'm about to do - I'll try to include it in a following release.

<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>

This comment was marked as spam.

@@ -36,9 +36,14 @@
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;

This comment was marked as spam.

* @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 {

This comment was marked as spam.


public class MemoryTokensStorage implements TokenStore {

private final Map<String, String> tokensStorage = new HashMap<>();

This comment was marked as spam.

@@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.google.auth.oauth2;
package com.google.auth.oauth2.storage;

This comment was marked as spam.

* @throws IOException An error saving the credentials.
*/
public void saveCredentialsToFile(InputStream credentials, String credentialsFileName) throws IOException {
String userDirectory = System.getProperty("user.dir");

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* @throws IOException An error storing the credentials.
*/
public void saveUserCredentials() throws IOException {
super.saveCredentialsToFile(getUserCredentialsStream(), USER_CREDENTIALS_FILE_NAME);

This comment was marked as spam.

@YuryBandarchuk16
Copy link
Contributor Author

@garrettjonesgoogle Could I ask you to review all the changes, please?

* @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 {

This comment was marked as spam.

}

/**
* Saves the end user credentials in file

This comment was marked as spam.

This comment was marked as spam.

.setClientSecret(CLIENT_SECRET)
.setRefreshToken(REFRESH_TOKEN)
.build();
String filePath = System.getProperty("user.dir") + File.separator + "GOOGLE_APPLICATION_CREDENTIALS";

This comment was marked as spam.

This comment was marked as spam.

import java.util.HashMap;
import java.util.Map;

public class MemoryTokensStorage implements TokenStore {

This comment was marked as spam.

@YuryBandarchuk16
Copy link
Contributor Author

@garrettjonesgoogle I think I made all the changes you asked for.

Copy link
Member

@garrettjonesgoogle garrettjonesgoogle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! I'll merge when the tests pass.

@garrettjonesgoogle garrettjonesgoogle merged commit 5e699a0 into googleapis:master Nov 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants