Don't show an error if password prompt is cancelled (#833)

This commit is contained in:
Fabian Henneke 2020-06-04 16:31:25 +02:00 committed by GitHub
parent 4172c70c86
commit 2ab44a4580
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 13 deletions

View file

@ -13,6 +13,8 @@ import com.github.ajalt.timberkt.e
import com.zeapo.pwdstore.PasswordStore import com.zeapo.pwdstore.PasswordStore
import com.zeapo.pwdstore.R import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.git.config.SshjSessionFactory import com.zeapo.pwdstore.git.config.SshjSessionFactory
import net.schmizz.sshj.common.DisconnectReason
import net.schmizz.sshj.common.SSHException
import net.schmizz.sshj.userauth.UserAuthException import net.schmizz.sshj.userauth.UserAuthException
import org.eclipse.jgit.api.CommitCommand import org.eclipse.jgit.api.CommitCommand
import org.eclipse.jgit.api.GitCommand import org.eclipse.jgit.api.GitCommand
@ -127,16 +129,36 @@ class GitAsyncTask(
return rootCause return rootCause
} }
private fun isExplicitlyUserInitiatedError(e: Exception): Boolean {
var cause: Exception? = e
while (cause != null) {
if (cause is SSHException &&
cause.disconnectReason == DisconnectReason.AUTH_CANCELLED_BY_USER)
return true
cause = cause.cause as? Exception
}
return false
}
override fun onPostExecute(maybeResult: Result?) { override fun onPostExecute(maybeResult: Result?) {
dialog.dismiss() dialog.dismiss()
when (val result = maybeResult ?: Result.Err(IOException("Unexpected error"))) { when (val result = maybeResult ?: Result.Err(IOException("Unexpected error"))) {
is Result.Err -> { is Result.Err -> {
if (isExplicitlyUserInitiatedError(result.err)) {
// Currently, this is only executed when the user cancels a password prompt
// during authentication.
if (finishWithResultOnEnd != null) {
activity?.setResult(Activity.RESULT_CANCELED)
activity?.finish()
}
} else {
e(result.err) e(result.err)
operation.onError(rootCauseException(result.err)) operation.onError(rootCauseException(result.err))
if (finishWithResultOnEnd != null) { if (finishWithResultOnEnd != null) {
activity?.setResult(Activity.RESULT_CANCELED) activity?.setResult(Activity.RESULT_CANCELED)
} }
} }
}
is Result.Ok -> { is Result.Ok -> {
operation.onSuccess() operation.onSuccess()
if (finishWithResultOnEnd != null) { if (finishWithResultOnEnd != null) {

View file

@ -12,6 +12,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.schmizz.sshj.SSHClient import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.common.Buffer.PlainBuffer import net.schmizz.sshj.common.Buffer.PlainBuffer
import net.schmizz.sshj.common.DisconnectReason
import net.schmizz.sshj.common.SSHException
import net.schmizz.sshj.common.SSHRuntimeException import net.schmizz.sshj.common.SSHRuntimeException
import net.schmizz.sshj.common.SecurityUtils import net.schmizz.sshj.common.SecurityUtils
import net.schmizz.sshj.connection.channel.direct.Session import net.schmizz.sshj.connection.channel.direct.Session
@ -54,12 +56,10 @@ abstract class InteractivePasswordFinder : PasswordFinder {
abstract fun askForPassword(cont: Continuation<String?>, isRetry: Boolean) abstract fun askForPassword(cont: Continuation<String?>, isRetry: Boolean)
private var isRetry = false private var isRetry = false
private var shouldRetry = true
private var lastPassword: CharArray? = null private var lastPassword: CharArray? = null
fun resetForReuse() { fun resetForReuse() {
isRetry = false isRetry = false
shouldRetry = true
} }
fun clearPassword() { fun clearPassword() {
@ -82,15 +82,11 @@ abstract class InteractivePasswordFinder : PasswordFinder {
} }
} }
isRetry = true isRetry = true
return if (password != null) { return password?.toCharArray()?.also { lastPassword = it }
password.toCharArray().also { lastPassword = it } ?: throw SSHException(DisconnectReason.AUTH_CANCELLED_BY_USER)
} else {
shouldRetry = false
CharArray(0)
}
} }
final override fun shouldRetry(resource: Resource<*>?) = shouldRetry final override fun shouldRetry(resource: Resource<*>?) = true
} }
class SshjSessionFactory(private val username: String, private val authData: SshAuthData, private val hostKeyFile: File) : SshSessionFactory() { class SshjSessionFactory(private val username: String, private val authData: SshAuthData, private val hostKeyFile: File) : SshSessionFactory() {