Improve Git/HTTPS URL generation

This commit is contained in:
Fabian Henneke 2020-05-14 09:11:22 +02:00 committed by Fabian Henneke
parent ea1c28d1e2
commit 42981cd52b

View file

@ -23,7 +23,6 @@ import com.zeapo.pwdstore.git.config.SshApiSessionFactory
import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.PasswordRepository
import com.zeapo.pwdstore.utils.getEncryptedPrefs import com.zeapo.pwdstore.utils.getEncryptedPrefs
import java.io.File import java.io.File
import java.net.MalformedURLException
import java.net.URI import java.net.URI
/** /**
@ -94,35 +93,42 @@ abstract class BaseGitActivity : AppCompatActivity() {
return false return false
val previousUrl = url ?: "" val previousUrl = url ?: ""
val hostnamePart = serverHostname // Whether we need the leading ssh:// depends on the use of a custom port.
val pathPart = if (serverPath.startsWith('/')) serverPath else "/$serverPath" val hostnamePart = serverHostname.removePrefix("ssh://")
// We only support relative paths and trim everything scheme-specific.
val pathPart = serverPath.trimStart('/', ':')
val newUrl = when (protocol) { val newUrl = when (protocol) {
Protocol.Ssh -> { Protocol.Ssh -> {
val userPart = if (serverUser.isEmpty()) "" else "$serverUser@" val userPart = if (serverUser.isEmpty()) "" else "${serverUser.trimEnd('@')}@"
val portPart = val portPart =
if (serverPort == "22" || serverPort.isEmpty()) "" else ":$serverPort" if (serverPort == "22" || serverPort.isEmpty()) "" else ":$serverPort"
if (hostnamePart.startsWith("ssh://")) if (portPart.isEmpty()) {
hostnamePart.replace("ssh://", "") "$userPart$hostnamePart:$pathPart"
// We have to specify the ssh scheme as this is the only way to pass a custom port. } else {
"ssh://$userPart$hostnamePart$portPart$pathPart" // 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 -> { Protocol.Https -> {
val portPart = val portPart =
if (serverPort == "443" || serverPort.isEmpty()) "" else ":$serverPort" if (serverPort == "443" || serverPort.isEmpty()) "" else ":$serverPort"
val urlWithFreeEntryScheme = "$hostnamePart$portPart$pathPart" val urlWithFreeEntryScheme = "$hostnamePart$portPart/$pathPart"
when { val url = when {
urlWithFreeEntryScheme.startsWith("https://") -> urlWithFreeEntryScheme urlWithFreeEntryScheme.startsWith("https://") -> urlWithFreeEntryScheme
urlWithFreeEntryScheme.startsWith("http://") -> urlWithFreeEntryScheme.replaceFirst("http", "https") urlWithFreeEntryScheme.startsWith("http://") -> urlWithFreeEntryScheme.replaceFirst("http", "https")
else -> "https://$urlWithFreeEntryScheme" 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) if (PasswordRepository.isInitialized)
PasswordRepository.addRemote("origin", newUrl, true) PasswordRepository.addRemote("origin", newUrl, true)
// HTTPS authentication sends the password to the server, so we must wipe the password when // HTTPS authentication sends the password to the server, so we must wipe the password when