From 8ec3320df793b58d20f632038b0b2a37f37103e3 Mon Sep 17 00:00:00 2001 From: Fabian Henneke Date: Wed, 26 Aug 2020 16:21:27 +0200 Subject: Get rid of explicit Git server protocol (#1054) * Double check Git server protocol Ensure that the Git server protocol is not at odds with the URL scheme. Also move the Protocol switches below the URL to make it clear that the URL should be entered first. * Remove protocol selection from server config The protocol is now extracted from the URL, and the authentication mode selection is validated by GitSettings Signed-off-by: Harsh Shandilya * Don't use pref values for auth modes Signed-off-by: Harsh Shandilya * Apply suggestions from code review Remove now unused protocol mismatch result type Co-authored-by: Fabian Henneke * Simplify migration logic and fix tests Signed-off-by: Harsh Shandilya * Revert "Simplify migration logic and fix tests" This reverts commit 1c4c4ba5fbc212843cb6b74dd29ac858eaea7582. * Detect URLs with null scheme as ssh * Add changelog entry Signed-off-by: Harsh Shandilya Co-authored-by: Harsh Shandilya Co-authored-by: Harsh Shandilya --- .../com/zeapo/pwdstore/git/config/GitSettings.kt | 37 ++++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/config') diff --git a/app/src/main/java/com/zeapo/pwdstore/git/config/GitSettings.kt b/app/src/main/java/com/zeapo/pwdstore/git/config/GitSettings.kt index b9c572fc..b0d931e0 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/config/GitSettings.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/config/GitSettings.kt @@ -53,13 +53,6 @@ object GitSettings { private val settings by lazy { Application.instance.sharedPrefs } private val encryptedSettings by lazy { Application.instance.getEncryptedPrefs("git_operation") } - var protocol - get() = Protocol.fromString(settings.getString(PreferenceKeys.GIT_REMOTE_PROTOCOL)) - private set(value) { - settings.edit { - putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, value.pref) - } - } var authMode get() = AuthMode.fromString(settings.getString(PreferenceKeys.GIT_REMOTE_AUTH)) private set(value) { @@ -104,23 +97,39 @@ object GitSettings { } } - enum class UpdateConnectionSettingsResult { - Valid, - FailedToParseUrl, - MissingUsername, + sealed class UpdateConnectionSettingsResult { + class MissingUsername(val newProtocol: Protocol) : UpdateConnectionSettingsResult() + class AuthModeMismatch(val newProtocol: Protocol, val validModes: List) : UpdateConnectionSettingsResult() + object Valid : UpdateConnectionSettingsResult() + object FailedToParseUrl : UpdateConnectionSettingsResult() } - fun updateConnectionSettingsIfValid(newProtocol: Protocol, newAuthMode: AuthMode, newUrl: String, newBranch: String): UpdateConnectionSettingsResult { + fun updateConnectionSettingsIfValid(newAuthMode: AuthMode, newUrl: String, newBranch: String): UpdateConnectionSettingsResult { val parsedUrl = try { URIish(newUrl) } catch (_: Exception) { return UpdateConnectionSettingsResult.FailedToParseUrl } + val newProtocol = when (parsedUrl.scheme) { + in listOf("http", "https") -> Protocol.Https + in listOf("ssh", null) -> Protocol.Ssh + else -> return UpdateConnectionSettingsResult.FailedToParseUrl + } if (newAuthMode != AuthMode.None && parsedUrl.user.isNullOrBlank()) - return UpdateConnectionSettingsResult.MissingUsername + return UpdateConnectionSettingsResult.MissingUsername(newProtocol) + + val validHttpsAuth = listOf(AuthMode.None, AuthMode.Password) + val validSshAuth = listOf(AuthMode.OpenKeychain, AuthMode.Password, AuthMode.SshKey) + when { + newProtocol == Protocol.Https && newAuthMode !in validHttpsAuth -> { + return UpdateConnectionSettingsResult.AuthModeMismatch(newProtocol, validHttpsAuth) + } + newProtocol == Protocol.Ssh && newAuthMode !in validSshAuth -> { + return UpdateConnectionSettingsResult.AuthModeMismatch(newProtocol, validSshAuth) + } + } url = newUrl - protocol = newProtocol authMode = newAuthMode branch = newBranch return UpdateConnectionSettingsResult.Valid -- cgit v1.2.3