fix(deps): update kotlinresult to v1.1.21 (#2958)

* fix(deps): update kotlinresult to v1.1.21

* refactor: fix deprecation warnings

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
renovate[bot] 2024-03-17 06:33:48 +00:00 committed by GitHub
parent b01fddaa56
commit 48067b4a01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 72 additions and 102 deletions

View file

@ -16,7 +16,7 @@ import app.passwordstore.injection.prefs.SettingsPreferences
import app.passwordstore.util.coroutines.DispatcherProvider import app.passwordstore.util.coroutines.DispatcherProvider
import app.passwordstore.util.settings.PreferenceKeys import app.passwordstore.util.settings.PreferenceKeys
import com.github.michaelbull.result.Result import com.github.michaelbull.result.Result
import com.github.michaelbull.result.getAll import com.github.michaelbull.result.filterValues
import com.github.michaelbull.result.mapBoth import com.github.michaelbull.result.mapBoth
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@ -57,7 +57,7 @@ constructor(
message: ByteArrayInputStream, message: ByteArrayInputStream,
out: ByteArrayOutputStream, out: ByteArrayOutputStream,
): Result<Unit, CryptoHandlerException> { ): Result<Unit, CryptoHandlerException> {
val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.getAll() val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
val decryptionOptions = PGPDecryptOptions.Builder().build() val decryptionOptions = PGPDecryptOptions.Builder().build()
return pgpCryptoHandler.decrypt(keys, password, message, out, decryptionOptions) return pgpCryptoHandler.decrypt(keys, password, message, out, decryptionOptions)
} }
@ -71,7 +71,7 @@ constructor(
PGPEncryptOptions.Builder() PGPEncryptOptions.Builder()
.withAsciiArmor(settings.getBoolean(PreferenceKeys.ASCII_ARMOR, false)) .withAsciiArmor(settings.getBoolean(PreferenceKeys.ASCII_ARMOR, false))
.build() .build()
val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.getAll() val keys = identities.map { id -> pgpKeyManager.getKeyById(id) }.filterValues()
return pgpCryptoHandler.encrypt( return pgpCryptoHandler.encrypt(
keys, keys,
content, content,

View file

@ -27,8 +27,6 @@ import app.passwordstore.util.features.Feature.EnablePGPPassphraseCache
import app.passwordstore.util.features.Features import app.passwordstore.util.features.Features
import app.passwordstore.util.settings.Constants import app.passwordstore.util.settings.Constants
import app.passwordstore.util.settings.PreferenceKeys import app.passwordstore.util.settings.PreferenceKeys
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.runCatching
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@ -214,20 +212,18 @@ class DecryptActivity : BasePGPActivity() {
authResult: BiometricResult, authResult: BiometricResult,
onSuccess: suspend () -> Unit = {}, onSuccess: suspend () -> Unit = {},
) { ) {
when (val result = decryptPGPStream(passphrase, identifiers)) { val result = decryptPGPStream(passphrase, identifiers)
is Ok -> { if (result.isOk) {
val entry = passwordEntryFactory.create(result.value.toByteArray()) val entry = passwordEntryFactory.create(result.value.toByteArray())
passwordEntry = entry passwordEntry = entry
createPasswordUI(entry) createPasswordUI(entry)
startAutoDismissTimer() startAutoDismissTimer()
onSuccess() onSuccess()
} } else {
is Err -> {
logcat(ERROR) { result.error.stackTraceToString() } logcat(ERROR) { result.error.stackTraceToString() }
decrypt(isError = true, authResult = authResult) decrypt(isError = true, authResult = authResult)
} }
} }
}
private suspend fun decryptPGPStream( private suspend fun decryptPGPStream(
passphrase: String, passphrase: String,
@ -242,10 +238,7 @@ class DecryptActivity : BasePGPActivity() {
message, message,
outputStream, outputStream,
) )
when (result) { if (result.isOk) outputStream else throw result.error
is Ok -> outputStream
is Err -> throw result.error
}
} }
private suspend fun createPasswordUI(entry: PasswordEntry) = private suspend fun createPasswordUI(entry: PasswordEntry) =

View file

@ -14,8 +14,6 @@ import app.passwordstore.crypto.KeyUtils.tryGetId
import app.passwordstore.crypto.PGPKey import app.passwordstore.crypto.PGPKey
import app.passwordstore.crypto.PGPKeyManager import app.passwordstore.crypto.PGPKeyManager
import app.passwordstore.crypto.errors.KeyAlreadyExistsException import app.passwordstore.crypto.errors.KeyAlreadyExistsException
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result import com.github.michaelbull.result.Result
import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -69,8 +67,7 @@ class PGPKeyImportActivity : AppCompatActivity() {
} }
private fun handleImportResult(result: Result<PGPKey?, Throwable>) { private fun handleImportResult(result: Result<PGPKey?, Throwable>) {
when (result) { if (result.isOk) {
is Ok<PGPKey?> -> {
val key = result.value val key = result.value
if (key == null) { if (key == null) {
setResult(RESULT_CANCELED) setResult(RESULT_CANCELED)
@ -88,8 +85,7 @@ class PGPKeyImportActivity : AppCompatActivity() {
} }
.setCancelable(false) .setCancelable(false)
.show() .show()
} } else {
is Err<Throwable> -> {
if (result.error is KeyAlreadyExistsException && lastBytes != null) { if (result.error is KeyAlreadyExistsException && lastBytes != null) {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.pgp_key_import_failed)) .setTitle(getString(R.string.pgp_key_import_failed))
@ -111,4 +107,3 @@ class PGPKeyImportActivity : AppCompatActivity() {
} }
} }
} }
}

