From 42981cd52ba6daa6b15af955bddd75abcc6be791 Mon Sep 17 00:00:00 2001 From: Fabian Henneke Date: Thu, 14 May 2020 09:11:22 +0200 Subject: Improve Git/HTTPS URL generation --- .../java/com/zeapo/pwdstore/git/BaseGitActivity.kt | 38 +++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt index 7f14cd7a..3421e6a8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/BaseGitActivity.kt @@ -23,7 +23,6 @@ import com.zeapo.pwdstore.git.config.SshApiSessionFactory import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.getEncryptedPrefs import java.io.File -import java.net.MalformedURLException import java.net.URI /** @@ -94,35 +93,42 @@ abstract class BaseGitActivity : AppCompatActivity() { return false val previousUrl = url ?: "" - val hostnamePart = serverHostname - val pathPart = if (serverPath.startsWith('/')) serverPath else "/$serverPath" + // Whether we need the leading ssh:// depends on the use of a custom port. + val hostnamePart = serverHostname.removePrefix("ssh://") + // We only support relative paths and trim everything scheme-specific. + val pathPart = serverPath.trimStart('/', ':') val newUrl = when (protocol) { Protocol.Ssh -> { - val userPart = if (serverUser.isEmpty()) "" else "$serverUser@" + val userPart = if (serverUser.isEmpty()) "" else "${serverUser.trimEnd('@')}@" val portPart = if (serverPort == "22" || serverPort.isEmpty()) "" else ":$serverPort" - if (hostnamePart.startsWith("ssh://")) - hostnamePart.replace("ssh://", "") - // We have to specify the ssh scheme as this is the only way to pass a custom port. - "ssh://$userPart$hostnamePart$portPart$pathPart" + if (portPart.isEmpty()) { + "$userPart$hostnamePart:$pathPart" + } else { + // We have to specify the ssh scheme as this is the only way to pass a custom + // port. + "ssh://$userPart$hostnamePart$portPart/$pathPart" + } } Protocol.Https -> { val portPart = if (serverPort == "443" || serverPort.isEmpty()) "" else ":$serverPort" - val urlWithFreeEntryScheme = "$hostnamePart$portPart$pathPart" - when { + val urlWithFreeEntryScheme = "$hostnamePart$portPart/$pathPart" + val url = when { urlWithFreeEntryScheme.startsWith("https://") -> urlWithFreeEntryScheme urlWithFreeEntryScheme.startsWith("http://") -> urlWithFreeEntryScheme.replaceFirst("http", "https") else -> "https://$urlWithFreeEntryScheme" } + try { + if (URI(url).rawAuthority != null) + url + else + return false + } catch (_: Exception) { + return false + } } } - try { - if (URI(newUrl).rawAuthority == null) - return false - } catch (_: MalformedURLException) { - return false - } if (PasswordRepository.isInitialized) PasswordRepository.addRemote("origin", newUrl, true) // HTTPS authentication sends the password to the server, so we must wipe the password when -- cgit v1.2.3