Skip to content

Commit b1b8f2f

Browse files
committed
Pass the iid token to authenticate Instance-ID migration to FIS.
1 parent 9aeb69d commit b1b8f2f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,23 @@ private String readExistingIidOrCreateFid(PersistedInstallationEntry prefs) {
349349
/** Registers the created Fid with FIS servers and update the persisted state. */
350350
private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEntry prefs)
351351
throws IOException {
352+
353+
// Note: Default value of instanceIdMigrationAuth: null
354+
String iidToken = null;
355+
356+
if (prefs.getFirebaseInstallationId().equals(iidStore.readIid())) {
357+
// For a default firebase installation, read the stored star scoped iid token. This token
358+
// will be used for authenticating Instance-ID when migrating to FIS.
359+
iidToken = iidStore.readToken();
360+
}
361+
352362
InstallationResponse response =
353363
serviceClient.createFirebaseInstallation(
354364
/*apiKey= */ firebaseApp.getOptions().getApiKey(),
355365
/*fid= */ prefs.getFirebaseInstallationId(),
356366
/*projectID= */ firebaseApp.getOptions().getProjectId(),
357367
/*appId= */ getApplicationId(),
358-
/* migration-header= */ null);
368+
/* migration-header= */ iidToken);
359369

360370
switch (response.getResponseCode()) {
361371
case OK:

firebase-installations/src/test/java/com/google/firebase/installations/FirebaseInstallationsTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public class FirebaseInstallationsTest {
9090

9191
public static final String TEST_INSTANCE_ID_1 = "ccccccccccc";
9292

93+
public static final String TEST_INSTANCE_ID_TOKEN_1 = "iid.token";
94+
9395
public static final InstallationResponse TEST_INSTALLATION_RESPONSE =
9496
InstallationResponse.builder()
9597
.setUri("/projects/" + TEST_PROJECT_ID + "/installations/" + TEST_FID_1)
@@ -185,6 +187,7 @@ public void testGetId_noNetwork_noIid() throws Exception {
185187
when(mockBackend.generateAuthToken(anyString(), anyString(), anyString(), anyString()))
186188
.thenThrow(new IOException());
187189
when(mockIidStore.readIid()).thenReturn(null);
190+
when(mockIidStore.readToken()).thenReturn(null);
188191

189192
// Do the actual getId() call under test. Confirm that it returns a generated FID and
190193
// and that the FID was written to storage.
@@ -210,11 +213,16 @@ public void testGetId_noNetwork_noIid() throws Exception {
210213
@Test
211214
public void testGetId_noNetwork_iidPresent() throws Exception {
212215
when(mockBackend.createFirebaseInstallation(
213-
anyString(), anyString(), anyString(), anyString(), any()))
216+
TEST_API_KEY,
217+
TEST_INSTANCE_ID_1,
218+
TEST_PROJECT_ID,
219+
TEST_APP_ID_1,
220+
TEST_INSTANCE_ID_TOKEN_1))
214221
.thenThrow(new IOException());
215222
when(mockBackend.generateAuthToken(anyString(), anyString(), anyString(), anyString()))
216223
.thenThrow(new IOException());
217224
when(mockIidStore.readIid()).thenReturn(TEST_INSTANCE_ID_1);
225+
when(mockIidStore.readToken()).thenReturn(TEST_INSTANCE_ID_TOKEN_1);
218226

219227
// Do the actual getId() call under test. Confirm that it returns a generated FID and
220228
// and that the FID was written to storage.
@@ -340,8 +348,9 @@ public void testGetId_UnRegisteredId_IssueCreateIdCall() throws Exception {
340348
@Test
341349
public void testGetId_migrateIid_successful() throws Exception {
342350
when(mockIidStore.readIid()).thenReturn(TEST_INSTANCE_ID_1);
351+
when(mockIidStore.readToken()).thenReturn(TEST_INSTANCE_ID_TOKEN_1);
343352
when(mockBackend.createFirebaseInstallation(
344-
anyString(), anyString(), anyString(), anyString(), any()))
353+
anyString(), anyString(), anyString(), anyString(), anyString()))
345354
.thenReturn(TEST_INSTALLATION_RESPONSE_WITH_IID);
346355

347356
// Do the actual getId() call under test.
@@ -367,6 +376,7 @@ public void testGetId_migrateIid_successful() throws Exception {
367376
@Test
368377
public void testGetId_multipleCalls_sameFIDReturned() throws Exception {
369378
when(mockIidStore.readIid()).thenReturn(null);
379+
when(mockIidStore.readToken()).thenReturn(null);
370380
when(mockBackend.createFirebaseInstallation(
371381
anyString(), anyString(), anyString(), anyString(), any()))
372382
.thenReturn(TEST_INSTALLATION_RESPONSE);

0 commit comments

Comments
 (0)