aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-01-09 17:04:16 +0530
committerGitHub <noreply@github.com>2022-01-09 17:04:16 +0530
commit1738879fb3ba1b2c2d0ca7933c861bf99ae32535 (patch)
tree42b339b07e3c45922310afd01e8ce357f99beeb6 /app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt
parent799f1393e49955d05f68b81af26d6cfaf9beadfd (diff)
downloadHardenedPass-1738879fb3ba1b2c2d0ca7933c861bf99ae32535.tar.gz
HardenedPass-1738879fb3ba1b2c2d0ca7933c861bf99ae32535.zip
Integrate PGPainless backend into the UI properly (#1647)
Diffstat (limited to 'app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt')
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt69
1 files changed, 42 insertions, 27 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt b/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt
index 8f4578cf..697d628d 100644
--- a/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt
@@ -21,10 +21,9 @@ import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import com.github.michaelbull.result.runCatching
import dagger.hilt.android.AndroidEntryPoint
-import dev.msfjarvis.aps.crypto.Key
+import dev.msfjarvis.aps.data.crypto.CryptoRepository
import dev.msfjarvis.aps.data.passfile.PasswordEntry
-import dev.msfjarvis.aps.injection.crypto.CryptoSet
-import dev.msfjarvis.aps.ui.crypto.DecryptActivityV2
+import dev.msfjarvis.aps.ui.crypto.PasswordDialog
import dev.msfjarvis.aps.util.autofill.AutofillPreferences
import dev.msfjarvis.aps.util.autofill.AutofillResponseBuilder
import dev.msfjarvis.aps.util.autofill.DirectoryStructure
@@ -33,6 +32,7 @@ import java.io.ByteArrayOutputStream
import java.io.File
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import logcat.LogPriority.ERROR
@@ -74,7 +74,7 @@ class AutofillDecryptActivityV2 : AppCompatActivity() {
}
@Inject lateinit var passwordEntryFactory: PasswordEntry.Factory
- @Inject lateinit var cryptos: CryptoSet
+ @Inject lateinit var repository: CryptoRepository
private lateinit var directoryStructure: DirectoryStructure
@@ -98,43 +98,58 @@ class AutofillDecryptActivityV2 : AppCompatActivity() {
val action = if (isSearchAction) AutofillAction.Search else AutofillAction.Match
directoryStructure = AutofillPreferences.directoryStructure(this)
logcat { action.toString() }
+ val dialog = PasswordDialog()
lifecycleScope.launch {
- val credentials = decryptCredential(File(filePath))
- if (credentials == null) {
- setResult(RESULT_CANCELED)
- } else {
- val fillInDataset =
- AutofillResponseBuilder.makeFillInDataset(
- this@AutofillDecryptActivityV2,
- credentials,
- clientState,
- action
- )
- withContext(Dispatchers.Main) {
- setResult(
- RESULT_OK,
- Intent().apply { putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, fillInDataset) }
- )
+ withContext(Dispatchers.Main) {
+ dialog.password.collectLatest { value ->
+ if (value != null) {
+ decrypt(File(filePath), clientState, action, value)
+ }
}
}
- withContext(Dispatchers.Main) { finish() }
}
+ dialog.show(supportFragmentManager, "PASSWORD_DIALOG")
+ }
+
+ private suspend fun decrypt(
+ filePath: File,
+ clientState: Bundle,
+ action: AutofillAction,
+ password: String,
+ ) {
+ val credentials = decryptCredential(filePath, password)
+ if (credentials == null) {
+ setResult(RESULT_CANCELED)
+ } else {
+ val fillInDataset =
+ AutofillResponseBuilder.makeFillInDataset(
+ this@AutofillDecryptActivityV2,
+ credentials,
+ clientState,
+ action
+ )
+ withContext(Dispatchers.Main) {
+ setResult(
+ RESULT_OK,
+ Intent().apply { putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, fillInDataset) }
+ )
+ }
+ }
+ withContext(Dispatchers.Main) { finish() }
}
- private suspend fun decryptCredential(file: File): Credentials? {
- runCatching { file.inputStream() }
+ private suspend fun decryptCredential(file: File, password: String): Credentials? {
+ runCatching { file.readBytes().inputStream() }
.onFailure { e ->
logcat(ERROR) { e.asLog("File to decrypt not found") }
return null
}
.onSuccess { encryptedInput ->
runCatching {
- val crypto = cryptos.first { it.canHandle(file.absolutePath) }
withContext(Dispatchers.IO) {
val outputStream = ByteArrayOutputStream()
- crypto.decrypt(
- Key(DecryptActivityV2.PRIV_KEY.encodeToByteArray()),
- DecryptActivityV2.PASS,
+ repository.decrypt(
+ password,
encryptedInput,
outputStream,
)