diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2024-07-24 00:32:33 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2024-07-24 00:32:33 +0530 |
commit | 06fbe6cf9c1b831c852d7cec227c7924870a43dd (patch) | |
tree | 385c5c97d7659df064ac12cfea6ccd00245b3cd8 /app | |
parent | 1c6bbc51c3851218e494656b356b92216a575804 (diff) |
refactor: refine `CryptoRepository#{encrypt,decrypt}` APIs
Diffstat (limited to 'app')
3 files changed, 12 insertions, 33 deletions
diff --git a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt index e3e165dd..d7785b1e 100644 --- a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt +++ b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt @@ -17,6 +17,7 @@ import app.passwordstore.util.coroutines.DispatcherProvider import app.passwordstore.util.settings.PreferenceKeys import com.github.michaelbull.result.Result import com.github.michaelbull.result.filterValues +import com.github.michaelbull.result.map import com.github.michaelbull.result.mapBoth import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream @@ -43,7 +44,10 @@ constructor( identities: List<PGPIdentifier>, message: ByteArrayInputStream, out: ByteArrayOutputStream, - ) = withContext(dispatcherProvider.io()) { decryptPgp(password, identities, message, out) } + ) = + withContext(dispatcherProvider.io()) { + decryptPgp(password, identities, message, out).map { out } + } suspend fun isPasswordProtected(identifiers: List<PGPIdentifier>): Boolean { val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues() @@ -54,7 +58,7 @@ constructor( identities: List<PGPIdentifier>, content: ByteArrayInputStream, out: ByteArrayOutputStream, - ) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out) } + ) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out).map { out } } private suspend fun decryptPgp( password: String, diff --git a/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt b/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt index 4855d588..9601d75e 100644 --- a/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/autofill/AutofillDecryptActivity.kt @@ -17,7 +17,6 @@ import androidx.lifecycle.lifecycleScope import app.passwordstore.Application.Companion.screenWasOff import app.passwordstore.R import app.passwordstore.crypto.PGPIdentifier -import app.passwordstore.crypto.errors.CryptoHandlerException import app.passwordstore.data.crypto.PGPPassphraseCache import app.passwordstore.data.passfile.PasswordEntry import app.passwordstore.data.repo.PasswordRepository @@ -34,14 +33,11 @@ import app.passwordstore.util.features.Features import app.passwordstore.util.settings.PreferenceKeys import com.github.androidpasswordstore.autofillparser.AutofillAction import com.github.androidpasswordstore.autofillparser.Credentials -import com.github.michaelbull.result.Result import com.github.michaelbull.result.getOrElse -import com.github.michaelbull.result.map import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.onSuccess import com.github.michaelbull.result.runCatching import dagger.hilt.android.AndroidEntryPoint -import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.File import javax.inject.Inject @@ -213,7 +209,9 @@ class AutofillDecryptActivity : BasePGPActivity() { return null } .onSuccess { encryptedInput -> - decryptPGPStream(encryptedInput, password, identifiers) + val outputStream = ByteArrayOutputStream() + repository + .decrypt(password, identifiers, encryptedInput, outputStream) .onFailure { e -> logcat(ERROR) { e.asLog("Decryption failed") } return null @@ -235,17 +233,6 @@ class AutofillDecryptActivity : BasePGPActivity() { return null } - private suspend fun decryptPGPStream( - message: ByteArrayInputStream, - passphrase: String, - gpgIdentifiers: List<PGPIdentifier>, - ): Result<ByteArrayOutputStream, CryptoHandlerException> { - val outputStream = ByteArrayOutputStream() - return repository.decrypt(passphrase, gpgIdentifiers, message, outputStream).map { - outputStream - } - } - companion object { private const val EXTRA_FILE_PATH = "app.passwordstore.autofill.oreo.EXTRA_FILE_PATH" diff --git a/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt b/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt index 055a3280..c25b5d1a 100644 --- a/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/crypto/DecryptActivity.kt @@ -15,7 +15,6 @@ import androidx.lifecycle.lifecycleScope import app.passwordstore.Application.Companion.screenWasOff import app.passwordstore.R import app.passwordstore.crypto.PGPIdentifier -import app.passwordstore.crypto.errors.CryptoHandlerException import app.passwordstore.crypto.errors.NonStandardAEAD import app.passwordstore.data.crypto.PGPPassphraseCache import app.passwordstore.data.passfile.PasswordEntry @@ -32,8 +31,6 @@ import app.passwordstore.util.features.Feature.EnablePGPPassphraseCache import app.passwordstore.util.features.Features import app.passwordstore.util.settings.Constants import app.passwordstore.util.settings.PreferenceKeys -import com.github.michaelbull.result.Result -import com.github.michaelbull.result.map import dagger.hilt.android.AndroidEntryPoint import java.io.ByteArrayOutputStream import java.io.File @@ -236,7 +233,9 @@ class DecryptActivity : BasePGPActivity() { authResult: BiometricResult, onSuccess: suspend () -> Unit = {}, ) { - val result = decryptPGPStream(passphrase, identifiers) + val message = withContext(dispatcherProvider.io()) { File(fullPath).readBytes().inputStream() } + val outputStream = ByteArrayOutputStream() + val result = repository.decrypt(passphrase, identifiers, message, outputStream) if (result.isOk) { if (features.isEnabled(EnablePGPPassphraseCache)) settings.edit { putBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, clearCache) } @@ -268,17 +267,6 @@ class DecryptActivity : BasePGPActivity() { } } - private suspend fun decryptPGPStream( - passphrase: String, - gpgIdentifiers: List<PGPIdentifier>, - ): Result<ByteArrayOutputStream, CryptoHandlerException> { - val message = withContext(dispatcherProvider.io()) { File(fullPath).readBytes().inputStream() } - val outputStream = ByteArrayOutputStream() - return repository.decrypt(passphrase, gpgIdentifiers, message, outputStream).map { - outputStream - } - } - private suspend fun createPasswordUI(entry: PasswordEntry) = withContext(dispatcherProvider.main()) { val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true) |