Modern Apps git

← Modern-Apps.git

passwords: Credential Manager returns the account identity, not a blank or display name

Commit
997a783d7597094ab4d3530b5be5b9e7d828b64c
Author
Vayun Mathur <[email protected]>
When
2026-12-01 08:09 UTC (5 days ago)
Parents
7ba660b
The autofill-save path creates email-only rows (email set,
username blank, display name 'Saved Login'). The Autofill
fill path resolves identity as username-else-email, but the
Credential Manager paths did not: enumeration offered the
entry under the display name, and on pick handlePassword
returned PasswordCredential(blank username, password) — an
unusable pair from the requesting app's side.

Identity resolution is now one shared rule
(passwordCredentialIdentity: username, else email) used both
to offer the entry and to build the returned credential, so
what the user picked is what is handed back. Four new tests
cover the autofill-saved shape, username-precedence, the
email fallback and the no-identity case (red pre-fix on the
offered name). Vault locking, domain matching, TOTP, KDBX
merge and passkey assertion were audited and cleared.
Verified: :passwords:testDevUnitTest 333 tests pass; lint
errors are pre-existing on untouched files.

Diff

passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.ktindex 8b8f9f0b95..6620322feb 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt@@ -11,6 +11,7 @@ import androidx.credentials.provider.PasswordCredentialEntry import androidx.credentials.provider.PublicKeyCredentialEntry import com.vayunmathur.library.log.Log import com.vayunmathur.passwords.data.PasskeyDao+import com.vayunmathur.passwords.data.Password import com.vayunmathur.passwords.data.PasswordDao import com.vayunmathur.passwords.data.PasswordRepository import com.vayunmathur.passwords.ui.PasskeyAuthActivity@@ -53,6 +54,17 @@ internal fun entryRequestCode(tag: Int, id: Long): Int = private fun entryText(value: String, fallback: String): String =     value.take(MAX_ENTRY_TEXT_LENGTH).ifBlank { fallback.take(MAX_ENTRY_TEXT_LENGTH) } +/**+ * The identity a saved password is offered under in Credential Manager —+ * and, when picked, the username handed back to the caller: the username,+ * else the email. Same rule the autofill fill path uses. Entries the+ * autofill-save path creates for an email-shaped username carry their+ * identity only in the email field, so skipping this fallback would offer+ * (and then return) them under no username at all.+ */+internal fun passwordCredentialIdentity(password: Password): String =+    password.username.ifBlank { password.email }+ /**  * Builds the credential entries (passkeys + saved passwords) shown to the  * Credential Manager for a get-credential request. Shared by@@ -178,7 +190,7 @@ private suspend fun addPasswordEntries(             )             PasswordCredentialEntry.Builder(                 context,-                entryText(pass.username.ifBlank { pass.name }, "Password"),+                entryText(passwordCredentialIdentity(pass).ifBlank { pass.name }, "Password"),                 pendingIntent,                 option,             ).setDisplayName(pass.name.take(MAX_ENTRY_TEXT_LENGTH).ifBlank { null })
passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.ktindex 8c0e648d86..47a8554972 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt@@ -18,6 +18,7 @@ import com.vayunmathur.passwords.platform.PasskeyCredentialService import com.vayunmathur.passwords.platform.PasskeyUtils import com.vayunmathur.passwords.platform.buildAssertionResponseJson import com.vayunmathur.passwords.platform.buildGetCredentialResponse+import com.vayunmathur.passwords.platform.passwordCredentialIdentity import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers@@ -239,7 +240,12 @@ class PasskeyAuthActivity : FragmentActivity() {             setResult(RESULT_CANCELED)             return         }-        val credentialResponse = androidx.credentials.PasswordCredential(password.username, password.password)+        // Same identity the entry was offered under (username, else email):+        // an email-only row must not hand the caller a blank username.+        val credentialResponse = androidx.credentials.PasswordCredential(+            passwordCredentialIdentity(password),+            password.password,+        )         val result = Intent()         PendingIntentHandler.setGetCredentialResponse(             result,
passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordCredentialIdentityTest.kt
diff --git a/passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordCredentialIdentityTest.kt b/passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordCredentialIdentityTest.ktnew file mode 100644index 0000000000..79249aa76e--- /dev/null+++ b/passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordCredentialIdentityTest.kt@@ -0,0 +1,95 @@+package com.vayunmathur.passwords.platform++import android.content.Context+import android.os.Bundle+import androidx.credentials.provider.BeginGetPasswordOption+import androidx.credentials.provider.PasswordCredentialEntry+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.passwords.data.Passkey+import com.vayunmathur.passwords.data.PasskeyDao+import com.vayunmathur.passwords.data.Password+import com.vayunmathur.passwords.data.PasswordDao+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.flowOf+import kotlinx.coroutines.runBlocking+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Regression: a credential whose identity lives in the email field (the shape+ * the autofill-save path itself creates for an email-shaped username) must be+ * offered by Credential Manager under that email, not under its display name,+ * and the assertion path must hand the caller that same identity.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [35])+class PasswordCredentialIdentityTest {++    private val ctx: Context get() = ApplicationProvider.getApplicationContext()++    private class FakePasskeyDao : PasskeyDao() {+        override fun getAllFlow(): Flow<List<Passkey>> = flowOf(emptyList())+        override suspend fun getAll(): List<Passkey> = emptyList()+        override suspend fun getByRpId(rpId: String): List<Passkey> = emptyList()+        override suspend fun getByCredentialId(credentialId: String): Passkey? = null+        override suspend fun getBySyncId(syncId: String): Passkey? = null+        override suspend fun getUpdatedSince(since: Long): List<Passkey> = emptyList()+        override suspend fun upsertRaw(passkey: Passkey): Long = passkey.id+        override suspend fun delete(passkey: Passkey): Int = 0+        override suspend fun deleteBySyncId(syncId: String): Int = 0+        override suspend fun deleteAll() = Unit+    }++    private class FakePasswordDao(private val rows: List<Password>) : PasswordDao() {+        override fun getAllFlow(): Flow<List<Password>> = flowOf(rows)+        override suspend fun getAll(): List<Password> = rows+        override fun getByIdFlow(id: Long): Flow<Password?> = flowOf(rows.firstOrNull { it.id == id })+        override suspend fun getById(id: Long): Password? = rows.firstOrNull { it.id == id }+        override suspend fun getBySyncId(syncId: String): Password? = null+        override suspend fun getUpdatedSince(since: Long): List<Password> = emptyList()+        override suspend fun upsertRaw(value: Password): Long = value.id+        override suspend fun delete(value: Password): Int = 0+        override suspend fun deleteBySyncId(syncId: String): Int = 0+        override suspend fun deleteAll() = Unit+    }++    @Test fun emailOnlyPasswordIsOfferedUnderItsEmail() = runBlocking {+        // Exact row newAutofillSavedPassword builds for username "[email protected]":+        // username blank, identity in email, generic display name.+        val saved = newAutofillSavedPassword(+            username = "[email protected]",+            password = "pw-value",+            targetPackage = null,+            targetWebDomain = "bank.example.com",+        )+        val response = buildGetCredentialResponse(+            ctx,+            listOf(BeginGetPasswordOption(emptySet(), Bundle(), "req-email-only")),+            FakePasskeyDao(),+            FakePasswordDao(listOf(saved.copy(id = 7))),+        )+        assertEquals(1, response.credentialEntries.size)+        val entry = response.credentialEntries.single() as PasswordCredentialEntry+        assertEquals("[email protected]", entry.username.toString())+    }++    @Test fun identityPrefersUsernameOverEmail() {+        val row = Password(username = "alice", email = "[email protected]")+        assertEquals("alice", passwordCredentialIdentity(row))+    }++    @Test fun identityFallsBackToEmailWhenUsernameBlank() {+        // Rows built by autofill save for an email-shaped username, CSV rows+        // with only an email column, and hand-made email-only entries all+        // carry the identity here and nowhere else.+        val row = Password(username = "", email = "[email protected]", name = "Saved Login")+        assertEquals("[email protected]", passwordCredentialIdentity(row))+    }++    @Test fun identityIsBlankWhenRowHasNoIdentity() {+        assertEquals("", passwordCredentialIdentity(Password(name = "No identity")))+    }+}