From cf111f197856673185817921460025e7324ac512 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 1 Feb 2022 19:21:01 +0530 Subject: Refactor PasswordEntry TOTP calculation into a cold flow (#1702) --- .../dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivity.kt | 3 +-- .../msfjarvis/aps/ui/autofill/AutofillDecryptActivityV2.kt | 2 +- .../main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt | 5 +++-- .../java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt | 5 +++-- .../dev/msfjarvis/aps/ui/crypto/PasswordCreationActivity.kt | 12 +++--------- .../msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt | 12 +++--------- .../dev/msfjarvis/aps/util/autofill/AutofillPreferences.kt | 4 +++- 7 files changed, 17 insertions(+), 26 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivity.kt index ccd4f649..b6b8a78a 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/autofill/AutofillDecryptActivity.kt @@ -43,7 +43,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import logcat.LogPriority.ERROR -import logcat.asLog import logcat.logcat import me.msfjarvis.openpgpktx.util.OpenPgpApi import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection @@ -204,7 +203,7 @@ class AutofillDecryptActivity : AppCompatActivity() { val entry = withContext(Dispatchers.IO) { @Suppress("BlockingMethodInNonBlockingContext") - passwordEntryFactory.create(lifecycleScope, decryptedOutput.toByteArray()) + passwordEntryFactory.create(decryptedOutput.toByteArray()) } AutofillPreferences.credentialsFromStoreEntry( this, 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 555366c4..a784f64b 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 @@ -162,7 +162,7 @@ class AutofillDecryptActivityV2 : AppCompatActivity() { } .onSuccess { result -> return runCatching { - val entry = passwordEntryFactory.create(lifecycleScope, result.toByteArray()) + val entry = passwordEntryFactory.create(result.toByteArray()) AutofillPreferences.credentialsFromStoreEntry(this, file, entry, directoryStructure) } .getOrElse { e -> diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt index 1fd4ee60..413e6f6a 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt @@ -31,6 +31,7 @@ import kotlin.time.ExperimentalTime import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -178,7 +179,7 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { startAutoDismissTimer() runCatching { val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true) - val entry = passwordEntryFactory.create(lifecycleScope, outputStream.toByteArray()) + val entry = passwordEntryFactory.create(outputStream.toByteArray()) if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) { copyPasswordToClipboard(entry.password) @@ -193,7 +194,7 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { } if (entry.hasTotp()) { - items.add(FieldItem.createOtpField(entry.totp.value)) + items.add(FieldItem.createOtpField(entry.totp.first())) } if (!entry.username.isNullOrBlank()) { diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt index 5b84478e..f5c02e49 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivityV2.kt @@ -31,6 +31,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -166,7 +167,7 @@ class DecryptActivityV2 : BasePgpActivity() { require(result.size() != 0) { "Incorrect password" } startAutoDismissTimer() - val entry = passwordEntryFactory.create(lifecycleScope, result.toByteArray()) + val entry = passwordEntryFactory.create(result.toByteArray()) passwordEntry = entry createPasswordUi(entry) } @@ -182,7 +183,7 @@ class DecryptActivityV2 : BasePgpActivity() { } if (entry.hasTotp()) { - items.add(FieldItem.createOtpField(entry.totp.value)) + items.add(FieldItem.createOtpField(entry.totp.first())) } if (!entry.username.isNullOrBlank()) { diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivity.kt index 1b23dc0a..37a16b16 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivity.kt @@ -293,10 +293,7 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB // User wants to disable username encryption, so we extract the // username from the encrypted extras and use it as the filename. val entry = - passwordEntryFactory.create( - lifecycleScope, - "PASSWORD\n${extraContent.text}".encodeToByteArray() - ) + passwordEntryFactory.create("PASSWORD\n${extraContent.text}".encodeToByteArray()) val username = entry.username // username should not be null here by the logic in @@ -370,10 +367,7 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB with(binding) { // Use PasswordEntry to parse extras for username val entry = - passwordEntryFactory.create( - lifecycleScope, - "PLACEHOLDER\n${extraContent.text}".encodeToByteArray() - ) + passwordEntryFactory.create("PLACEHOLDER\n${extraContent.text}".encodeToByteArray()) encryptUsername.apply { if (visibility != View.VISIBLE) return@apply val hasUsernameInFileName = filename.text.toString().isNotBlank() @@ -529,7 +523,7 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB if (shouldGeneratePassword) { val directoryStructure = AutofillPreferences.directoryStructure(applicationContext) - val entry = passwordEntryFactory.create(lifecycleScope, content.encodeToByteArray()) + val entry = passwordEntryFactory.create(content.encodeToByteArray()) returnIntent.putExtra(RETURN_EXTRA_PASSWORD, entry.password) val username = entry.username ?: directoryStructure.getUsernameFor(file) returnIntent.putExtra(RETURN_EXTRA_USERNAME, username) diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt index a191ba61..e6ee7a09 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/PasswordCreationActivityV2.kt @@ -223,10 +223,7 @@ class PasswordCreationActivityV2 : BasePgpActivity() { // User wants to disable username encryption, so we extract the // username from the encrypted extras and use it as the filename. val entry = - passwordEntryFactory.create( - lifecycleScope, - "PASSWORD\n${extraContent.text}".encodeToByteArray() - ) + passwordEntryFactory.create("PASSWORD\n${extraContent.text}".encodeToByteArray()) val username = entry.username // username should not be null here by the logic in @@ -300,10 +297,7 @@ class PasswordCreationActivityV2 : BasePgpActivity() { with(binding) { // Use PasswordEntry to parse extras for username val entry = - passwordEntryFactory.create( - lifecycleScope, - "PLACEHOLDER\n${extraContent.text}".encodeToByteArray() - ) + passwordEntryFactory.create("PLACEHOLDER\n${extraContent.text}".encodeToByteArray()) encryptUsername.apply { if (visibility != View.VISIBLE) return@apply val hasUsernameInFileName = filename.text.toString().isNotBlank() @@ -406,7 +400,7 @@ class PasswordCreationActivityV2 : BasePgpActivity() { if (shouldGeneratePassword) { val directoryStructure = AutofillPreferences.directoryStructure(applicationContext) - val entry = passwordEntryFactory.create(lifecycleScope, content.encodeToByteArray()) + val entry = passwordEntryFactory.create(content.encodeToByteArray()) returnIntent.putExtra(RETURN_EXTRA_PASSWORD, entry.password) val username = entry.username ?: directoryStructure.getUsernameFor(file) returnIntent.putExtra(RETURN_EXTRA_USERNAME, username) diff --git a/app/src/main/java/dev/msfjarvis/aps/util/autofill/AutofillPreferences.kt b/app/src/main/java/dev/msfjarvis/aps/util/autofill/AutofillPreferences.kt index fdbd15af..2028fedb 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/autofill/AutofillPreferences.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/autofill/AutofillPreferences.kt @@ -15,6 +15,8 @@ import dev.msfjarvis.aps.util.services.getDefaultUsername import dev.msfjarvis.aps.util.settings.PreferenceKeys import java.io.File import java.nio.file.Paths +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking enum class DirectoryStructure(val value: String) { EncryptedUsername("encrypted_username"), @@ -141,6 +143,6 @@ object AutofillPreferences { // Always give priority to a username stored in the encrypted extras val username = entry.username ?: directoryStructure.getUsernameFor(file) ?: context.getDefaultUsername() - return Credentials(username, entry.password, entry.totp.value) + return Credentials(username, entry.password, runBlocking { entry.totp.first() }) } } -- cgit v1.2.3