Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Bug 186779 -- Migrate to using the app-services FxA state machine
Browse files Browse the repository at this point in the history
Also hooked up the new secret debug menu entries for testing it.
  • Loading branch information
bendk committed Feb 15, 2024
1 parent ccb0196 commit 82329b2
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 531 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import mozilla.appservices.fxaclient.FxaRustAuthState
import mozilla.components.concept.base.crash.CrashReporting
import mozilla.components.concept.sync.AccountEvent
import mozilla.components.concept.sync.AccountEventsObserver
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.concept.sync.StatePersistenceCallback
import mozilla.components.lib.dataprotect.SecureAbove22Preferences
import mozilla.components.service.fxa.manager.FxaAccountManager
Expand All @@ -26,16 +25,16 @@ const val FXA_STATE_KEY = "fxaState"
* Represents state of our account on disk - is it new, or restored?
*/
internal sealed class AccountOnDisk : WithAccount {
data class Restored(val account: OAuthAccount) : AccountOnDisk() {
data class Restored(val account: FirefoxAccount) : AccountOnDisk() {
override fun account() = account
}
data class New(val account: OAuthAccount) : AccountOnDisk() {
data class New(val account: FirefoxAccount) : AccountOnDisk() {
override fun account() = account
}
}

internal interface WithAccount {
fun account(): OAuthAccount
fun account(): FirefoxAccount
}

/**
Expand Down Expand Up @@ -80,16 +79,16 @@ open class StorageWrapper(
}
}

private fun watchAccount(account: OAuthAccount) {
private fun watchAccount(account: FirefoxAccount) {
account.registerPersistenceCallback(statePersistenceCallback)
account.deviceConstellation().register(accountEventsIntegration)
}

/**
* Exists strictly for testing purposes, allowing tests to specify their own implementation of [OAuthAccount].
* Exists strictly for testing purposes, allowing tests to specify their own implementation of [FirefoxAccount].
*/
@VisibleForTesting
open fun obtainAccount(): OAuthAccount = FirefoxAccount(serverConfig, crashReporter)
open fun obtainAccount(): FirefoxAccount = FirefoxAccount(serverConfig, crashReporter)
}

/**
Expand All @@ -110,7 +109,7 @@ internal class AccountEventsIntegration(

internal interface AccountStorage {
@Throws(Exception::class)
fun read(): OAuthAccount?
fun read(): FirefoxAccount?
fun write(accountState: String)
fun clear()
}
Expand Down Expand Up @@ -156,7 +155,7 @@ internal class SharedPrefAccountStorage(
* @throws FxaException if JSON failed to parse into a [FirefoxAccount].
*/
@Throws(FxaException::class)
override fun read(): OAuthAccount? {
override fun read(): FirefoxAccount? {
val savedJSON = accountPreferences().getString(FXA_STATE_KEY, null)
?: return null

Expand Down Expand Up @@ -244,7 +243,7 @@ internal class SecureAbove22AccountStorage(
* @throws FxaException if JSON failed to parse into a [FirefoxAccount].
*/
@Throws(FxaException::class)
override fun read(): OAuthAccount? {
override fun read(): FirefoxAccount? {
return store.getString(KEY_ACCOUNT_STATE).also {
// If account state is missing, but we expected it to be present, report an exception.
if (it == null && prefs.getBoolean(PREF_KEY_HAS_STATE, false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext
import mozilla.appservices.fxaclient.FxaClient
import mozilla.appservices.fxaclient.FxaEvent
import mozilla.appservices.fxaclient.FxaState
import mozilla.components.concept.base.crash.CrashReporting
import mozilla.components.concept.sync.AuthFlowUrl
import mozilla.components.concept.sync.DeviceConstellation
Expand Down Expand Up @@ -96,6 +98,14 @@ class FirefoxAccount internal constructor(

internal fun getAuthState() = inner.getAuthState()

internal fun processEvent(event: FxaEvent): FxaState = inner.processEvent(event)

internal fun simulateNetworkError() = inner.simulateNetworkError()

internal fun simulateTemporaryAuthTokenIssue() = inner.simulateTemporaryAuthTokenIssue()

internal fun simulatePermanentAuthTokenIssue() = inner.simulatePermanentAuthTokenIssue()

override suspend fun beginOAuthFlow(
scopes: Set<String>,
entryPoint: FxAEntryPoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
import mozilla.appservices.fxaclient.FxaClient
import mozilla.appservices.fxaclient.FxaException
import mozilla.appservices.fxaclient.FxaStateCheckerEvent
import mozilla.appservices.fxaclient.FxaStateCheckerState
import mozilla.appservices.syncmanager.SyncTelemetry
import mozilla.components.concept.base.crash.CrashReporting
import mozilla.components.concept.sync.AccountEvent
Expand All @@ -27,7 +25,6 @@ import mozilla.components.concept.sync.DeviceConstellation
import mozilla.components.concept.sync.DeviceConstellationObserver
import mozilla.components.concept.sync.DevicePushSubscription
import mozilla.components.concept.sync.ServiceResult
import mozilla.components.service.fxa.manager.AppServicesStateMachineChecker
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.base.observer.Observable
import mozilla.components.support.base.observer.ObserverRegistry
Expand Down Expand Up @@ -89,33 +86,22 @@ class FxaDeviceConstellation(
val capabilities = config.capabilities.map { it.into() }.toSet()
if (finalizeAction == DeviceFinalizeAction.Initialize) {
try {
AppServicesStateMachineChecker.checkInternalState(FxaStateCheckerState.InitializeDevice)
account.initializeDevice(config.name, config.type.into(), capabilities)
AppServicesStateMachineChecker.handleInternalEvent(FxaStateCheckerEvent.InitializeDeviceSuccess)
ServiceResult.Ok
} catch (e: FxaPanicException) {
AppServicesStateMachineChecker.handleInternalEvent(FxaStateCheckerEvent.CallError)
throw e
} catch (e: FxaUnauthorizedException) {
AppServicesStateMachineChecker.handleInternalEvent(FxaStateCheckerEvent.CallError)
ServiceResult.AuthError
} catch (e: FxaException) {
AppServicesStateMachineChecker.handleInternalEvent(FxaStateCheckerEvent.CallError)
ServiceResult.OtherError
}
} else {
try {
AppServicesStateMachineChecker.checkInternalState(FxaStateCheckerState.EnsureDeviceCapabilities)
account.ensureCapabilities(capabilities)
AppServicesStateMachineChecker.handleInternalEvent(
FxaStateCheckerEvent.EnsureDeviceCapabilitiesSuccess,
)
ServiceResult.Ok
} catch (e: FxaPanicException) {
AppServicesStateMachineChecker.handleInternalEvent(FxaStateCheckerEvent.CallError)
throw e
} catch (e: FxaUnauthorizedException) {
AppServicesStateMachineChecker.handleInternalEvent(FxaStateCheckerEvent.EnsureCapabilitiesAuthError)
// Unless we've added a new capability, in practice 'ensureCapabilities' isn't
// actually expected to do any work: everything should have been done by initializeDevice.
// So if it did, and failed, let's report this so that we're aware of this!
Expand All @@ -125,7 +111,6 @@ class FxaDeviceConstellation(
)
ServiceResult.AuthError
} catch (e: FxaException) {
AppServicesStateMachineChecker.handleInternalEvent(FxaStateCheckerEvent.CallError)
ServiceResult.OtherError
}
}
Expand Down

This file was deleted.

Loading

1 comment on commit 82329b2

@firefoxci-taskcluster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh! Looks like an error! Details

Failed to fetch task artifact public/github/customCheckRunText.md for GitHub integration.
Make sure the artifact exists on the worker or other location.

Please sign in to comment.