diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2022-03-11 01:52:39 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-10 20:22:39 +0000 |
commit | 2f034bc2372d29b4ddebf74d532279073fb3d92b (patch) | |
tree | dc9197276622c56f32c9b395e9339a046f608103 /app/src | |
parent | 3e988b2a3428c3759535a2bd1f3e1ba0b5e411a3 (diff) |
Show remaining time in TOTP field (#1766)
* Pass down remaining time for TOTPs to UI layer
* format-common: switch TOTP flow to use co-operative cancelation
* format-common: add a regression test for OTP duration calculation
* Abstract out labels
* Switch to launchIn
Diffstat (limited to 'app/src')
5 files changed, 28 insertions, 29 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/data/password/FieldItem.kt b/app/src/main/java/dev/msfjarvis/aps/data/password/FieldItem.kt index 5b4931e7..e7c88ef1 100644 --- a/app/src/main/java/dev/msfjarvis/aps/data/password/FieldItem.kt +++ b/app/src/main/java/dev/msfjarvis/aps/data/password/FieldItem.kt @@ -5,31 +5,39 @@ package dev.msfjarvis.aps.data.password +import dev.msfjarvis.aps.data.passfile.Totp +import kotlin.time.ExperimentalTime + +@OptIn(ExperimentalTime::class) class FieldItem(val key: String, val value: String, val action: ActionType) { enum class ActionType { COPY, HIDE } - enum class ItemType(val type: String) { - USERNAME("Username"), - PASSWORD("Password"), - OTP("OTP") + enum class ItemType(val type: String, val label: String) { + USERNAME("Username", "Username"), + PASSWORD("Password", "Password"), + OTP("OTP", "OTP (expires in %ds)"), } companion object { // Extra helper methods - fun createOtpField(otp: String): FieldItem { - return FieldItem(ItemType.OTP.type, otp, ActionType.COPY) + fun createOtpField(totp: Totp): FieldItem { + return FieldItem( + ItemType.OTP.label.format(totp.remainingTime.inWholeSeconds), + totp.value, + ActionType.COPY, + ) } fun createPasswordField(password: String): FieldItem { - return FieldItem(ItemType.PASSWORD.type, password, ActionType.HIDE) + return FieldItem(ItemType.PASSWORD.label, password, ActionType.HIDE) } fun createUsernameField(username: String): FieldItem { - return FieldItem(ItemType.USERNAME.type, username, ActionType.COPY) + return FieldItem(ItemType.USERNAME.label, username, ActionType.COPY) } } } diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/adapters/FieldItemAdapter.kt b/app/src/main/java/dev/msfjarvis/aps/ui/adapters/FieldItemAdapter.kt index d019bcae..ea96f961 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/adapters/FieldItemAdapter.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/adapters/FieldItemAdapter.kt @@ -13,6 +13,7 @@ import androidx.core.content.ContextCompat import androidx.recyclerview.widget.RecyclerView import com.google.android.material.textfield.TextInputLayout import dev.msfjarvis.aps.R +import dev.msfjarvis.aps.data.passfile.Totp import dev.msfjarvis.aps.data.password.FieldItem import dev.msfjarvis.aps.databinding.ItemFieldBinding @@ -35,13 +36,13 @@ class FieldItemAdapter( return fieldItemList.size } - fun updateOTPCode(code: String) { + fun updateOTPCode(totp: Totp) { var otpItemPosition = -1 fieldItemList = fieldItemList.mapIndexed { position, item -> - if (item.key.equals(FieldItem.ItemType.OTP.type, true)) { + if (item.key.startsWith(FieldItem.ItemType.OTP.type, true)) { otpItemPosition = position - return@mapIndexed FieldItem.createOtpField(code) + return@mapIndexed FieldItem.createOtpField(totp) } return@mapIndexed item 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 413e6f6a..c6fda844 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 @@ -30,8 +30,8 @@ import kotlin.time.Duration 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.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -210,12 +210,7 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { binding.recyclerView.adapter = adapter if (entry.hasTotp()) { - lifecycleScope.launch { - entry - .totp - .onEach { code -> withContext(Dispatchers.Main) { adapter.updateOTPCode(code) } } - .collect() - } + entry.totp.onEach(adapter::updateOTPCode).launchIn(lifecycleScope) } } .onFailure { e -> logcat(ERROR) { e.asLog() } } 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 f5c02e49..21cd42aa 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 @@ -25,17 +25,18 @@ import dev.msfjarvis.aps.util.settings.PreferenceKeys import java.io.ByteArrayOutputStream import java.io.File import javax.inject.Inject -import kotlin.time.Duration +import kotlin.time.Duration.Companion.seconds import kotlin.time.ExperimentalTime 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.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +@OptIn(ExperimentalTime::class) @AndroidEntryPoint class DecryptActivityV2 : BasePgpActivity() { @@ -90,10 +91,9 @@ class DecryptActivityV2 : BasePgpActivity() { * Automatically finishes the activity 60 seconds after decryption succeeded to prevent * information leaks from stale activities. */ - @OptIn(ExperimentalTime::class) private fun startAutoDismissTimer() { lifecycleScope.launch { - delay(Duration.seconds(60)) + delay(60.seconds) finish() } } @@ -198,12 +198,7 @@ class DecryptActivityV2 : BasePgpActivity() { binding.recyclerView.adapter = adapter if (entry.hasTotp()) { - lifecycleScope.launch { - entry - .totp - .onEach { code -> withContext(Dispatchers.Main) { adapter.updateOTPCode(code) } } - .collect() - } + entry.totp.onEach(adapter::updateOTPCode).launchIn(lifecycleScope) } } 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 051693d2..50d2684a 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 @@ -143,7 +143,7 @@ object AutofillPreferences { // Always give priority to a username stored in the encrypted extras val username = entry.username ?: directoryStructure.getUsernameFor(file) ?: context.getDefaultUsername() - val totp = if (entry.hasTotp()) runBlocking { entry.totp.first() } else null + val totp = if (entry.hasTotp()) runBlocking { entry.totp.first().value } else null return Credentials(username, entry.password, totp) } } |