From 07d60f9f9576a2e5bee20b45e2002c30007db317 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 13 Dec 2016 12:30:35 +0100 Subject: [PATCH] IDEA-164692 Only one instance of password is saved --- .../src/CredentialStoreWrapper.kt | 2 +- .../src/KeePassCredentialStore.kt | 2 +- .../credential-store/src/credentialStore.kt | 2 -- .../test/CredentialStoreTest.kt | 2 +- .../credentialStore/CredentialAttributes.kt | 5 +++-- .../diagnostic/ErrorReportConfigurable.kt | 3 ++- .../execution/HgCommandAuthenticator.java | 22 ++++++++++--------- .../src/IcsCredentialsStore.kt | 3 ++- 8 files changed, 22 insertions(+), 19 deletions(-) diff --git a/platform/credential-store/src/CredentialStoreWrapper.kt b/platform/credential-store/src/CredentialStoreWrapper.kt index 9d0d6720e333..7c1bf27898f5 100644 --- a/platform/credential-store/src/CredentialStoreWrapper.kt +++ b/platform/credential-store/src/CredentialStoreWrapper.kt @@ -80,7 +80,7 @@ private class CredentialStoreWrapper(private val store: CredentialStore) : Passw val appInfo = ApplicationInfoEx.getInstanceEx() if (appInfo.isEAP || appInfo.build.isSnapshot) { - setNew(CredentialAttributes("IntelliJ Platform", "${requestor.name}/$userName"))?.let { return it } + setNew(CredentialAttributes(SERVICE_NAME_PREFIX, "${requestor.name}/$userName"))?.let { return it } } } return null diff --git a/platform/credential-store/src/KeePassCredentialStore.kt b/platform/credential-store/src/KeePassCredentialStore.kt index 5075814fdec3..f0be5377838c 100644 --- a/platform/credential-store/src/KeePassCredentialStore.kt +++ b/platform/credential-store/src/KeePassCredentialStore.kt @@ -33,7 +33,7 @@ import java.util.* import java.util.concurrent.atomic.AtomicBoolean import javax.crypto.spec.SecretKeySpec -private const val GROUP_NAME = "IntelliJ Platform" +private const val GROUP_NAME = SERVICE_NAME_PREFIX internal val DB_FILE_NAME = "c.kdbx" diff --git a/platform/credential-store/src/credentialStore.kt b/platform/credential-store/src/credentialStore.kt index da8e458391ad..781d51e7d072 100644 --- a/platform/credential-store/src/credentialStore.kt +++ b/platform/credential-store/src/credentialStore.kt @@ -27,8 +27,6 @@ import javax.crypto.spec.SecretKeySpec internal val LOG = Logger.getInstance(CredentialStore::class.java) -internal val SERVICE_NAME_PREFIX = "IntelliJ Platform" - private fun toOldKey(hash: ByteArray) = "old-hashed-key|" + Base64.getEncoder().encodeToString(hash) internal fun toOldKeyAsIdentity(hash: ByteArray) = CredentialAttributes(SERVICE_NAME_PREFIX, toOldKey(hash)) diff --git a/platform/credential-store/test/CredentialStoreTest.kt b/platform/credential-store/test/CredentialStoreTest.kt index d75c32ef448a..148f22875eca 100644 --- a/platform/credential-store/test/CredentialStoreTest.kt +++ b/platform/credential-store/test/CredentialStoreTest.kt @@ -6,7 +6,7 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.Test import java.util.* -private const val TEST_SERVICE_NAME = "IntelliJ Platform Test" +private const val TEST_SERVICE_NAME = "$SERVICE_NAME_PREFIX Test" inline fun macTest(task: () -> Unit) { if (SystemInfo.isMacIntel64 && !UsefulTestCase.IS_UNDER_TEAMCITY) { diff --git a/platform/platform-api/src/com/intellij/credentialStore/CredentialAttributes.kt b/platform/platform-api/src/com/intellij/credentialStore/CredentialAttributes.kt index c156af30d340..7a1dc0f3ccb1 100644 --- a/platform/platform-api/src/com/intellij/credentialStore/CredentialAttributes.kt +++ b/platform/platform-api/src/com/intellij/credentialStore/CredentialAttributes.kt @@ -26,11 +26,12 @@ import java.nio.CharBuffer import java.nio.charset.CodingErrorAction import java.util.concurrent.atomic.AtomicReference +const val SERVICE_NAME_PREFIX = "IntelliJ Platform" + /** * requestor is deprecated. Never use it in new code. */ -data class CredentialAttributes @JvmOverloads constructor(val serviceName: String, val userName: String? = null, val requestor: Class<*>? = null, val isPasswordMemoryOnly: Boolean = false) { -} +data class CredentialAttributes @JvmOverloads constructor(val serviceName: String, val userName: String? = null, val requestor: Class<*>? = null, val isPasswordMemoryOnly: Boolean = false) fun CredentialAttributes.toPasswordStoreable() = if (isPasswordMemoryOnly) CredentialAttributes(serviceName, userName, requestor) else this diff --git a/platform/platform-impl/src/com/intellij/diagnostic/ErrorReportConfigurable.kt b/platform/platform-impl/src/com/intellij/diagnostic/ErrorReportConfigurable.kt index f111eacfa481..6049c4619318 100644 --- a/platform/platform-impl/src/com/intellij/diagnostic/ErrorReportConfigurable.kt +++ b/platform/platform-impl/src/com/intellij/diagnostic/ErrorReportConfigurable.kt @@ -17,6 +17,7 @@ package com.intellij.diagnostic import com.intellij.credentialStore.CredentialAttributes import com.intellij.credentialStore.Credentials +import com.intellij.credentialStore.SERVICE_NAME_PREFIX import com.intellij.ide.passwordSafe.PasswordSafe import com.intellij.openapi.components.* import com.intellij.util.io.decodeBase64 @@ -33,7 +34,7 @@ internal class ErrorReportConfigurable : PersistentStateComponent { companion object { @JvmStatic - val SERVICE_NAME = "IntelliJ Platform — JetBrains Account" + val SERVICE_NAME = "$SERVICE_NAME_PREFIX — JetBrains Account" val instance: ErrorReportConfigurable get() = ServiceManager.getService(ErrorReportConfigurable::class.java) diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandAuthenticator.java b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandAuthenticator.java index a0017effbf07..279c661e2b98 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandAuthenticator.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandAuthenticator.java @@ -12,6 +12,8 @@ // limitations under the License. package org.zmlx.hg4idea.execution; +import com.intellij.credentialStore.CredentialAttributes; +import com.intellij.credentialStore.CredentialAttributesKt; import com.intellij.credentialStore.Credentials; import com.intellij.ide.passwordSafe.PasswordSafe; import com.intellij.openapi.application.ApplicationManager; @@ -26,8 +28,6 @@ import org.zmlx.hg4idea.HgGlobalSettings; import org.zmlx.hg4idea.HgVcs; import org.zmlx.hg4idea.HgVcsMessages; -import static com.intellij.credentialStore.CredentialAttributesKt.CredentialAttributes; - /** * Base class for any command interacting with a remote repository and which needs authentication. */ @@ -45,7 +45,7 @@ class HgCommandAuthenticator { public void forgetPassword() { if (myGetPassword == null) return; // prompt was not suggested; String url = VirtualFileManager.extractPath(myGetPassword.getURL()); - PasswordSafe.getInstance().setPassword(HgCommandAuthenticator.class, keyForUrlAndLogin(url, getUserName()), null); + PasswordSafe.getInstance().set(createCredentialAttributes(url, null), null); } public boolean promptForAuthentication(Project project, @NotNull String proposedLogin, @NotNull String uri, @NotNull String path, @Nullable ModalityState state) { @@ -84,6 +84,7 @@ class HgCommandAuthenticator { mySilent = silent; } + @Override public void run() { // find if we've already been here @@ -105,10 +106,10 @@ class HgCommandAuthenticator { login = rememberedLoginsForUrl; } - String password = null; - if (!StringUtil.isEmptyOrSpaces(login)) { - // if we've logged in with this login, search for password - password = PasswordSafe.getInstance().getPassword(HgCommandAuthenticator.class, keyForUrlAndLogin(url, login)); + Credentials savedCredentials = PasswordSafe.getInstance().get(createCredentialAttributes(url, null)); + String password = savedCredentials == null ? null : savedCredentials.getPasswordAsString(); + if (savedCredentials != null && StringUtil.isEmptyOrSpaces(login)) { + login = savedCredentials.getUserName(); } // don't show dialog if we don't have to (both fields are known) except force authorization required @@ -130,7 +131,7 @@ class HgCommandAuthenticator { ok = true; Credentials credentials = new Credentials(dialog.getUsername(), dialog.getPassword()); myCredentials = credentials; - PasswordSafe.getInstance().set(CredentialAttributes(HgCommandAuthenticator.class, keyForUrlAndLogin(url, credentials.getUserName())), credentials, !dialog.isRememberPassword()); + PasswordSafe.getInstance().set(createCredentialAttributes(url, login), credentials, !dialog.isRememberPassword()); hgGlobalSettings.addRememberedUrl(url, credentials.getUserName()); } } @@ -153,7 +154,8 @@ class HgCommandAuthenticator { } } - private static String keyForUrlAndLogin(String stringUrl, String login) { - return login + ":" + stringUrl; + @NotNull + private static CredentialAttributes createCredentialAttributes(@NotNull String url, @Nullable String login) { + return new CredentialAttributes(CredentialAttributesKt.SERVICE_NAME_PREFIX + " HG — " + url, login); } } diff --git a/plugins/settings-repository/src/IcsCredentialsStore.kt b/plugins/settings-repository/src/IcsCredentialsStore.kt index 557c73a2c1aa..e7420c9c1434 100644 --- a/plugins/settings-repository/src/IcsCredentialsStore.kt +++ b/plugins/settings-repository/src/IcsCredentialsStore.kt @@ -17,6 +17,7 @@ package org.jetbrains.settingsRepository import com.intellij.credentialStore.CredentialAttributes import com.intellij.credentialStore.Credentials +import com.intellij.credentialStore.SERVICE_NAME_PREFIX import com.intellij.ide.passwordSafe.PasswordSafe /** @@ -33,7 +34,7 @@ class IcsCredentialsStore() { private fun CredentialAttributes(host: String?, sshKeyFile: String?, accountName: String?): CredentialAttributes? { if (sshKeyFile == null) { - return CredentialAttributes("IntelliJ Platform Settings Repository — $host", accountName) + return CredentialAttributes("$SERVICE_NAME_PREFIX Settings Repository — $host", accountName) } else { return CredentialAttributes("SSH", sshKeyFile)