@firebase/app-check 0.8.9 → 0.8.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/esm/index.esm2017.js +1632 -1632
  2. package/dist/esm/index.esm2017.js.map +1 -1
  3. package/dist/esm/src/api.d.ts +105 -105
  4. package/dist/esm/src/api.test.d.ts +17 -17
  5. package/dist/esm/src/client.d.ts +30 -30
  6. package/dist/esm/src/client.test.d.ts +17 -17
  7. package/dist/esm/src/constants.d.ts +40 -40
  8. package/dist/esm/src/debug.d.ts +22 -22
  9. package/dist/esm/src/debug.test.d.ts +17 -17
  10. package/dist/esm/src/errors.d.ts +61 -61
  11. package/dist/esm/src/factory.d.ts +31 -31
  12. package/dist/esm/src/index.d.ts +14 -14
  13. package/dist/esm/src/indexeddb.d.ts +22 -22
  14. package/dist/esm/src/internal-api.d.ts +44 -44
  15. package/dist/esm/src/internal-api.test.d.ts +17 -17
  16. package/dist/esm/src/logger.d.ts +18 -18
  17. package/dist/esm/src/proactive-refresh.d.ts +35 -35
  18. package/dist/esm/src/proactive-refresh.test.d.ts +17 -17
  19. package/dist/esm/src/providers.d.ts +108 -108
  20. package/dist/esm/src/providers.test.d.ts +17 -17
  21. package/dist/esm/src/public-types.d.ts +85 -85
  22. package/dist/esm/src/recaptcha.d.ts +43 -43
  23. package/dist/esm/src/recaptcha.test.d.ts +17 -17
  24. package/dist/esm/src/state.d.ts +54 -54
  25. package/dist/esm/src/storage.d.ts +27 -27
  26. package/dist/esm/src/storage.test.d.ts +17 -17
  27. package/dist/esm/src/types.d.ts +66 -66
  28. package/dist/esm/src/util.d.ts +21 -21
  29. package/dist/esm/test/setup.d.ts +17 -17
  30. package/dist/esm/test/util.d.ts +34 -34
  31. package/dist/index.cjs.js +1632 -1632
  32. package/dist/index.cjs.js.map +1 -1
  33. package/dist/src/api.d.ts +105 -105
  34. package/dist/src/api.test.d.ts +17 -17
  35. package/dist/src/client.d.ts +30 -30
  36. package/dist/src/client.test.d.ts +17 -17
  37. package/dist/src/constants.d.ts +40 -40
  38. package/dist/src/debug.d.ts +22 -22
  39. package/dist/src/debug.test.d.ts +17 -17
  40. package/dist/src/errors.d.ts +61 -61
  41. package/dist/src/factory.d.ts +31 -31
  42. package/dist/src/index.d.ts +14 -14
  43. package/dist/src/indexeddb.d.ts +22 -22
  44. package/dist/src/internal-api.d.ts +44 -44
  45. package/dist/src/internal-api.test.d.ts +17 -17
  46. package/dist/src/logger.d.ts +18 -18
  47. package/dist/src/proactive-refresh.d.ts +35 -35
  48. package/dist/src/proactive-refresh.test.d.ts +17 -17
  49. package/dist/src/providers.d.ts +108 -108
  50. package/dist/src/providers.test.d.ts +17 -17
  51. package/dist/src/public-types.d.ts +85 -85
  52. package/dist/src/recaptcha.d.ts +43 -43
  53. package/dist/src/recaptcha.test.d.ts +17 -17
  54. package/dist/src/state.d.ts +54 -54
  55. package/dist/src/storage.d.ts +27 -27
  56. package/dist/src/storage.test.d.ts +17 -17
  57. package/dist/src/types.d.ts +66 -66
  58. package/dist/src/util.d.ts +21 -21
  59. package/dist/test/setup.d.ts +17 -17
  60. package/dist/test/util.d.ts +34 -34
  61. package/package.json +6 -6
@@ -3,1672 +3,1672 @@ import { Component } from '@firebase/component';
3
3
  import { Deferred, ErrorFactory, isIndexedDBAvailable, uuidv4, getGlobal, base64, issuedAtTime, calculateBackoffMillis, getModularInstance } from '@firebase/util';
4
4
  import { Logger } from '@firebase/logger';
5
5
 
