aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-11-02 23:27:32 +0100
committerDanilo M. <danix@danix.xyz>2026-11-02 23:27:32 +0100
commit2329d0236ea39fd119b1199ae40d2b9f5a316506 (patch)
tree74f5df6efca79fe46c96313a0ef7756e9cdad7d1 /app
parent8ae382468d2fea8b42bc9d2e3bd67be9144873ed (diff)
downloadHardenedPass-2329d0236ea39fd119b1199ae40d2b9f5a316506.tar.gz
HardenedPass-2329d0236ea39fd119b1199ae40d2b9f5a316506.zip
feat(app): add the entry view model and the OTP period
Totp now carries its period, so the entry view can draw how much of the code's lifetime is left. EntryFields groups a decrypted entry the way the spec lays it out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/app/passwordstore/ui/entry/EntryModel.kt35
-rw-r--r--app/src/test/java/app/passwordstore/ui/entry/EntryModelTest.kt57
2 files changed, 92 insertions, 0 deletions
diff --git a/app/src/main/java/app/passwordstore/ui/entry/EntryModel.kt b/app/src/main/java/app/passwordstore/ui/entry/EntryModel.kt
new file mode 100644
index 00000000..8dbc9eb9
--- /dev/null
+++ b/app/src/main/java/app/passwordstore/ui/entry/EntryModel.kt
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package app.passwordstore.ui.entry
+
+import androidx.compose.runtime.Immutable
+import app.passwordstore.data.passfile.PasswordEntry
+import app.passwordstore.data.passfile.Totp
+import kotlinx.collections.immutable.ImmutableList
+import kotlinx.collections.immutable.toImmutableList
+
+/**
+ * What the entry view shows. [password] and [username] open the first group (the one-time code
+ * joins them from the entry's TOTP flow); [extras] are the remaining `key: value` lines in file
+ * order, plus lines that are not `key: value` under the parser's "Extra Content" key. Blank values
+ * are null, so their rows are not shown.
+ */
+@Immutable
+data class EntryFields(
+ val password: String?,
+ val username: String?,
+ val extras: ImmutableList<Pair<String, String>>,
+)
+
+fun entryFields(entry: PasswordEntry): EntryFields =
+ EntryFields(
+ password = entry.password?.takeIf { it.isNotBlank() },
+ username = entry.username?.takeIf { it.isNotBlank() },
+ extras = entry.extraContent.toList().toImmutableList(),
+ )
+
+/** Share of the code's lifetime left: 1 right after it was generated, toward 0 at expiry. */
+fun Totp.fractionLeft(): Float = (remainingTime / period).toFloat()
diff --git a/app/src/test/java/app/passwordstore/ui/entry/EntryModelTest.kt b/app/src/test/java/app/passwordstore/ui/entry/EntryModelTest.kt
new file mode 100644
index 00000000..290c51a9
--- /dev/null
+++ b/app/src/test/java/app/passwordstore/ui/entry/EntryModelTest.kt
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package app.passwordstore.ui.entry
+
+import app.passwordstore.data.passfile.PasswordEntry
+import app.passwordstore.data.passfile.Totp
+import app.passwordstore.util.time.UserClock
+import app.passwordstore.util.totp.UriTotpFinder
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertNull
+import kotlin.test.assertTrue
+import kotlin.time.Duration.Companion.seconds
+
+class EntryModelTest {
+
+ private fun entry(content: String) =
+ PasswordEntry(UserClock(), UriTotpFinder(), content.encodeToByteArray())
+
+ @Test
+ fun firstGroupAndExtrasInFileOrder() {
+ val fields =
+ entryFields(entry("hunter2\nusername: alice\nurl: https://example.org\npin: 1234\n"))
+ assertEquals("hunter2", fields.password)
+ assertEquals("alice", fields.username)
+ assertEquals(listOf("url" to "https://example.org", "pin" to "1234"), fields.extras)
+ }
+
+ @Test
+ fun blankPasswordAndMissingUsernameAreLeftOut() {
+ val fields = entryFields(entry("\nnote: none\n"))
+ assertNull(fields.password)
+ assertNull(fields.username)
+ assertEquals(listOf("note" to "none"), fields.extras)
+ }
+
+ @Test
+ fun otpUriIsNotAnExtraField() {
+ val fields =
+ entryFields(
+ entry(
+ "s3cret\notpauth://totp/example.com:carol?secret=JBSWY3DPEHPK3PXP&issuer=example.com\n"
+ )
+ )
+ assertEquals("s3cret", fields.password)
+ assertTrue(fields.extras.isEmpty())
+ }
+
+ @Test
+ fun fractionLeftOfTheOtpPeriod() {
+ assertEquals(1f, Totp("123456", 30.seconds, 30.seconds).fractionLeft())
+ assertEquals(0.5f, Totp("123456", 15.seconds, 30.seconds).fractionLeft())
+ }
+}