01a36beeebcd
add PasswordHistoryItem model + passwordHistory field; capture old password in VaultRepository.updateEntry with 7 unit tests
- time
- 2026-06-07 16:02 UTC · 5 months ago
- machine
- koh2
- agent
- claude-opus-4-8
- session
- password history
- parent
- 99cdeb70e753
↓ peach-android-01a36bee.tar.gzatlas at this save
files changed4 files
app/src/main/java/com/peachpasswords/vault/PasswordHistoryItem.kt+23 −0app/src/main/java/com/peachpasswords/vault/VaultEntry.kt+2 −0app/src/main/java/com/peachpasswords/vault/VaultRepository.kt+20 −0app/src/test/java/com/peachpasswords/vault/VaultRepositoryTest.kt+71 −0app/src/main/java/com/peachpasswords/vault/PasswordHistoryItem.kt+23 −0
@@ -0,0 +1,23 @@1+package com.peachpasswords.vault2+3+import android.os.Parcelable4+import kotlinx.parcelize.Parcelize5+import kotlinx.serialization.Serializable6+7+/**8+ * One past password kept in [VaultEntry.passwordHistory]. A snapshot of the old9+ * password is recorded whenever an entry's password changes, so the user can10+ * reveal, copy, or restore a value they used before. Mirrors the browser11+ * `PasswordHistoryItem` shape.12+ */13+@Parcelize14+@Serializable15+data class PasswordHistoryItem(16+ /** The old password value, replaced by a newer one. */17+ val password: String,18+ /**19+ * When this password stopped being current — the `passwordChangedAt`20+ * (or `createdAt`) it carried at the moment it was swapped out.21+ */22+ val changedAt: Long,23+) : Parcelableapp/src/main/java/com/peachpasswords/vault/VaultEntry.kt+2 −0
@@ -19,6 +19,8 @@1919 val notes: String? = null,2020 val breach: Int? = null,2121 val passwordChangedAt: Long? = null,22+ /** Past passwords, newest first. Captured automatically on password change. */23+ val passwordHistory: List<PasswordHistoryItem>? = null,2224 val canary: Canary? = null,2325 val createdAt: Long,2426 val modifiedAt: Long,app/src/main/java/com/peachpasswords/vault/VaultRepository.kt+20 −0
@@ -14,6 +14,9 @@1414 private fun b64(bytes: ByteArray): String = Base64.getEncoder().withoutPadding().encodeToString(bytes)1515 private fun unb64(s: String): ByteArray = Base64.getDecoder().decode(s)1616 17+/** Cap on retained past passwords per entry. Mirrors the browser PASSWORD_HISTORY_MAX. */18+private const val PASSWORD_HISTORY_MAX = 5019+1720 sealed interface UnlockResult {1821 data object Success : UnlockResult1922 data object WrongPassword : UnlockResult@@ -245,10 +248,27 @@245248 val stamp = VaultCrypto.monotonicEntryTimestamp(current.modifiedAt)246249 // Only refresh passwordChangedAt when the password value actually changes.247250 val passwordChanged = patch.password != null && patch.password != current.password251+ // Capture the outgoing password (newest first) so the user can reveal/copy/252+ // restore it later. History is computed authoritatively from `current`, not253+ // the patch — updateEntry is a full-entry replace, so trusting patch's value254+ // would let a stale form clobber the real history. Record the age the old255+ // password carried, skip an empty prior, and skip an immediate exact dupe256+ // (e.g. restore-then-restore). Capped at PASSWORD_HISTORY_MAX.257+ val priorPassword = current.password258+ val shouldCapture = passwordChanged &&259+ !priorPassword.isNullOrEmpty() &&260+ current.passwordHistory?.firstOrNull()?.password != priorPassword261+ val nextHistory = if (shouldCapture) {262+ (listOf(PasswordHistoryItem(priorPassword!!, current.passwordChangedAt ?: current.createdAt)) +263+ (current.passwordHistory ?: emptyList())).take(PASSWORD_HISTORY_MAX)264+ } else {265+ current.passwordHistory266+ }248267 val updated = patch.copy(249268 createdAt = current.createdAt,250269 modifiedAt = stamp,251270 passwordChangedAt = if (passwordChanged) stamp else current.passwordChangedAt,271+ passwordHistory = nextHistory,252272 )253273 val rollback = entryList.toMutableList()254274 entryList[idx] = updatedapp/src/test/java/com/peachpasswords/vault/VaultRepositoryTest.kt+71 −0
@@ -91,6 +91,77 @@9191 assertTrue(pwChange.passwordChangedAt!! > firstChanged)9292 }9393 94+ @Test fun password_history_captures_old_password_newest_first() = runBlocking {95+ val repo = VaultRepository(FakeStorage())96+ repo.createVault("pw")97+ val e = repo.addEntry(login("H", password = "first"))98+ assertNull(e.passwordHistory)99+ val v2 = repo.updateEntry(e.copy(password = "second"))100+ val v3 = repo.updateEntry(v2.copy(password = "third"))101+ assertEquals(listOf("second", "first"), v3.passwordHistory!!.map { it.password })102+ assertEquals("third", v3.password)103+ }104+105+ @Test fun password_history_ignores_metadata_only_update() = runBlocking {106+ val repo = VaultRepository(FakeStorage())107+ repo.createVault("pw")108+ val e = repo.addEntry(login("M", password = "keepme"))109+ val r = repo.updateEntry(e.copy(title = "Renamed"))110+ assertNull(r.passwordHistory)111+ }112+113+ @Test fun password_history_item_carries_prior_age() = runBlocking {114+ val repo = VaultRepository(FakeStorage())115+ repo.createVault("pw")116+ val e = repo.addEntry(login("S", password = "v1"))117+ val firstChanged = e.passwordChangedAt!!118+ Thread.sleep(2)119+ val r = repo.updateEntry(e.copy(password = "v2"))120+ assertEquals(firstChanged, r.passwordHistory!!.first().changedAt)121+ }122+123+ @Test fun password_history_skips_identical_resave() = runBlocking {124+ val repo = VaultRepository(FakeStorage())125+ repo.createVault("pw")126+ val e = repo.addEntry(login("N", password = "same"))127+ val r = repo.updateEntry(e.copy(password = "same"))128+ assertNull(r.passwordHistory)129+ }130+131+ @Test fun password_history_restore_re_records_swapped_out_password() = runBlocking {132+ val repo = VaultRepository(FakeStorage())133+ repo.createVault("pw")134+ val e = repo.addEntry(login("R", password = "old"))135+ val v2 = repo.updateEntry(e.copy(password = "new"))136+ val restored = repo.updateEntry(v2.copy(password = "old"))137+ assertEquals("old", restored.password)138+ assertEquals(listOf("new", "old"), restored.passwordHistory!!.map { it.password })139+ }140+141+ @Test fun password_history_persists_across_lock_unlock() = runBlocking {142+ val storage = FakeStorage()143+ val repo = VaultRepository(storage)144+ repo.createVault("pw")145+ val e = repo.addEntry(login("P", password = "p1"))146+ repo.updateEntry(e.copy(password = "p2"))147+ repo.lock()148+ assertEquals(UnlockResult.Success, repo.unlock("pw"))149+ val reloaded = repo.getEntries().first { it.id == e.id }150+ assertEquals(listOf("p1"), reloaded.passwordHistory!!.map { it.password })151+ }152+153+ @Test fun password_history_caps_at_fifty() = runBlocking {154+ val repo = VaultRepository(FakeStorage())155+ repo.createVault("pw")156+ var e = repo.addEntry(login("C", password = "pw-0"))157+ for (i in 1..55) e = repo.updateEntry(e.copy(password = "pw-$i"))158+ val hist = e.passwordHistory!!159+ assertEquals(50, hist.size)160+ // pw-55 is current; newest retired is pw-54, oldest kept is pw-5.161+ assertEquals("pw-54", hist.first().password)162+ assertEquals("pw-5", hist.last().password)163+ }164+94165 @Test fun apply_merged_remote_does_not_bump_version() = runBlocking {95166 val repo = VaultRepository(FakeStorage())96167 repo.createVault("pw")