6
- /**
7
- * @license
8
- * Copyright 2020 Google LLC
9
- *
10
- * Licensed under the Apache License, Version 2.0 (the "License");
11
- * you may not use this file except in compliance with the License.
12
- * You may obtain a copy of the License at
13
- *
14
- * http://www.apache.org/licenses/LICENSE-2.0
15
- *
16
- * Unless required by applicable law or agreed to in writing, software
17
- * distributed under the License is distributed on an "AS IS" BASIS,
18
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- * See the License for the specific language governing permissions and
20
- * limitations under the License.
21
- */
22
- const APP_CHECK_STATES = new Map();
23
- const DEFAULT_STATE = {
24
- activated: false,
25
- tokenObservers: []
26
- };
27
- const DEBUG_STATE = {
28
- initialized: false,
29
- enabled: false
30
- };
31
- /**
32
- * Gets a reference to the state object.
33
- */
34
- function getStateReference(app) {
35
- return APP_CHECK_STATES.get(app) || Object.assign({}, DEFAULT_STATE);
36
- }
37
- /**
38
- * Set once on initialization. The map should hold the same reference to the
39
- * same object until this entry is deleted.
40
- */
41
- function setInitialState(app, state) {
42
- APP_CHECK_STATES.set(app, state);
43
- return APP_CHECK_STATES.get(app);
44
- }
45
- function getDebugState() {
46
- return DEBUG_STATE;
6
+ /**
7
+ * @license
8
+ * Copyright 2020 Google LLC
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const APP_CHECK_STATES = new Map();
23
+ const DEFAULT_STATE = {
24
+ activated: false,
25
+ tokenObservers: []
26
+ };
27
+ const DEBUG_STATE = {
28
+ initialized: false,
29
+ enabled: false
30
+ };
31
+ /**
32
+ * Gets a reference to the state object.
33
+ */
34
+ function getStateReference(app) {
35
+ return APP_CHECK_STATES.get(app) || Object.assign({}, DEFAULT_STATE);
36
+ }
37
+ /**
38
+ * Set once on initialization. The map should hold the same reference to the
39
+ * same object until this entry is deleted.
40
+ */
41
+ function setInitialState(app, state) {
42
+ APP_CHECK_STATES.set(app, state);
43
+ return APP_CHECK_STATES.get(app);
44
+ }
45
+ function getDebugState() {
46
+ return DEBUG_STATE;
47
47
  }
48
48
 
49
- /**
50
- * @license
51
- * Copyright 2020 Google LLC
52
- *
53
- * Licensed under the Apache License, Version 2.0 (the "License");
54
- * you may not use this file except in compliance with the License.
55
- * You may obtain a copy of the License at
56
- *
57
- * http://www.apache.org/licenses/LICENSE-2.0
58
- *
59
- * Unless required by applicable law or agreed to in writing, software
60
- * distributed under the License is distributed on an "AS IS" BASIS,
61
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
62
- * See the License for the specific language governing permissions and
63
- * limitations under the License.
64
- */
65
- const BASE_ENDPOINT = 'https://content-firebaseappcheck.googleapis.com/v1';
66
- const EXCHANGE_RECAPTCHA_TOKEN_METHOD = 'exchangeRecaptchaV3Token';
67
- const EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD = 'exchangeRecaptchaEnterpriseToken';
68
- const EXCHANGE_DEBUG_TOKEN_METHOD = 'exchangeDebugToken';
69
- const TOKEN_REFRESH_TIME = {
70
- /**
71
- * The offset time before token natural expiration to run the refresh.
72
- * This is currently 5 minutes.
73
- */
74
- OFFSET_DURATION: 5 * 60 * 1000,
75
- /**
76
- * This is the first retrial wait after an error. This is currently
77
- * 30 seconds.
78
- */
79
- RETRIAL_MIN_WAIT: 30 * 1000,
80
- /**
81
- * This is the maximum retrial wait, currently 16 minutes.
82
- */
83
- RETRIAL_MAX_WAIT: 16 * 60 * 1000
84
- };
85
- /**
86
- * One day in millis, for certain error code backoffs.
87
- */
49
+ /**
50
+ * @license
51
+ * Copyright 2020 Google LLC
52
+ *
53
+ * Licensed under the Apache License, Version 2.0 (the "License");
54
+ * you may not use this file except in compliance with the License.
55
+ * You may obtain a copy of the License at
56
+ *
57
+ * http://www.apache.org/licenses/LICENSE-2.0
58
+ *
59
+ * Unless required by applicable law or agreed to in writing, software
60
+ * distributed under the License is distributed on an "AS IS" BASIS,
61
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
62
+ * See the License for the specific language governing permissions and
63
+ * limitations under the License.
64
+ */
65
+ const BASE_ENDPOINT = 'https://content-firebaseappcheck.googleapis.com/v1';
66
+ const EXCHANGE_RECAPTCHA_TOKEN_METHOD = 'exchangeRecaptchaV3Token';
67
+ const EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD = 'exchangeRecaptchaEnterpriseToken';
68
+ const EXCHANGE_DEBUG_TOKEN_METHOD = 'exchangeDebugToken';
69
+ const TOKEN_REFRESH_TIME = {
70
+ /**
71
+ * The offset time before token natural expiration to run the refresh.
72
+ * This is currently 5 minutes.
73
+ */
74
+ OFFSET_DURATION: 5 * 60 * 1000,
75
+ /**
76
+ * This is the first retrial wait after an error. This is currently
77
+ * 30 seconds.
78
+ */
79
+ RETRIAL_MIN_WAIT: 30 * 1000,
80
+ /**
81
+ * This is the maximum retrial wait, currently 16 minutes.
82
+ */
83
+ RETRIAL_MAX_WAIT: 16 * 60 * 1000
84
+ };
85
+ /**
86
+ * One day in millis, for certain error code backoffs.
87
+ */
88
88
  const ONE_DAY = 24 * 60 * 60 * 1000;
89
89
 
90
- /**
91
- * @license
92
- * Copyright 2020 Google LLC
93
- *
94
- * Licensed under the Apache License, Version 2.0 (the "License");
95
- * you may not use this file except in compliance with the License.
96
- * You may obtain a copy of the License at
97
- *
98
- * http://www.apache.org/licenses/LICENSE-2.0
99
- *
100
- * Unless required by applicable law or agreed to in writing, software
101
- * distributed under the License is distributed on an "AS IS" BASIS,
102
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
103
- * See the License for the specific language governing permissions and
104
- * limitations under the License.
105
- */
106
- /**
107
- * Port from auth proactiverefresh.js
108
- *
109
- */
110
- // TODO: move it to @firebase/util?
111
- // TODO: allow to config whether refresh should happen in the background
112
- class Refresher {
113
- constructor(operation, retryPolicy, getWaitDuration, lowerBound, upperBound) {
114
- this.operation = operation;
115
- this.retryPolicy = retryPolicy;
116
- this.getWaitDuration = getWaitDuration;
117
- this.lowerBound = lowerBound;
118
- this.upperBound = upperBound;
119
- this.pending = null;
120
- this.nextErrorWaitInterval = lowerBound;
121
- if (lowerBound > upperBound) {
122
- throw new Error('Proactive refresh lower bound greater than upper bound!');
123
- }
124
- }
125
- start() {
126
- this.nextErrorWaitInterval = this.lowerBound;
127
- this.process(true).catch(() => {
128
- /* we don't care about the result */
129
- });
130
- }
131
- stop() {
132
- if (this.pending) {
133
- this.pending.reject('cancelled');
134
- this.pending = null;
135
- }
136
- }
137
- isRunning() {
138
- return !!this.pending;
139
- }
140
- async process(hasSucceeded) {
141
- this.stop();
142
- try {
143
- this.pending = new Deferred();
144
- this.pending.promise.catch(_e => {
145
- /* ignore */
146
- });
147
- await sleep(this.getNextRun(hasSucceeded));
148
- // Why do we resolve a promise, then immediate wait for it?
149
- // We do it to make the promise chain cancellable.
150
- // We can call stop() which rejects the promise before the following line execute, which makes
151
- // the code jump to the catch block.
152
- // TODO: unit test this
153
- this.pending.resolve();
154
- await this.pending.promise;
155
- this.pending = new Deferred();
156
- this.pending.promise.catch(_e => {
157
- /* ignore */
158
- });
159
- await this.operation();
160
- this.pending.resolve();
161
- await this.pending.promise;
162
- this.process(true).catch(() => {
163
- /* we don't care about the result */
164
- });
165
- }
166
- catch (error) {
167
- if (this.retryPolicy(error)) {
168
- this.process(false).catch(() => {
169
- /* we don't care about the result */
170
- });
171
- }
172
- else {
173
- this.stop();
174
- }
175
- }
176
- }
177
- getNextRun(hasSucceeded) {
178
- if (hasSucceeded) {
179
- // If last operation succeeded, reset next error wait interval and return
180
- // the default wait duration.
181
- this.nextErrorWaitInterval = this.lowerBound;
182
- // Return typical wait duration interval after a successful operation.
183
- return this.getWaitDuration();
184
- }
185
- else {
186
- // Get next error wait interval.
187
- const currentErrorWaitInterval = this.nextErrorWaitInterval;
188
- // Double interval for next consecutive error.
189
- this.nextErrorWaitInterval *= 2;
190
- // Make sure next wait interval does not exceed the maximum upper bound.
191
- if (this.nextErrorWaitInterval > this.upperBound) {
192
- this.nextErrorWaitInterval = this.upperBound;
193
- }
194
- return currentErrorWaitInterval;
195
- }
196
- }
197
- }
198
- function sleep(ms) {
199
- return new Promise(resolve => {
200
- setTimeout(resolve, ms);
201
- });
90
+ /**
91
+ * @license
92
+ * Copyright 2020 Google LLC
93
+ *
94
+ * Licensed under the Apache License, Version 2.0 (the "License");
95
+ * you may not use this file except in compliance with the License.
96
+ * You may obtain a copy of the License at
97
+ *
98
+ * http://www.apache.org/licenses/LICENSE-2.0
99
+ *
100
+ * Unless required by applicable law or agreed to in writing, software
101
+ * distributed under the License is distributed on an "AS IS" BASIS,
102
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
103
+ * See the License for the specific language governing permissions and
104
+ * limitations under the License.
105
+ */
106
+ /**
107
+ * Port from auth proactiverefresh.js
108
+ *
109
+ */
110
+ // TODO: move it to @firebase/util?
111
+ // TODO: allow to config whether refresh should happen in the background
112
+ class Refresher {
113
+ constructor(operation, retryPolicy, getWaitDuration, lowerBound, upperBound) {
114
+ this.operation = operation;
115
+ this.retryPolicy = retryPolicy;
116
+ this.getWaitDuration = getWaitDuration;
117
+ this.lowerBound = lowerBound;
118
+ this.upperBound = upperBound;
119
+ this.pending = null;
120
+ this.nextErrorWaitInterval = lowerBound;
121
+ if (lowerBound > upperBound) {
122
+ throw new Error('Proactive refresh lower bound greater than upper bound!');
123
+ }
124
+ }
125
+ start() {
126
+ this.nextErrorWaitInterval = this.lowerBound;
127
+ this.process(true).catch(() => {
128
+ /* we don't care about the result */
129
+ });
130
+ }
131
+ stop() {
132
+ if (this.pending) {
133
+ this.pending.reject('cancelled');
134
+ this.pending = null;
135
+ }
136
+ }
137
+ isRunning() {
138
+ return !!this.pending;
139
+ }
140
+ async process(hasSucceeded) {
141
+ this.stop();
142
+ try {
143
+ this.pending = new Deferred();
144
+ this.pending.promise.catch(_e => {
145
+ /* ignore */
146
+ });
147
+ await sleep(this.getNextRun(hasSucceeded));
148
+ // Why do we resolve a promise, then immediate wait for it?
149
+ // We do it to make the promise chain cancellable.
150
+ // We can call stop() which rejects the promise before the following line execute, which makes
151
+ // the code jump to the catch block.
152
+ // TODO: unit test this
153
+ this.pending.resolve();
154
+ await this.pending.promise;
155
+ this.pending = new Deferred();
156
+ this.pending.promise.catch(_e => {
157
+ /* ignore */
158
+ });
159
+ await this.operation();
160
+ this.pending.resolve();
161
+ await this.pending.promise;
162
+ this.process(true).catch(() => {
163
+ /* we don't care about the result */
164
+ });
165
+ }
166
+ catch (error) {
167
+ if (this.retryPolicy(error)) {
168
+ this.process(false).catch(() => {
169
+ /* we don't care about the result */
170
+ });
171
+ }
172
+ else {
173
+ this.stop();
174
+ }
175
+ }
176
+ }
177
+ getNextRun(hasSucceeded) {
178
+ if (hasSucceeded) {
179
+ // If last operation succeeded, reset next error wait interval and return
180
+ // the default wait duration.
181
+ this.nextErrorWaitInterval = this.lowerBound;
182
+ // Return typical wait duration interval after a successful operation.
183
+ return this.getWaitDuration();
184
+ }
185
+ else {
186
+ // Get next error wait interval.
187
+ const currentErrorWaitInterval = this.nextErrorWaitInterval;
188
+ // Double interval for next consecutive error.
189
+ this.nextErrorWaitInterval *= 2;
190
+ // Make sure next wait interval does not exceed the maximum upper bound.
191
+ if (this.nextErrorWaitInterval > this.upperBound) {
192
+ this.nextErrorWaitInterval = this.upperBound;
193
+ }
194
+ return currentErrorWaitInterval;
195
+ }
196
+ }
197
+ }
198
+ function sleep(ms) {
199
+ return new Promise(resolve => {
200
+ setTimeout(resolve, ms);
201
+ });
202
202
  }
203
203
 
204
- /**
205
- * @license
206
- * Copyright 2020 Google LLC
207
- *
208
- * Licensed under the Apache License, Version 2.0 (the "License");
209
- * you may not use this file except in compliance with the License.
210
- * You may obtain a copy of the License at
211
- *
212
- * http://www.apache.org/licenses/LICENSE-2.0
213
- *
214
- * Unless required by applicable law or agreed to in writing, software
215
- * distributed under the License is distributed on an "AS IS" BASIS,
216
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
217
- * See the License for the specific language governing permissions and
218
- * limitations under the License.
219
- */
220
- const ERRORS = {
221
- ["already-initialized" /* AppCheckError.ALREADY_INITIALIZED */]: 'You have already called initializeAppCheck() for FirebaseApp {$appName} with ' +
222
- 'different options. To avoid this error, call initializeAppCheck() with the ' +
223
- 'same options as when it was originally called. This will return the ' +
224
- 'already initialized instance.',
225
- ["use-before-activation" /* AppCheckError.USE_BEFORE_ACTIVATION */]: 'App Check is being used before initializeAppCheck() is called for FirebaseApp {$appName}. ' +
226
- 'Call initializeAppCheck() before instantiating other Firebase services.',
227
- ["fetch-network-error" /* AppCheckError.FETCH_NETWORK_ERROR */]: 'Fetch failed to connect to a network. Check Internet connection. ' +
228
- 'Original error: {$originalErrorMessage}.',
229
- ["fetch-parse-error" /* AppCheckError.FETCH_PARSE_ERROR */]: 'Fetch client could not parse response.' +
230
- ' Original error: {$originalErrorMessage}.',
231
- ["fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */]: 'Fetch server returned an HTTP error status. HTTP status: {$httpStatus}.',
232
- ["storage-open" /* AppCheckError.STORAGE_OPEN */]: 'Error thrown when opening storage. Original error: {$originalErrorMessage}.',
233
- ["storage-get" /* AppCheckError.STORAGE_GET */]: 'Error thrown when reading from storage. Original error: {$originalErrorMessage}.',
234
- ["storage-set" /* AppCheckError.STORAGE_WRITE */]: 'Error thrown when writing to storage. Original error: {$originalErrorMessage}.',
235
- ["recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */]: 'ReCAPTCHA error.',
236
- ["throttled" /* AppCheckError.THROTTLED */]: `Requests throttled due to {$httpStatus} error. Attempts allowed again after {$time}`
237
- };
204
+ /**
205
+ * @license
206
+ * Copyright 2020 Google LLC
207
+ *
208
+ * Licensed under the Apache License, Version 2.0 (the "License");
209
+ * you may not use this file except in compliance with the License.
210
+ * You may obtain a copy of the License at
211
+ *
212
+ * http://www.apache.org/licenses/LICENSE-2.0
213
+ *
214
+ * Unless required by applicable law or agreed to in writing, software
215
+ * distributed under the License is distributed on an "AS IS" BASIS,
216
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
217
+ * See the License for the specific language governing permissions and
218
+ * limitations under the License.
219
+ */
220
+ const ERRORS = {
221
+ ["already-initialized" /* AppCheckError.ALREADY_INITIALIZED */]: 'You have already called initializeAppCheck() for FirebaseApp {$appName} with ' +
222
+ 'different options. To avoid this error, call initializeAppCheck() with the ' +
223
+ 'same options as when it was originally called. This will return the ' +
224
+ 'already initialized instance.',
225
+ ["use-before-activation" /* AppCheckError.USE_BEFORE_ACTIVATION */]: 'App Check is being used before initializeAppCheck() is called for FirebaseApp {$appName}. ' +
226
+ 'Call initializeAppCheck() before instantiating other Firebase services.',
227
+ ["fetch-network-error" /* AppCheckError.FETCH_NETWORK_ERROR */]: 'Fetch failed to connect to a network. Check Internet connection. ' +
228
+ 'Original error: {$originalErrorMessage}.',
229
+ ["fetch-parse-error" /* AppCheckError.FETCH_PARSE_ERROR */]: 'Fetch client could not parse response.' +
230
+ ' Original error: {$originalErrorMessage}.',
231
+ ["fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */]: 'Fetch server returned an HTTP error status. HTTP status: {$httpStatus}.',
232
+ ["storage-open" /* AppCheckError.STORAGE_OPEN */]: 'Error thrown when opening storage. Original error: {$originalErrorMessage}.',
233
+ ["storage-get" /* AppCheckError.STORAGE_GET */]: 'Error thrown when reading from storage. Original error: {$originalErrorMessage}.',
234
+ ["storage-set" /* AppCheckError.STORAGE_WRITE */]: 'Error thrown when writing to storage. Original error: {$originalErrorMessage}.',
235
+ ["recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */]: 'ReCAPTCHA error.',
236
+ ["throttled" /* AppCheckError.THROTTLED */]: `Requests throttled due to {$httpStatus} error. Attempts allowed again after {$time}`
237
+ };
238
238
  const ERROR_FACTORY = new ErrorFactory('appCheck', 'AppCheck', ERRORS);
239
239
 
240
- /**
241
- * @license
242
- * Copyright 2020 Google LLC
243
- *
244
- * Licensed under the Apache License, Version 2.0 (the "License");
245
- * you may not use this file except in compliance with the License.
246
- * You may obtain a copy of the License at
247
- *
248
- * http://www.apache.org/licenses/LICENSE-2.0
249
- *
250
- * Unless required by applicable law or agreed to in writing, software
251
- * distributed under the License is distributed on an "AS IS" BASIS,
252
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
253
- * See the License for the specific language governing permissions and
254
- * limitations under the License.
255
- */
256
- function getRecaptcha(isEnterprise = false) {
257
- var _a;
258
- if (isEnterprise) {
259
- return (_a = self.grecaptcha) === null || _a === void 0 ? void 0 : _a.enterprise;
260
- }
261
- return self.grecaptcha;
262
- }
263
- function ensureActivated(app) {
264
- if (!getStateReference(app).activated) {
265
- throw ERROR_FACTORY.create("use-before-activation" /* AppCheckError.USE_BEFORE_ACTIVATION */, {
266
- appName: app.name
267
- });
268
- }
269
- }
270
- function getDurationString(durationInMillis) {
271
- const totalSeconds = Math.round(durationInMillis / 1000);
272
- const days = Math.floor(totalSeconds / (3600 * 24));
273
- const hours = Math.floor((totalSeconds - days * 3600 * 24) / 3600);
274
- const minutes = Math.floor((totalSeconds - days * 3600 * 24 - hours * 3600) / 60);
275
- const seconds = totalSeconds - days * 3600 * 24 - hours * 3600 - minutes * 60;
276
- let result = '';
277
- if (days) {
278
- result += pad(days) + 'd:';
279
- }
280
- if (hours) {
281
- result += pad(hours) + 'h:';
282
- }
283
- result += pad(minutes) + 'm:' + pad(seconds) + 's';
284
- return result;
285
- }
286
- function pad(value) {
287
- if (value === 0) {
288
- return '00';
289
- }
290
- return value >= 10 ? value.toString() : '0' + value;
240
+ /**
241
+ * @license
242
+ * Copyright 2020 Google LLC
243
+ *
244
+ * Licensed under the Apache License, Version 2.0 (the "License");
245
+ * you may not use this file except in compliance with the License.
246
+ * You may obtain a copy of the License at
247
+ *
248
+ * http://www.apache.org/licenses/LICENSE-2.0
249
+ *
250
+ * Unless required by applicable law or agreed to in writing, software
251
+ * distributed under the License is distributed on an "AS IS" BASIS,
252
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
253
+ * See the License for the specific language governing permissions and
254
+ * limitations under the License.
255
+ */
256
+ function getRecaptcha(isEnterprise = false) {
257
+ var _a;
258
+ if (isEnterprise) {
259
+ return (_a = self.grecaptcha) === null || _a === void 0 ? void 0 : _a.enterprise;
260
+ }
261
+ return self.grecaptcha;
262
+ }
263
+ function ensureActivated(app) {
264
+ if (!getStateReference(app).activated) {
265
+ throw ERROR_FACTORY.create("use-before-activation" /* AppCheckError.USE_BEFORE_ACTIVATION */, {
266
+ appName: app.name
267
+ });
268
+ }
269
+ }
270
+ function getDurationString(durationInMillis) {
271
+ const totalSeconds = Math.round(durationInMillis / 1000);
272
+ const days = Math.floor(totalSeconds / (3600 * 24));
273
+ const hours = Math.floor((totalSeconds - days * 3600 * 24) / 3600);
274
+ const minutes = Math.floor((totalSeconds - days * 3600 * 24 - hours * 3600) / 60);
275
+ const seconds = totalSeconds - days * 3600 * 24 - hours * 3600 - minutes * 60;
276
+ let result = '';
277
+ if (days) {
278
+ result += pad(days) + 'd:';
279
+ }
280
+ if (hours) {
281
+ result += pad(hours) + 'h:';
282
+ }
283
+ result += pad(minutes) + 'm:' + pad(seconds) + 's';
284
+ return result;
285
+ }
286
+ function pad(value) {
287
+ if (value === 0) {
288
+ return '00';
289
+ }
290
+ return value >= 10 ? value.toString() : '0' + value;
291
291
  }
292
292
 
293
- /**
294
- * @license
295
- * Copyright 2020 Google LLC
296
- *
297
- * Licensed under the Apache License, Version 2.0 (the "License");
298
- * you may not use this file except in compliance with the License.
299
- * You may obtain a copy of the License at
300
- *
301
- * http://www.apache.org/licenses/LICENSE-2.0
302
- *
303
- * Unless required by applicable law or agreed to in writing, software
304
- * distributed under the License is distributed on an "AS IS" BASIS,
305
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
306
- * See the License for the specific language governing permissions and
307
- * limitations under the License.
308
- */
309
- async function exchangeToken({ url, body }, heartbeatServiceProvider) {
310
- const headers = {
311
- 'Content-Type': 'application/json'
312
- };
313
- // If heartbeat service exists, add heartbeat header string to the header.
314
- const heartbeatService = heartbeatServiceProvider.getImmediate({
315
- optional: true
316
- });
317
- if (heartbeatService) {
318
- const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
319
- if (heartbeatsHeader) {
320
- headers['X-Firebase-Client'] = heartbeatsHeader;
321
- }
322
- }
323
- const options = {
324
- method: 'POST',
325
- body: JSON.stringify(body),
326
- headers
327
- };
328
- let response;
329
- try {
330
- response = await fetch(url, options);
331
- }
332
- catch (originalError) {
333
- throw ERROR_FACTORY.create("fetch-network-error" /* AppCheckError.FETCH_NETWORK_ERROR */, {
334
- originalErrorMessage: originalError === null || originalError === void 0 ? void 0 : originalError.message
335
- });
336
- }
337
- if (response.status !== 200) {
338
- throw ERROR_FACTORY.create("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */, {
339
- httpStatus: response.status
340
- });
341
- }
342
- let responseBody;
343
- try {
344
- // JSON parsing throws SyntaxError if the response body isn't a JSON string.
345
- responseBody = await response.json();
346
- }
347
- catch (originalError) {
348
- throw ERROR_FACTORY.create("fetch-parse-error" /* AppCheckError.FETCH_PARSE_ERROR */, {
349
- originalErrorMessage: originalError === null || originalError === void 0 ? void 0 : originalError.message
350
- });
351
- }
352
- // Protobuf duration format.
353
- // https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Duration
354
- const match = responseBody.ttl.match(/^([\d.]+)(s)$/);
355
- if (!match || !match[2] || isNaN(Number(match[1]))) {
356
- throw ERROR_FACTORY.create("fetch-parse-error" /* AppCheckError.FETCH_PARSE_ERROR */, {
357
- originalErrorMessage: `ttl field (timeToLive) is not in standard Protobuf Duration ` +
358
- `format: ${responseBody.ttl}`
359
- });
360
- }
361
- const timeToLiveAsNumber = Number(match[1]) * 1000;
362
- const now = Date.now();
363
- return {
364
- token: responseBody.token,
365
- expireTimeMillis: now + timeToLiveAsNumber,
366
- issuedAtTimeMillis: now
367
- };
368
- }
369
- function getExchangeRecaptchaV3TokenRequest(app, reCAPTCHAToken) {
370
- const { projectId, appId, apiKey } = app.options;
371
- return {
372
- url: `${BASE_ENDPOINT}/projects/${projectId}/apps/${appId}:${EXCHANGE_RECAPTCHA_TOKEN_METHOD}?key=${apiKey}`,
373
- body: {
374
- 'recaptcha_v3_token': reCAPTCHAToken
375
- }
376
- };
377
- }
378
- function getExchangeRecaptchaEnterpriseTokenRequest(app, reCAPTCHAToken) {
379
- const { projectId, appId, apiKey } = app.options;
380
- return {
381
- url: `${BASE_ENDPOINT}/projects/${projectId}/apps/${appId}:${EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD}?key=${apiKey}`,
382
- body: {
383
- 'recaptcha_enterprise_token': reCAPTCHAToken
384
- }
385
- };
386
- }
387
- function getExchangeDebugTokenRequest(app, debugToken) {
388
- const { projectId, appId, apiKey } = app.options;
389
- return {
390
- url: `${BASE_ENDPOINT}/projects/${projectId}/apps/${appId}:${EXCHANGE_DEBUG_TOKEN_METHOD}?key=${apiKey}`,
391
- body: {
392
- // eslint-disable-next-line
393
- debug_token: debugToken
394
- }
395
- };
293
+ /**
294
+ * @license
295
+ * Copyright 2020 Google LLC
296
+ *
297
+ * Licensed under the Apache License, Version 2.0 (the "License");
298
+ * you may not use this file except in compliance with the License.
299
+ * You may obtain a copy of the License at
300
+ *
301
+ * http://www.apache.org/licenses/LICENSE-2.0
302
+ *
303
+ * Unless required by applicable law or agreed to in writing, software
304
+ * distributed under the License is distributed on an "AS IS" BASIS,
305
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
306
+ * See the License for the specific language governing permissions and
307
+ * limitations under the License.
308
+ */
309
+ async function exchangeToken({ url, body }, heartbeatServiceProvider) {
310
+ const headers = {
311
+ 'Content-Type': 'application/json'
312
+ };
313
+ // If heartbeat service exists, add heartbeat header string to the header.
314
+ const heartbeatService = heartbeatServiceProvider.getImmediate({
315
+ optional: true
316
+ });
317
+ if (heartbeatService) {
318
+ const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
319
+ if (heartbeatsHeader) {
320
+ headers['X-Firebase-Client'] = heartbeatsHeader;
321
+ }
322
+ }
323
+ const options = {
324
+ method: 'POST',
325
+ body: JSON.stringify(body),
326
+ headers
327
+ };
328
+ let response;
329
+ try {
330
+ response = await fetch(url, options);
331
+ }
332
+ catch (originalError) {
333
+ throw ERROR_FACTORY.create("fetch-network-error" /* AppCheckError.FETCH_NETWORK_ERROR */, {
334
+ originalErrorMessage: originalError === null || originalError === void 0 ? void 0 : originalError.message
335
+ });
336
+ }
337
+ if (response.status !== 200) {
338
+ throw ERROR_FACTORY.create("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */, {
339
+ httpStatus: response.status
340
+ });
341
+ }
342
+ let responseBody;
343
+ try {
344
+ // JSON parsing throws SyntaxError if the response body isn't a JSON string.
345
+ responseBody = await response.json();
346
+ }
347
+ catch (originalError) {
348
+ throw ERROR_FACTORY.create("fetch-parse-error" /* AppCheckError.FETCH_PARSE_ERROR */, {
349
+ originalErrorMessage: originalError === null || originalError === void 0 ? void 0 : originalError.message
350
+ });
351
+ }
352
+ // Protobuf duration format.
353
+ // https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Duration
354
+ const match = responseBody.ttl.match(/^([\d.]+)(s)$/);
355
+ if (!match || !match[2] || isNaN(Number(match[1]))) {
356
+ throw ERROR_FACTORY.create("fetch-parse-error" /* AppCheckError.FETCH_PARSE_ERROR */, {
357
+ originalErrorMessage: `ttl field (timeToLive) is not in standard Protobuf Duration ` +
358
+ `format: ${responseBody.ttl}`
359
+ });
360
+ }
361
+ const timeToLiveAsNumber = Number(match[1]) * 1000;
362
+ const now = Date.now();
363
+ return {
364
+ token: responseBody.token,
365
+ expireTimeMillis: now + timeToLiveAsNumber,
366
+ issuedAtTimeMillis: now
367
+ };
368
+ }
369
+ function getExchangeRecaptchaV3TokenRequest(app, reCAPTCHAToken) {
370
+ const { projectId, appId, apiKey } = app.options;
371
+ return {
372
+ url: `${BASE_ENDPOINT}/projects/${projectId}/apps/${appId}:${EXCHANGE_RECAPTCHA_TOKEN_METHOD}?key=${apiKey}`,
373
+ body: {
374
+ 'recaptcha_v3_token': reCAPTCHAToken
375
+ }
376
+ };
377
+ }
378
+ function getExchangeRecaptchaEnterpriseTokenRequest(app, reCAPTCHAToken) {
379
+ const { projectId, appId, apiKey } = app.options;
380
+ return {
381
+ url: `${BASE_ENDPOINT}/projects/${projectId}/apps/${appId}:${EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD}?key=${apiKey}`,
382
+ body: {
383
+ 'recaptcha_enterprise_token': reCAPTCHAToken
384
+ }
385
+ };
386
+ }
387
+ function getExchangeDebugTokenRequest(app, debugToken) {
388
+ const { projectId, appId, apiKey } = app.options;
389
+ return {
390
+ url: `${BASE_ENDPOINT}/projects/${projectId}/apps/${appId}:${EXCHANGE_DEBUG_TOKEN_METHOD}?key=${apiKey}`,
391
+ body: {
392
+ // eslint-disable-next-line
393
+ debug_token: debugToken
394
+ }
395
+ };
396
396
  }
397
397
 
398
- /**
399
- * @license
400
- * Copyright 2020 Google LLC
401
- *
402
- * Licensed under the Apache License, Version 2.0 (the "License");
403
- * you may not use this file except in compliance with the License.
404
- * You may obtain a copy of the License at
405
- *
406
- * http://www.apache.org/licenses/LICENSE-2.0
407
- *
408
- * Unless required by applicable law or agreed to in writing, software
409
- * distributed under the License is distributed on an "AS IS" BASIS,
410
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
411
- * See the License for the specific language governing permissions and
412
- * limitations under the License.
413
- */
414
- const DB_NAME = 'firebase-app-check-database';
415
- const DB_VERSION = 1;
416
- const STORE_NAME = 'firebase-app-check-store';
417
- const DEBUG_TOKEN_KEY = 'debug-token';
418
- let dbPromise = null;
419
- function getDBPromise() {
420
- if (dbPromise) {
421
- return dbPromise;
422
- }
423
- dbPromise = new Promise((resolve, reject) => {
424
- try {
425
- const request = indexedDB.open(DB_NAME, DB_VERSION);
426
- request.onsuccess = event => {
427
- resolve(event.target.result);
428
- };
429
- request.onerror = event => {
430
- var _a;
431
- reject(ERROR_FACTORY.create("storage-open" /* AppCheckError.STORAGE_OPEN */, {
432
- originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
433
- }));
434
- };
435
- request.onupgradeneeded = event => {
436
- const db = event.target.result;
437
- // We don't use 'break' in this switch statement, the fall-through
438
- // behavior is what we want, because if there are multiple versions between
439
- // the old version and the current version, we want ALL the migrations
440
- // that correspond to those versions to run, not only the last one.
441
- // eslint-disable-next-line default-case
442
- switch (event.oldVersion) {
443
- case 0:
444
- db.createObjectStore(STORE_NAME, {
445
- keyPath: 'compositeKey'
446
- });
447
- }
448
- };
449
- }
450
- catch (e) {
451
- reject(ERROR_FACTORY.create("storage-open" /* AppCheckError.STORAGE_OPEN */, {
452
- originalErrorMessage: e === null || e === void 0 ? void 0 : e.message
453
- }));
454
- }
455
- });
456
- return dbPromise;
457
- }
458
- function readTokenFromIndexedDB(app) {
459
- return read(computeKey(app));
460
- }
461
- function writeTokenToIndexedDB(app, token) {
462
- return write(computeKey(app), token);
463
- }
464
- function writeDebugTokenToIndexedDB(token) {
465
- return write(DEBUG_TOKEN_KEY, token);
466
- }
467
- function readDebugTokenFromIndexedDB() {
468
- return read(DEBUG_TOKEN_KEY);
469
- }
470
- async function write(key, value) {
471
- const db = await getDBPromise();
472
- const transaction = db.transaction(STORE_NAME, 'readwrite');
473
- const store = transaction.objectStore(STORE_NAME);
474
- const request = store.put({
475
- compositeKey: key,
476
- value
477
- });
478
- return new Promise((resolve, reject) => {
479
- request.onsuccess = _event => {
480
- resolve();
481
- };
482
- transaction.onerror = event => {
483
- var _a;
484
- reject(ERROR_FACTORY.create("storage-set" /* AppCheckError.STORAGE_WRITE */, {
485
- originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
486
- }));
487
- };
488
- });
489
- }
490
- async function read(key) {
491
- const db = await getDBPromise();
492
- const transaction = db.transaction(STORE_NAME, 'readonly');
493
- const store = transaction.objectStore(STORE_NAME);
494
- const request = store.get(key);
495
- return new Promise((resolve, reject) => {
496
- request.onsuccess = event => {
497
- const result = event.target.result;
498
- if (result) {
499
- resolve(result.value);
500
- }
501
- else {
502
- resolve(undefined);
503
- }
504
- };
505
- transaction.onerror = event => {
506
- var _a;
507
- reject(ERROR_FACTORY.create("storage-get" /* AppCheckError.STORAGE_GET */, {
508
- originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
509
- }));
510
- };
511
- });
512
- }
513
- function computeKey(app) {
514
- return `${app.options.appId}-${app.name}`;
398
+ /**
399
+ * @license
400
+ * Copyright 2020 Google LLC
401
+ *
402
+ * Licensed under the Apache License, Version 2.0 (the "License");
403
+ * you may not use this file except in compliance with the License.
404
+ * You may obtain a copy of the License at
405
+ *
406
+ * http://www.apache.org/licenses/LICENSE-2.0
407
+ *
408
+ * Unless required by applicable law or agreed to in writing, software
409
+ * distributed under the License is distributed on an "AS IS" BASIS,
410
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
411
+ * See the License for the specific language governing permissions and
412
+ * limitations under the License.
413
+ */
414
+ const DB_NAME = 'firebase-app-check-database';
415
+ const DB_VERSION = 1;
416
+ const STORE_NAME = 'firebase-app-check-store';
417
+ const DEBUG_TOKEN_KEY = 'debug-token';
418
+ let dbPromise = null;
419
+ function getDBPromise() {
420
+ if (dbPromise) {
421
+ return dbPromise;
422
+ }
423
+ dbPromise = new Promise((resolve, reject) => {
424
+ try {
425
+ const request = indexedDB.open(DB_NAME, DB_VERSION);
426
+ request.onsuccess = event => {
427
+ resolve(event.target.result);
428
+ };
429
+ request.onerror = event => {
430
+ var _a;
431
+ reject(ERROR_FACTORY.create("storage-open" /* AppCheckError.STORAGE_OPEN */, {
432
+ originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
433
+ }));
434
+ };
435
+ request.onupgradeneeded = event => {
436
+ const db = event.target.result;
437
+ // We don't use 'break' in this switch statement, the fall-through
438
+ // behavior is what we want, because if there are multiple versions between
439
+ // the old version and the current version, we want ALL the migrations
440
+ // that correspond to those versions to run, not only the last one.
441
+ // eslint-disable-next-line default-case
442
+ switch (event.oldVersion) {
443
+ case 0:
444
+ db.createObjectStore(STORE_NAME, {
445
+ keyPath: 'compositeKey'
446
+ });
447
+ }
448
+ };
449
+ }
450
+ catch (e) {
451
+ reject(ERROR_FACTORY.create("storage-open" /* AppCheckError.STORAGE_OPEN */, {
452
+ originalErrorMessage: e === null || e === void 0 ? void 0 : e.message
453
+ }));
454
+ }
455
+ });
456
+ return dbPromise;
457
+ }
458
+ function readTokenFromIndexedDB(app) {
459
+ return read(computeKey(app));
460
+ }
461
+ function writeTokenToIndexedDB(app, token) {
462
+ return write(computeKey(app), token);
463
+ }
464
+ function writeDebugTokenToIndexedDB(token) {
465
+ return write(DEBUG_TOKEN_KEY, token);
466
+ }
467
+ function readDebugTokenFromIndexedDB() {
468
+ return read(DEBUG_TOKEN_KEY);
469
+ }
470
+ async function write(key, value) {
471
+ const db = await getDBPromise();
472
+ const transaction = db.transaction(STORE_NAME, 'readwrite');
473
+ const store = transaction.objectStore(STORE_NAME);
474
+ const request = store.put({
475
+ compositeKey: key,
476
+ value
477
+ });
478
+ return new Promise((resolve, reject) => {
479
+ request.onsuccess = _event => {
480
+ resolve();
481
+ };
482
+ transaction.onerror = event => {
483
+ var _a;
484
+ reject(ERROR_FACTORY.create("storage-set" /* AppCheckError.STORAGE_WRITE */, {
485
+ originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
486
+ }));
487
+ };
488
+ });
489
+ }
490
+ async function read(key) {
491
+ const db = await getDBPromise();
492
+ const transaction = db.transaction(STORE_NAME, 'readonly');
493
+ const store = transaction.objectStore(STORE_NAME);
494
+ const request = store.get(key);
495
+ return new Promise((resolve, reject) => {
496
+ request.onsuccess = event => {
497
+ const result = event.target.result;
498
+ if (result) {
499
+ resolve(result.value);
500
+ }
501
+ else {
502
+ resolve(undefined);
503
+ }
504
+ };
505
+ transaction.onerror = event => {
506
+ var _a;
507
+ reject(ERROR_FACTORY.create("storage-get" /* AppCheckError.STORAGE_GET */, {
508
+ originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
509
+ }));
510
+ };
511
+ });
512
+ }
513
+ function computeKey(app) {
514
+ return `${app.options.appId}-${app.name}`;
515
515
  }
516
516
 
517
- /**
518
- * @license
519
- * Copyright 2020 Google LLC
520
- *
521
- * Licensed under the Apache License, Version 2.0 (the "License");
522
- * you may not use this file except in compliance with the License.
523
- * You may obtain a copy of the License at
524
- *
525
- * http://www.apache.org/licenses/LICENSE-2.0
526
- *
527
- * Unless required by applicable law or agreed to in writing, software
528
- * distributed under the License is distributed on an "AS IS" BASIS,
529
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
530
- * See the License for the specific language governing permissions and
531
- * limitations under the License.
532
- */
517
+ /**
518
+ * @license
519
+ * Copyright 2020 Google LLC
520
+ *
521
+ * Licensed under the Apache License, Version 2.0 (the "License");
522
+ * you may not use this file except in compliance with the License.
523
+ * You may obtain a copy of the License at
524
+ *
525
+ * http://www.apache.org/licenses/LICENSE-2.0
526
+ *
527
+ * Unless required by applicable law or agreed to in writing, software
528
+ * distributed under the License is distributed on an "AS IS" BASIS,
529
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
530
+ * See the License for the specific language governing permissions and
531
+ * limitations under the License.
532
+ */
533
533
  const logger = new Logger('@firebase/app-check');
534
534
 
535
- /**
536
- * @license
537
- * Copyright 2020 Google LLC
538
- *
539
- * Licensed under the Apache License, Version 2.0 (the "License");
540
- * you may not use this file except in compliance with the License.
541
- * You may obtain a copy of the License at
542
- *
543
- * http://www.apache.org/licenses/LICENSE-2.0
544
- *
545
- * Unless required by applicable law or agreed to in writing, software
546
- * distributed under the License is distributed on an "AS IS" BASIS,
547
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
548
- * See the License for the specific language governing permissions and
549
- * limitations under the License.
550
- */
551
- /**
552
- * Always resolves. In case of an error reading from indexeddb, resolve with undefined
553
- */
554
- async function readTokenFromStorage(app) {
555
- if (isIndexedDBAvailable()) {
556
- let token = undefined;
557
- try {
558
- token = await readTokenFromIndexedDB(app);
559
- }
560
- catch (e) {
561
- // swallow the error and return undefined
562
- logger.warn(`Failed to read token from IndexedDB. Error: ${e}`);
563
- }
564
- return token;
565
- }
566
- return undefined;
567
- }
568
- /**
569
- * Always resolves. In case of an error writing to indexeddb, print a warning and resolve the promise
570
- */
571
- function writeTokenToStorage(app, token) {
572
- if (isIndexedDBAvailable()) {
573
- return writeTokenToIndexedDB(app, token).catch(e => {
574
- // swallow the error and resolve the promise
575
- logger.warn(`Failed to write token to IndexedDB. Error: ${e}`);
576
- });
577
- }
578
- return Promise.resolve();
579
- }
580
- async function readOrCreateDebugTokenFromStorage() {
581
- /**
582
- * Theoretically race condition can happen if we read, then write in 2 separate transactions.
583
- * But it won't happen here, because this function will be called exactly once.
584
- */
585
- let existingDebugToken = undefined;
586
- try {
587
- existingDebugToken = await readDebugTokenFromIndexedDB();
588
- }
589
- catch (_e) {
590
- // failed to read from indexeddb. We assume there is no existing debug token, and generate a new one.
591
- }
592
- if (!existingDebugToken) {
593
- // create a new debug token
594
- const newToken = uuidv4();
595
- // We don't need to block on writing to indexeddb
596
- // In case persistence failed, a new debug token will be generated every time the page is refreshed.
597
- // It renders the debug token useless because you have to manually register(whitelist) the new token in the firebase console again and again.
598
- // If you see this error trying to use debug token, it probably means you are using a browser that doesn't support indexeddb.
599
- // You should switch to a different browser that supports indexeddb
600
- writeDebugTokenToIndexedDB(newToken).catch(e => logger.warn(`Failed to persist debug token to IndexedDB. Error: ${e}`));
601
- return newToken;
602
- }
603
- else {
604
- return existingDebugToken;
605
- }
535
+ /**
536
+ * @license
537
+ * Copyright 2020 Google LLC
538
+ *
539
+ * Licensed under the Apache License, Version 2.0 (the "License");
540
+ * you may not use this file except in compliance with the License.
541
+ * You may obtain a copy of the License at
542
+ *
543
+ * http://www.apache.org/licenses/LICENSE-2.0
544
+ *
545
+ * Unless required by applicable law or agreed to in writing, software
546
+ * distributed under the License is distributed on an "AS IS" BASIS,
547
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
548
+ * See the License for the specific language governing permissions and
549
+ * limitations under the License.
550
+ */
551
+ /**
552
+ * Always resolves. In case of an error reading from indexeddb, resolve with undefined
553
+ */
554
+ async function readTokenFromStorage(app) {
555
+ if (isIndexedDBAvailable()) {
556
+ let token = undefined;
557
+ try {
558
+ token = await readTokenFromIndexedDB(app);
559
+ }
560
+ catch (e) {
561
+ // swallow the error and return undefined
562
+ logger.warn(`Failed to read token from IndexedDB. Error: ${e}`);
563
+ }
564
+ return token;
565
+ }
566
+ return undefined;
567
+ }
568
+ /**
569
+ * Always resolves. In case of an error writing to indexeddb, print a warning and resolve the promise
570
+ */
571
+ function writeTokenToStorage(app, token) {
572
+ if (isIndexedDBAvailable()) {
573
+ return writeTokenToIndexedDB(app, token).catch(e => {
574
+ // swallow the error and resolve the promise
575
+ logger.warn(`Failed to write token to IndexedDB. Error: ${e}`);
576
+ });
577
+ }
578
+ return Promise.resolve();
579
+ }
580
+ async function readOrCreateDebugTokenFromStorage() {
581
+ /**
582
+ * Theoretically race condition can happen if we read, then write in 2 separate transactions.
583
+ * But it won't happen here, because this function will be called exactly once.
584
+ */
585
+ let existingDebugToken = undefined;
586
+ try {
587
+ existingDebugToken = await readDebugTokenFromIndexedDB();
588
+ }
589
+ catch (_e) {
590
+ // failed to read from indexeddb. We assume there is no existing debug token, and generate a new one.
591
+ }
592
+ if (!existingDebugToken) {
593
+ // create a new debug token
594
+ const newToken = uuidv4();
595
+ // We don't need to block on writing to indexeddb
596
+ // In case persistence failed, a new debug token will be generated every time the page is refreshed.
597
+ // It renders the debug token useless because you have to manually register(whitelist) the new token in the firebase console again and again.
598
+ // If you see this error trying to use debug token, it probably means you are using a browser that doesn't support indexeddb.
599
+ // You should switch to a different browser that supports indexeddb
600
+ writeDebugTokenToIndexedDB(newToken).catch(e => logger.warn(`Failed to persist debug token to IndexedDB. Error: ${e}`));
601
+ return newToken;
602
+ }
603
+ else {
604
+ return existingDebugToken;
605
+ }
606
606
  }
607
607
 
608
- /**
609
- * @license
610
- * Copyright 2020 Google LLC
611
- *
612
- * Licensed under the Apache License, Version 2.0 (the "License");
613
- * you may not use this file except in compliance with the License.
614
- * You may obtain a copy of the License at
615
- *
616
- * http://www.apache.org/licenses/LICENSE-2.0
617
- *
618
- * Unless required by applicable law or agreed to in writing, software
619
- * distributed under the License is distributed on an "AS IS" BASIS,
620
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
621
- * See the License for the specific language governing permissions and
622
- * limitations under the License.
623
- */
624
- function isDebugMode() {
625
- const debugState = getDebugState();
626
- return debugState.enabled;
627
- }
628
- async function getDebugToken() {
629
- const state = getDebugState();
630
- if (state.enabled && state.token) {
631
- return state.token.promise;
632
- }
633
- else {
634
- // should not happen!
608
+ /**
609
+ * @license
610
+ * Copyright 2020 Google LLC
611
+ *
612
+ * Licensed under the Apache License, Version 2.0 (the "License");
613
+ * you may not use this file except in compliance with the License.
614
+ * You may obtain a copy of the License at
615
+ *
616
+ * http://www.apache.org/licenses/LICENSE-2.0
617
+ *
618
+ * Unless required by applicable law or agreed to in writing, software
619
+ * distributed under the License is distributed on an "AS IS" BASIS,
620
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
621
+ * See the License for the specific language governing permissions and
622
+ * limitations under the License.
623
+ */
624
+ function isDebugMode() {
625
+ const debugState = getDebugState();
626
+ return debugState.enabled;
627
+ }
628
+ async function getDebugToken() {
629
+ const state = getDebugState();
630
+ if (state.enabled && state.token) {
631
+ return state.token.promise;
632
+ }
633
+ else {
634
+ // should not happen!
635
635
  throw Error(`
636
636
  Can't get debug token in production mode.
637
- `);
638
- }
639
- }
640
- function initializeDebugMode() {
641
- const globals = getGlobal();
642
- const debugState = getDebugState();
643
- // Set to true if this function has been called, whether or not
644
- // it enabled debug mode.
645
- debugState.initialized = true;
646
- if (typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== 'string' &&
647
- globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== true) {
648
- return;
649
- }
650
- debugState.enabled = true;
651
- const deferredToken = new Deferred();
652
- debugState.token = deferredToken;
653
- if (typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN === 'string') {
654
- deferredToken.resolve(globals.FIREBASE_APPCHECK_DEBUG_TOKEN);
655
- }
656
- else {
657
- deferredToken.resolve(readOrCreateDebugTokenFromStorage());
658
- }
637
+ `);
638
+ }
639
+ }
640
+ function initializeDebugMode() {
641
+ const globals = getGlobal();
642
+ const debugState = getDebugState();
643
+ // Set to true if this function has been called, whether or not
644
+ // it enabled debug mode.
645
+ debugState.initialized = true;
646
+ if (typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== 'string' &&
647
+ globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== true) {
648
+ return;
649
+ }
650
+ debugState.enabled = true;
651
+ const deferredToken = new Deferred();
652
+ debugState.token = deferredToken;
653
+ if (typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN === 'string') {
654
+ deferredToken.resolve(globals.FIREBASE_APPCHECK_DEBUG_TOKEN);
655
+ }
656
+ else {
657
+ deferredToken.resolve(readOrCreateDebugTokenFromStorage());
658
+ }
659
659
  }
660
660
 
661
- /**
662
- * @license
663
- * Copyright 2020 Google LLC
664
- *
665
- * Licensed under the Apache License, Version 2.0 (the "License");
666
- * you may not use this file except in compliance with the License.
667
- * You may obtain a copy of the License at
668
- *
669
- * http://www.apache.org/licenses/LICENSE-2.0
670
- *
671
- * Unless required by applicable law or agreed to in writing, software
672
- * distributed under the License is distributed on an "AS IS" BASIS,
673
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
674
- * See the License for the specific language governing permissions and
675
- * limitations under the License.
676
- */
677
- // Initial hardcoded value agreed upon across platforms for initial launch.
678
- // Format left open for possible dynamic error values and other fields in the future.
679
- const defaultTokenErrorData = { error: 'UNKNOWN_ERROR' };
680
- /**
681
- * Stringify and base64 encode token error data.
682
- *
683
- * @param tokenError Error data, currently hardcoded.
684
- */
685
- function formatDummyToken(tokenErrorData) {
686
- return base64.encodeString(JSON.stringify(tokenErrorData),
687
- /* webSafe= */ false);
688
- }
689
- /**
690
- * This function always resolves.
691
- * The result will contain an error field if there is any error.
692
- * In case there is an error, the token field in the result will be populated with a dummy value
693
- */
694
- async function getToken$2(appCheck, forceRefresh = false) {
695
- const app = appCheck.app;
696
- ensureActivated(app);
697
- const state = getStateReference(app);
698
- /**
699
- * First check if there is a token in memory from a previous `getToken()` call.
700
- */
701
- let token = state.token;
702
- let error = undefined;
703
- /**
704
- * If an invalid token was found in memory, clear token from
705
- * memory and unset the local variable `token`.
706
- */
707
- if (token && !isValid(token)) {
708
- state.token = undefined;
709
- token = undefined;
710
- }
711
- /**
712
- * If there is no valid token in memory, try to load token from indexedDB.
713
- */
714
- if (!token) {
715
- // cachedTokenPromise contains the token found in IndexedDB or undefined if not found.
716
- const cachedToken = await state.cachedTokenPromise;
717
- if (cachedToken) {
718
- if (isValid(cachedToken)) {
719
- token = cachedToken;
720
- }
721
- else {
722
- // If there was an invalid token in the indexedDB cache, clear it.
723
- await writeTokenToStorage(app, undefined);
724
- }
725
- }
726
- }
727
- // Return the cached token (from either memory or indexedDB) if it's valid
728
- if (!forceRefresh && token && isValid(token)) {
729
- return {
730
- token: token.token
731
- };
732
- }
733
- // Only set to true if this `getToken()` call is making the actual
734
- // REST call to the exchange endpoint, versus waiting for an already
735
- // in-flight call (see debug and regular exchange endpoint paths below)
736
- let shouldCallListeners = false;
737
- /**
738
- * DEBUG MODE
739
- * If debug mode is set, and there is no cached token, fetch a new App
740
- * Check token using the debug token, and return it directly.
741
- */
742
- if (isDebugMode()) {
743
- // Avoid making another call to the exchange endpoint if one is in flight.
744
- if (!state.exchangeTokenPromise) {
745
- state.exchangeTokenPromise = exchangeToken(getExchangeDebugTokenRequest(app, await getDebugToken()), appCheck.heartbeatServiceProvider).finally(() => {
746
- // Clear promise when settled - either resolved or rejected.
747
- state.exchangeTokenPromise = undefined;
748
- });
749
- shouldCallListeners = true;
750
- }
751
- const tokenFromDebugExchange = await state.exchangeTokenPromise;
752
- // Write debug token to indexedDB.
753
- await writeTokenToStorage(app, tokenFromDebugExchange);
754
- // Write debug token to state.
755
- state.token = tokenFromDebugExchange;
756
- return { token: tokenFromDebugExchange.token };
757
- }
758
- /**
759
- * There are no valid tokens in memory or indexedDB and we are not in
760
- * debug mode.
761
- * Request a new token from the exchange endpoint.
762
- */
763
- try {
764
- // Avoid making another call to the exchange endpoint if one is in flight.
765
- if (!state.exchangeTokenPromise) {
766
- // state.provider is populated in initializeAppCheck()
767
- // ensureActivated() at the top of this function checks that
768
- // initializeAppCheck() has been called.
769
- state.exchangeTokenPromise = state.provider.getToken().finally(() => {
770
- // Clear promise when settled - either resolved or rejected.
771
- state.exchangeTokenPromise = undefined;
772
- });
773
- shouldCallListeners = true;
774
- }
775
- token = await getStateReference(app).exchangeTokenPromise;
776
- }
777
- catch (e) {
778
- if (e.code === `appCheck/${"throttled" /* AppCheckError.THROTTLED */}`) {
779
- // Warn if throttled, but do not treat it as an error.
780
- logger.warn(e.message);
781
- }
782
- else {
783
- // `getToken()` should never throw, but logging error text to console will aid debugging.
784
- logger.error(e);
785
- }
786
- // Always save error to be added to dummy token.
787
- error = e;
788
- }
789
- let interopTokenResult;
790
- if (!token) {
791
- // If token is undefined, there must be an error.
792
- // Return a dummy token along with the error.
793
- interopTokenResult = makeDummyTokenResult(error);
794
- }
795
- else if (error) {
796
- if (isValid(token)) {
797
- // It's also possible a valid token exists, but there's also an error.
798
- // (Such as if the token is almost expired, tries to refresh, and
799
- // the exchange request fails.)
800
- // We add a special error property here so that the refresher will
801
- // count this as a failed attempt and use the backoff instead of
802
- // retrying repeatedly with no delay, but any 3P listeners will not
803
- // be hindered in getting the still-valid token.
804
- interopTokenResult = {
805
- token: token.token,
806
- internalError: error
807
- };
808
- }
809
- else {
810
- // No invalid tokens should make it to this step. Memory and cached tokens
811
- // are checked. Other tokens are from fresh exchanges. But just in case.
812
- interopTokenResult = makeDummyTokenResult(error);
813
- }
814
- }
815
- else {
816
- interopTokenResult = {
817
- token: token.token
818
- };
819
- // write the new token to the memory state as well as the persistent storage.
820
- // Only do it if we got a valid new token
821
- state.token = token;
822
- await writeTokenToStorage(app, token);
823
- }
824
- if (shouldCallListeners) {
825
- notifyTokenListeners(app, interopTokenResult);
826
- }
827
- return interopTokenResult;
828
- }
829
- /**
830
- * Internal API for limited use tokens. Skips all FAC state and simply calls
831
- * the underlying provider.
832
- */
833
- async function getLimitedUseToken$1(appCheck) {
834
- const app = appCheck.app;
835
- ensureActivated(app);
836
- const { provider } = getStateReference(app);
837
- if (isDebugMode()) {
838
- const debugToken = await getDebugToken();
839
- const { token } = await exchangeToken(getExchangeDebugTokenRequest(app, debugToken), appCheck.heartbeatServiceProvider);
840
- return { token };
841
- }
842
- else {
843
- // provider is definitely valid since we ensure AppCheck was activated
844
- const { token } = await provider.getToken();
845
- return { token };
846
- }
847
- }
848
- function addTokenListener(appCheck, type, listener, onError) {
849
- const { app } = appCheck;
850
- const state = getStateReference(app);
851
- const tokenObserver = {
852
- next: listener,
853
- error: onError,
854
- type
855
- };
856
- state.tokenObservers = [...state.tokenObservers, tokenObserver];
857
- // Invoke the listener async immediately if there is a valid token
858
- // in memory.
859
- if (state.token && isValid(state.token)) {
860
- const validToken = state.token;
861
- Promise.resolve()
862
- .then(() => {
863
- listener({ token: validToken.token });
864
- initTokenRefresher(appCheck);
865
- })
866
- .catch(() => {
867
- /* we don't care about exceptions thrown in listeners */
868
- });
869
- }
870
- /**
871
- * Wait for any cached token promise to resolve before starting the token
872
- * refresher. The refresher checks to see if there is an existing token
873
- * in state and calls the exchange endpoint if not. We should first let the
874
- * IndexedDB check have a chance to populate state if it can.
875
- *
876
- * Listener call isn't needed here because cachedTokenPromise will call any
877
- * listeners that exist when it resolves.
878
- */
879
- // state.cachedTokenPromise is always populated in `activate()`.
880
- void state.cachedTokenPromise.then(() => initTokenRefresher(appCheck));
881
- }
882
- function removeTokenListener(app, listener) {
883
- const state = getStateReference(app);
884
- const newObservers = state.tokenObservers.filter(tokenObserver => tokenObserver.next !== listener);
885
- if (newObservers.length === 0 &&
886
- state.tokenRefresher &&
887
- state.tokenRefresher.isRunning()) {
888
- state.tokenRefresher.stop();
889
- }
890
- state.tokenObservers = newObservers;
891
- }
892
- /**
893
- * Logic to create and start refresher as needed.
894
- */
895
- function initTokenRefresher(appCheck) {
896
- const { app } = appCheck;
897
- const state = getStateReference(app);
898
- // Create the refresher but don't start it if `isTokenAutoRefreshEnabled`
899
- // is not true.
900
- let refresher = state.tokenRefresher;
901
- if (!refresher) {
902
- refresher = createTokenRefresher(appCheck);
903
- state.tokenRefresher = refresher;
904
- }
905
- if (!refresher.isRunning() && state.isTokenAutoRefreshEnabled) {
906
- refresher.start();
907
- }
908
- }
909
- function createTokenRefresher(appCheck) {
910
- const { app } = appCheck;
911
- return new Refresher(
912
- // Keep in mind when this fails for any reason other than the ones
913
- // for which we should retry, it will effectively stop the proactive refresh.
914
- async () => {
915
- const state = getStateReference(app);
916
- // If there is no token, we will try to load it from storage and use it
917
- // If there is a token, we force refresh it because we know it's going to expire soon
918
- let result;
919
- if (!state.token) {
920
- result = await getToken$2(appCheck);
921
- }
922
- else {
923
- result = await getToken$2(appCheck, true);
924
- }
925
- /**
926
- * getToken() always resolves. In case the result has an error field defined, it means
927
- * the operation failed, and we should retry.
928
- */
929
- if (result.error) {
930
- throw result.error;
931
- }
932
- /**
933
- * A special `internalError` field reflects that there was an error
934
- * getting a new token from the exchange endpoint, but there's still a
935
- * previous token that's valid for now and this should be passed to 2P/3P
936
- * requests for a token. But we want this callback (`this.operation` in
937
- * `Refresher`) to throw in order to kick off the Refresher's retry
938
- * backoff. (Setting `hasSucceeded` to false.)
939
- */
940
- if (result.internalError) {
941
- throw result.internalError;
942
- }
943
- }, () => {
944
- return true;
945
- }, () => {
946
- const state = getStateReference(app);
947
- if (state.token) {
948
- // issuedAtTime + (50% * total TTL) + 5 minutes
949
- let nextRefreshTimeMillis = state.token.issuedAtTimeMillis +
950
- (state.token.expireTimeMillis - state.token.issuedAtTimeMillis) *
951
- 0.5 +
952
- 5 * 60 * 1000;
953
- // Do not allow refresh time to be past (expireTime - 5 minutes)
954
- const latestAllowableRefresh = state.token.expireTimeMillis - 5 * 60 * 1000;
955
- nextRefreshTimeMillis = Math.min(nextRefreshTimeMillis, latestAllowableRefresh);
956
- return Math.max(0, nextRefreshTimeMillis - Date.now());
957
- }
958
- else {
959
- return 0;
960
- }
961
- }, TOKEN_REFRESH_TIME.RETRIAL_MIN_WAIT, TOKEN_REFRESH_TIME.RETRIAL_MAX_WAIT);
962
- }
963
- function notifyTokenListeners(app, token) {
964
- const observers = getStateReference(app).tokenObservers;
965
- for (const observer of observers) {
966
- try {
967
- if (observer.type === "EXTERNAL" /* ListenerType.EXTERNAL */ && token.error != null) {
968
- // If this listener was added by a 3P call, send any token error to
969
- // the supplied error handler. A 3P observer always has an error
970
- // handler.
971
- observer.error(token.error);
972
- }
973
- else {
974
- // If the token has no error field, always return the token.
975
- // If this is a 2P listener, return the token, whether or not it
976
- // has an error field.
977
- observer.next(token);
978
- }
979
- }
980
- catch (e) {
981
- // Errors in the listener function itself are always ignored.
982
- }
983
- }
984
- }
985
- function isValid(token) {
986
- return token.expireTimeMillis - Date.now() > 0;
987
- }
988
- function makeDummyTokenResult(error) {
989
- return {
990
- token: formatDummyToken(defaultTokenErrorData),
991
- error
992
- };
661
+ /**
662
+ * @license
663
+ * Copyright 2020 Google LLC
664
+ *
665
+ * Licensed under the Apache License, Version 2.0 (the "License");
666
+ * you may not use this file except in compliance with the License.
667
+ * You may obtain a copy of the License at
668
+ *
669
+ * http://www.apache.org/licenses/LICENSE-2.0
670
+ *
671
+ * Unless required by applicable law or agreed to in writing, software
672
+ * distributed under the License is distributed on an "AS IS" BASIS,
673
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
674
+ * See the License for the specific language governing permissions and
675
+ * limitations under the License.
676
+ */
677
+ // Initial hardcoded value agreed upon across platforms for initial launch.
678
+ // Format left open for possible dynamic error values and other fields in the future.
679
+ const defaultTokenErrorData = { error: 'UNKNOWN_ERROR' };
680
+ /**
681
+ * Stringify and base64 encode token error data.
682
+ *
683
+ * @param tokenError Error data, currently hardcoded.
684
+ */
685
+ function formatDummyToken(tokenErrorData) {
686
+ return base64.encodeString(JSON.stringify(tokenErrorData),
687
+ /* webSafe= */ false);
688
+ }
689
+ /**
690
+ * This function always resolves.
691
+ * The result will contain an error field if there is any error.
692
+ * In case there is an error, the token field in the result will be populated with a dummy value
693
+ */
694
+ async function getToken$2(appCheck, forceRefresh = false) {
695
+ const app = appCheck.app;
696
+ ensureActivated(app);
697
+ const state = getStateReference(app);
698
+ /**
699
+ * First check if there is a token in memory from a previous `getToken()` call.
700
+ */
701
+ let token = state.token;
702
+ let error = undefined;
703
+ /**
704
+ * If an invalid token was found in memory, clear token from
705
+ * memory and unset the local variable `token`.
706
+ */
707
+ if (token && !isValid(token)) {
708
+ state.token = undefined;
709
+ token = undefined;
710
+ }
711
+ /**
712
+ * If there is no valid token in memory, try to load token from indexedDB.
713
+ */
714
+ if (!token) {
715
+ // cachedTokenPromise contains the token found in IndexedDB or undefined if not found.
716
+ const cachedToken = await state.cachedTokenPromise;
717
+ if (cachedToken) {
718
+ if (isValid(cachedToken)) {
719
+ token = cachedToken;
720
+ }
721
+ else {
722
+ // If there was an invalid token in the indexedDB cache, clear it.
723
+ await writeTokenToStorage(app, undefined);
724
+ }
725
+ }
726
+ }
727
+ // Return the cached token (from either memory or indexedDB) if it's valid
728
+ if (!forceRefresh && token && isValid(token)) {
729
+ return {
730
+ token: token.token
731
+ };
732
+ }
733
+ // Only set to true if this `getToken()` call is making the actual
734
+ // REST call to the exchange endpoint, versus waiting for an already
735
+ // in-flight call (see debug and regular exchange endpoint paths below)
736
+ let shouldCallListeners = false;
737
+ /**
738
+ * DEBUG MODE
739
+ * If debug mode is set, and there is no cached token, fetch a new App
740
+ * Check token using the debug token, and return it directly.
741
+ */
742
+ if (isDebugMode()) {
743
+ // Avoid making another call to the exchange endpoint if one is in flight.
744
+ if (!state.exchangeTokenPromise) {
745
+ state.exchangeTokenPromise = exchangeToken(getExchangeDebugTokenRequest(app, await getDebugToken()), appCheck.heartbeatServiceProvider).finally(() => {
746
+ // Clear promise when settled - either resolved or rejected.
747
+ state.exchangeTokenPromise = undefined;
748
+ });
749
+ shouldCallListeners = true;
750
+ }
751
+ const tokenFromDebugExchange = await state.exchangeTokenPromise;
752
+ // Write debug token to indexedDB.
753
+ await writeTokenToStorage(app, tokenFromDebugExchange);
754
+ // Write debug token to state.
755
+ state.token = tokenFromDebugExchange;
756
+ return { token: tokenFromDebugExchange.token };
757
+ }
758
+ /**
759
+ * There are no valid tokens in memory or indexedDB and we are not in
760
+ * debug mode.
761
+ * Request a new token from the exchange endpoint.
762
+ */
763
+ try {
764
+ // Avoid making another call to the exchange endpoint if one is in flight.
765
+ if (!state.exchangeTokenPromise) {
766
+ // state.provider is populated in initializeAppCheck()
767
+ // ensureActivated() at the top of this function checks that
768
+ // initializeAppCheck() has been called.
769
+ state.exchangeTokenPromise = state.provider.getToken().finally(() => {
770
+ // Clear promise when settled - either resolved or rejected.
771
+ state.exchangeTokenPromise = undefined;
772
+ });
773
+ shouldCallListeners = true;
774
+ }
775
+ token = await getStateReference(app).exchangeTokenPromise;
776
+ }
777
+ catch (e) {
778
+ if (e.code === `appCheck/${"throttled" /* AppCheckError.THROTTLED */}`) {
779
+ // Warn if throttled, but do not treat it as an error.
780
+ logger.warn(e.message);
781
+ }
782
+ else {
783
+ // `getToken()` should never throw, but logging error text to console will aid debugging.
784
+ logger.error(e);
785
+ }
786
+ // Always save error to be added to dummy token.
787
+ error = e;
788
+ }
789
+ let interopTokenResult;
790
+ if (!token) {
791
+ // If token is undefined, there must be an error.
792
+ // Return a dummy token along with the error.
793
+ interopTokenResult = makeDummyTokenResult(error);
794
+ }
795
+ else if (error) {
796
+ if (isValid(token)) {
797
+ // It's also possible a valid token exists, but there's also an error.
798
+ // (Such as if the token is almost expired, tries to refresh, and
799
+ // the exchange request fails.)
800
+ // We add a special error property here so that the refresher will
801
+ // count this as a failed attempt and use the backoff instead of
802
+ // retrying repeatedly with no delay, but any 3P listeners will not
803
+ // be hindered in getting the still-valid token.
804
+ interopTokenResult = {
805
+ token: token.token,
806
+ internalError: error
807
+ };
808
+ }
809
+ else {
810
+ // No invalid tokens should make it to this step. Memory and cached tokens
811
+ // are checked. Other tokens are from fresh exchanges. But just in case.
812
+ interopTokenResult = makeDummyTokenResult(error);
813
+ }
814
+ }
815
+ else {
816
+ interopTokenResult = {
817
+ token: token.token
818
+ };
819
+ // write the new token to the memory state as well as the persistent storage.
820
+ // Only do it if we got a valid new token
821
+ state.token = token;
822
+ await writeTokenToStorage(app, token);
823
+ }
824
+ if (shouldCallListeners) {
825
+ notifyTokenListeners(app, interopTokenResult);
826
+ }
827
+ return interopTokenResult;
828
+ }
829
+ /**
830
+ * Internal API for limited use tokens. Skips all FAC state and simply calls
831
+ * the underlying provider.
832
+ */
833
+ async function getLimitedUseToken$1(appCheck) {
834
+ const app = appCheck.app;
835
+ ensureActivated(app);
836
+ const { provider } = getStateReference(app);
837
+ if (isDebugMode()) {
838
+ const debugToken = await getDebugToken();
839
+ const { token } = await exchangeToken(getExchangeDebugTokenRequest(app, debugToken), appCheck.heartbeatServiceProvider);
840
+ return { token };
841
+ }
842
+ else {
843
+ // provider is definitely valid since we ensure AppCheck was activated
844
+ const { token } = await provider.getToken();
845
+ return { token };
846
+ }
847
+ }
848
+ function addTokenListener(appCheck, type, listener, onError) {
849
+ const { app } = appCheck;
850
+ const state = getStateReference(app);
851
+ const tokenObserver = {
852
+ next: listener,
853
+ error: onError,
854
+ type
855
+ };
856
+ state.tokenObservers = [...state.tokenObservers, tokenObserver];
857
+ // Invoke the listener async immediately if there is a valid token
858
+ // in memory.
859
+ if (state.token && isValid(state.token)) {
860
+ const validToken = state.token;
861
+ Promise.resolve()
862
+ .then(() => {
863
+ listener({ token: validToken.token });
864
+ initTokenRefresher(appCheck);
865
+ })
866
+ .catch(() => {
867
+ /* we don't care about exceptions thrown in listeners */
868
+ });
869
+ }
870
+ /**
871
+ * Wait for any cached token promise to resolve before starting the token
872
+ * refresher. The refresher checks to see if there is an existing token
873
+ * in state and calls the exchange endpoint if not. We should first let the
874
+ * IndexedDB check have a chance to populate state if it can.
875
+ *
876
+ * Listener call isn't needed here because cachedTokenPromise will call any
877
+ * listeners that exist when it resolves.
878
+ */
879
+ // state.cachedTokenPromise is always populated in `activate()`.
880
+ void state.cachedTokenPromise.then(() => initTokenRefresher(appCheck));
881
+ }
882
+ function removeTokenListener(app, listener) {
883
+ const state = getStateReference(app);
884
+ const newObservers = state.tokenObservers.filter(tokenObserver => tokenObserver.next !== listener);
885
+ if (newObservers.length === 0 &&
886
+ state.tokenRefresher &&
887
+ state.tokenRefresher.isRunning()) {
888
+ state.tokenRefresher.stop();
889
+ }
890
+ state.tokenObservers = newObservers;
891
+ }
892
+ /**
893
+ * Logic to create and start refresher as needed.
894
+ */
895
+ function initTokenRefresher(appCheck) {
896
+ const { app } = appCheck;
897
+ const state = getStateReference(app);
898
+ // Create the refresher but don't start it if `isTokenAutoRefreshEnabled`
899
+ // is not true.
900
+ let refresher = state.tokenRefresher;
901
+ if (!refresher) {
902
+ refresher = createTokenRefresher(appCheck);
903
+ state.tokenRefresher = refresher;
904
+ }
905
+ if (!refresher.isRunning() && state.isTokenAutoRefreshEnabled) {
906
+ refresher.start();
907
+ }
908
+ }
909
+ function createTokenRefresher(appCheck) {
910
+ const { app } = appCheck;
911
+ return new Refresher(
912
+ // Keep in mind when this fails for any reason other than the ones
913
+ // for which we should retry, it will effectively stop the proactive refresh.
914
+ async () => {
915
+ const state = getStateReference(app);
916
+ // If there is no token, we will try to load it from storage and use it
917
+ // If there is a token, we force refresh it because we know it's going to expire soon
918
+ let result;
919
+ if (!state.token) {
920
+ result = await getToken$2(appCheck);
921
+ }
922
+ else {
923
+ result = await getToken$2(appCheck, true);
924
+ }
925
+ /**
926
+ * getToken() always resolves. In case the result has an error field defined, it means
927
+ * the operation failed, and we should retry.
928
+ */
929
+ if (result.error) {
930
+ throw result.error;
931
+ }
932
+ /**
933
+ * A special `internalError` field reflects that there was an error
934
+ * getting a new token from the exchange endpoint, but there's still a
935
+ * previous token that's valid for now and this should be passed to 2P/3P
936
+ * requests for a token. But we want this callback (`this.operation` in
937
+ * `Refresher`) to throw in order to kick off the Refresher's retry
938
+ * backoff. (Setting `hasSucceeded` to false.)
939
+ */
940
+ if (result.internalError) {
941
+ throw result.internalError;
942
+ }
943
+ }, () => {
944
+ return true;
945
+ }, () => {
946
+ const state = getStateReference(app);
947
+ if (state.token) {
948
+ // issuedAtTime + (50% * total TTL) + 5 minutes
949
+ let nextRefreshTimeMillis = state.token.issuedAtTimeMillis +
950
+ (state.token.expireTimeMillis - state.token.issuedAtTimeMillis) *
951
+ 0.5 +
952
+ 5 * 60 * 1000;
953
+ // Do not allow refresh time to be past (expireTime - 5 minutes)
954
+ const latestAllowableRefresh = state.token.expireTimeMillis - 5 * 60 * 1000;
955
+ nextRefreshTimeMillis = Math.min(nextRefreshTimeMillis, latestAllowableRefresh);
956
+ return Math.max(0, nextRefreshTimeMillis - Date.now());
957
+ }
958
+ else {
959
+ return 0;
960
+ }
961
+ }, TOKEN_REFRESH_TIME.RETRIAL_MIN_WAIT, TOKEN_REFRESH_TIME.RETRIAL_MAX_WAIT);
962
+ }
963
+ function notifyTokenListeners(app, token) {
964
+ const observers = getStateReference(app).tokenObservers;
965
+ for (const observer of observers) {
966
+ try {
967
+ if (observer.type === "EXTERNAL" /* ListenerType.EXTERNAL */ && token.error != null) {
968
+ // If this listener was added by a 3P call, send any token error to
969
+ // the supplied error handler. A 3P observer always has an error
970
+ // handler.
971
+ observer.error(token.error);
972
+ }
973
+ else {
974
+ // If the token has no error field, always return the token.
975
+ // If this is a 2P listener, return the token, whether or not it
976
+ // has an error field.
977
+ observer.next(token);
978
+ }
979
+ }
980
+ catch (e) {
981
+ // Errors in the listener function itself are always ignored.
982
+ }
983
+ }
984
+ }
985
+ function isValid(token) {
986
+ return token.expireTimeMillis - Date.now() > 0;
987
+ }
988
+ function makeDummyTokenResult(error) {
989
+ return {
990
+ token: formatDummyToken(defaultTokenErrorData),
991
+ error
992
+ };
993
993
  }
994
994
 
995
- /**
996
- * @license
997
- * Copyright 2020 Google LLC
998
- *
999
- * Licensed under the Apache License, Version 2.0 (the "License");
1000
- * you may not use this file except in compliance with the License.
1001
- * You may obtain a copy of the License at
1002
- *
1003
- * http://www.apache.org/licenses/LICENSE-2.0
1004
- *
1005
- * Unless required by applicable law or agreed to in writing, software
1006
- * distributed under the License is distributed on an "AS IS" BASIS,
1007
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1008
- * See the License for the specific language governing permissions and
1009
- * limitations under the License.
1010
- */
1011
- /**
1012
- * AppCheck Service class.
1013
- */
1014
- class AppCheckService {
1015
- constructor(app, heartbeatServiceProvider) {
1016
- this.app = app;
1017
- this.heartbeatServiceProvider = heartbeatServiceProvider;
1018
- }
1019
- _delete() {
1020
- const { tokenObservers } = getStateReference(this.app);
1021
- for (const tokenObserver of tokenObservers) {
1022
- removeTokenListener(this.app, tokenObserver.next);
1023
- }
1024
- return Promise.resolve();
1025
- }
1026
- }
1027
- function factory(app, heartbeatServiceProvider) {
1028
- return new AppCheckService(app, heartbeatServiceProvider);
1029
- }
1030
- function internalFactory(appCheck) {
1031
- return {
1032
- getToken: forceRefresh => getToken$2(appCheck, forceRefresh),
1033
- getLimitedUseToken: () => getLimitedUseToken$1(appCheck),
1034
- addTokenListener: listener => addTokenListener(appCheck, "INTERNAL" /* ListenerType.INTERNAL */, listener),
1035
- removeTokenListener: listener => removeTokenListener(appCheck.app, listener)
1036
- };
995
+ /**
996
+ * @license
997
+ * Copyright 2020 Google LLC
998
+ *
999
+ * Licensed under the Apache License, Version 2.0 (the "License");
1000
+ * you may not use this file except in compliance with the License.
1001
+ * You may obtain a copy of the License at
1002
+ *
1003
+ * http://www.apache.org/licenses/LICENSE-2.0
1004
+ *
1005
+ * Unless required by applicable law or agreed to in writing, software
1006
+ * distributed under the License is distributed on an "AS IS" BASIS,
1007
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1008
+ * See the License for the specific language governing permissions and
1009
+ * limitations under the License.
1010
+ */
1011
+ /**
1012
+ * AppCheck Service class.
1013
+ */
1014
+ class AppCheckService {
1015
+ constructor(app, heartbeatServiceProvider) {
1016
+ this.app = app;
1017
+ this.heartbeatServiceProvider = heartbeatServiceProvider;
1018
+ }
1019
+ _delete() {
1020
+ const { tokenObservers } = getStateReference(this.app);
1021
+ for (const tokenObserver of tokenObservers) {
1022
+ removeTokenListener(this.app, tokenObserver.next);
1023
+ }
1024
+ return Promise.resolve();
1025
+ }
1026
+ }
1027
+ function factory(app, heartbeatServiceProvider) {
1028
+ return new AppCheckService(app, heartbeatServiceProvider);
1029
+ }
1030
+ function internalFactory(appCheck) {
1031
+ return {
1032
+ getToken: forceRefresh => getToken$2(appCheck, forceRefresh),
1033
+ getLimitedUseToken: () => getLimitedUseToken$1(appCheck),
1034
+ addTokenListener: listener => addTokenListener(appCheck, "INTERNAL" /* ListenerType.INTERNAL */, listener),
1035
+ removeTokenListener: listener => removeTokenListener(appCheck.app, listener)
1036
+ };
1037
1037
  }
1038
1038
 
1039
1039
  const name = "@firebase/app-check";
1040
- const version = "0.8.9";
1040
+ const version = "0.8.10";
1041
1041
 
1042
- /**
1043
- * @license
1044
- * Copyright 2020 Google LLC
1045
- *
1046
- * Licensed under the Apache License, Version 2.0 (the "License");
1047
- * you may not use this file except in compliance with the License.
1048
- * You may obtain a copy of the License at
1049
- *
1050
- * http://www.apache.org/licenses/LICENSE-2.0
1051
- *
1052
- * Unless required by applicable law or agreed to in writing, software
1053
- * distributed under the License is distributed on an "AS IS" BASIS,
1054
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1055
- * See the License for the specific language governing permissions and
1056
- * limitations under the License.
1057
- */
1058
- const RECAPTCHA_URL = 'https://www.google.com/recaptcha/api.js';
1059
- const RECAPTCHA_ENTERPRISE_URL = 'https://www.google.com/recaptcha/enterprise.js';
1060
- function initializeV3(app, siteKey) {
1061
- const initialized = new Deferred();
1062
- const state = getStateReference(app);
1063
- state.reCAPTCHAState = { initialized };
1064
- const divId = makeDiv(app);
1065
- const grecaptcha = getRecaptcha(false);
1066
- if (!grecaptcha) {
1067
- loadReCAPTCHAV3Script(() => {
1068
- const grecaptcha = getRecaptcha(false);
1069
- if (!grecaptcha) {
1070
- // it shouldn't happen.
1071
- throw new Error('no recaptcha');
1072
- }
1073
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1074
- });
1075
- }
1076
- else {
1077
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1078
- }
1079
- return initialized.promise;
1080
- }
1081
- function initializeEnterprise(app, siteKey) {
1082
- const initialized = new Deferred();
1083
- const state = getStateReference(app);
1084
- state.reCAPTCHAState = { initialized };
1085
- const divId = makeDiv(app);
1086
- const grecaptcha = getRecaptcha(true);
1087
- if (!grecaptcha) {
1088
- loadReCAPTCHAEnterpriseScript(() => {
1089
- const grecaptcha = getRecaptcha(true);
1090
- if (!grecaptcha) {
1091
- // it shouldn't happen.
1092
- throw new Error('no recaptcha');
1093
- }
1094
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1095
- });
1096
- }
1097
- else {
1098
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1099
- }
1100
- return initialized.promise;
1101
- }
1102
- /**
1103
- * Add listener to render the widget and resolve the promise when
1104
- * the grecaptcha.ready() event fires.
1105
- */
1106
- function queueWidgetRender(app, siteKey, grecaptcha, container, initialized) {
1107
- grecaptcha.ready(() => {
1108
- // Invisible widgets allow us to set a different siteKey for each widget,
1109
- // so we use them to support multiple apps
1110
- renderInvisibleWidget(app, siteKey, grecaptcha, container);
1111
- initialized.resolve(grecaptcha);
1112
- });
1113
- }
1114
- /**
1115
- * Add invisible div to page.
1116
- */
1117
- function makeDiv(app) {
1118
- const divId = `fire_app_check_${app.name}`;
1119
- const invisibleDiv = document.createElement('div');
1120
- invisibleDiv.id = divId;
1121
- invisibleDiv.style.display = 'none';
1122
- document.body.appendChild(invisibleDiv);
1123
- return divId;
1124
- }
1125
- async function getToken$1(app) {
1126
- ensureActivated(app);
1127
- // ensureActivated() guarantees that reCAPTCHAState is set
1128
- const reCAPTCHAState = getStateReference(app).reCAPTCHAState;
1129
- const recaptcha = await reCAPTCHAState.initialized.promise;
1130
- return new Promise((resolve, _reject) => {
1131
- // Updated after initialization is complete.
1132
- const reCAPTCHAState = getStateReference(app).reCAPTCHAState;
1133
- recaptcha.ready(() => {
1134
- resolve(
1135
- // widgetId is guaranteed to be available if reCAPTCHAState.initialized.promise resolved.
1136
- recaptcha.execute(reCAPTCHAState.widgetId, {
1137
- action: 'fire_app_check'
1138
- }));
1139
- });
1140
- });
1141
- }
1142
- /**
1143
- *
1144
- * @param app
1145
- * @param container - Id of a HTML element.
1146
- */
1147
- function renderInvisibleWidget(app, siteKey, grecaptcha, container) {
1148
- const widgetId = grecaptcha.render(container, {
1149
- sitekey: siteKey,
1150
- size: 'invisible',
1151
- // Success callback - set state
1152
- callback: () => {
1153
- getStateReference(app).reCAPTCHAState.succeeded = true;
1154
- },
1155
- // Failure callback - set state
1156
- 'error-callback': () => {
1157
- getStateReference(app).reCAPTCHAState.succeeded = false;
1158
- }
1159
- });
1160
- const state = getStateReference(app);
1161
- state.reCAPTCHAState = Object.assign(Object.assign({}, state.reCAPTCHAState), { // state.reCAPTCHAState is set in the initialize()
1162
- widgetId });
1163
- }
1164
- function loadReCAPTCHAV3Script(onload) {
1165
- const script = document.createElement('script');
1166
- script.src = RECAPTCHA_URL;
1167
- script.onload = onload;
1168
- document.head.appendChild(script);
1169
- }
1170
- function loadReCAPTCHAEnterpriseScript(onload) {
1171
- const script = document.createElement('script');
1172
- script.src = RECAPTCHA_ENTERPRISE_URL;
1173
- script.onload = onload;
1174
- document.head.appendChild(script);
1042
+ /**
1043
+ * @license
1044
+ * Copyright 2020 Google LLC
1045
+ *
1046
+ * Licensed under the Apache License, Version 2.0 (the "License");
1047
+ * you may not use this file except in compliance with the License.
1048
+ * You may obtain a copy of the License at
1049
+ *
1050
+ * http://www.apache.org/licenses/LICENSE-2.0
1051
+ *
1052
+ * Unless required by applicable law or agreed to in writing, software
1053
+ * distributed under the License is distributed on an "AS IS" BASIS,
1054
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1055
+ * See the License for the specific language governing permissions and
1056
+ * limitations under the License.
1057
+ */
1058
+ const RECAPTCHA_URL = 'https://www.google.com/recaptcha/api.js';
1059
+ const RECAPTCHA_ENTERPRISE_URL = 'https://www.google.com/recaptcha/enterprise.js';
1060
+ function initializeV3(app, siteKey) {
1061
+ const initialized = new Deferred();
1062
+ const state = getStateReference(app);
1063
+ state.reCAPTCHAState = { initialized };
1064
+ const divId = makeDiv(app);
1065
+ const grecaptcha = getRecaptcha(false);
1066
+ if (!grecaptcha) {
1067
+ loadReCAPTCHAV3Script(() => {
1068
+ const grecaptcha = getRecaptcha(false);
1069
+ if (!grecaptcha) {
1070
+ // it shouldn't happen.
1071
+ throw new Error('no recaptcha');
1072
+ }
1073
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1074
+ });
1075
+ }
1076
+ else {
1077
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1078
+ }
1079
+ return initialized.promise;
1080
+ }
1081
+ function initializeEnterprise(app, siteKey) {
1082
+ const initialized = new Deferred();
1083
+ const state = getStateReference(app);
1084
+ state.reCAPTCHAState = { initialized };
1085
+ const divId = makeDiv(app);
1086
+ const grecaptcha = getRecaptcha(true);
1087
+ if (!grecaptcha) {
1088
+ loadReCAPTCHAEnterpriseScript(() => {
1089
+ const grecaptcha = getRecaptcha(true);
1090
+ if (!grecaptcha) {
1091
+ // it shouldn't happen.
1092
+ throw new Error('no recaptcha');
1093
+ }
1094
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1095
+ });
1096
+ }
1097
+ else {
1098
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1099
+ }
1100
+ return initialized.promise;
1101
+ }
1102
+ /**
1103
+ * Add listener to render the widget and resolve the promise when
1104
+ * the grecaptcha.ready() event fires.
1105
+ */
1106
+ function queueWidgetRender(app, siteKey, grecaptcha, container, initialized) {
1107
+ grecaptcha.ready(() => {
1108
+ // Invisible widgets allow us to set a different siteKey for each widget,
1109
+ // so we use them to support multiple apps
1110
+ renderInvisibleWidget(app, siteKey, grecaptcha, container);
1111
+ initialized.resolve(grecaptcha);
1112
+ });
1113
+ }
1114
+ /**
1115
+ * Add invisible div to page.
1116
+ */
1117
+ function makeDiv(app) {
1118
+ const divId = `fire_app_check_${app.name}`;
1119
+ const invisibleDiv = document.createElement('div');
1120
+ invisibleDiv.id = divId;
1121
+ invisibleDiv.style.display = 'none';
1122
+ document.body.appendChild(invisibleDiv);
1123
+ return divId;
1124
+ }
1125
+ async function getToken$1(app) {
1126
+ ensureActivated(app);
1127
+ // ensureActivated() guarantees that reCAPTCHAState is set
1128
+ const reCAPTCHAState = getStateReference(app).reCAPTCHAState;
1129
+ const recaptcha = await reCAPTCHAState.initialized.promise;
1130
+ return new Promise((resolve, _reject) => {
1131
+ // Updated after initialization is complete.
1132
+ const reCAPTCHAState = getStateReference(app).reCAPTCHAState;
1133
+ recaptcha.ready(() => {
1134
+ resolve(
1135
+ // widgetId is guaranteed to be available if reCAPTCHAState.initialized.promise resolved.
1136
+ recaptcha.execute(reCAPTCHAState.widgetId, {
1137
+ action: 'fire_app_check'
1138
+ }));
1139
+ });
1140
+ });
1141
+ }
1142
+ /**
1143
+ *
1144
+ * @param app
1145
+ * @param container - Id of a HTML element.
1146
+ */
1147
+ function renderInvisibleWidget(app, siteKey, grecaptcha, container) {
1148
+ const widgetId = grecaptcha.render(container, {
1149
+ sitekey: siteKey,
1150
+ size: 'invisible',
1151
+ // Success callback - set state
1152
+ callback: () => {
1153
+ getStateReference(app).reCAPTCHAState.succeeded = true;
1154
+ },
1155
+ // Failure callback - set state
1156
+ 'error-callback': () => {
1157
+ getStateReference(app).reCAPTCHAState.succeeded = false;
1158
+ }
1159
+ });
1160
+ const state = getStateReference(app);
1161
+ state.reCAPTCHAState = Object.assign(Object.assign({}, state.reCAPTCHAState), { // state.reCAPTCHAState is set in the initialize()
1162
+ widgetId });
1163
+ }
1164
+ function loadReCAPTCHAV3Script(onload) {
1165
+ const script = document.createElement('script');
1166
+ script.src = RECAPTCHA_URL;
1167
+ script.onload = onload;
1168
+ document.head.appendChild(script);
1169
+ }
1170
+ function loadReCAPTCHAEnterpriseScript(onload) {
1171
+ const script = document.createElement('script');
1172
+ script.src = RECAPTCHA_ENTERPRISE_URL;
1173
+ script.onload = onload;
1174
+ document.head.appendChild(script);
1175
1175
  }
1176
1176
 
1177
- /**
1178
- * @license
1179
- * Copyright 2021 Google LLC
1180
- *
1181
- * Licensed under the Apache License, Version 2.0 (the "License");
1182
- * you may not use this file except in compliance with the License.
1183
- * You may obtain a copy of the License at
1184
- *
1185
- * http://www.apache.org/licenses/LICENSE-2.0
1186
- *
1187
- * Unless required by applicable law or agreed to in writing, software
1188
- * distributed under the License is distributed on an "AS IS" BASIS,
1189
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1190
- * See the License for the specific language governing permissions and
1191
- * limitations under the License.
1192
- */
1193
- /**
1194
- * App Check provider that can obtain a reCAPTCHA V3 token and exchange it
1195
- * for an App Check token.
1196
- *
1197
- * @public
1198
- */
1199
- class ReCaptchaV3Provider {
1200
- /**
1201
- * Create a ReCaptchaV3Provider instance.
1202
- * @param siteKey - ReCAPTCHA V3 siteKey.
1203
- */
1204
- constructor(_siteKey) {
1205
- this._siteKey = _siteKey;
1206
- /**
1207
- * Throttle requests on certain error codes to prevent too many retries
1208
- * in a short time.
1209
- */
1210
- this._throttleData = null;
1211
- }
1212
- /**
1213
- * Returns an App Check token.
1214
- * @internal
1215
- */
1216
- async getToken() {
1217
- var _a, _b, _c;
1218
- throwIfThrottled(this._throttleData);
1219
- // Top-level `getToken()` has already checked that App Check is initialized
1220
- // and therefore this._app and this._heartbeatServiceProvider are available.
1221
- const attestedClaimsToken = await getToken$1(this._app).catch(_e => {
1222
- // reCaptcha.execute() throws null which is not very descriptive.
1223
- throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1224
- });
1225
- // Check if a failure state was set by the recaptcha "error-callback".
1226
- if (!((_a = getStateReference(this._app).reCAPTCHAState) === null || _a === void 0 ? void 0 : _a.succeeded)) {
1227
- throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1228
- }
1229
- let result;
1230
- try {
1231
- result = await exchangeToken(getExchangeRecaptchaV3TokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider);
1232
- }
1233
- catch (e) {
1234
- if ((_b = e.code) === null || _b === void 0 ? void 0 : _b.includes("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */)) {
1235
- this._throttleData = setBackoff(Number((_c = e.customData) === null || _c === void 0 ? void 0 : _c.httpStatus), this._throttleData);
1236
- throw ERROR_FACTORY.create("throttled" /* AppCheckError.THROTTLED */, {
1237
- time: getDurationString(this._throttleData.allowRequestsAfter - Date.now()),
1238
- httpStatus: this._throttleData.httpStatus
1239
- });
1240
- }
1241
- else {
1242
- throw e;
1243
- }
1244
- }
1245
- // If successful, clear throttle data.
1246
- this._throttleData = null;
1247
- return result;
1248
- }
1249
- /**
1250
- * @internal
1251
- */
1252
- initialize(app) {
1253
- this._app = app;
1254
- this._heartbeatServiceProvider = _getProvider(app, 'heartbeat');
1255
- initializeV3(app, this._siteKey).catch(() => {
1256
- /* we don't care about the initialization result */
1257
- });
1258
- }
1259
- /**
1260
- * @internal
1261
- */
1262
- isEqual(otherProvider) {
1263
- if (otherProvider instanceof ReCaptchaV3Provider) {
1264
- return this._siteKey === otherProvider._siteKey;
1265
- }
1266
- else {
1267
- return false;
1268
- }
1269
- }
1270
- }
1271
- /**
1272
- * App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it
1273
- * for an App Check token.
1274
- *
1275
- * @public
1276
- */
1277
- class ReCaptchaEnterpriseProvider {
1278
- /**
1279
- * Create a ReCaptchaEnterpriseProvider instance.
1280
- * @param siteKey - reCAPTCHA Enterprise score-based site key.
1281
- */
1282
- constructor(_siteKey) {
1283
- this._siteKey = _siteKey;
1284
- /**
1285
- * Throttle requests on certain error codes to prevent too many retries
1286
- * in a short time.
1287
- */
1288
- this._throttleData = null;
1289
- }
1290
- /**
1291
- * Returns an App Check token.
1292
- * @internal
1293
- */
1294
- async getToken() {
1295
- var _a, _b, _c;
1296
- throwIfThrottled(this._throttleData);
1297
- // Top-level `getToken()` has already checked that App Check is initialized
1298
- // and therefore this._app and this._heartbeatServiceProvider are available.
1299
- const attestedClaimsToken = await getToken$1(this._app).catch(_e => {
1300
- // reCaptcha.execute() throws null which is not very descriptive.
1301
- throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1302
- });
1303
- // Check if a failure state was set by the recaptcha "error-callback".
1304
- if (!((_a = getStateReference(this._app).reCAPTCHAState) === null || _a === void 0 ? void 0 : _a.succeeded)) {
1305
- throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1306
- }
1307
- let result;
1308
- try {
1309
- result = await exchangeToken(getExchangeRecaptchaEnterpriseTokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider);
1310
- }
1311
- catch (e) {
1312
- if ((_b = e.code) === null || _b === void 0 ? void 0 : _b.includes("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */)) {
1313
- this._throttleData = setBackoff(Number((_c = e.customData) === null || _c === void 0 ? void 0 : _c.httpStatus), this._throttleData);
1314
- throw ERROR_FACTORY.create("throttled" /* AppCheckError.THROTTLED */, {
1315
- time: getDurationString(this._throttleData.allowRequestsAfter - Date.now()),
1316
- httpStatus: this._throttleData.httpStatus
1317
- });
1318
- }
1319
- else {
1320
- throw e;
1321
- }
1322
- }
1323
- // If successful, clear throttle data.
1324
- this._throttleData = null;
1325
- return result;
1326
- }
1327
- /**
1328
- * @internal
1329
- */
1330
- initialize(app) {
1331
- this._app = app;
1332
- this._heartbeatServiceProvider = _getProvider(app, 'heartbeat');
1333
- initializeEnterprise(app, this._siteKey).catch(() => {
1334
- /* we don't care about the initialization result */
1335
- });
1336
- }
1337
- /**
1338
- * @internal
1339
- */
1340
- isEqual(otherProvider) {
1341
- if (otherProvider instanceof ReCaptchaEnterpriseProvider) {
1342
- return this._siteKey === otherProvider._siteKey;
1343
- }
1344
- else {
1345
- return false;
1346
- }
1347
- }
1348
- }
1349
- /**
1350
- * Custom provider class.
1351
- * @public
1352
- */
1353
- class CustomProvider {
1354
- constructor(_customProviderOptions) {
1355
- this._customProviderOptions = _customProviderOptions;
1356
- }
1357
- /**
1358
- * @internal
1359
- */
1360
- async getToken() {
1361
- // custom provider
1362
- const customToken = await this._customProviderOptions.getToken();
1363
- // Try to extract IAT from custom token, in case this token is not
1364
- // being newly issued. JWT timestamps are in seconds since epoch.
1365
- const issuedAtTimeSeconds = issuedAtTime(customToken.token);
1366
- // Very basic validation, use current timestamp as IAT if JWT
1367
- // has no `iat` field or value is out of bounds.
1368
- const issuedAtTimeMillis = issuedAtTimeSeconds !== null &&
1369
- issuedAtTimeSeconds < Date.now() &&
1370
- issuedAtTimeSeconds > 0
1371
- ? issuedAtTimeSeconds * 1000
1372
- : Date.now();
1373
- return Object.assign(Object.assign({}, customToken), { issuedAtTimeMillis });
1374
- }
1375
- /**
1376
- * @internal
1377
- */
1378
- initialize(app) {
1379
- this._app = app;
1380
- }
1381
- /**
1382
- * @internal
1383
- */
1384
- isEqual(otherProvider) {
1385
- if (otherProvider instanceof CustomProvider) {
1386
- return (this._customProviderOptions.getToken.toString() ===
1387
- otherProvider._customProviderOptions.getToken.toString());
1388
- }
1389
- else {
1390
- return false;
1391
- }
1392
- }
1393
- }
1394
- /**
1395
- * Set throttle data to block requests until after a certain time
1396
- * depending on the failed request's status code.
1397
- * @param httpStatus - Status code of failed request.
1398
- * @param throttleData - `ThrottleData` object containing previous throttle
1399
- * data state.
1400
- * @returns Data about current throttle state and expiration time.
1401
- */
1402
- function setBackoff(httpStatus, throttleData) {
1403
- /**
1404
- * Block retries for 1 day for the following error codes:
1405
- *
1406
- * 404: Likely malformed URL.
1407
- *
1408
- * 403:
1409
- * - Attestation failed
1410
- * - Wrong API key
1411
- * - Project deleted
1412
- */
1413
- if (httpStatus === 404 || httpStatus === 403) {
1414
- return {
1415
- backoffCount: 1,
1416
- allowRequestsAfter: Date.now() + ONE_DAY,
1417
- httpStatus
1418
- };
1419
- }
1420
- else {
1421
- /**
1422
- * For all other error codes, the time when it is ok to retry again
1423
- * is based on exponential backoff.
1424
- */
1425
- const backoffCount = throttleData ? throttleData.backoffCount : 0;
1426
- const backoffMillis = calculateBackoffMillis(backoffCount, 1000, 2);
1427
- return {
1428
- backoffCount: backoffCount + 1,
1429
- allowRequestsAfter: Date.now() + backoffMillis,
1430
- httpStatus
1431
- };
1432
- }
1433
- }
1434
- function throwIfThrottled(throttleData) {
1435
- if (throttleData) {
1436
- if (Date.now() - throttleData.allowRequestsAfter <= 0) {
1437
- // If before, throw.
1438
- throw ERROR_FACTORY.create("throttled" /* AppCheckError.THROTTLED */, {
1439
- time: getDurationString(throttleData.allowRequestsAfter - Date.now()),
1440
- httpStatus: throttleData.httpStatus
1441
- });
1442
- }
1443
- }
1177
+ /**
1178
+ * @license
1179
+ * Copyright 2021 Google LLC
1180
+ *
1181
+ * Licensed under the Apache License, Version 2.0 (the "License");
1182
+ * you may not use this file except in compliance with the License.
1183
+ * You may obtain a copy of the License at
1184
+ *
1185
+ * http://www.apache.org/licenses/LICENSE-2.0
1186
+ *
1187
+ * Unless required by applicable law or agreed to in writing, software
1188
+ * distributed under the License is distributed on an "AS IS" BASIS,
1189
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1190
+ * See the License for the specific language governing permissions and
1191
+ * limitations under the License.
1192
+ */
1193
+ /**
1194
+ * App Check provider that can obtain a reCAPTCHA V3 token and exchange it
1195
+ * for an App Check token.
1196
+ *
1197
+ * @public
1198
+ */
1199
+ class ReCaptchaV3Provider {
1200
+ /**
1201
+ * Create a ReCaptchaV3Provider instance.
1202
+ * @param siteKey - ReCAPTCHA V3 siteKey.
1203
+ */
1204
+ constructor(_siteKey) {
1205
+ this._siteKey = _siteKey;
1206
+ /**
1207
+ * Throttle requests on certain error codes to prevent too many retries
1208
+ * in a short time.
1209
+ */
1210
+ this._throttleData = null;
1211
+ }
1212
+ /**
1213
+ * Returns an App Check token.
1214
+ * @internal
1215
+ */
1216
+ async getToken() {
1217
+ var _a, _b, _c;
1218
+ throwIfThrottled(this._throttleData);
1219
+ // Top-level `getToken()` has already checked that App Check is initialized
1220
+ // and therefore this._app and this._heartbeatServiceProvider are available.
1221
+ const attestedClaimsToken = await getToken$1(this._app).catch(_e => {
1222
+ // reCaptcha.execute() throws null which is not very descriptive.
1223
+ throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1224
+ });
1225
+ // Check if a failure state was set by the recaptcha "error-callback".
1226
+ if (!((_a = getStateReference(this._app).reCAPTCHAState) === null || _a === void 0 ? void 0 : _a.succeeded)) {
1227
+ throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1228
+ }
1229
+ let result;
1230
+ try {
1231
+ result = await exchangeToken(getExchangeRecaptchaV3TokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider);
1232
+ }
1233
+ catch (e) {
1234
+ if ((_b = e.code) === null || _b === void 0 ? void 0 : _b.includes("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */)) {
1235
+ this._throttleData = setBackoff(Number((_c = e.customData) === null || _c === void 0 ? void 0 : _c.httpStatus), this._throttleData);
1236
+ throw ERROR_FACTORY.create("throttled" /* AppCheckError.THROTTLED */, {
1237
+ time: getDurationString(this._throttleData.allowRequestsAfter - Date.now()),
1238
+ httpStatus: this._throttleData.httpStatus
1239
+ });
1240
+ }
1241
+ else {
1242
+ throw e;
1243
+ }
1244
+ }
1245
+ // If successful, clear throttle data.
1246
+ this._throttleData = null;
1247
+ return result;
1248
+ }
1249
+ /**
1250
+ * @internal
1251
+ */
1252
+ initialize(app) {
1253
+ this._app = app;
1254
+ this._heartbeatServiceProvider = _getProvider(app, 'heartbeat');
1255
+ initializeV3(app, this._siteKey).catch(() => {
1256
+ /* we don't care about the initialization result */
1257
+ });
1258
+ }
1259
+ /**
1260
+ * @internal
1261
+ */
1262
+ isEqual(otherProvider) {
1263
+ if (otherProvider instanceof ReCaptchaV3Provider) {
1264
+ return this._siteKey === otherProvider._siteKey;
1265
+ }
1266
+ else {
1267
+ return false;
1268
+ }
1269
+ }
1270
+ }
1271
+ /**
1272
+ * App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it
1273
+ * for an App Check token.
1274
+ *
1275
+ * @public
1276
+ */
1277
+ class ReCaptchaEnterpriseProvider {
1278
+ /**
1279
+ * Create a ReCaptchaEnterpriseProvider instance.
1280
+ * @param siteKey - reCAPTCHA Enterprise score-based site key.
1281
+ */
1282
+ constructor(_siteKey) {
1283
+ this._siteKey = _siteKey;
1284
+ /**
1285
+ * Throttle requests on certain error codes to prevent too many retries
1286
+ * in a short time.
1287
+ */
1288
+ this._throttleData = null;
1289
+ }
1290
+ /**
1291
+ * Returns an App Check token.
1292
+ * @internal
1293
+ */
1294
+ async getToken() {
1295
+ var _a, _b, _c;
1296
+ throwIfThrottled(this._throttleData);
1297
+ // Top-level `getToken()` has already checked that App Check is initialized
1298
+ // and therefore this._app and this._heartbeatServiceProvider are available.
1299
+ const attestedClaimsToken = await getToken$1(this._app).catch(_e => {
1300
+ // reCaptcha.execute() throws null which is not very descriptive.
1301
+ throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1302
+ });
1303
+ // Check if a failure state was set by the recaptcha "error-callback".
1304
+ if (!((_a = getStateReference(this._app).reCAPTCHAState) === null || _a === void 0 ? void 0 : _a.succeeded)) {
1305
+ throw ERROR_FACTORY.create("recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */);
1306
+ }
1307
+ let result;
1308
+ try {
1309
+ result = await exchangeToken(getExchangeRecaptchaEnterpriseTokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider);
1310
+ }
1311
+ catch (e) {
1312
+ if ((_b = e.code) === null || _b === void 0 ? void 0 : _b.includes("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */)) {
1313
+ this._throttleData = setBackoff(Number((_c = e.customData) === null || _c === void 0 ? void 0 : _c.httpStatus), this._throttleData);
1314
+ throw ERROR_FACTORY.create("throttled" /* AppCheckError.THROTTLED */, {
1315
+ time: getDurationString(this._throttleData.allowRequestsAfter - Date.now()),
1316
+ httpStatus: this._throttleData.httpStatus
1317
+ });
1318
+ }
1319
+ else {
1320
+ throw e;
1321
+ }
1322
+ }
1323
+ // If successful, clear throttle data.
1324
+ this._throttleData = null;
1325
+ return result;
1326
+ }
1327
+ /**
1328
+ * @internal
1329
+ */
1330
+ initialize(app) {
1331
+ this._app = app;
1332
+ this._heartbeatServiceProvider = _getProvider(app, 'heartbeat');
1333
+ initializeEnterprise(app, this._siteKey).catch(() => {
1334
+ /* we don't care about the initialization result */
1335
+ });
1336
+ }
1337
+ /**
1338
+ * @internal
1339
+ */
1340
+ isEqual(otherProvider) {
1341
+ if (otherProvider instanceof ReCaptchaEnterpriseProvider) {
1342
+ return this._siteKey === otherProvider._siteKey;
1343
+ }
1344
+ else {
1345
+ return false;
1346
+ }
1347
+ }
1348
+ }
1349
+ /**
1350
+ * Custom provider class.
1351
+ * @public
1352
+ */
1353
+ class CustomProvider {
1354
+ constructor(_customProviderOptions) {
1355
+ this._customProviderOptions = _customProviderOptions;
1356
+ }
1357
+ /**
1358
+ * @internal
1359
+ */
1360
+ async getToken() {
1361
+ // custom provider
1362
+ const customToken = await this._customProviderOptions.getToken();
1363
+ // Try to extract IAT from custom token, in case this token is not
1364
+ // being newly issued. JWT timestamps are in seconds since epoch.
1365
+ const issuedAtTimeSeconds = issuedAtTime(customToken.token);
1366
+ // Very basic validation, use current timestamp as IAT if JWT
1367
+ // has no `iat` field or value is out of bounds.
1368
+ const issuedAtTimeMillis = issuedAtTimeSeconds !== null &&
1369
+ issuedAtTimeSeconds < Date.now() &&
1370
+ issuedAtTimeSeconds > 0
1371
+ ? issuedAtTimeSeconds * 1000
1372
+ : Date.now();
1373
+ return Object.assign(Object.assign({}, customToken), { issuedAtTimeMillis });
1374
+ }
1375
+ /**
1376
+ * @internal
1377
+ */
1378
+ initialize(app) {
1379
+ this._app = app;
1380
+ }
1381
+ /**
1382
+ * @internal
1383
+ */
1384
+ isEqual(otherProvider) {
1385
+ if (otherProvider instanceof CustomProvider) {
1386
+ return (this._customProviderOptions.getToken.toString() ===
1387
+ otherProvider._customProviderOptions.getToken.toString());
1388
+ }
1389
+ else {
1390
+ return false;
1391
+ }
1392
+ }
1393
+ }
1394
+ /**
1395
+ * Set throttle data to block requests until after a certain time
1396
+ * depending on the failed request's status code.
1397
+ * @param httpStatus - Status code of failed request.
1398
+ * @param throttleData - `ThrottleData` object containing previous throttle
1399
+ * data state.
1400
+ * @returns Data about current throttle state and expiration time.
1401
+ */
1402
+ function setBackoff(httpStatus, throttleData) {
1403
+ /**
1404
+ * Block retries for 1 day for the following error codes:
1405
+ *
1406
+ * 404: Likely malformed URL.
1407
+ *
1408
+ * 403:
1409
+ * - Attestation failed
1410
+ * - Wrong API key
1411
+ * - Project deleted
1412
+ */
1413
+ if (httpStatus === 404 || httpStatus === 403) {
1414
+ return {
1415
+ backoffCount: 1,
1416
+ allowRequestsAfter: Date.now() + ONE_DAY,
1417
+ httpStatus
1418
+ };
1419
+ }
1420
+ else {
1421
+ /**
1422
+ * For all other error codes, the time when it is ok to retry again
1423
+ * is based on exponential backoff.
1424
+ */
1425
+ const backoffCount = throttleData ? throttleData.backoffCount : 0;
1426
+ const backoffMillis = calculateBackoffMillis(backoffCount, 1000, 2);
1427
+ return {
1428
+ backoffCount: backoffCount + 1,
1429
+ allowRequestsAfter: Date.now() + backoffMillis,
1430
+ httpStatus
1431
+ };
1432
+ }
1433
+ }
1434
+ function throwIfThrottled(throttleData) {
1435
+ if (throttleData) {
1436
+ if (Date.now() - throttleData.allowRequestsAfter <= 0) {
1437
+ // If before, throw.
1438
+ throw ERROR_FACTORY.create("throttled" /* AppCheckError.THROTTLED */, {
1439
+ time: getDurationString(throttleData.allowRequestsAfter - Date.now()),
1440
+ httpStatus: throttleData.httpStatus
1441
+ });
1442
+ }
1443
+ }
1444
1444
  }
1445
1445
 
1446
- /**
1447
- * @license
1448
- * Copyright 2020 Google LLC
1449
- *
1450
- * Licensed under the Apache License, Version 2.0 (the "License");
1451
- * you may not use this file except in compliance with the License.
1452
- * You may obtain a copy of the License at
1453
- *
1454
- * http://www.apache.org/licenses/LICENSE-2.0
1455
- *
1456
- * Unless required by applicable law or agreed to in writing, software
1457
- * distributed under the License is distributed on an "AS IS" BASIS,
1458
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1459
- * See the License for the specific language governing permissions and
1460
- * limitations under the License.
1461
- */
1462
- /**
1463
- * Activate App Check for the given app. Can be called only once per app.
1464
- * @param app - the {@link @firebase/app#FirebaseApp} to activate App Check for
1465
- * @param options - App Check initialization options
1466
- * @public
1467
- */
1468
- function initializeAppCheck(app = getApp(), options) {
1469
- app = getModularInstance(app);
1470
- const provider = _getProvider(app, 'app-check');
1471
- // Ensure initializeDebugMode() is only called once.
1472
- if (!getDebugState().initialized) {
1473
- initializeDebugMode();
1474
- }
1475
- // Log a message containing the debug token when `initializeAppCheck()`
1476
- // is called in debug mode.
1477
- if (isDebugMode()) {
1478
- // Do not block initialization to get the token for the message.
1479
- void getDebugToken().then(token =>
1480
- // Not using logger because I don't think we ever want this accidentally hidden.
1481
- console.log(`App Check debug token: ${token}. You will need to add it to your app's App Check settings in the Firebase console for it to work.`));
1482
- }
1483
- if (provider.isInitialized()) {
1484
- const existingInstance = provider.getImmediate();
1485
- const initialOptions = provider.getOptions();
1486
- if (initialOptions.isTokenAutoRefreshEnabled ===
1487
- options.isTokenAutoRefreshEnabled &&
1488
- initialOptions.provider.isEqual(options.provider)) {
1489
- return existingInstance;
1490
- }
1491
- else {
1492
- throw ERROR_FACTORY.create("already-initialized" /* AppCheckError.ALREADY_INITIALIZED */, {
1493
- appName: app.name
1494
- });
1495
- }
1496
- }
1497
- const appCheck = provider.initialize({ options });
1498
- _activate(app, options.provider, options.isTokenAutoRefreshEnabled);
1499
- // If isTokenAutoRefreshEnabled is false, do not send any requests to the
1500
- // exchange endpoint without an explicit call from the user either directly
1501
- // or through another Firebase library (storage, functions, etc.)
1502
- if (getStateReference(app).isTokenAutoRefreshEnabled) {
1503
- // Adding a listener will start the refresher and fetch a token if needed.
1504
- // This gets a token ready and prevents a delay when an internal library
1505
- // requests the token.
1506
- // Listener function does not need to do anything, its base functionality
1507
- // of calling getToken() already fetches token and writes it to memory/storage.
1508
- addTokenListener(appCheck, "INTERNAL" /* ListenerType.INTERNAL */, () => { });
1509
- }
1510
- return appCheck;
1511
- }
1512
- /**
1513
- * Activate App Check
1514
- * @param app - Firebase app to activate App Check for.
1515
- * @param provider - reCAPTCHA v3 provider or
1516
- * custom token provider.
1517
- * @param isTokenAutoRefreshEnabled - If true, the SDK automatically
1518
- * refreshes App Check tokens as needed. If undefined, defaults to the
1519
- * value of `app.automaticDataCollectionEnabled`, which defaults to
1520
- * false and can be set in the app config.
1521
- */
1522
- function _activate(app, provider, isTokenAutoRefreshEnabled) {
1523
- // Create an entry in the APP_CHECK_STATES map. Further changes should
1524
- // directly mutate this object.
1525
- const state = setInitialState(app, Object.assign({}, DEFAULT_STATE));
1526
- state.activated = true;
1527
- state.provider = provider; // Read cached token from storage if it exists and store it in memory.
1528
- state.cachedTokenPromise = readTokenFromStorage(app).then(cachedToken => {
1529
- if (cachedToken && isValid(cachedToken)) {
1530
- state.token = cachedToken;
1531
- // notify all listeners with the cached token
1532
- notifyTokenListeners(app, { token: cachedToken.token });
1533
- }
1534
- return cachedToken;
1535
- });
1536
- // Use value of global `automaticDataCollectionEnabled` (which
1537
- // itself defaults to false if not specified in config) if
1538
- // `isTokenAutoRefreshEnabled` param was not provided by user.
1539
- state.isTokenAutoRefreshEnabled =
1540
- isTokenAutoRefreshEnabled === undefined
1541
- ? app.automaticDataCollectionEnabled
1542
- : isTokenAutoRefreshEnabled;
1543
- state.provider.initialize(app);
1544
- }
1545
- /**
1546
- * Set whether App Check will automatically refresh tokens as needed.
1547
- *
1548
- * @param appCheckInstance - The App Check service instance.
1549
- * @param isTokenAutoRefreshEnabled - If true, the SDK automatically
1550
- * refreshes App Check tokens as needed. This overrides any value set
1551
- * during `initializeAppCheck()`.
1552
- * @public
1553
- */
1554
- function setTokenAutoRefreshEnabled(appCheckInstance, isTokenAutoRefreshEnabled) {
1555
- const app = appCheckInstance.app;
1556
- const state = getStateReference(app);
1557
- // This will exist if any product libraries have called
1558
- // `addTokenListener()`
1559
- if (state.tokenRefresher) {
1560
- if (isTokenAutoRefreshEnabled === true) {
1561
- state.tokenRefresher.start();
1562
- }
1563
- else {
1564
- state.tokenRefresher.stop();
1565
- }
1566
- }
1567
- state.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabled;
1568
- }
1569
- /**
1570
- * Get the current App Check token. If `forceRefresh` is false, this function first
1571
- * checks for a valid token in memory, then local persistence (IndexedDB).
1572
- * If not found, or if `forceRefresh` is true, it makes a request to the
1573
- * App Check endpoint for a fresh token. That request attaches
1574
- * to the most recent in-flight request if one is present.
1575
- *
1576
- * @param appCheckInstance - The App Check service instance.
1577
- * @param forceRefresh - If true, will always try to fetch a fresh token.
1578
- * If false, will use a cached token if found in storage.
1579
- * @public
1580
- */
1581
- async function getToken(appCheckInstance, forceRefresh) {
1582
- const result = await getToken$2(appCheckInstance, forceRefresh);
1583
- if (result.error) {
1584
- throw result.error;
1585
- }
1586
- return { token: result.token };
1587
- }
1588
- /**
1589
- * Requests a Firebase App Check token. This method should be used
1590
- * only if you need to authorize requests to a non-Firebase backend.
1591
- *
1592
- * Returns limited-use tokens that are intended for use with your
1593
- * non-Firebase backend endpoints that are protected with
1594
- * <a href="https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection">
1595
- * Replay Protection</a>. This method
1596
- * does not affect the token generation behavior of the
1597
- * #getAppCheckToken() method.
1598
- *
1599
- * @param appCheckInstance - The App Check service instance.
1600
- * @returns The limited use token.
1601
- * @public
1602
- */
1603
- function getLimitedUseToken(appCheckInstance) {
1604
- return getLimitedUseToken$1(appCheckInstance);
1605
- }
1606
- /**
1607
- * Wraps `addTokenListener`/`removeTokenListener` methods in an `Observer`
1608
- * pattern for public use.
1609
- */
1610
- function onTokenChanged(appCheckInstance, onNextOrObserver, onError,
1611
- /**
1612
- * NOTE: Although an `onCompletion` callback can be provided, it will
1613
- * never be called because the token stream is never-ending.
1614
- * It is added only for API consistency with the observer pattern, which
1615
- * we follow in JS APIs.
1616
- */
1617
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1618
- onCompletion) {
1619
- let nextFn = () => { };
1620
- let errorFn = () => { };
1621
- if (onNextOrObserver.next != null) {
1622
- nextFn = onNextOrObserver.next.bind(onNextOrObserver);
1623
- }
1624
- else {
1625
- nextFn = onNextOrObserver;
1626
- }
1627
- if (onNextOrObserver.error != null) {
1628
- errorFn = onNextOrObserver.error.bind(onNextOrObserver);
1629
- }
1630
- else if (onError) {
1631
- errorFn = onError;
1632
- }
1633
- addTokenListener(appCheckInstance, "EXTERNAL" /* ListenerType.EXTERNAL */, nextFn, errorFn);
1634
- return () => removeTokenListener(appCheckInstance.app, nextFn);
1446
+ /**
1447
+ * @license
1448
+ * Copyright 2020 Google LLC
1449
+ *
1450
+ * Licensed under the Apache License, Version 2.0 (the "License");
1451
+ * you may not use this file except in compliance with the License.
1452
+ * You may obtain a copy of the License at
1453
+ *
1454
+ * http://www.apache.org/licenses/LICENSE-2.0
1455
+ *
1456
+ * Unless required by applicable law or agreed to in writing, software
1457
+ * distributed under the License is distributed on an "AS IS" BASIS,
1458
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1459
+ * See the License for the specific language governing permissions and
1460
+ * limitations under the License.
1461
+ */
1462
+ /**
1463
+ * Activate App Check for the given app. Can be called only once per app.
1464
+ * @param app - the {@link @firebase/app#FirebaseApp} to activate App Check for
1465
+ * @param options - App Check initialization options
1466
+ * @public
1467
+ */
1468
+ function initializeAppCheck(app = getApp(), options) {
1469
+ app = getModularInstance(app);
1470
+ const provider = _getProvider(app, 'app-check');
1471
+ // Ensure initializeDebugMode() is only called once.
1472
+ if (!getDebugState().initialized) {
1473
+ initializeDebugMode();
1474
+ }
1475
+ // Log a message containing the debug token when `initializeAppCheck()`
1476
+ // is called in debug mode.
1477
+ if (isDebugMode()) {
1478
+ // Do not block initialization to get the token for the message.
1479
+ void getDebugToken().then(token =>
1480
+ // Not using logger because I don't think we ever want this accidentally hidden.
1481
+ console.log(`App Check debug token: ${token}. You will need to add it to your app's App Check settings in the Firebase console for it to work.`));
1482
+ }
1483
+ if (provider.isInitialized()) {
1484
+ const existingInstance = provider.getImmediate();
1485
+ const initialOptions = provider.getOptions();
1486
+ if (initialOptions.isTokenAutoRefreshEnabled ===
1487
+ options.isTokenAutoRefreshEnabled &&
1488
+ initialOptions.provider.isEqual(options.provider)) {
1489
+ return existingInstance;
1490
+ }
1491
+ else {
1492
+ throw ERROR_FACTORY.create("already-initialized" /* AppCheckError.ALREADY_INITIALIZED */, {
1493
+ appName: app.name
1494
+ });
1495
+ }
1496
+ }
1497
+ const appCheck = provider.initialize({ options });
1498
+ _activate(app, options.provider, options.isTokenAutoRefreshEnabled);
1499
+ // If isTokenAutoRefreshEnabled is false, do not send any requests to the
1500
+ // exchange endpoint without an explicit call from the user either directly
1501
+ // or through another Firebase library (storage, functions, etc.)
1502
+ if (getStateReference(app).isTokenAutoRefreshEnabled) {
1503
+ // Adding a listener will start the refresher and fetch a token if needed.
1504
+ // This gets a token ready and prevents a delay when an internal library
1505
+ // requests the token.
1506
+ // Listener function does not need to do anything, its base functionality
1507
+ // of calling getToken() already fetches token and writes it to memory/storage.
1508
+ addTokenListener(appCheck, "INTERNAL" /* ListenerType.INTERNAL */, () => { });
1509
+ }
1510
+ return appCheck;
1511
+ }
1512
+ /**
1513
+ * Activate App Check
1514
+ * @param app - Firebase app to activate App Check for.
1515
+ * @param provider - reCAPTCHA v3 provider or
1516
+ * custom token provider.
1517
+ * @param isTokenAutoRefreshEnabled - If true, the SDK automatically
1518
+ * refreshes App Check tokens as needed. If undefined, defaults to the
1519
+ * value of `app.automaticDataCollectionEnabled`, which defaults to
1520
+ * false and can be set in the app config.
1521
+ */
1522
+ function _activate(app, provider, isTokenAutoRefreshEnabled) {
1523
+ // Create an entry in the APP_CHECK_STATES map. Further changes should
1524
+ // directly mutate this object.
1525
+ const state = setInitialState(app, Object.assign({}, DEFAULT_STATE));
1526
+ state.activated = true;
1527
+ state.provider = provider; // Read cached token from storage if it exists and store it in memory.
1528
+ state.cachedTokenPromise = readTokenFromStorage(app).then(cachedToken => {
1529
+ if (cachedToken && isValid(cachedToken)) {
1530
+ state.token = cachedToken;
1531
+ // notify all listeners with the cached token
1532
+ notifyTokenListeners(app, { token: cachedToken.token });
1533
+ }
1534
+ return cachedToken;
1535
+ });
1536
+ // Use value of global `automaticDataCollectionEnabled` (which
1537
+ // itself defaults to false if not specified in config) if
1538
+ // `isTokenAutoRefreshEnabled` param was not provided by user.
1539
+ state.isTokenAutoRefreshEnabled =
1540
+ isTokenAutoRefreshEnabled === undefined
1541
+ ? app.automaticDataCollectionEnabled
1542
+ : isTokenAutoRefreshEnabled;
1543
+ state.provider.initialize(app);
1544
+ }
1545
+ /**
1546
+ * Set whether App Check will automatically refresh tokens as needed.
1547
+ *
1548
+ * @param appCheckInstance - The App Check service instance.
1549
+ * @param isTokenAutoRefreshEnabled - If true, the SDK automatically
1550
+ * refreshes App Check tokens as needed. This overrides any value set
1551
+ * during `initializeAppCheck()`.
1552
+ * @public
1553
+ */
1554
+ function setTokenAutoRefreshEnabled(appCheckInstance, isTokenAutoRefreshEnabled) {
1555
+ const app = appCheckInstance.app;
1556
+ const state = getStateReference(app);
1557
+ // This will exist if any product libraries have called
1558
+ // `addTokenListener()`
1559
+ if (state.tokenRefresher) {
1560
+ if (isTokenAutoRefreshEnabled === true) {
1561
+ state.tokenRefresher.start();
1562
+ }
1563
+ else {
1564
+ state.tokenRefresher.stop();
1565
+ }
1566
+ }
1567
+ state.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabled;
1568
+ }
1569
+ /**
1570
+ * Get the current App Check token. If `forceRefresh` is false, this function first
1571
+ * checks for a valid token in memory, then local persistence (IndexedDB).
1572
+ * If not found, or if `forceRefresh` is true, it makes a request to the
1573
+ * App Check endpoint for a fresh token. That request attaches
1574
+ * to the most recent in-flight request if one is present.
1575
+ *
1576
+ * @param appCheckInstance - The App Check service instance.
1577
+ * @param forceRefresh - If true, will always try to fetch a fresh token.
1578
+ * If false, will use a cached token if found in storage.
1579
+ * @public
1580
+ */
1581
+ async function getToken(appCheckInstance, forceRefresh) {
1582
+ const result = await getToken$2(appCheckInstance, forceRefresh);
1583
+ if (result.error) {
1584
+ throw result.error;
1585
+ }
1586
+ return { token: result.token };
1587
+ }
1588
+ /**
1589
+ * Requests a Firebase App Check token. This method should be used
1590
+ * only if you need to authorize requests to a non-Firebase backend.
1591
+ *
1592
+ * Returns limited-use tokens that are intended for use with your
1593
+ * non-Firebase backend endpoints that are protected with
1594
+ * <a href="https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection">
1595
+ * Replay Protection</a>. This method
1596
+ * does not affect the token generation behavior of the
1597
+ * #getAppCheckToken() method.
1598
+ *
1599
+ * @param appCheckInstance - The App Check service instance.
1600
+ * @returns The limited use token.
1601
+ * @public
1602
+ */
1603
+ function getLimitedUseToken(appCheckInstance) {
1604
+ return getLimitedUseToken$1(appCheckInstance);
1605
+ }
1606
+ /**
1607
+ * Wraps `addTokenListener`/`removeTokenListener` methods in an `Observer`
1608
+ * pattern for public use.
1609
+ */
1610
+ function onTokenChanged(appCheckInstance, onNextOrObserver, onError,
1611
+ /**
1612
+ * NOTE: Although an `onCompletion` callback can be provided, it will
1613
+ * never be called because the token stream is never-ending.
1614
+ * It is added only for API consistency with the observer pattern, which
1615
+ * we follow in JS APIs.
1616
+ */
1617
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1618
+ onCompletion) {
1619
+ let nextFn = () => { };
1620
+ let errorFn = () => { };
1621
+ if (onNextOrObserver.next != null) {
1622
+ nextFn = onNextOrObserver.next.bind(onNextOrObserver);
1623
+ }
1624
+ else {
1625
+ nextFn = onNextOrObserver;
1626
+ }
1627
+ if (onNextOrObserver.error != null) {
1628
+ errorFn = onNextOrObserver.error.bind(onNextOrObserver);
1629
+ }
1630
+ else if (onError) {
1631
+ errorFn = onError;
1632
+ }
1633
+ addTokenListener(appCheckInstance, "EXTERNAL" /* ListenerType.EXTERNAL */, nextFn, errorFn);
1634
+ return () => removeTokenListener(appCheckInstance.app, nextFn);
1635
1635
  }
1636
1636
 
1637
- /**
1638
- * The Firebase App Check Web SDK.
1639
- *
1640
- * @remarks
1641
- * Firebase App Check does not work in a Node.js environment using `ReCaptchaV3Provider` or
1642
- * `ReCaptchaEnterpriseProvider`, but can be used in Node.js if you use
1643
- * `CustomProvider` and write your own attestation method.
1644
- *
1645
- * @packageDocumentation
1646
- */
1647
- const APP_CHECK_NAME = 'app-check';
1648
- const APP_CHECK_NAME_INTERNAL = 'app-check-internal';
1649
- function registerAppCheck() {
1650
- // The public interface
1651
- _registerComponent(new Component(APP_CHECK_NAME, container => {
1652
- // getImmediate for FirebaseApp will always succeed
1653
- const app = container.getProvider('app').getImmediate();
1654
- const heartbeatServiceProvider = container.getProvider('heartbeat');
1655
- return factory(app, heartbeatServiceProvider);
1656
- }, "PUBLIC" /* ComponentType.PUBLIC */)
1657
- .setInstantiationMode("EXPLICIT" /* InstantiationMode.EXPLICIT */)
1658
- /**
1659
- * Initialize app-check-internal after app-check is initialized to make AppCheck available to
1660
- * other Firebase SDKs
1661
- */
1662
- .setInstanceCreatedCallback((container, _identifier, _appcheckService) => {
1663
- container.getProvider(APP_CHECK_NAME_INTERNAL).initialize();
1664
- }));
1665
- // The internal interface used by other Firebase products
1666
- _registerComponent(new Component(APP_CHECK_NAME_INTERNAL, container => {
1667
- const appCheck = container.getProvider('app-check').getImmediate();
1668
- return internalFactory(appCheck);
1669
- }, "PUBLIC" /* ComponentType.PUBLIC */).setInstantiationMode("EXPLICIT" /* InstantiationMode.EXPLICIT */));
1670
- registerVersion(name, version);
1671
- }
1637
+ /**
1638
+ * The Firebase App Check Web SDK.
1639
+ *
1640
+ * @remarks
1641
+ * Firebase App Check does not work in a Node.js environment using `ReCaptchaV3Provider` or
1642
+ * `ReCaptchaEnterpriseProvider`, but can be used in Node.js if you use
1643
+ * `CustomProvider` and write your own attestation method.
1644
+ *
1645
+ * @packageDocumentation
1646
+ */
1647
+ const APP_CHECK_NAME = 'app-check';
1648
+ const APP_CHECK_NAME_INTERNAL = 'app-check-internal';
1649
+ function registerAppCheck() {
1650
+ // The public interface
1651
+ _registerComponent(new Component(APP_CHECK_NAME, container => {
1652
+ // getImmediate for FirebaseApp will always succeed
1653
+ const app = container.getProvider('app').getImmediate();
1654
+ const heartbeatServiceProvider = container.getProvider('heartbeat');
1655
+ return factory(app, heartbeatServiceProvider);
1656
+ }, "PUBLIC" /* ComponentType.PUBLIC */)
1657
+ .setInstantiationMode("EXPLICIT" /* InstantiationMode.EXPLICIT */)
1658
+ /**
1659
+ * Initialize app-check-internal after app-check is initialized to make AppCheck available to
1660
+ * other Firebase SDKs
1661
+ */
1662
+ .setInstanceCreatedCallback((container, _identifier, _appcheckService) => {
1663
+ container.getProvider(APP_CHECK_NAME_INTERNAL).initialize();
1664
+ }));
1665
+ // The internal interface used by other Firebase products
1666
+ _registerComponent(new Component(APP_CHECK_NAME_INTERNAL, container => {
1667
+ const appCheck = container.getProvider('app-check').getImmediate();
1668
+ return internalFactory(appCheck);
1669
+ }, "PUBLIC" /* ComponentType.PUBLIC */).setInstantiationMode("EXPLICIT" /* InstantiationMode.EXPLICIT */));
1670
+ registerVersion(name, version);
1671
+ }
1672
1672
  registerAppCheck();
1673
1673
 
1674
1674
  export { CustomProvider, ReCaptchaEnterpriseProvider, ReCaptchaV3Provider, getLimitedUseToken, getToken, initializeAppCheck, onTokenChanged, setTokenAutoRefreshEnabled };