passwords: compare all Passkey fields in equals/hashCode
- Commit
e9ca13b33eb5b6171b3d71bb0187149f6cfc5d40- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:15 UTC (28 days ago)
- Parents
05b6fca
Passkey overrode equals/hashCode with only id, rpId, credentialId, userId and linkedPasswordSyncId. viewModel.passkeys is a StateFlow of lists, and list equality uses element equality, so any update touching only the omitted fields was conflated away and the UI kept showing stale data. The concrete trigger is WebAuthnAuthenticator.signAssertion, which persists only a signCount/lastUsedTime bump after every assertion: the re-emitted row compared equal to the previous one, so the passkey detail screen's last-used card never updated. Name edits (rpName/userName/userDisplayName) and sync bookkeeping (syncId/updatedAt) were dropped the same way. The custom override exists because privateKeyBytes needs content comparison (the data-class default would use referential equality for ByteArray), so the fix keeps the override but covers every field, using contentEquals/contentHashCode for the key bytes. Also extends PasskeyEqualityTest with regression tests: signCount/lastUsed-only changes, display-field changes, and differing key bytes are unequal; identical key-byte content in distinct arrays is equal. Verified with :passwords:testDevUnitTest (full suite green; the three new tests failed before the fix).
Diff
passwords/src/main/java/com/vayunmathur/passwords/data/Passkey.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/data/Passkey.kt b/passwords/src/main/java/com/vayunmathur/passwords/data/Passkey.ktindex e02ae64b36..8f2f75f8f4 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/data/Passkey.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/data/Passkey.kt@@ -33,24 +33,48 @@ data class Passkey( val syncId: String = newSyncId(), val updatedAt: Long = System.currentTimeMillis(), ) : DatabaseItem {+ /**+ * Full-field equality with content (not referential) comparison of+ * [privateKeyBytes]. `viewModel.passkeys` is a StateFlow, which conflates by+ * equality: leaving a field out means a change to that field alone produces an+ * equal list and the emission is dropped, so the UI silently keeps showing+ * stale data (e.g. `signAssertion` persists only a `signCount`/`lastUsedTime`+ * bump, and the "last used" card would never update).+ */ override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is Passkey) return false return id == other.id && rpId == other.rpId &&+ rpName == other.rpName && credentialId == other.credentialId && userId == other.userId &&- // StateFlow conflates by equality, so a link-only change would never reach the UI if- // this were left out.- linkedPasswordSyncId == other.linkedPasswordSyncId+ userName == other.userName &&+ userDisplayName == other.userDisplayName &&+ privateKeyBytes.contentEquals(other.privateKeyBytes) &&+ creationTime == other.creationTime &&+ lastUsedTime == other.lastUsedTime &&+ signCount == other.signCount &&+ linkedPasswordSyncId == other.linkedPasswordSyncId &&+ syncId == other.syncId &&+ updatedAt == other.updatedAt } override fun hashCode(): Int { var result = id.hashCode() result = 31 * result + rpId.hashCode()+ result = 31 * result + rpName.hashCode() result = 31 * result + credentialId.hashCode() result = 31 * result + userId.hashCode()+ result = 31 * result + userName.hashCode()+ result = 31 * result + userDisplayName.hashCode()+ result = 31 * result + privateKeyBytes.contentHashCode()+ result = 31 * result + creationTime.hashCode()+ result = 31 * result + lastUsedTime.hashCode()+ result = 31 * result + signCount result = 31 * result + linkedPasswordSyncId.hashCode()+ result = 31 * result + syncId.hashCode()+ result = 31 * result + updatedAt.hashCode() return result } }
passwords/src/test/java/com/vayunmathur/passwords/data/PasskeyEqualityTest.kt
diff --git a/passwords/src/test/java/com/vayunmathur/passwords/data/PasskeyEqualityTest.kt b/passwords/src/test/java/com/vayunmathur/passwords/data/PasskeyEqualityTest.ktindex 94f00fea55..c29b3f30ca 100644--- a/passwords/src/test/java/com/vayunmathur/passwords/data/PasskeyEqualityTest.kt+++ b/passwords/src/test/java/com/vayunmathur/passwords/data/PasskeyEqualityTest.kt@@ -1,6 +1,7 @@ package com.vayunmathur.passwords.data import kotlin.test.Test+import kotlin.test.assertEquals import kotlin.test.assertNotEquals /**@@ -21,4 +22,42 @@ class PasskeyEqualityTest { assertNotEquals(listOf(pk), listOf(linked)) assertNotEquals(pk.hashCode(), linked.hashCode()) }++ /**+ * `WebAuthnAuthenticator.signAssertion` persists only a `signCount`/`lastUsedTime`+ * bump. If those fields stay out of [Passkey.equals] the re-emitted row compares+ * equal to the previous one, the `passkeys` StateFlow drops the emission, and the+ * "last used" card in the UI never updates.+ */+ @Test fun aSignCountOnlyChangeIsNotEqual() {+ val pk = Passkey(id = 1, rpId = "example.com", credentialId = "cred", userId = "user")+ val used = pk.copy(signCount = pk.signCount + 1, lastUsedTime = pk.lastUsedTime + 1)++ assertNotEquals(pk, used)+ assertNotEquals(listOf(pk), listOf(used))+ assertNotEquals(pk.hashCode(), used.hashCode())+ }++ @Test fun aDisplayFieldChangeIsNotEqual() {+ val pk = Passkey(id = 1, rpId = "example.com", credentialId = "cred", userId = "user")++ assertNotEquals(pk, pk.copy(rpName = "Example"))+ assertNotEquals(pk, pk.copy(userName = "alice"))+ assertNotEquals(pk, pk.copy(userDisplayName = "Alice"))+ }++ @Test fun equalKeyBytesContentIsEqual() {+ val a = Passkey(+ id = 1,+ rpId = "example.com",+ credentialId = "cred",+ userId = "user",+ privateKeyBytes = byteArrayOf(1, 2, 3),+ )+ val b = a.copy(privateKeyBytes = byteArrayOf(1, 2, 3))++ assertEquals(a, b)+ assertEquals(a.hashCode(), b.hashCode())+ assertNotEquals(a, a.copy(privateKeyBytes = byteArrayOf(4, 5, 6)))+ } }