Skip to content

Commit 2952e2f

Browse files
committed
Initialize debug mode on initializeAppCheck
1 parent 5f8b168 commit 2952e2f

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

packages/app-check/src/api.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
import '../test/setup';
1818
import { expect } from 'chai';
19-
import { spy, stub } from 'sinon';
19+
import { match, spy, stub } from 'sinon';
2020
import {
2121
setTokenAutoRefreshEnabled,
2222
initializeAppCheck,
@@ -38,10 +38,12 @@ import * as logger from './logger';
3838
import * as client from './client';
3939
import * as storage from './storage';
4040
import * as internalApi from './internal-api';
41+
import * as indexeddb from './indexeddb';
4142
import { deleteApp, FirebaseApp } from '@firebase/app';
4243
import { CustomProvider, ReCaptchaV3Provider } from './providers';
4344
import { AppCheckService } from './factory';
4445
import { AppCheckToken } from './public-types';
46+
import { getDebugToken } from './debug';
4547

4648
describe('api', () => {
4749
let app: FirebaseApp;
@@ -118,6 +120,42 @@ describe('api', () => {
118120
})
119121
).to.equal(appCheckInstance);
120122
});
123+
it('starts debug mode on first call', async () => {
124+
const fakeWrite = ():Promise<void> => Promise.resolve();
125+
const writeToIndexedDBStub = stub(
126+
indexeddb,
127+
'writeDebugTokenToIndexedDB'
128+
).callsFake(fakeWrite);
129+
stub(
130+
indexeddb,
131+
'readDebugTokenFromIndexedDB'
132+
).callsFake(() => Promise.resolve(undefined));
133+
const logStub = stub(logger.logger, 'warn');
134+
const consoleStub = stub(console, 'log');
135+
self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;
136+
initializeAppCheck(app, {
137+
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
138+
});
139+
await fakeWrite();
140+
const token = writeToIndexedDBStub.args[0];
141+
expect(logStub).to.not.be.called;
142+
expect(consoleStub.args[0][0]).to.include(token);
143+
self.FIREBASE_APPCHECK_DEBUG_TOKEN = undefined;
144+
});
145+
it('warns about debug mode on second call', async () => {
146+
const logStub = stub(logger.logger, 'warn');
147+
self.FIREBASE_APPCHECK_DEBUG_TOKEN = 'abcdefg';
148+
initializeAppCheck(app, {
149+
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
150+
});
151+
initializeAppCheck(app, {
152+
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
153+
});
154+
const token = await getDebugToken();
155+
expect(token).to.equal('abcdefg');
156+
expect(logStub).to.be.calledWith(match('abcdefg'));
157+
self.FIREBASE_APPCHECK_DEBUG_TOKEN = undefined;
158+
});
121159

122160
it('initialize reCAPTCHA when a ReCaptchaV3Provider is provided', () => {
123161
const initReCAPTCHAStub = stub(reCAPTCHA, 'initialize').returns(

packages/app-check/src/api.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
isValid
3636
} from './internal-api';
3737
import { readTokenFromStorage } from './storage';
38+
import { getDebugToken, initializeDebugMode, isDebugMode } from './debug';
39+
import { logger } from './logger';
3840

3941
declare module '@firebase/component' {
4042
interface NameServiceMapping {
@@ -65,6 +67,21 @@ export function initializeAppCheck(
6567
options.isTokenAutoRefreshEnabled &&
6668
initialOptions.provider.isEqual(options.provider)
6769
) {
70+
// Log a warning if `initializeAppCheck()` is called after the first time
71+
// if this app is still in debug mode, and show the token.
72+
// If it's the first time, `initializeDebugMode()` already logs a message.
73+
if (isDebugMode()) {
74+
logger.warn(
75+
`App Check is in debug mode. To turn off debug mode, unset ` +
76+
`the global variable FIREBASE_APPCHECK_DEBUG_TOKEN and ` +
77+
`restart the app.`
78+
);
79+
// Make this a separate console statement so user will at least have the
80+
// first message if the token promise doesn't resolve in time.
81+
void getDebugToken().then(token =>
82+
logger.warn(`Debug token is ${token}.`)
83+
);
84+
}
6885
return existingInstance;
6986
} else {
7087
throw ERROR_FACTORY.create(AppCheckError.ALREADY_INITIALIZED, {
@@ -73,6 +90,9 @@ export function initializeAppCheck(
7390
}
7491
}
7592

93+
// Only read global variable on first call to `initializeAppCheck()`.
94+
initializeDebugMode();
95+
7696
const appCheck = provider.initialize({ options });
7797
_activate(app, options.provider, options.isTokenAutoRefreshEnabled);
7898

packages/app-check/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
} from '@firebase/component';
2929
import { _AppCheckComponentName } from './public-types';
3030
import { factory, internalFactory } from './factory';
31-
import { initializeDebugMode } from './debug';
3231
import { _AppCheckInternalComponentName } from './types';
3332
import { name, version } from '../package.json';
3433

@@ -82,4 +81,3 @@ function registerAppCheck(): void {
8281
}
8382

8483
registerAppCheck();
85-
initializeDebugMode();

0 commit comments

Comments
 (0)