Skip to content

Commit 0fa9d3a

Browse files
authored
Add the platform logging header to generateAuthToken requests (#2400)
1 parent 91e40c1 commit 0fa9d3a

21 files changed

+210
-148
lines changed

packages/installations/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"license": "Apache-2.0",
1010
"scripts": {
1111
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
12-
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts'",
12+
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
1313
"build": "rollup -c",
1414
"test": "yarn type-check && yarn test:karma && yarn lint",
1515
"test:karma": "karma start --single-run",

packages/installations/src/api/create-installation-request.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
RequestStatus
2626
} from '../interfaces/installation-entry';
2727
import { compareHeaders } from '../testing/compare-headers';
28-
import { getFakeAppConfig } from '../testing/get-fake-app';
28+
import { getFakeAppConfig } from '../testing/fake-generators';
2929
import '../testing/setup';
3030
import {
3131
INSTALLATIONS_API_URL,

packages/installations/src/api/delete-installation-request.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
RequestStatus
2525
} from '../interfaces/installation-entry';
2626
import { compareHeaders } from '../testing/compare-headers';
27-
import { getFakeAppConfig } from '../testing/get-fake-app';
27+
import { getFakeAppConfig } from '../testing/fake-generators';
2828
import '../testing/setup';
2929
import {
3030
INSTALLATIONS_API_URL,

packages/installations/src/api/generate-auth-token-request.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import { FirebaseError } from '@firebase/util';
1919
import { expect } from 'chai';
2020
import { SinonStub, stub } from 'sinon';
2121
import { GenerateAuthTokenResponse } from '../interfaces/api-response';
22-
import { AppConfig } from '../interfaces/app-config';
22+
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
2323
import {
2424
CompletedAuthToken,
2525
RegisteredInstallationEntry,
2626
RequestStatus
2727
} from '../interfaces/installation-entry';
2828
import { compareHeaders } from '../testing/compare-headers';
29-
import { getFakeAppConfig } from '../testing/get-fake-app';
29+
import { getFakeDependencies } from '../testing/fake-generators';
3030
import '../testing/setup';
3131
import {
3232
INSTALLATIONS_API_URL,
@@ -39,13 +39,13 @@ import { generateAuthTokenRequest } from './generate-auth-token-request';
3939
const FID = 'evil-has-no-boundaries';
4040

4141
describe('generateAuthTokenRequest', () => {
42-
let appConfig: AppConfig;
42+
let dependencies: FirebaseDependencies;
4343
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
4444
let registeredInstallationEntry: RegisteredInstallationEntry;
4545
let response: GenerateAuthTokenResponse;
4646

4747
beforeEach(() => {
48-
appConfig = getFakeAppConfig();
48+
dependencies = getFakeDependencies();
4949

5050
registeredInstallationEntry = {
5151
fid: FID,
@@ -72,7 +72,7 @@ describe('generateAuthTokenRequest', () => {
7272

7373
it('fetches a new Authentication Token', async () => {
7474
const completedAuthToken: CompletedAuthToken = await generateAuthTokenRequest(
75-
appConfig,
75+
dependencies,
7676
registeredInstallationEntry
7777
);
7878
expect(completedAuthToken.requestStatus).to.equal(
@@ -85,7 +85,8 @@ describe('generateAuthTokenRequest', () => {
8585
'Content-Type': 'application/json',
8686
Accept: 'application/json',
8787
Authorization: `${INTERNAL_AUTH_VERSION} refreshToken`,
88-
'x-goog-api-key': 'apiKey'
88+
'x-goog-api-key': 'apiKey',
89+
'x-firebase-client': 'a/1.2.3 b/2.3.4'
8990
});
9091
const expectedBody = {
9192
installation: {
@@ -99,7 +100,7 @@ describe('generateAuthTokenRequest', () => {
99100
};
100101
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations/${FID}/authTokens:generate`;
101102

102-
await generateAuthTokenRequest(appConfig, registeredInstallationEntry);
103+
await generateAuthTokenRequest(dependencies, registeredInstallationEntry);
103104

104105
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
105106
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
@@ -122,7 +123,7 @@ describe('generateAuthTokenRequest', () => {
122123
);
123124

124125
await expect(
125-
generateAuthTokenRequest(appConfig, registeredInstallationEntry)
126+
generateAuthTokenRequest(dependencies, registeredInstallationEntry)
126127
).to.be.rejectedWith(FirebaseError);
127128
});
128129

@@ -141,7 +142,7 @@ describe('generateAuthTokenRequest', () => {
141142
fetchSpy.onCall(1).resolves(new Response(JSON.stringify(response)));
142143

143144
await expect(
144-
generateAuthTokenRequest(appConfig, registeredInstallationEntry)
145+
generateAuthTokenRequest(dependencies, registeredInstallationEntry)
145146
).to.be.fulfilled;
146147
expect(fetchSpy).to.be.calledTwice;
147148
});

packages/installations/src/api/generate-auth-token-request.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { GenerateAuthTokenResponse } from '../interfaces/api-response';
1919
import { AppConfig } from '../interfaces/app-config';
20+
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
2021
import {
2122
CompletedAuthToken,
2223
RegisteredInstallationEntry
@@ -31,12 +32,21 @@ import {
3132
} from './common';
3233

3334
export async function generateAuthTokenRequest(
34-
appConfig: AppConfig,
35+
{ appConfig, platformLoggerProvider }: FirebaseDependencies,
3536
installationEntry: RegisteredInstallationEntry
3637
): Promise<CompletedAuthToken> {
3738
const endpoint = getGenerateAuthTokenEndpoint(appConfig, installationEntry);
3839

3940
const headers = getHeadersWithAuth(appConfig, installationEntry);
41+
42+
// If platform logger exists, add the platform info string to the header.
43+
const platformLogger = platformLoggerProvider.getImmediate({
44+
optional: true
45+
});
46+
if (platformLogger) {
47+
headers.append('x-firebase-client', platformLogger.getPlatformInfoString());
48+
}
49+
4050
const body = {
4151
installation: {
4252
sdkVersion: PACKAGE_VERSION

packages/installations/src/functions/delete-installation.test.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp } from '@firebase/app-types';
1918
import { expect } from 'chai';
2019
import { SinonStub, stub } from 'sinon';
2120
import * as deleteInstallationRequestModule from '../api/delete-installation-request';
22-
import { extractAppConfig } from '../helpers/extract-app-config';
2321
import { get, set } from '../helpers/idb-manager';
2422
import { AppConfig } from '../interfaces/app-config';
23+
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
2524
import {
2625
InProgressInstallationEntry,
2726
RegisteredInstallationEntry,
2827
RequestStatus,
2928
UnregisteredInstallationEntry
3029
} from '../interfaces/installation-entry';
31-
import { getFakeApp } from '../testing/get-fake-app';
30+
import { getFakeDependencies } from '../testing/fake-generators';
3231
import '../testing/setup';
3332
import { ErrorCode } from '../util/errors';
3433
import { sleep } from '../util/sleep';
@@ -37,16 +36,14 @@ import { deleteInstallation } from './delete-installation';
3736
const FID = 'children-of-the-damned';
3837

3938
describe('deleteInstallation', () => {
40-
let app: FirebaseApp;
41-
let appConfig: AppConfig;
39+
let dependencies: FirebaseDependencies;
4240
let deleteInstallationRequestSpy: SinonStub<
4341
[AppConfig, RegisteredInstallationEntry],
4442
Promise<void>
4543
>;
4644

4745
beforeEach(() => {
48-
app = getFakeApp();
49-
appConfig = extractAppConfig(app);
46+
dependencies = getFakeDependencies();
5047

5148
deleteInstallationRequestSpy = stub(
5249
deleteInstallationRequestModule,
@@ -57,7 +54,7 @@ describe('deleteInstallation', () => {
5754
});
5855

5956
it('resolves without calling server API if there is no installation', async () => {
60-
await expect(deleteInstallation(app)).to.be.fulfilled;
57+
await expect(deleteInstallation(dependencies)).to.be.fulfilled;
6158
expect(deleteInstallationRequestSpy).not.to.have.been.called;
6259
});
6360

@@ -66,11 +63,11 @@ describe('deleteInstallation', () => {
6663
registrationStatus: RequestStatus.NOT_STARTED,
6764
fid: FID
6865
};
69-
await set(appConfig, entry);
66+
await set(dependencies.appConfig, entry);
7067

71-
await expect(deleteInstallation(app)).to.be.fulfilled;
68+
await expect(deleteInstallation(dependencies)).to.be.fulfilled;
7269
expect(deleteInstallationRequestSpy).not.to.have.been.called;
73-
await expect(get(appConfig)).to.eventually.be.undefined;
70+
await expect(get(dependencies.appConfig)).to.eventually.be.undefined;
7471
});
7572

7673
it('rejects without calling server API if the installation is pending', async () => {
@@ -79,9 +76,9 @@ describe('deleteInstallation', () => {
7976
registrationStatus: RequestStatus.IN_PROGRESS,
8077
registrationTime: Date.now() - 3 * 1000
8178
};
82-
await set(appConfig, entry);
79+
await set(dependencies.appConfig, entry);
8380

84-
await expect(deleteInstallation(app)).to.be.rejectedWith(
81+
await expect(deleteInstallation(dependencies)).to.be.rejectedWith(
8582
ErrorCode.DELETE_PENDING_REGISTRATION
8683
);
8784
expect(deleteInstallationRequestSpy).not.to.have.been.called;
@@ -99,10 +96,10 @@ describe('deleteInstallation', () => {
9996
creationTime: Date.now()
10097
}
10198
};
102-
await set(appConfig, entry);
99+
await set(dependencies.appConfig, entry);
103100
stub(navigator, 'onLine').value(false);
104101

105-
await expect(deleteInstallation(app)).to.be.rejectedWith(
102+
await expect(deleteInstallation(dependencies)).to.be.rejectedWith(
106103
ErrorCode.APP_OFFLINE
107104
);
108105
expect(deleteInstallationRequestSpy).not.to.have.been.called;
@@ -120,13 +117,13 @@ describe('deleteInstallation', () => {
120117
creationTime: Date.now()
121118
}
122119
};
123-
await set(appConfig, entry);
120+
await set(dependencies.appConfig, entry);
124121

125-
await expect(deleteInstallation(app)).to.be.fulfilled;
122+
await expect(deleteInstallation(dependencies)).to.be.fulfilled;
126123
expect(deleteInstallationRequestSpy).to.have.been.calledOnceWith(
127-
appConfig,
124+
dependencies.appConfig,
128125
entry
129126
);
130-
await expect(get(appConfig)).to.eventually.be.undefined;
127+
await expect(get(dependencies.appConfig)).to.eventually.be.undefined;
131128
});
132129
});

packages/installations/src/functions/delete-installation.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp } from '@firebase/app-types';
1918
import { deleteInstallationRequest } from '../api/delete-installation-request';
20-
import { extractAppConfig } from '../helpers/extract-app-config';
2119
import { remove, update } from '../helpers/idb-manager';
20+
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
2221
import { RequestStatus } from '../interfaces/installation-entry';
2322
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
2423

25-
export async function deleteInstallation(app: FirebaseApp): Promise<void> {
26-
const appConfig = extractAppConfig(app);
24+
export async function deleteInstallation(
25+
dependencies: FirebaseDependencies
26+
): Promise<void> {
27+
const { appConfig } = dependencies;
2728

2829
const entry = await update(appConfig, oldEntry => {
2930
if (oldEntry && oldEntry.registrationStatus === RequestStatus.NOT_STARTED) {

packages/installations/src/functions/get-id.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@ import { SinonStub, stub } from 'sinon';
2020
import * as getInstallationEntryModule from '../helpers/get-installation-entry';
2121
import * as refreshAuthTokenModule from '../helpers/refresh-auth-token';
2222
import { AppConfig } from '../interfaces/app-config';
23+
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
2324
import {
24-
RequestStatus,
25-
RegisteredInstallationEntry
25+
RegisteredInstallationEntry,
26+
RequestStatus
2627
} from '../interfaces/installation-entry';
27-
import { getFakeApp } from '../testing/get-fake-app';
28+
import { getFakeDependencies } from '../testing/fake-generators';
2829
import '../testing/setup';
2930
import { getId } from './get-id';
3031

3132
const FID = 'disciples-of-the-watch';
3233

3334
describe('getId', () => {
35+
let dependencies: FirebaseDependencies;
3436
let getInstallationEntrySpy: SinonStub<
3537
[AppConfig],
3638
Promise<getInstallationEntryModule.InstallationEntryWithRegistrationPromise>
3739
>;
3840

3941
beforeEach(() => {
42+
dependencies = getFakeDependencies();
43+
4044
getInstallationEntrySpy = stub(
4145
getInstallationEntryModule,
4246
'getInstallationEntry'
@@ -52,8 +56,7 @@ describe('getId', () => {
5256
registrationPromise: Promise.resolve({} as RegisteredInstallationEntry)
5357
});
5458

55-
const firebaseApp = getFakeApp();
56-
const fid = await getId(firebaseApp);
59+
const fid = await getId(dependencies);
5760
expect(fid).to.equal(FID);
5861
expect(getInstallationEntrySpy).to.be.calledOnce;
5962
});
@@ -80,8 +83,7 @@ describe('getId', () => {
8083
creationTime: Date.now()
8184
});
8285

83-
const firebaseApp = getFakeApp();
84-
await getId(firebaseApp);
86+
await getId(dependencies);
8587
expect(refreshAuthTokenSpy).to.be.calledOnce;
8688
});
8789
});

packages/installations/src/functions/get-id.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp } from '@firebase/app-types';
19-
import { extractAppConfig } from '../helpers/extract-app-config';
2018
import { getInstallationEntry } from '../helpers/get-installation-entry';
2119
import { refreshAuthToken } from '../helpers/refresh-auth-token';
20+
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
2221

23-
export async function getId(app: FirebaseApp): Promise<string> {
24-
const appConfig = extractAppConfig(app);
22+
export async function getId(
23+
dependencies: FirebaseDependencies
24+
): Promise<string> {
2525
const { installationEntry, registrationPromise } = await getInstallationEntry(
26-
appConfig
26+
dependencies.appConfig
2727
);
2828

2929
if (registrationPromise) {
3030
registrationPromise.catch(console.error);
3131
} else {
3232
// If the installation is already registered, update the authentication
3333
// token if needed.
34-
refreshAuthToken(appConfig).catch(console.error);
34+
refreshAuthToken(dependencies).catch(console.error);
3535
}
3636

3737
return installationEntry.fid;

0 commit comments

Comments
 (0)