View file

@ -5,8 +5,6 @@
package app.passwordstore.util.extensions package app.passwordstore.util.extensions
import app.passwordstore.data.repo.PasswordRepository import app.passwordstore.data.repo.PasswordRepository
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.getOrElse import com.github.michaelbull.result.getOrElse
import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.runCatching
import java.io.File import java.io.File
@ -68,8 +66,3 @@ fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE) { ini
/** A convenience extension to turn a [Throwable] with a message into a loggable string. */ /** A convenience extension to turn a [Throwable] with a message into a loggable string. */
fun Throwable.asLog(message: String): String = "$message\n${asLog()}" fun Throwable.asLog(message: String): String = "$message\n${asLog()}"
/** Extension on [Result] that returns if the type is [Err] */
fun <V, E> Result<V, E>.isErr(): Boolean {
return this is Err<E>
}

View file

@ -8,9 +8,8 @@ import androidx.lifecycle.viewModelScope
import app.passwordstore.crypto.KeyUtils import app.passwordstore.crypto.KeyUtils
import app.passwordstore.crypto.PGPIdentifier import app.passwordstore.crypto.PGPIdentifier
import app.passwordstore.crypto.PGPKeyManager import app.passwordstore.crypto.PGPKeyManager
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.map import com.github.michaelbull.result.map
import com.github.michaelbull.result.onSuccess
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject import javax.inject.Inject
import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableList
@ -28,15 +27,10 @@ class PGPKeyListViewModel @Inject constructor(private val keyManager: PGPKeyMana
fun updateKeySet() { fun updateKeySet() {
viewModelScope.launch { viewModelScope.launch {
when ( keyManager
val result = .getAllKeys()
keyManager.getAllKeys().map { keys -> .map { keys -> keys.mapNotNull { key -> KeyUtils.tryGetEmail(key) } }
keys.mapNotNull { key -> KeyUtils.tryGetEmail(key) } .onSuccess { keys = it.toPersistentList() }
}
) {
is Ok -> keys = result.value.toPersistentList()
is Err -> TODO()
}
} }
} }

View file

@ -7,8 +7,6 @@ import app.passwordstore.crypto.errors.KeyAlreadyExistsException
import app.passwordstore.crypto.errors.KeyNotFoundException import app.passwordstore.crypto.errors.KeyNotFoundException
import app.passwordstore.crypto.errors.NoKeysAvailableException import app.passwordstore.crypto.errors.NoKeysAvailableException
import app.passwordstore.crypto.errors.UnusableKeyException import app.passwordstore.crypto.errors.UnusableKeyException
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.unwrap import com.github.michaelbull.result.unwrap
import com.github.michaelbull.result.unwrapError import com.github.michaelbull.result.unwrapError
import java.io.File import java.io.File
@ -18,8 +16,8 @@ import kotlin.test.assertEquals
import kotlin.test.assertIs import kotlin.test.assertIs
import kotlin.test.assertNotEquals import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Rule import org.junit.Rule
import org.junit.rules.TemporaryFolder import org.junit.rules.TemporaryFolder
@ -28,7 +26,6 @@ class PGPKeyManagerTest {
@get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder() @get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder()
private val dispatcher = StandardTestDispatcher() private val dispatcher = StandardTestDispatcher()
private val scope = TestScope(dispatcher)
private val filesDir by unsafeLazy { temporaryFolder.root } private val filesDir by unsafeLazy { temporaryFolder.root }
private val keysDir by unsafeLazy { File(filesDir, PGPKeyManager.KEY_DIR_NAME) } private val keysDir by unsafeLazy { File(filesDir, PGPKeyManager.KEY_DIR_NAME) }
private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) } private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) }
@ -170,24 +167,24 @@ class PGPKeyManagerTest {
@Test @Test
fun replaceSecretKeyWithPublicKey() = fun replaceSecretKeyWithPublicKey() =
runTest(dispatcher) { runTest(dispatcher) {
assertIs<Ok<PGPKey>>(keyManager.addKey(secretKey)) assertTrue(keyManager.addKey(secretKey).isOk)
assertIs<Err<KeyAlreadyExistsException>>(keyManager.addKey(publicKey)) assertTrue(keyManager.addKey(publicKey).isErr)
} }
@Test @Test
fun replacePublicKeyWithSecretKey() = fun replacePublicKeyWithSecretKey() =
runTest(dispatcher) { runTest(dispatcher) {
assertIs<Ok<PGPKey>>(keyManager.addKey(publicKey)) assertTrue(keyManager.addKey(publicKey).isOk)
assertIs<Ok<PGPKey>>(keyManager.addKey(secretKey)) assertTrue(keyManager.addKey(secretKey).isOk)
} }
@Test @Test
fun replacePublicKeyWithPublicKey() = fun replacePublicKeyWithPublicKey() =
runTest(dispatcher) { runTest(dispatcher) {
assertIs<Ok<PGPKey>>(keyManager.addKey(publicKey)) assertTrue(keyManager.addKey(publicKey).isOk)
assertIs<Ok<PGPKey>>(keyManager.addKey(publicKey)) assertTrue(keyManager.addKey(publicKey).isOk)
val allKeys = keyManager.getAllKeys() val allKeys = keyManager.getAllKeys()
assertIs<Ok<List<PGPKey>>>(allKeys) assertTrue(allKeys.isOk)
assertEquals(1, allKeys.value.size) assertEquals(1, allKeys.value.size)
val key = allKeys.value[0] val key = allKeys.value[0]
assertContentEquals(publicKey.contents, key.contents) assertContentEquals(publicKey.contents, key.contents)
@ -196,8 +193,8 @@ class PGPKeyManagerTest {
@Test @Test
fun replaceSecretKeyWithSecretKey() = fun replaceSecretKeyWithSecretKey() =
runTest(dispatcher) { runTest(dispatcher) {
assertIs<Ok<PGPKey>>(keyManager.addKey(secretKey)) assertTrue(keyManager.addKey(secretKey).isOk)
assertIs<Err<KeyAlreadyExistsException>>(keyManager.addKey(secretKey)) assertTrue(keyManager.addKey(secretKey).isErr)
} }
@Test @Test
@ -207,11 +204,11 @@ class PGPKeyManagerTest {
PGPKey(this::class.java.classLoader.getResource("alice_owner@example_com")!!.readBytes()) PGPKey(this::class.java.classLoader.getResource("alice_owner@example_com")!!.readBytes())
val bobby = val bobby =
PGPKey(this::class.java.classLoader.getResource("bobby_owner@example_com")!!.readBytes()) PGPKey(this::class.java.classLoader.getResource("bobby_owner@example_com")!!.readBytes())
assertIs<Ok<PGPKey>>(keyManager.addKey(alice)) assertTrue(keyManager.addKey(alice).isOk)
assertIs<Ok<PGPKey>>(keyManager.addKey(bobby)) assertTrue(keyManager.addKey(bobby).isOk)
keyManager.getAllKeys().apply { keyManager.getAllKeys().apply {
assertIs<Ok<List<PGPKey>>>(this) assertTrue(this.isOk)
assertEquals(2, this.value.size) assertEquals(2, this.value.size)
} }
val longKeyIds = val longKeyIds =
@ -228,8 +225,8 @@ class PGPKeyManagerTest {
for (idCollection in arrayOf(longKeyIds, userIds)) { for (idCollection in arrayOf(longKeyIds, userIds)) {
val alice1 = keyManager.getKeyById(idCollection[0]) val alice1 = keyManager.getKeyById(idCollection[0])
val bobby1 = keyManager.getKeyById(idCollection[1]) val bobby1 = keyManager.getKeyById(idCollection[1])
assertIs<Ok<PGPKey>>(alice1) assertTrue(alice1.isOk)
assertIs<Ok<PGPKey>>(bobby1) assertTrue(bobby1.isOk)
assertNotEquals(alice1.value.contents, bobby1.value.contents) assertNotEquals(alice1.value.contents, bobby1.value.contents)
} }
} }

View file

@ -9,8 +9,6 @@ package app.passwordstore.crypto
import app.passwordstore.crypto.CryptoConstants.KEY_PASSPHRASE import app.passwordstore.crypto.CryptoConstants.KEY_PASSPHRASE
import app.passwordstore.crypto.CryptoConstants.PLAIN_TEXT import app.passwordstore.crypto.CryptoConstants.PLAIN_TEXT
import app.passwordstore.crypto.errors.IncorrectPassphraseException import app.passwordstore.crypto.errors.IncorrectPassphraseException
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.getError import com.github.michaelbull.result.getError
import com.google.testing.junit.testparameterinjector.TestParameter import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector import com.google.testing.junit.testparameterinjector.TestParameterInjector
@ -48,7 +46,7 @@ class PGPainlessCryptoHandlerTest {
ciphertextStream, ciphertextStream,
PGPEncryptOptions.Builder().build(), PGPEncryptOptions.Builder().build(),
) )
assertIs<Ok<Unit>>(encryptRes) assertTrue(encryptRes.isOk)
val plaintextStream = ByteArrayOutputStream() val plaintextStream = ByteArrayOutputStream()
val decryptRes = val decryptRes =
cryptoHandler.decrypt( cryptoHandler.decrypt(
@ -58,7 +56,7 @@ class PGPainlessCryptoHandlerTest {
plaintextStream, plaintextStream,
PGPDecryptOptions.Builder().build(), PGPDecryptOptions.Builder().build(),
) )
assertIs<Ok<Unit>>(decryptRes) assertTrue(decryptRes.isOk)
assertEquals(PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8)) assertEquals(PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8))
} }
@ -72,7 +70,7 @@ class PGPainlessCryptoHandlerTest {
ciphertextStream, ciphertextStream,
PGPEncryptOptions.Builder().build(), PGPEncryptOptions.Builder().build(),
) )
assertIs<Ok<Unit>>(encryptRes) assertTrue(encryptRes.isOk)
val plaintextStream = ByteArrayOutputStream() val plaintextStream = ByteArrayOutputStream()
val result = val result =
cryptoHandler.decrypt( cryptoHandler.decrypt(
@ -82,7 +80,7 @@ class PGPainlessCryptoHandlerTest {
plaintextStream, plaintextStream,
PGPDecryptOptions.Builder().build(), PGPDecryptOptions.Builder().build(),
) )
assertIs<Err<Throwable>>(result) assertTrue(result.isErr)
assertIs<IncorrectPassphraseException>(result.getError()) assertIs<IncorrectPassphraseException>(result.getError())
} }
@ -96,7 +94,7 @@ class PGPainlessCryptoHandlerTest {
ciphertextStream, ciphertextStream,
PGPEncryptOptions.Builder().withAsciiArmor(true).build(), PGPEncryptOptions.Builder().withAsciiArmor(true).build(),
) )
assertIs<Ok<Unit>>(encryptRes) assertTrue(encryptRes.isOk)
val ciphertext = ciphertextStream.toString(Charsets.UTF_8) val ciphertext = ciphertextStream.toString(Charsets.UTF_8)
assertContains(ciphertext, "Version: PGPainless") assertContains(ciphertext, "Version: PGPainless")
assertContains(ciphertext, "-----BEGIN PGP MESSAGE-----") assertContains(ciphertext, "-----BEGIN PGP MESSAGE-----")
@ -118,7 +116,7 @@ class PGPainlessCryptoHandlerTest {
ciphertextStream, ciphertextStream,
PGPEncryptOptions.Builder().withAsciiArmor(true).build(), PGPEncryptOptions.Builder().withAsciiArmor(true).build(),
) )
assertIs<Ok<Unit>>(encryptRes) assertTrue(encryptRes.isOk)
val message = ciphertextStream.toByteArray().decodeToString() val message = ciphertextStream.toByteArray().decodeToString()
val info = MessageInspector.determineEncryptionInfoForMessage(message) val info = MessageInspector.determineEncryptionInfoForMessage(message)
assertTrue(info.isEncrypted) assertTrue(info.isEncrypted)
@ -135,7 +133,7 @@ class PGPainlessCryptoHandlerTest {
plaintextStream, plaintextStream,
PGPDecryptOptions.Builder().build(), PGPDecryptOptions.Builder().build(),
) )
assertIs<Ok<Unit>>(res) assertTrue(res.isOk)
} }
} }

View file

@ -8,7 +8,7 @@ coroutines = "1.8.0"
flowbinding = "1.2.0" flowbinding = "1.2.0"
hilt = "2.51" hilt = "2.51"
kotlin = "1.9.23" kotlin = "1.9.23"
kotlinResult = "1.1.20" kotlinResult = "1.1.21"
lifecycle = "2.7.0" lifecycle = "2.7.0"
moshi = "1.15.1" moshi = "1.15.1"