Skip to content
This repository was archived by the owner on Nov 1, 2022. It is now read-only.
Closed
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 @@ -47,10 +47,12 @@ data class DeviceConfig(
* @property supportedEngines A set of supported sync engines, exposed via [GlobalSyncableStoreProvider].
* @property syncPeriodInMinutes Optional, how frequently periodic sync should happen. If this is `null`,
* periodic syncing will be disabled.
* @property tokenServerUrl Optional, override default tokenServerUrl to support self-hosted sync storage.
*/
data class SyncConfig(

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@grigoryk - So it seems that the ServerConfig (which was the suggestion on where to overwrite this value in the original issue) is pulled from here in application services and doesn't currently support setting a custom token server url.

Since #3729 was filed specifically for this repo, I wanted to first confirm with you that you still think it makes sense for the ServerConfig to set this value. If so, I can close this PR and first open a PR over in application services to support setting a token server url. Then, once that merges, I can re-open this one and adjust to use that value instead.

Since that's a much larger scope of changes, I figured it'd make sense to confirm this approach with you first.

val supportedEngines: Set<SyncEngine>,
val syncPeriodInMinutes: Long? = null
val syncPeriodInMinutes: Long? = null,
val tokenServerUrl: String? = null
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,16 @@ open class FxaAccountManager(
return
}

var tokenServerUrl = syncConfig?.tokenServerUrl

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A more succinct way to write this (and avoid using a var):
val tokenServerUrl = syncConfig?.toknServerUrl ?: account.getTokenServerEndpointURL()

if (tokenServerUrl == null) {
tokenServerUrl = account.getTokenServerEndpointURL()
}

logger.info("caching tokenServerUrl: $tokenServerUrl")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's not log the URL itself - it could be considered PII. Just mention the fact that this is a custom tokenServerUrl.

// NB: this call will inform authErrorRegistry in case an auth error is encountered.
account.getAccessTokenAsync(SCOPE_SYNC).await()?.let {
SyncAuthInfoCache(context).setToCache(
it.asSyncAuthInfo(account.getTokenServerEndpointURL())
it.asSyncAuthInfo(tokenServerUrl)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,62 @@ class FxaAccountManagerTest {
assertFalse(manager.isSyncActive())
}

@Test
fun `updating sync config with optional tokenServerUrl`() = runBlocking {
val accountStorage: AccountStorage = mock()
val profile = Profile("testUid", "test@example.com", null, "Test Profile")
val constellation: DeviceConstellation = mockDeviceConstellation()

val syncAccessTokenExpiresAt = System.currentTimeMillis() + 10 * 60 * 1000L
val account = StatePersistenceTestableAccount(profile, constellation, tokenServerEndpointUrl = "https://some.server.com/test") {
AccessTokenInfo(
SCOPE_SYNC,
"arda",
OAuthScopedKey("kty-test", SCOPE_SYNC, "kid-test", "k-test"),
syncAccessTokenExpiresAt
)
}

// With a sync config including a tokenServerUrl this time
var latestSyncManager: TestSyncManager? = null
val syncConfig = SyncConfig(setOf(SyncEngine.History), syncPeriodInMinutes = 120L, tokenServerUrl = "https://different.server.com/test")
val manager = object : TestableFxaAccountManager(
context = testContext,
config = ServerConfig.release("dummyId", "http://auth-url/redirect"),
storage = accountStorage,
capabilities = setOf(DeviceCapability.SEND_TAB),
syncConfig = syncConfig,
coroutineContext = this@runBlocking.coroutineContext,
block = { account }
) {
override fun createSyncManager(config: SyncConfig): SyncManager {
return TestSyncManager(config).also { latestSyncManager = it }
}
}

`when`(constellation.ensureCapabilitiesAsync(any())).thenReturn(CompletableDeferred(true))
// We have an account at the start.
`when`(accountStorage.read()).thenReturn(account)

val syncStatusObserver = TestSyncStatusObserver()
val lifecycleOwner: LifecycleOwner = mock()
val lifecycle: Lifecycle = mock()
`when`(lifecycle.currentState).thenReturn(Lifecycle.State.STARTED)
`when`(lifecycleOwner.lifecycle).thenReturn(lifecycle)
manager.registerForSyncEvents(syncStatusObserver, lifecycleOwner, true)
manager.initAsync().await()

// Make sure that tokenServerUrl was cached correctly.
assertFalse(SyncAuthInfoCache(testContext).expired())
val cachedAuthInfo = SyncAuthInfoCache(testContext).getCached()
assertNotNull(cachedAuthInfo)
assertEquals("https://different.server.com/test", cachedAuthInfo!!.tokenServerUrl)

// Can trigger syncs.
manager.syncNowAsync(SyncReason.User).await()
verify(latestSyncManager!!.dispatcher.inner, times(1)).syncNow(SyncReason.User, debounce = false)
}

@Test
fun `migrating an account via copyAccountAsync - creating a new session token`() = runBlocking {
// We'll test three scenarios:
Expand Down Expand Up @@ -1842,4 +1898,4 @@ class FxaAccountManagerTest {
`when`(c.refreshDevicesAsync()).thenReturn(CompletableDeferred(true))
return c
}
}
}
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ permalink: /changelog/
* Labels in labeled metrics now have a correct, and slightly stricter, regular expression.
See [label format](https://mozilla.github.io/glean/user/metrics/index.html#label-format) for more information.

* **services-firefox-accounts**
* Adds an optional `tokenServerUrl` to `SyncConfig` to support self-hosted sync.

# 32.0.0

Expand Down