diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 20:38:34 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 22:49:37 +0530 |
commit | e18416fa0e2ed9d87720c8605ae0fd4655352db1 (patch) | |
tree | d712b8ce979b1e1887d19186d9ba2e8663df6b73 /app/src/main/java | |
parent | e6675d989d8bc21a392c73d2597a8f3507149926 (diff) |
SshKey: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/git/sshj/SshKey.kt | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/sshj/SshKey.kt b/app/src/main/java/com/zeapo/pwdstore/git/sshj/SshKey.kt index da49111e..ce09f1a9 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/sshj/SshKey.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/sshj/SshKey.kt @@ -18,6 +18,8 @@ import androidx.security.crypto.EncryptedFile import androidx.security.crypto.MasterKey import com.github.ajalt.timberkt.d import com.github.ajalt.timberkt.e +import com.github.michaelbull.result.getOrElse +import com.github.michaelbull.result.runCatching import com.zeapo.pwdstore.Application import com.zeapo.pwdstore.R import com.zeapo.pwdstore.utils.PreferenceKeys @@ -81,7 +83,7 @@ object SshKey { get() = type != null val mustAuthenticate: Boolean get() { - return try { + return runCatching { if (type !in listOf(Type.KeystoreNative, Type.KeystoreWrappedEd25519)) return false when (val key = androidKeystore.getKey(KEYSTORE_ALIAS, null)) { @@ -95,7 +97,7 @@ object SshKey { } else -> throw IllegalStateException("SSH key does not exist in Keystore") } - } catch (error: Exception) { + }.getOrElse { error -> // It is fine to swallow the exception here since it will reappear when the key is // used for SSH authentication and can then be shown in the UI. d(error) @@ -284,16 +286,16 @@ object SshKey { private object KeystoreNativeKeyProvider : KeyProvider { - override fun getPublic(): PublicKey = try { + override fun getPublic(): PublicKey = runCatching { androidKeystore.sshPublicKey!! - } catch (error: Throwable) { + }.getOrElse { error -> e(error) throw IOException("Failed to get public key '$KEYSTORE_ALIAS' from Android Keystore", error) } - override fun getPrivate(): PrivateKey = try { + override fun getPrivate(): PrivateKey = runCatching { androidKeystore.sshPrivateKey!! - } catch (error: Throwable) { + }.getOrElse { error -> e(error) throw IOException("Failed to access private key '$KEYSTORE_ALIAS' from Android Keystore", error) } @@ -303,14 +305,14 @@ object SshKey { private object KeystoreWrappedEd25519KeyProvider : KeyProvider { - override fun getPublic(): PublicKey = try { + override fun getPublic(): PublicKey = runCatching { parseSshPublicKey(sshPublicKey!!)!! - } catch (error: Throwable) { + }.getOrElse { error -> e(error) throw IOException("Failed to get the public key for wrapped ed25519 key", error) } - override fun getPrivate(): PrivateKey = try { + override fun getPrivate(): PrivateKey = runCatching { // The current MasterKey API does not allow getting a reference to an existing one // without specifying the KeySpec for a new one. However, the value for passed here // for `requireAuthentication` is not used as the key already exists at this point. @@ -319,7 +321,7 @@ object SshKey { } val rawPrivateKey = encryptedPrivateKeyFile.openFileInput().use { it.readBytes() } EdDSAPrivateKey(EdDSAPrivateKeySpec(rawPrivateKey, EdDSANamedCurveTable.ED_25519_CURVE_SPEC)) - } catch (error: Throwable) { + }.getOrElse { error -> e(error) throw IOException("Failed to unwrap wrapped ed25519 key", error) } |