Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ private constructor(
internal var loginPicker =
with(loginDelegate) {
loginPickerView?.let {
LoginPicker(store, it, onManageLogins, customTabId)
LoginPicker(store, it, onManageLogins, customTabId, onSelectLogin)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ interface LoginDelegate {
/** Callback invoked when a user selects "Manage logins" from the select login prompt. */
val onManageLogins: () -> Unit
get() = {}

/**
* Callback invoked when a user selects a login from the select login prompt, before it is filled. Invoke
* `onConfirm` to fill the login, or `onDismiss` to dismiss the prompt without filling it. The login is filled right
* away by default.
*/
val onSelectLogin: (onConfirm: () -> Unit, onDismiss: () -> Unit) -> Unit
get() = { onConfirm, _ -> onConfirm() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mozilla.components.feature.prompts.login

import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.selector.findTabOrCustomTabOrSelectedTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.prompt.PromptRequest
import mozilla.components.concept.storage.Login
Expand All @@ -25,12 +26,16 @@ import mozilla.components.support.base.log.logger.Logger
* @property loginSelectBar The [AutocompletePrompt] view into which the select login "prompt" will be inflated.
* @property manageLoginsCallback A callback invoked when a user selects "manage logins" from the select login prompt.
* @property sessionId This is the id of the session which requested the prompt.
* @property selectLoginCallback A callback invoked when a user selects a login, before it is filled. See
* [LoginDelegate.onSelectLogin].
*/
internal class LoginPicker(
private val store: BrowserStore,
private val loginSelectBar: AutocompletePrompt<Login>,
private val manageLoginsCallback: () -> Unit = {},
private var sessionId: String? = null,
private val selectLoginCallback: (onConfirm: () -> Unit, onDismiss: () -> Unit) -> Unit =
object : LoginDelegate {}.onSelectLogin,
) : SelectablePromptView.Listener<Login> {

init {
Expand All @@ -44,11 +49,32 @@ internal class LoginPicker(
}

override fun onOptionSelect(option: Login) {
store.consumePromptFrom<PromptRequest.SelectLoginPrompt>(sessionId) {
it.onConfirm(option)
}
emitLoginAutofillPerformedFact()
loginSelectBar.hidePrompt()

// The callback can complete later, e.g. after the user authenticated, so only the request the login was
// selected from is completed, even if the page replaced it in the meantime.
val requestUid =
store.state
.findTabOrCustomTabOrSelectedTab(sessionId)
?.content
?.promptRequests
?.lastOrNull { it is PromptRequest.SelectLoginPrompt }
?.uid ?: return

selectLoginCallback(
{
store.consumePromptFrom(sessionId, requestUid) {
(it as PromptRequest.SelectLoginPrompt).onConfirm(option)
emitLoginAutofillPerformedFact()
}
},
{
store.consumePromptFrom(sessionId, requestUid) {
(it as PromptRequest.SelectLoginPrompt).onDismiss()
emitLoginAutofillDismissedFact()
}
},
)
}

override fun onManageOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import mozilla.components.support.test.middleware.CaptureActionsMiddleware
import mozilla.components.support.test.mock
import mozilla.components.support.test.whenever
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -94,6 +96,96 @@ class LoginPickerTest {
verify(loginSelectBar).hidePrompt()
}

@Test
fun `WHEN a login is selected THEN it is only filled once the select login callback confirms it`() {
val selectedSession = prepareSelectedSession(request)
var onConfirm: () -> Unit = {}
loginPicker =
LoginPicker(store, loginSelectBar, onManageLogins, selectedSession.id) { confirm, _ -> onConfirm = confirm }

loginPicker.handleSelectLoginRequest(request)
loginPicker.onOptionSelect(login)

verify(loginSelectBar).hidePrompt()
assertNull(confirmedLogin)
assertFalse(onDismissWasCalled)

onConfirm()

assertEquals(login, confirmedLogin)
assertFalse(onDismissWasCalled)
}

@Test
fun `WHEN a login is selected and the select login callback dismisses it THEN nothing is filled`() {
val selectedSession = prepareSelectedSession(request)
var onDismiss: () -> Unit = {}
loginPicker =
LoginPicker(store, loginSelectBar, onManageLogins, selectedSession.id) { _, dismiss -> onDismiss = dismiss }

loginPicker.handleSelectLoginRequest(request)
loginPicker.onOptionSelect(login)
onDismiss()

assertNull(confirmedLogin)
assertTrue(onDismissWasCalled)
captureMiddleware.assertFirstAction(ContentAction.ConsumePromptRequestAction::class) { action ->
assertEquals(request, action.promptRequest)
}
}

@Test
fun `GIVEN the request was replaced before the select login callback confirms WHEN confirming THEN the new request is not filled`() {
val selectedSession = prepareSelectedSession(request)
var onConfirm: () -> Unit = {}
loginPicker =
LoginPicker(store, loginSelectBar, onManageLogins, selectedSession.id) { confirm, _ -> onConfirm = confirm }
var newRequestConfirmedLogin: Login? = null
var newRequestDismissed = false
val newRequest =
PromptRequest.SelectLoginPrompt(
logins = listOf(login2),
generatedPassword = null,
onConfirm = { newRequestConfirmedLogin = it },
onDismiss = { newRequestDismissed = true },
)

loginPicker.handleSelectLoginRequest(request)
loginPicker.onOptionSelect(login)
prepareSelectedSession(newRequest)
onConfirm()

assertNull(confirmedLogin)
assertNull(newRequestConfirmedLogin)
assertFalse(newRequestDismissed)
captureMiddleware.assertNotDispatched(ContentAction.ConsumePromptRequestAction::class)
}

@Test
fun `GIVEN the request was replaced before the select login callback dismisses WHEN dismissing THEN the new request is not dismissed`() {
val selectedSession = prepareSelectedSession(request)
var onDismiss: () -> Unit = {}
loginPicker =
LoginPicker(store, loginSelectBar, onManageLogins, selectedSession.id) { _, dismiss -> onDismiss = dismiss }
var newRequestDismissed = false
val newRequest =
PromptRequest.SelectLoginPrompt(
logins = listOf(login2),
generatedPassword = null,
onConfirm = {},
onDismiss = { newRequestDismissed = true },
)

loginPicker.handleSelectLoginRequest(request)
loginPicker.onOptionSelect(login)
prepareSelectedSession(newRequest)
onDismiss()

assertFalse(onDismissWasCalled)
assertFalse(newRequestDismissed)
captureMiddleware.assertNotDispatched(ContentAction.ConsumePromptRequestAction::class)
}

@Test
fun `LoginPicker invokes manage logins and hides view`() {
manageLoginsCalled = false
Expand Down
2 changes: 2 additions & 0 deletions mobile/android/android-components/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ permalink: /changelog/
---

# 163.0 (In Development)
* **feature-prompts**
* Added `LoginDelegate.onSelectLogin`, invoked when a login is selected from the select login prompt before it is filled, e.g. to ask the user to authenticate first. [Bug 2081840](https://bugzilla.mozilla.org/show_bug.cgi?id=2081840)

# 162.0
* **concept-engine**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,14 +740,14 @@ open class FenixApplication : Application(), Provider, ThemeProvider {

/**
* Registers or unregisters [AutofillService] so the application is only offered as an Android autofill service on
* devices where autofill is supported. The component enabled state is persisted by the system across application
* updates, so it has to be restored when autofill becomes supported again. Third party autofill services are
* unaffected.
* devices where autofill is supported. It stays disabled on Android Automotive OS, which does not let users select
* an autofill service. The component enabled state is persisted by the system across application updates, so it has
* to be restored when autofill becomes supported again. Third party autofill services are unaffected.
*/
private fun updateAutofillServiceState() {
val service = ComponentName(this, AutofillService::class.java)
val state =
if (components.settings.isAutofillSupported) {
if (components.settings.isAutofillSupported && !isAndroidAutomotiveAvailable()) {
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
} else {
PackageManager.COMPONENT_ENABLED_STATE_DISABLED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ import org.mozilla.fenix.pbmlock.observePrivateModeLock
import org.mozilla.fenix.pdf.PdfToolsIntegration
import org.mozilla.fenix.perf.MarkersFragmentLifecycleCallbacks
import org.mozilla.fenix.search.awesomebar.AwesomeBarComposable
import org.mozilla.fenix.settings.biometric.AutomotiveSensitiveDataAuthenticator
import org.mozilla.fenix.settings.biometric.BiometricPromptFeature
import org.mozilla.fenix.settings.biometric.DefaultAutomotiveSensitiveDataAuthenticator
import org.mozilla.fenix.settings.downloads.DownloadLocationManager
import org.mozilla.fenix.snackbar.FenixSnackbarDelegate
import org.mozilla.fenix.snackbar.SnackbarBinding
Expand Down Expand Up @@ -332,6 +334,10 @@ abstract class BaseBrowserFragment :
private val promptsFeature = ViewBoundFeatureWrapper<PromptFeature>()

@VisibleForTesting internal val findInPageIntegration = ViewBoundFeatureWrapper<FindInPageIntegration>()

@VisibleForTesting
internal var sensitiveDataAuthenticator: AutomotiveSensitiveDataAuthenticator =
DefaultAutomotiveSensitiveDataAuthenticator()
private val toolbarsIntegration = ViewBoundFeatureWrapper<ToolbarsIntegration>()
private val pdfToolsIntegration = ViewBoundFeatureWrapper<PdfToolsIntegration>()
private val bottomToolbarContainerIntegration = ViewBoundFeatureWrapper<BottomToolbarContainerIntegration>()
Expand Down Expand Up @@ -1121,6 +1127,10 @@ abstract class BaseBrowserFragment :
val directions = NavGraphDirections.actionGlobalSavedLoginsAuthFragment()
findNavController().navigate(directions)
}

override val onSelectLogin = { onConfirm: () -> Unit, onDismiss: () -> Unit ->
authenticateBeforeFillingLogin(onConfirm, onDismiss)
}
},
suggestStrongPasswordDelegate =
object : SuggestStrongPasswordDelegate {
Expand Down Expand Up @@ -1836,6 +1846,22 @@ abstract class BaseBrowserFragment :
}

/** Shows a biometric prompt and fallback to prompting for the password. */
/**
* Fills a login selected from the select login prompt, asking the user to authenticate first when required.
*
* @param onConfirm Fills the selected login.
* @param onDismiss Dismisses the select login prompt without filling the login.
*/
@VisibleForTesting
internal fun authenticateBeforeFillingLogin(onConfirm: () -> Unit, onDismiss: () -> Unit) {
sensitiveDataAuthenticator.authenticate(
activity = requireActivity(),
title = getString(R.string.logins_biometric_prompt_autofill_message),
onAuthenticated = onConfirm,
onBlocked = onDismiss,
)
}

private fun showBiometricPrompt(context: Context) {
if (BiometricPromptFeature.canUseFeature(BiometricManager.from(context))) {
biometricPromptFeature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class Core(
fontInflationEnabled = context.components.settings.shouldUseAutoSize,
suspendMediaWhenInactive = false,
forceUserScalableContent = context.components.settings.forceEnableZoom,
loginAutofillEnabled = context.components.settings.shouldAutofillLogins,
loginAutofillEnabled = context.components.settings.shouldAutofillLoginsWithoutPrompt,
creditCardSecurityCodeEnabled = context.components.settings.isCreditCardSecurityCodeEnabled,
enterpriseRootsEnabled = context.components.settings.allowThirdPartyRootCerts,
clearColor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ class SavedLoginsAuthFragment : PreferenceFragmentCompat(), SystemInsetsPaddedFr
onPreferenceChangeListener =
object : SharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
context.components.core.engine.settings.loginAutofillEnabled = newValue as Boolean
return super.onPreferenceChange(preference, newValue)
val result = super.onPreferenceChange(preference, newValue)
context.components.core.engine.settings.loginAutofillEnabled =
context.components.settings.shouldAutofillLoginsWithoutPrompt
return result
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,14 @@ class Settings(
default = { isAutofillSupported },
)

/**
* Whether the engine can fill a saved login without the user selecting it, e.g. when the page loads. Always false
* on Android Automotive OS, where the user has to authenticate before a password is filled, so logins can only be
* filled from the select login prompt.
*/
val shouldAutofillLoginsWithoutPrompt: Boolean
get() = shouldAutofillLogins && !appContext.isAndroidAutomotiveAvailable()

/**
* In Bug 1853113, we changed the type of [searchWidgetInstalled] from int to boolean without changing the pref key,
* now we have to migrate users that were using the previous type int to the new one boolean. The migration will
Expand Down
2 changes: 2 additions & 0 deletions mobile/android/fenix/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,8 @@
<string name="saved_login_hide_password">Hide password</string>
<!-- Message displayed in biometric prompt displayed for authentication before allowing users to view their passwords -->
<string name="logins_biometric_prompt_message_2">Unlock to view your saved passwords</string>
<!-- Message displayed in the authentication prompt shown, asking the user to unlock the device in order to use autofill with saved passwords. -->
<string name="logins_biometric_prompt_autofill_message">Unlock to use saved passwords</string>
<!-- Button for unlocking logins on the biometric lock screen -->
<string name="logins_biometric_unlock_button">Unlock</string>
<!-- Button for leaving logins on the biometric lock screen -->
Expand Down
Loading