refactor: refine CryptoRepository#{encrypt,decrypt}
APIs
This commit is contained in:
parent
1c6bbc51c3
commit
06fbe6cf9c
3 changed files with 12 additions and 33 deletions
|
@ -17,6 +17,7 @@ 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.filterValues
|
import com.github.michaelbull.result.filterValues
|
||||||
|
import com.github.michaelbull.result.map
|
||||||
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
|
||||||
|
@ -43,7 +44,10 @@ constructor(
|
||||||
identities: List<PGPIdentifier>,
|
identities: List<PGPIdentifier>,
|
||||||
message: ByteArrayInputStream,
|
message: ByteArrayInputStream,
|
||||||
out: ByteArrayOutputStream,
|
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 {
|
suspend fun isPasswordProtected(identifiers: List<PGPIdentifier>): Boolean {
|
||||||
val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues()
|
val keys = identifiers.map { pgpKeyManager.getKeyById(it) }.filterValues()
|
||||||
|
@ -54,7 +58,7 @@ constructor(
|
||||||
identities: List<PGPIdentifier>,
|
identities: List<PGPIdentifier>,
|
||||||
content: ByteArrayInputStream,
|
content: ByteArrayInputStream,
|
||||||
out: ByteArrayOutputStream,
|
out: ByteArrayOutputStream,
|
||||||
) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out) }
|
) = withContext(dispatcherProvider.io()) { encryptPgp(identities, content, out).map { out } }
|
||||||
|
|
||||||
private suspend fun decryptPgp(
|
private suspend fun decryptPgp(
|
||||||
password: String,
|
password: String,
|
||||||
|
|
|
@ -17,7 +17,6 @@ import androidx.lifecycle.lifecycleScope
|
||||||
import app.passwordstore.Application.Companion.screenWasOff
|
import app.passwordstore.Application.Companion.screenWasOff
|
||||||
import app.passwordstore.R
|
import app.passwordstore.R
|
||||||
import app.passwordstore.crypto.PGPIdentifier
|
import app.passwordstore.crypto.PGPIdentifier
|
||||||
import app.passwordstore.crypto.errors.CryptoHandlerException
|
|
||||||
import app.passwordstore.data.crypto.PGPPassphraseCache
|
import app.passwordstore.data.crypto.PGPPassphraseCache
|
||||||
import app.passwordstore.data.passfile.PasswordEntry
|
import app.passwordstore.data.passfile.PasswordEntry
|
||||||
import app.passwordstore.data.repo.PasswordRepository
|
import app.passwordstore.data.repo.PasswordRepository
|
||||||
|
@ -34,14 +33,11 @@ import app.passwordstore.util.features.Features
|
||||||
import app.passwordstore.util.settings.PreferenceKeys
|
import app.passwordstore.util.settings.PreferenceKeys
|
||||||
import com.github.androidpasswordstore.autofillparser.AutofillAction
|
import com.github.androidpasswordstore.autofillparser.AutofillAction
|
||||||
import com.github.androidpasswordstore.autofillparser.Credentials
|
import com.github.androidpasswordstore.autofillparser.Credentials
|
||||||
import com.github.michaelbull.result.Result
|
|
||||||
import com.github.michaelbull.result.getOrElse
|
import com.github.michaelbull.result.getOrElse
|
||||||
import com.github.michaelbull.result.map
|
|
||||||
import com.github.michaelbull.result.onFailure
|
import com.github.michaelbull.result.onFailure
|
||||||
import com.github.michaelbull.result.onSuccess
|
import com.github.michaelbull.result.onSuccess
|
||||||
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.ByteArrayInputStream
|
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
@ -213,7 +209,9 @@ class AutofillDecryptActivity : BasePGPActivity() {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
.onSuccess { encryptedInput ->
|
.onSuccess { encryptedInput ->
|
||||||
decryptPGPStream(encryptedInput, password, identifiers)
|
val outputStream = ByteArrayOutputStream()
|
||||||
|
repository
|
||||||
|
.decrypt(password, identifiers, encryptedInput, outputStream)
|
||||||
.onFailure { e ->
|
.onFailure { e ->
|
||||||
logcat(ERROR) { e.asLog("Decryption failed") }
|
logcat(ERROR) { e.asLog("Decryption failed") }
|
||||||
return null
|
return null
|
||||||
|
@ -235,17 +233,6 @@ class AutofillDecryptActivity : BasePGPActivity() {
|
||||||
return null
|
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 {
|
companion object {
|
||||||
|
|
||||||
private const val EXTRA_FILE_PATH = "app.passwordstore.autofill.oreo.EXTRA_FILE_PATH"
|
private const val EXTRA_FILE_PATH = "app.passwordstore.autofill.oreo.EXTRA_FILE_PATH"
|
||||||
|
|
|
@ -15,7 +15,6 @@ import androidx.lifecycle.lifecycleScope
|
||||||
import app.passwordstore.Application.Companion.screenWasOff
|
import app.passwordstore.Application.Companion.screenWasOff
|
||||||
import app.passwordstore.R
|
import app.passwordstore.R
|
||||||
import app.passwordstore.crypto.PGPIdentifier
|
import app.passwordstore.crypto.PGPIdentifier
|
||||||
import app.passwordstore.crypto.errors.CryptoHandlerException
|
|
||||||
import app.passwordstore.crypto.errors.NonStandardAEAD
|
import app.passwordstore.crypto.errors.NonStandardAEAD
|
||||||
import app.passwordstore.data.crypto.PGPPassphraseCache
|
import app.passwordstore.data.crypto.PGPPassphraseCache
|
||||||
import app.passwordstore.data.passfile.PasswordEntry
|
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.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.Result
|
|
||||||
import com.github.michaelbull.result.map
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -236,7 +233,9 @@ class DecryptActivity : BasePGPActivity() {
|
||||||
authResult: BiometricResult,
|
authResult: BiometricResult,
|
||||||
onSuccess: suspend () -> Unit = {},
|
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 (result.isOk) {
|
||||||
if (features.isEnabled(EnablePGPPassphraseCache))
|
if (features.isEnabled(EnablePGPPassphraseCache))
|
||||||
settings.edit { putBoolean(PreferenceKeys.CLEAR_PASSPHRASE_CACHE, clearCache) }
|
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) =
|
private suspend fun createPasswordUI(entry: PasswordEntry) =
|
||||||
withContext(dispatcherProvider.main()) {
|
withContext(dispatcherProvider.main()) {
|
||||||
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
|
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
|
||||||
|
|
Loading…
Reference in a new issue