summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRajeev Dayal <rdayal@google.com>2014-05-14 16:39:55 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2014-05-14 16:39:56 +0000
commitaa76e3e21d43183a5578cac1cffd49803415aaaf (patch)
treed98c0acb826f1a0b9d8b8748d774512abf1ada86
parent55aabfef31bfafa38ea36dc9d21cac0f5386d307 (diff)
parent5f9f76486b8845fbe2d69060a7490ab361fced0e (diff)
downloadcloud-aa76e3e21d43183a5578cac1cffd49803415aaaf.tar.gz
Merge "Google Login - Add functionality to store users data persistently." into idea133
-rw-r--r--lib/google.login.jarbin0 -> 19046 bytes
-rw-r--r--login/login.iml25
-rw-r--r--login/src/com/google/android/login/GoogleLoginPrefs.java179
3 files changed, 204 insertions, 0 deletions
diff --git a/lib/google.login.jar b/lib/google.login.jar
new file mode 100644
index 0000000..56a11a3
--- /dev/null
+++ b/lib/google.login.jar
Binary files differ
diff --git a/login/login.iml b/login/login.iml
new file mode 100644
index 0000000..1cb9e89
--- /dev/null
+++ b/login/login.iml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <exclude-output />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="module-library">
+ <library>
+ <CLASSES>
+ <root url="jar://$MODULE_DIR$/../lib/google.login.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES>
+ <root url="jar://$MODULE_DIR$/../lib/google.login.jar!/" />
+ </SOURCES>
+ </library>
+ </orderEntry>
+ <orderEntry type="module" module-name="openapi" />
+ <orderEntry type="library" name="Guava" level="project" />
+ </component>
+</module>
+
diff --git a/login/src/com/google/android/login/GoogleLoginPrefs.java b/login/src/com/google/android/login/GoogleLoginPrefs.java
new file mode 100644
index 0000000..ca12801
--- /dev/null
+++ b/login/src/com/google/android/login/GoogleLoginPrefs.java
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.android.login;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
+import com.google.gdt.eclipse.login.common.OAuthData;
+
+import com.intellij.openapi.diagnostic.Logger;
+
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.prefs.BackingStoreException;
+import java.util.prefs.Preferences;
+
+/**
+ * The class responsible for storing the active user's {@link OAuthData}
+ * object persistently, retrieving it, and clearing it. Only the active
+ * user's data is managed at any given time.
+ */
+public class GoogleLoginPrefs {
+ // Delimiter for the list of scopes.
+ private static final String SCOPE_DELIMITER = " ";
+
+ private static final String PREFERENCES_PATH = "/com/google/android/login";
+ private static String preferencesPath = PREFERENCES_PATH;
+
+ private static final String OAUTH_DATA_EMAIL_KEY = "credentials_email";
+ private static final String OAUTH_DATA_ACCESS_TOKEN_KEY = "credentials_access_token";
+ private static final String OAUTH_DATA_ACCESS_TOKEN_EXPIRY_TIME_KEY =
+ "credentials_access_token_expiry_time";
+ private static final String OAUTH_DATA_REFRESH_TOKEN_KEY = "credentials_refresh_token";
+ private static final String ICON_ONLY_KEY = "icon_only";
+ private static final String LOGOUT_ON_EXIT_KEY = "logout_on_exit";
+ private static final String OAUTH_SCOPES_KEY = "oauth_scopes";
+
+ public static final Logger LOG = Logger.getInstance(GoogleLoginPrefs.class);
+
+ /**
+ * Stores the specified {@link OAuthData} object for the active user persistently.
+ * @param credentials the specified {@code Credentials object}
+ */
+ public static void saveOAuthData(OAuthData credentials) {
+ Preferences prefs = getPrefs();
+ prefs.put(getCustomUserKey(OAUTH_DATA_ACCESS_TOKEN_KEY), credentials.getAccessToken());
+ prefs.put(getCustomUserKey(OAUTH_DATA_REFRESH_TOKEN_KEY), credentials.getRefreshToken());
+ prefs.put(
+ getCustomUserKey(OAUTH_DATA_ACCESS_TOKEN_EXPIRY_TIME_KEY),
+ Long.toString(credentials.getAccessTokenExpiryTime()));
+
+ // we save the scopes so that if the user updates the plugin and the
+ // scopes change, we can force the plugin to log out.
+ Joiner joiner = Joiner.on(SCOPE_DELIMITER);
+ prefs.put(getCustomUserKey(OAUTH_SCOPES_KEY), joiner.join(credentials.getStoredScopes()));
+
+ String storedEmail = credentials.getStoredEmail();
+ if (storedEmail != null) {
+ prefs.put(getCustomUserKey(OAUTH_DATA_EMAIL_KEY), storedEmail);
+ }
+ flushPrefs(prefs);
+ }
+
+ /**
+ * Retrieves the persistently stored {@link OAuthData} object for the active user, if any.
+ * @return the persistently stored {@code OAuthData} object for the active user if it exists or
+ * an {@code OAuthData} object all of whose getters return {@code null} .
+ */
+ public static OAuthData loadOAuthData() {
+ Preferences prefs = getPrefs();
+
+ String accessToken = prefs.get(getCustomUserKey(OAUTH_DATA_ACCESS_TOKEN_KEY), null);
+ String refreshToken = prefs.get(getCustomUserKey(OAUTH_DATA_REFRESH_TOKEN_KEY), null);
+ String storedEmail = prefs.get(getCustomUserKey(OAUTH_DATA_EMAIL_KEY), null);
+ String storedScopesString = prefs.get(getCustomUserKey(OAUTH_SCOPES_KEY), null);
+
+ // Use a set to ensure uniqueness.
+ SortedSet<String> storedScopes = new TreeSet<String>();
+ if (storedScopesString != null) {
+ for (String scope : storedScopesString.split(SCOPE_DELIMITER)) {
+ storedScopes.add(scope);
+ }
+ }
+ long accessTokenExpiryTime = 0;
+ String accessTokenExpiryTimeString = prefs.get(getCustomUserKey(OAUTH_DATA_ACCESS_TOKEN_EXPIRY_TIME_KEY), null);
+ if (accessTokenExpiryTimeString != null) {
+ accessTokenExpiryTime = Long.parseLong(accessTokenExpiryTimeString);
+ }
+ return new OAuthData(
+ accessToken, refreshToken, storedEmail, storedScopes, accessTokenExpiryTime);
+ }
+
+ /**
+ * Clears the persistently stored {@link OAuthData} object for the active user, if any.
+ */
+ public static void clearStoredOAuthData() {
+ Preferences prefs = getPrefs();
+ prefs.remove(getCustomUserKey(OAUTH_DATA_ACCESS_TOKEN_KEY));
+ prefs.remove(getCustomUserKey(OAUTH_DATA_REFRESH_TOKEN_KEY));
+ prefs.remove(getCustomUserKey(OAUTH_DATA_EMAIL_KEY));
+ prefs.remove(getCustomUserKey(OAUTH_SCOPES_KEY));
+ prefs.remove(getCustomUserKey(OAUTH_DATA_ACCESS_TOKEN_EXPIRY_TIME_KEY));
+ flushPrefs(prefs);
+ }
+
+ /**
+ * Stores the specified preference of the active user to display only the icon in the login panel.
+ * @param logoutOnExit the preference of the active user to display only the icon in the
+ * login panel.
+ */
+ public static void saveIconOnlyPref(boolean logoutOnExit) {
+ Preferences prefs = getPrefs();
+ prefs.putBoolean(getCustomUserKey(ICON_ONLY_KEY), logoutOnExit);
+ flushPrefs(prefs);
+ }
+
+ /**
+ * Retrieves the persistently stored preference of the active user, if any, to
+ * logout on closing Android Studio.
+ * @return the persistently stored preference of the active user, if any, to
+ * logout on closing Android Studio or false if preference does not exist.
+ */
+ public static boolean getLogoutOnExitPref() {
+ return getPrefs().getBoolean(getCustomUserKey(LOGOUT_ON_EXIT_KEY), false);
+ }
+
+ /**
+ * Stores the specified preference of the active user to logout on closing Android Studio.
+ * @param logoutOnExit the preference of the active user to logout on closing Android Studio.
+ */
+ public static void saveLogoutOnExitPref(boolean logoutOnExit) {
+ Preferences prefs = getPrefs();
+ prefs.putBoolean(getCustomUserKey(LOGOUT_ON_EXIT_KEY), logoutOnExit);
+ flushPrefs(prefs);
+ }
+
+ @VisibleForTesting
+ public static void setTestPreferencesPath(String testPreferencesPath) {
+ preferencesPath = testPreferencesPath;
+ }
+
+ private static void flushPrefs(Preferences prefs) {
+ try {
+ prefs.flush();
+ } catch (BackingStoreException e) {
+ LOG.error("Could not flush preferences while saving login credentials", e);
+ }
+ }
+
+ private static Preferences getPrefs() {
+ return Preferences.userRoot().node(preferencesPath);
+ }
+
+ private static String getCustomUserKey(String key) {
+
+ // ToDo: uncomment when merged User object
+ /**
+ User activeUser = GoogleLogin.getInstance().getActiveUser();
+ if(activeUser == null) {
+ return key;
+ }
+
+ return key + "_" + activeUser.getEmail();
+ **/
+ return key;
+ }
+}