-
Notifications
You must be signed in to change notification settings - Fork 229
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
Implemented in-memory TokenStore and added opportunity to save user credentials into file #129
Conversation
… save current credentials in UserCredentials.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.*; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this 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.
@lesv Updated UserAuthorized, so that now it uses MemoryTokensStorage as default one if TokenStore is not specified. |
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? |
@lesv In GoogleCredentials.java I am saving the following credentials (updated comment above the method):
As in the issue, the main purpose is to store the refresh token. |
Removed test, since there is always TokenStore exist by default.
@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 |
@YuryBandarchuk16 - @garrettjonesgoogle is out until at least next week. Do you need this merged and released soon, or can hold it for a bit? |
@lesv Sure, can hold, there is no rush. |
@lesv are there any updates? |
@garrettjonesgoogle Do you have an opinion? It seems like a reasonable change to me. |
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@YuryBandarchuk16 Could you address Garrett's asks - we are trying to release this asap. |
…veCredentialsToFile'.
…archuk16/google-auth-library-java into MemoryCrenetialsStorage
@lesv Sorry for the delay, thanks for letting me know. I think I have already fixed everything @garrettjonesgoogle asked me to fix. |
There was a problem hiding this 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.
oauth2_http/pom.xml
Outdated
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
<dependency> |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
This comment was marked as spam.
Sorry, something went wrong.
* @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.
Sorry, something went wrong.
|
||
public class MemoryTokensStorage implements TokenStore { | ||
|
||
private final Map<String, String> tokensStorage = new HashMap<>(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
This comment was marked as spam.
Sorry, something went wrong.
* @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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
* @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.
This comment was marked as spam.
Sorry, something went wrong.
… to copy the inputStream to fileOutputStream.
… packages and indentation.
@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.
This comment was marked as spam.
Sorry, something went wrong.
} | ||
|
||
/** | ||
* Saves the end user credentials in file |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
.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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class MemoryTokensStorage implements TokenStore { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@garrettjonesgoogle I think I made all the changes you asked for. |
There was a problem hiding this 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.
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.