Skip to content

Commit 0526b87

Browse files
committed
Review fixes.
1 parent 61ec38d commit 0526b87

File tree

6 files changed

+32
-122
lines changed

6 files changed

+32
-122
lines changed

docs-devsite/app.firebaseserverappsettings.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ export interface FirebaseServerAppSettings extends Omit<FirebaseAppSettings, 'na
2323
2424
| Property | Type | Description |
2525
| --- | --- | --- |
26-
| [appCheckToken](./app.firebaseserverappsettings.md#firebaseserverappsettingsappchecktoken) | string | An optional App Check token. If provided, the Firebase SDKs that use App Check will utilize this App Check token in place of requiring an instance of App Check to be initialized. |
27-
| [authIdToken](./app.firebaseserverappsettings.md#firebaseserverappsettingsauthidtoken) | string | An optional Auth ID token used to resume a signed in user session from a client runtime environment.<!-- -->Invoking <code>getAuth</code> with a <code>FirebaseServerApp</code> configured with a validated <code>authIdToken</code> causes an automatic attempt to sign in the user that the <code>authIdToken</code> represents. The token needs to have been recently minted for this operation to succeed.<!-- -->If the token fails local verification, or if the Auth service has failed to validate it when the Auth SDK is initialized, then a warning is logged to the console and the Auth SDK will not sign in a user on initialization.<!-- -->If a user is successfully signed in, then the Auth instance's <code>onAuthStateChanged</code> callback is invoked with the <code>User</code> object as per standard Auth flows. However, <code>User</code> objects created via an <code>authIdToken</code> do not have a refresh token. Attempted <code>refreshToken</code> operations fail. |
26+
| [appCheckToken](./app.firebaseserverappsettings.md#firebaseserverappsettingsappchecktoken) | string | An optional App Check token. If provided, the Firebase SDKs that use App Check will utilize this App Check token in place of requiring an instance of App Check to be initialized.<!-- -->If the token fails local verification due to expiration or parsing errors, then a console error is logged at the time of initialization of the <code>FirebaseServerApp</code> instance. |
27+
| [authIdToken](./app.firebaseserverappsettings.md#firebaseserverappsettingsauthidtoken) | string | An optional Auth ID token used to resume a signed in user session from a client runtime environment.<!-- -->Invoking <code>getAuth</code> with a <code>FirebaseServerApp</code> configured with a validated <code>authIdToken</code> causes an automatic attempt to sign in the user that the <code>authIdToken</code> represents. The token needs to have been recently minted for this operation to succeed.<!-- -->If the token fails local verification due to expiration or parsing errors, then a console error is logged at the time of initialization of the <code>FirebaseServerApp</code> instance.<!-- -->If the Auth service has failed to validate the token when the Auth SDK is initialized, then an warning is logged to the console and the Auth SDK will not sign in a user on initialization.<!-- -->If a user is successfully signed in, then the Auth instance's <code>onAuthStateChanged</code> callback is invoked with the <code>User</code> object as per standard Auth flows. However, <code>User</code> objects created via an <code>authIdToken</code> do not have a refresh token. Attempted <code>refreshToken</code> operations fail. |
2828
| [releaseOnDeref](./app.firebaseserverappsettings.md#firebaseserverappsettingsreleaseonderef) | object | An optional object. If provided, the Firebase SDK uses a <code>FinalizationRegistry</code> object to monitor the garbage collection status of the provided object. The Firebase SDK releases its reference on the <code>FirebaseServerApp</code> instance when the provided <code>releaseOnDeref</code> object is garbage collected.<!-- -->You can use this field to reduce memory management overhead for your application. If provided, an app running in a SSR pass does not need to perform <code>FirebaseServerApp</code> cleanup, so long as the reference object is deleted (by falling out of SSR scope, for instance.)<!-- -->If an object is not provided then the application must clean up the <code>FirebaseServerApp</code> instance by invoking <code>deleteApp</code>.<!-- -->If the application provides an object in this parameter, but the application is executed in a JavaScript engine that predates the support of <code>FinalizationRegistry</code> (introduced in node v14.6.0, for instance), then an error is thrown at <code>FirebaseServerApp</code> initialization. |
2929
3030
## FirebaseServerAppSettings.appCheckToken
3131
3232
An optional App Check token. If provided, the Firebase SDKs that use App Check will utilize this App Check token in place of requiring an instance of App Check to be initialized.
3333
34+
If the token fails local verification due to expiration or parsing errors, then a console error is logged at the time of initialization of the `FirebaseServerApp` instance.
35+
3436
<b>Signature:</b>
3537
3638
```typescript
@@ -43,7 +45,9 @@ An optional Auth ID token used to resume a signed in user session from a client
4345
4446
Invoking `getAuth` with a `FirebaseServerApp` configured with a validated `authIdToken` causes an automatic attempt to sign in the user that the `authIdToken` represents. The token needs to have been recently minted for this operation to succeed.
4547
46-
If the token fails local verification, or if the Auth service has failed to validate it when the Auth SDK is initialized, then a warning is logged to the console and the Auth SDK will not sign in a user on initialization.
48+
If the token fails local verification due to expiration or parsing errors, then a console error is logged at the time of initialization of the `FirebaseServerApp` instance.
49+
50+
If the Auth service has failed to validate the token when the Auth SDK is initialized, then an warning is logged to the console and the Auth SDK will not sign in a user on initialization.
4751
4852
If a user is successfully signed in, then the Auth instance's `onAuthStateChanged` callback is invoked with the `User` object as per standard Auth flows. However, `User` objects created via an `authIdToken` do not have a refresh token. Attempted `refreshToken` operations fail.
4953

packages/app/src/errors.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export const enum AppError {
3131
IDB_WRITE = 'idb-set',
3232
IDB_DELETE = 'idb-delete',
3333
FINALIZATION_REGISTRY_NOT_SUPPORTED = 'finalization-registry-not-supported',
34-
INVALID_SERVER_APP_ENVIRONMENT = 'invalid-server-app-environment',
35-
INVALID_SERVER_APP_TOKEN_FORMAT = 'invalid-server-app-token-format',
36-
SERVER_APP_TOKEN_EXPIRED = 'server-app-token-expired'
34+
INVALID_SERVER_APP_ENVIRONMENT = 'invalid-server-app-environment'
3735
}
3836

3937
const ERRORS: ErrorMap<AppError> = {
@@ -63,11 +61,7 @@ const ERRORS: ErrorMap<AppError> = {
6361
[AppError.FINALIZATION_REGISTRY_NOT_SUPPORTED]:
6462
'FirebaseServerApp deleteOnDeref field defined but the JS runtime does not support FinalizationRegistry.',
6563
[AppError.INVALID_SERVER_APP_ENVIRONMENT]:
66-
'FirebaseServerApp is not for use in browser environments.',
67-
[AppError.INVALID_SERVER_APP_TOKEN_FORMAT]:
68-
'FirebaseServerApp {$tokenName} could not be parsed.',
69-
[AppError.SERVER_APP_TOKEN_EXPIRED]:
70-
'FirebaseServerApp {$tokenName} could not be parsed.'
64+
'FirebaseServerApp is not for use in browser environments.'
7165
};
7266

7367
interface ErrorParams {
@@ -81,8 +75,6 @@ interface ErrorParams {
8175
[AppError.IDB_WRITE]: { originalErrorMessage?: string };
8276
[AppError.IDB_DELETE]: { originalErrorMessage?: string };
8377
[AppError.FINALIZATION_REGISTRY_NOT_SUPPORTED]: { appName?: string };
84-
[AppError.INVALID_SERVER_APP_TOKEN_FORMAT]: { tokenName: string };
85-
[AppError.SERVER_APP_TOKEN_EXPIRED]: { tokenName: string };
8678
}
8779

8880
export const ERROR_FACTORY = new ErrorFactory<AppError, ErrorParams>(

packages/app/src/firebaseServerApp.test.ts

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -193,53 +193,6 @@ describe('FirebaseServerApp', () => {
193193
expect(encounteredError).to.be.false;
194194
});
195195

196-
it('throws when authIdToken has expired', () => {
197-
const options = { apiKey: 'APIKEY' };
198-
const authIdToken = createServerAppTokenWithOffset(/*daysOffset=*/ -1);
199-
const serverAppSettings: FirebaseServerAppSettings = {
200-
automaticDataCollectionEnabled: false,
201-
releaseOnDeref: options,
202-
authIdToken
203-
};
204-
let encounteredError = false;
205-
try {
206-
new FirebaseServerAppImpl(
207-
options,
208-
serverAppSettings,
209-
'testName',
210-
new ComponentContainer('test')
211-
);
212-
} catch (e) {
213-
encounteredError = true;
214-
expect((e as Error).toString()).to.contain(
215-
'app/server-app-token-expired'
216-
);
217-
}
218-
expect(encounteredError).to.be.true;
219-
});
220-
221-
it('throws when authIdToken has too few parts', () => {
222-
const options = { apiKey: 'APIKEY' };
223-
const authIdToken = 'blah';
224-
const serverAppSettings: FirebaseServerAppSettings = {
225-
automaticDataCollectionEnabled: false,
226-
releaseOnDeref: options,
227-
authIdToken: base64Encode(authIdToken)
228-
};
229-
let encounteredError = false;
230-
try {
231-
new FirebaseServerAppImpl(
232-
options,
233-
serverAppSettings,
234-
'testName',
235-
new ComponentContainer('test')
236-
);
237-
} catch (e) {
238-
encounteredError = true;
239-
}
240-
expect(encounteredError).to.be.true;
241-
});
242-
243196
it('accepts a valid appCheckToken expiration', () => {
244197
const options = { apiKey: 'APIKEY' };
245198
const appCheckToken = createServerAppTokenWithOffset(/*daysOffset=*/ 1);
@@ -261,51 +214,4 @@ describe('FirebaseServerApp', () => {
261214
}
262215
expect(encounteredError).to.be.false;
263216
});
264-
265-
it('throws when appCheckToken has expired', () => {
266-
const options = { apiKey: 'APIKEY' };
267-
const appCheckToken = createServerAppTokenWithOffset(/*daysOffset=*/ -1);
268-
const serverAppSettings: FirebaseServerAppSettings = {
269-
automaticDataCollectionEnabled: false,
270-
releaseOnDeref: options,
271-
appCheckToken
272-
};
273-
let encounteredError = false;
274-
try {
275-
new FirebaseServerAppImpl(
276-
options,
277-
serverAppSettings,
278-
'testName',
279-
new ComponentContainer('test')
280-
);
281-
} catch (e) {
282-
encounteredError = true;
283-
expect((e as Error).toString()).to.contain(
284-
'app/server-app-token-expired'
285-
);
286-
}
287-
expect(encounteredError).to.be.true;
288-
});
289-
290-
it('throws when appCheckToken has too few parts', () => {
291-
const options = { apiKey: 'APIKEY' };
292-
const appCheckToken = 'blah';
293-
const serverAppSettings: FirebaseServerAppSettings = {
294-
automaticDataCollectionEnabled: false,
295-
releaseOnDeref: options,
296-
appCheckToken: base64Encode(appCheckToken)
297-
};
298-
let encounteredError = false;
299-
try {
300-
new FirebaseServerAppImpl(
301-
options,
302-
serverAppSettings,
303-
'testName',
304-
new ComponentContainer('test')
305-
);
306-
} catch (e) {
307-
encounteredError = true;
308-
}
309-
expect(encounteredError).to.be.true;
310-
});
311217
});

packages/app/src/firebaseServerApp.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,30 @@ import { name as packageName, version } from '../package.json';
2929
import { base64Decode } from '@firebase/util';
3030

3131
// Parse the token and check to see if the `exp` claim is in the future.
32-
// Throws an error if the token or claim could not be parsed, or if `exp` is in the past.
32+
// Reports an error to the console if the token or claim could not be parsed, or if `exp` is in
33+
// the past.
3334
function validateTokenTTL(base64Token: string, tokenName: string): void {
3435
const secondPart = base64Decode(base64Token.split('.')[1]);
3536
if (secondPart === null) {
36-
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_TOKEN_FORMAT, {
37-
tokenName
38-
});
37+
console.error(
38+
`FirebaseServerApp ${tokenName} is invalid: second part could not be parsed.`
39+
);
40+
return;
3941
}
4042
const expClaim = JSON.parse(secondPart).exp;
4143
if (expClaim === undefined) {
42-
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_TOKEN_FORMAT, {
43-
tokenName
44-
});
44+
console.error(
45+
`FirebaseServerApp ${tokenName} is invalid: expiration claim could not be parsed`
46+
);
47+
return;
4548
}
4649
const exp = JSON.parse(secondPart).exp * 1000;
4750
const now = new Date().getTime();
4851
const diff = exp - now;
4952
if (diff <= 0) {
50-
throw ERROR_FACTORY.create(AppError.SERVER_APP_TOKEN_EXPIRED, {
51-
tokenName
52-
});
53+
console.error(
54+
`FirebaseServerApp ${tokenName} is invalid: the token has expired.`
55+
);
5356
}
5457
}
5558

packages/app/src/public-types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,11 @@ export interface FirebaseServerAppSettings
185185
* causes an automatic attempt to sign in the user that the `authIdToken` represents. The token
186186
* needs to have been recently minted for this operation to succeed.
187187
*
188-
* If the token fails local verification, or if the Auth service has failed to validate it when
189-
* the Auth SDK is initialized, then a warning is logged to the console and the Auth SDK will not
190-
* sign in a user on initialization.
188+
* If the token fails local verification due to expiration or parsing errors, then a console error
189+
* is logged at the time of initialization of the `FirebaseServerApp` instance.
190+
*
191+
* If the Auth service has failed to validate the token when the Auth SDK is initialized, then an
192+
* warning is logged to the console and the Auth SDK will not sign in a user on initialization.
191193
*
192194
* If a user is successfully signed in, then the Auth instance's `onAuthStateChanged` callback
193195
* is invoked with the `User` object as per standard Auth flows. However, `User` objects
@@ -199,6 +201,9 @@ export interface FirebaseServerAppSettings
199201
/**
200202
* An optional App Check token. If provided, the Firebase SDKs that use App Check will utilize
201203
* this App Check token in place of requiring an instance of App Check to be initialized.
204+
*
205+
* If the token fails local verification due to expiration or parsing errors, then a console error
206+
* is logged at the time of initialization of the `FirebaseServerApp` instance.
202207
*/
203208
appCheckToken?: string;
204209

packages/firestore/src/api/credentials.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ export class FirebaseAppCheckTokenProvider
569569
}
570570

571571
getToken(): Promise<Token | null> {
572-
if (this.serverAppAppCheckToken !== null) {
572+
if (this.serverAppAppCheckToken) {
573573
return Promise.resolve(new AppCheckToken(this.serverAppAppCheckToken));
574574
}
575575
debugAssert(
@@ -647,7 +647,7 @@ export class LiteAppCheckTokenProvider implements CredentialsProvider<string> {
647647
}
648648

649649
getToken(): Promise<Token | null> {
650-
if (this.serverAppAppCheckToken !== null) {
650+
if (this.serverAppAppCheckToken) {
651651
return Promise.resolve(new AppCheckToken(this.serverAppAppCheckToken));
652652
}
653653

0 commit comments

Comments
 (0)