From d7013bf73308af2b0b0a59fff095c99d37486b54 Mon Sep 17 00:00:00 2001 From: Chris Lemaire Date: Tue, 14 May 2024 15:56:50 +0200 Subject: [PATCH] [gitlab] Consider server URIs starting with HTTP/HTTPS the same (IJPL-148811) GitOrigin-RevId: 6de72d9a4260e63bd515ff7ef46d47f20d32eb4d --- .../org/jetbrains/plugins/gitlab/api/GitLabServerPath.kt | 9 +++++++++ .../plugins/gitlab/authentication/GitLabLoginUtil.kt | 3 ++- .../authentication/accounts/GitLabAccountManager.kt | 3 ++- .../gitlab/git/http/GitLabHttpAuthDataProvider.kt | 3 ++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/api/GitLabServerPath.kt b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/api/GitLabServerPath.kt index 2c85c6f8c8e9..422d9ed50ffc 100644 --- a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/api/GitLabServerPath.kt +++ b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/api/GitLabServerPath.kt @@ -4,6 +4,7 @@ package org.jetbrains.plugins.gitlab.api import com.intellij.collaboration.api.ServerPath import com.intellij.collaboration.util.resolveRelative import com.intellij.openapi.util.NlsSafe +import com.intellij.util.withScheme import kotlinx.serialization.Serializable import java.net.URI import java.net.URL @@ -60,4 +61,12 @@ class GitLabServerPath : ServerPath { companion object { val DEFAULT_SERVER = GitLabServerPath("https://gitlab.com") } +} + +/** + * Gets the URI for this server path with HTTP schemas replaced with HTTPS. + */ +internal fun GitLabServerPath.toHttpsNormalizedURI(): URI { + val uri = toURI() + return uri.withScheme(if (uri.scheme == "http") "https" else uri.scheme) } \ No newline at end of file diff --git a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/GitLabLoginUtil.kt b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/GitLabLoginUtil.kt index d373fc54ba78..30374301fdf8 100644 --- a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/GitLabLoginUtil.kt +++ b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/GitLabLoginUtil.kt @@ -13,6 +13,7 @@ import com.intellij.util.asSafely import com.intellij.util.concurrency.annotations.RequiresEdt import org.jetbrains.annotations.Nls import org.jetbrains.plugins.gitlab.api.GitLabServerPath +import org.jetbrains.plugins.gitlab.api.toHttpsNormalizedURI import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccount import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabProjectDefaultAccountHolder import org.jetbrains.plugins.gitlab.authentication.ui.GitLabChooseAccountDialog @@ -127,7 +128,7 @@ object GitLabLoginUtil { } fun isAccountUnique(accounts: Collection, server: GitLabServerPath, username: String): Boolean = - accounts.none { it.server == server && it.name == username } + accounts.none { it.server.toHttpsNormalizedURI() == server.toHttpsNormalizedURI() && it.name == username } } sealed interface LoginResult { diff --git a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/accounts/GitLabAccountManager.kt b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/accounts/GitLabAccountManager.kt index 9d49d7a92e13..1188340bbc98 100644 --- a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/accounts/GitLabAccountManager.kt +++ b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/authentication/accounts/GitLabAccountManager.kt @@ -9,6 +9,7 @@ import com.intellij.openapi.Disposable import com.intellij.openapi.components.service import com.intellij.openapi.diagnostic.logger import org.jetbrains.plugins.gitlab.api.GitLabServerPath +import org.jetbrains.plugins.gitlab.api.toHttpsNormalizedURI import org.jetbrains.plugins.gitlab.util.GitLabUtil internal interface GitLabAccountManager : AccountManager, Disposable { @@ -31,7 +32,7 @@ class PersistentGitLabAccountManager : override fun isAccountUnique(server: GitLabServerPath, accountName: String): Boolean { return accountsState.value.none { account: GitLabAccount -> - account.server == server && account.name == accountName + account.server.toHttpsNormalizedURI() == server.toHttpsNormalizedURI() && account.name == accountName } } } \ No newline at end of file diff --git a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/git/http/GitLabHttpAuthDataProvider.kt b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/git/http/GitLabHttpAuthDataProvider.kt index edf6ef3b9928..c21b2a618c66 100644 --- a/plugins/gitlab/src/org/jetbrains/plugins/gitlab/git/http/GitLabHttpAuthDataProvider.kt +++ b/plugins/gitlab/src/org/jetbrains/plugins/gitlab/git/http/GitLabHttpAuthDataProvider.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.jetbrains.plugins.gitlab.GitLabServersManager import org.jetbrains.plugins.gitlab.api.GitLabServerPath +import org.jetbrains.plugins.gitlab.api.toHttpsNormalizedURI import org.jetbrains.plugins.gitlab.authentication.GitLabLoginUtil import org.jetbrains.plugins.gitlab.authentication.LoginResult import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccountManager @@ -69,7 +70,7 @@ private suspend fun findKnownMatchingServer(url: String): GitLabServerPath? { private suspend fun getLoginResult(project: Project, url: String, server: GitLabServerPath, login: String?): LoginResult { val accountManager = service() val accountsWithTokens = accountManager.accountsState.value - .filter { it.server == server } + .filter { it.server.toHttpsNormalizedURI() == server.toHttpsNormalizedURI() } .associateWith { accountManager.findCredentials(it) } val loginResult = withContext(Dispatchers.EDT + ModalityState.any().asContextElement()) {