|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 microG Project Team |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.microg.gms.ui |
| 7 | + |
| 8 | +import android.content.Context |
| 9 | +import android.os.Bundle |
| 10 | +import android.text.format.DateUtils |
| 11 | +import android.util.Base64 |
| 12 | +import android.util.Log |
| 13 | +import android.widget.Toast |
| 14 | +import androidx.appcompat.app.AlertDialog |
| 15 | +import androidx.lifecycle.lifecycleScope |
| 16 | +import androidx.preference.Preference |
| 17 | +import androidx.preference.PreferenceCategory |
| 18 | +import androidx.preference.PreferenceFragmentCompat |
| 19 | +import com.google.android.gms.R |
| 20 | +import com.google.android.gms.fido.fido2.api.common.PublicKeyCredentialUserEntity |
| 21 | +import kotlinx.coroutines.Dispatchers |
| 22 | +import kotlinx.coroutines.withContext |
| 23 | +import org.microg.gms.fido.core.Database |
| 24 | +import org.microg.gms.fido.core.KnownRegistration |
| 25 | +import org.microg.gms.fido.core.transport.Transport |
| 26 | +import org.microg.gms.fido.core.transport.screenlock.ScreenLockCredentialStore |
| 27 | +import org.microg.gms.profile.Build |
| 28 | + |
| 29 | +class PasskeyManagerFragment : PreferenceFragmentCompat() { |
| 30 | + |
| 31 | + private lateinit var category: PreferenceCategory |
| 32 | + private lateinit var emptyPlaceholder: Preference |
| 33 | + |
| 34 | + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { |
| 35 | + addPreferencesFromResource(R.xml.preferences_passkey_manager) |
| 36 | + category = preferenceScreen.findPreference(PREFCAT_PASSKEYS) ?: return |
| 37 | + emptyPlaceholder = preferenceScreen.findPreference(PREF_PASSKEYS_NONE) ?: return |
| 38 | + } |
| 39 | + |
| 40 | + override fun onResume() { |
| 41 | + super.onResume() |
| 42 | + updateContent() |
| 43 | + } |
| 44 | + |
| 45 | + private fun updateContent() { |
| 46 | + val ctx = requireContext().applicationContext |
| 47 | + lifecycleScope.launchWhenResumed { |
| 48 | + val list = withContext(Dispatchers.IO) { |
| 49 | + runCatching { Database(ctx).getAllKnownRegistrations() } |
| 50 | + .onFailure { Log.w(TAG, "Failed to load passkeys", it) } |
| 51 | + .getOrDefault(emptyList()) |
| 52 | + } |
| 53 | + category.removeAll() |
| 54 | + if (list.isEmpty()) { |
| 55 | + category.addPreference(emptyPlaceholder) |
| 56 | + } else { |
| 57 | + list.forEachIndexed { index, item -> |
| 58 | + category.addPreference(buildPasskeyPreference(ctx, item, index)) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private fun buildPasskeyPreference(ctx: Context, item: KnownRegistration, order: Int): Preference = |
| 65 | + Preference(ctx).apply { |
| 66 | + key = "pref_passkey_${item.rpId}_${item.credentialId}" |
| 67 | + this.order = order |
| 68 | + isIconSpaceReserved = false |
| 69 | + widgetLayoutResource = R.layout.widget_passkey_delete |
| 70 | + title = item.rpId |
| 71 | + summary = buildSummary(ctx, item) |
| 72 | + setOnPreferenceClickListener { |
| 73 | + confirmDelete(item) |
| 74 | + true |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private fun confirmDelete(item: KnownRegistration) { |
| 79 | + val displayUser = formatPasskeyUser(requireContext(), item.userJson) |
| 80 | + AlertDialog.Builder(requireContext()) |
| 81 | + .setTitle(R.string.pref_passkey_manager_delete_dialog_title) |
| 82 | + .setMessage(getString(R.string.pref_passkey_manager_delete_dialog_message, item.rpId, displayUser)) |
| 83 | + .setNegativeButton(android.R.string.cancel, null) |
| 84 | + .setPositiveButton(R.string.pref_passkey_manager_delete_dialog_confirm) { _, _ -> |
| 85 | + performDelete(item) |
| 86 | + } |
| 87 | + .show() |
| 88 | + } |
| 89 | + |
| 90 | + private fun performDelete(item: KnownRegistration) { |
| 91 | + val ctx = requireContext().applicationContext |
| 92 | + lifecycleScope.launchWhenResumed { |
| 93 | + val ok = withContext(Dispatchers.IO) { |
| 94 | + runCatching { |
| 95 | + if (Build.VERSION.SDK_INT >= 23) { |
| 96 | + val keyId = Base64.decode(item.credentialId, Base64.URL_SAFE or Base64.NO_PADDING or Base64.NO_WRAP) |
| 97 | + ScreenLockCredentialStore(ctx).deleteKey(item.rpId, keyId) |
| 98 | + } |
| 99 | + Database(ctx).deleteKnownRegistration(item.rpId, item.credentialId) |
| 100 | + }.onFailure { Log.w(TAG, "Failed to delete passkey", it) }.isSuccess |
| 101 | + } |
| 102 | + Log.d(TAG, "performDelete ok? = $ok") |
| 103 | + updateContent() |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + companion object { |
| 108 | + private const val TAG = "PasskeyManager" |
| 109 | + private const val PREFCAT_PASSKEYS = "prefcat_passkeys" |
| 110 | + private const val PREF_PASSKEYS_NONE = "pref_passkeys_none" |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +internal fun formatPasskeyUser(context: Context, userJson: String?): String { |
| 115 | + if (userJson.isNullOrBlank()) return context.getString(R.string.pref_passkey_manager_unknown_user) |
| 116 | + return try { |
| 117 | + val entity = PublicKeyCredentialUserEntity.parseJson(userJson) |
| 118 | + val displayName = entity.displayName?.takeIf { it.isNotBlank() } |
| 119 | + val name = entity.name?.takeIf { it.isNotBlank() } |
| 120 | + when { |
| 121 | + displayName != null && name != null && displayName != name -> "$displayName ($name)" |
| 122 | + displayName != null -> displayName |
| 123 | + name != null -> name |
| 124 | + else -> context.getString(R.string.pref_passkey_manager_unknown_user) |
| 125 | + } |
| 126 | + } catch (e: Exception) { |
| 127 | + context.getString(R.string.pref_passkey_manager_unknown_user) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +private fun buildSummary(context: Context, item: KnownRegistration): String { |
| 132 | + val user = formatPasskeyUser(context, item.userJson) |
| 133 | + val time = DateUtils.getRelativeTimeSpanString( |
| 134 | + item.timestamp, |
| 135 | + System.currentTimeMillis(), |
| 136 | + DateUtils.MINUTE_IN_MILLIS |
| 137 | + ).toString() |
| 138 | + val transportLabel = context.getString(transportLabelRes(item.transport)) |
| 139 | + val credId = context.getString( |
| 140 | + R.string.pref_passkey_manager_credential_id_format_internal, |
| 141 | + truncateCredentialId(item.credentialId) |
| 142 | + ) |
| 143 | + return "$user\n$time · $transportLabel\n$credId" |
| 144 | +} |
| 145 | + |
| 146 | +private fun transportLabelRes(transport: Transport): Int = when (transport) { |
| 147 | + Transport.SCREEN_LOCK -> R.string.pref_passkey_manager_transport_screen_lock |
| 148 | + Transport.USB -> R.string.pref_passkey_manager_transport_usb |
| 149 | + Transport.NFC -> R.string.pref_passkey_manager_transport_nfc |
| 150 | + Transport.BLUETOOTH -> R.string.pref_passkey_manager_transport_bluetooth |
| 151 | + Transport.HYBRID -> R.string.pref_passkey_manager_transport_hybrid |
| 152 | +} |
| 153 | + |
| 154 | +private fun truncateCredentialId(id: String): String { |
| 155 | + if (id.length <= 16) return id |
| 156 | + return id.substring(0, 6) + "…" + id.substring(id.length - 6) |
| 157 | +} |
0 commit comments