@firebase/app-check 0.11.3 → 0.11.4-eap-crashlytics.558ee841d

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.
@@ -37,7 +37,7 @@ export declare interface AppCheckOptions {
37
37
  /**
38
38
  * A reCAPTCHA V3 provider, reCAPTCHA Enterprise provider, or custom provider.
39
39
  */
40
- provider: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider;
40
+ provider?: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider;
41
41
  /**
42
42
  * If set to true, enables automatic background refresh of App Check token.
43
43
  */
@@ -144,7 +144,9 @@ export declare function getToken(appCheckInstance: AppCheck, forceRefresh?: bool
144
144
  * @param options - App Check initialization options
145
145
  * @public
146
146
  */
147
- export declare function initializeAppCheck(app: FirebaseApp | undefined, options: AppCheckOptions): AppCheck;
147
+ export declare function initializeAppCheck(app?: FirebaseApp, options?: AppCheckOptions): AppCheck;
148
+
149
+ /* Excluded from this release type: _initializeAppCheckInternal */
148
150
 
149
151
  /**
150
152
  * Registers a listener to changes in the token state. There can be more
@@ -43,7 +43,7 @@ export declare interface AppCheckOptions {
43
43
  /**
44
44
  * A reCAPTCHA V3 provider, reCAPTCHA Enterprise provider, or custom provider.
45
45
  */
46
- provider: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider;
46
+ provider?: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider;
47
47
  /**
48
48
  * If set to true, enables automatic background refresh of App Check token.
49
49
  */
@@ -55,7 +55,7 @@ declare interface AppCheckProvider {
55
55
  * Returns an App Check token.
56
56
  * @internal
57
57
  */
58
- getToken: () => Promise<AppCheckTokenInternal>;
58
+ getToken: (isLimitedUse?: boolean) => Promise<AppCheckTokenInternal>;
59
59
  /**
60
60
  * @internal
61
61
  */
@@ -166,7 +166,15 @@ export declare function getToken(appCheckInstance: AppCheck, forceRefresh?: bool
166
166
  * @param options - App Check initialization options
167
167
  * @public
168
168
  */
169
- export declare function initializeAppCheck(app: FirebaseApp | undefined, options: AppCheckOptions): AppCheck;
169
+ export declare function initializeAppCheck(app?: FirebaseApp, options?: AppCheckOptions): AppCheck;
170
+
171
+ /**
172
+ * Internal wrapper that sets a state variable flagging that this was
173
+ * initialized under the hood by a product SDK.
174
+ *
175
+ * @internal
176
+ */
177
+ export declare function _initializeAppCheckInternal(initializerName: string, app?: FirebaseApp, options?: AppCheckOptions): AppCheck;
170
178
 
171
179
  /**
172
180
  * Registers a listener to changes in the token state. There can be more
@@ -229,7 +237,7 @@ export declare class ReCaptchaEnterpriseProvider implements AppCheckProvider {
229
237
  * Returns an App Check token.
230
238
  * @internal
231
239
  */
232
- getToken(): Promise<AppCheckTokenInternal>;
240
+ getToken(isLimitedUse?: boolean): Promise<AppCheckTokenInternal>;
233
241
  /**
234
242
  * @internal
235
243
  */
@@ -264,7 +272,7 @@ export declare class ReCaptchaV3Provider implements AppCheckProvider {
264
272
  * Returns an App Check token.
265
273
  * @internal
266
274
  */
267
- getToken(): Promise<AppCheckTokenInternal>;
275
+ getToken(isLimitedUse?: boolean): Promise<AppCheckTokenInternal>;
268
276
  /**
269
277
  * @internal
270
278
  */
@@ -222,6 +222,9 @@ const ERRORS = {
222
222
  'different options. To avoid this error, call initializeAppCheck() with the ' +
223
223
  'same options as when it was originally called. This will return the ' +
224
224
  'already initialized instance.',
225
+ ["already-internally-initialized" /* AppCheckError.ALREADY_INTERNALLY_INITIALIZED */]: 'App Check has already been automatically initialized by {$initializerName} ' +
226
+ 'with default options. If you want to initialize App Check with custom options, ' +
227
+ 'call initializeAppCheck() with those options before initializing {$initializerName}.',
225
228
  ["use-before-activation" /* AppCheckError.USE_BEFORE_ACTIVATION */]: 'App Check is being used before initializeAppCheck() is called for FirebaseApp {$appName}. ' +
226
229
  'Call initializeAppCheck() before instantiating other Firebase services.',
227
230
  ["fetch-network-error" /* AppCheckError.FETCH_NETWORK_ERROR */]: 'Fetch failed to connect to a network. Check Internet connection. ' +
@@ -233,6 +236,8 @@ const ERRORS = {
233
236
  ["storage-get" /* AppCheckError.STORAGE_GET */]: 'Error thrown when reading from storage. Original error: {$originalErrorMessage}.',
234
237
  ["storage-set" /* AppCheckError.STORAGE_WRITE */]: 'Error thrown when writing to storage. Original error: {$originalErrorMessage}.',
235
238
  ["recaptcha-error" /* AppCheckError.RECAPTCHA_ERROR */]: 'ReCAPTCHA error.',
239
+ ["no-provider" /* AppCheckError.NO_PROVIDER */]: 'No attestation provider was passed to initializeAppCheck() and ' +
240
+ 'no ReCAPTCHA Enterprise site key was found in the Firebase config.',
236
241
  ["initial-throttle" /* AppCheckError.INITIAL_THROTTLE */]: `{$httpStatus} error. Attempts allowed again after {$time}`,
237
242
  ["throttled" /* AppCheckError.THROTTLED */]: `Requests throttled due to previous {$httpStatus} error. Attempts allowed again after {$time}`
238
243
  };
@@ -850,12 +855,14 @@ async function getLimitedUseToken$1(appCheck) {
850
855
  const { provider } = getStateReference(app);
851
856
  if (isDebugMode()) {
852
857
  const debugToken = await getDebugToken();
853
- const { token } = await exchangeToken(getExchangeDebugTokenRequest(app, debugToken), appCheck.heartbeatServiceProvider);
858
+ const request = getExchangeDebugTokenRequest(app, debugToken);
859
+ request.body['limited_use'] = true;
860
+ const { token } = await exchangeToken(request, appCheck.heartbeatServiceProvider);
854
861
  return { token };
855
862
  }
856
863
  else {
857
864
  // provider is definitely valid since we ensure AppCheck was activated
858
- const { token } = await provider.getToken();
865
+ const { token } = await provider.getToken(true /* isLimitedUse */);
859
866
  return { token };
860
867
  }
861
868
  }
@@ -1051,7 +1058,7 @@ function internalFactory(appCheck) {
1051
1058
  }
1052
1059
 
1053
1060
  const name = "@firebase/app-check";
1054
- const version = "0.11.3";
1061
+ const version = "0.11.4-eap-crashlytics.558ee841d";
1055
1062
 
1056
1063
  /**
1057
1064
  * @license
@@ -1185,7 +1192,8 @@ function loadReCAPTCHAV3Script(onload) {
1185
1192
  }
1186
1193
  function loadReCAPTCHAEnterpriseScript(onload) {
1187
1194
  const script = document.createElement('script');
1188
- script.src = RECAPTCHA_ENTERPRISE_URL;
1195
+ // This param is required when we plan to render a widget explicitly.
1196
+ script.src = RECAPTCHA_ENTERPRISE_URL + '?render=explicit';
1189
1197
  script.onload = onload;
1190
1198
  document.head.appendChild(script);
1191
1199
  }
@@ -1229,7 +1237,7 @@ class ReCaptchaV3Provider {
1229
1237
  * Returns an App Check token.
1230
1238
  * @internal
1231
1239
  */
1232
- async getToken() {
1240
+ async getToken(isLimitedUse = false) {
1233
1241
  throwIfThrottled(this._throttleData);
1234
1242
  // Top-level `getToken()` has already checked that App Check is initialized
1235
1243
  // and therefore this._app and this._heartbeatServiceProvider are available.
@@ -1243,7 +1251,11 @@ class ReCaptchaV3Provider {
1243
1251
  }
1244
1252
  let result;
1245
1253
  try {
1246
- result = await exchangeToken(getExchangeRecaptchaV3TokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider);
1254
+ const request = getExchangeRecaptchaV3TokenRequest(this._app, attestedClaimsToken);
1255
+ if (isLimitedUse) {
1256
+ request.body['limited_use'] = true;
1257
+ }
1258
+ result = await exchangeToken(request, this._heartbeatServiceProvider);
1247
1259
  }
1248
1260
  catch (e) {
1249
1261
  if (e.code?.includes("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */)) {
@@ -1306,7 +1318,7 @@ class ReCaptchaEnterpriseProvider {
1306
1318
  * Returns an App Check token.
1307
1319
  * @internal
1308
1320
  */
1309
- async getToken() {
1321
+ async getToken(isLimitedUse = false) {
1310
1322
  throwIfThrottled(this._throttleData);
1311
1323
  // Top-level `getToken()` has already checked that App Check is initialized
1312
1324
  // and therefore this._app and this._heartbeatServiceProvider are available.
@@ -1320,7 +1332,11 @@ class ReCaptchaEnterpriseProvider {
1320
1332
  }
1321
1333
  let result;
1322
1334
  try {
1323
- result = await exchangeToken(getExchangeRecaptchaEnterpriseTokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider);
1335
+ const request = getExchangeRecaptchaEnterpriseTokenRequest(this._app, attestedClaimsToken);
1336
+ if (isLimitedUse) {
1337
+ request.body['limited_use'] = true;
1338
+ }
1339
+ result = await exchangeToken(request, this._heartbeatServiceProvider);
1324
1340
  }
1325
1341
  catch (e) {
1326
1342
  if (e.code?.includes("fetch-status-error" /* AppCheckError.FETCH_STATUS_ERROR */)) {
@@ -1481,7 +1497,6 @@ function throwIfThrottled(throttleData) {
1481
1497
  */
1482
1498
  function initializeAppCheck(app = getApp(), options) {
1483
1499
  app = getModularInstance(app);
1484
- const provider = _getProvider(app, 'app-check');
1485
1500
  // Ensure initializeDebugMode() is only called once.
1486
1501
  if (!getDebugState().initialized) {
1487
1502
  initializeDebugMode();
@@ -1494,22 +1509,53 @@ function initializeAppCheck(app = getApp(), options) {
1494
1509
  // Not using logger because I don't think we ever want this accidentally hidden.
1495
1510
  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.`));
1496
1511
  }
1497
- if (provider.isInitialized()) {
1498
- const existingInstance = provider.getImmediate();
1499
- const initialOptions = provider.getOptions();
1500
- if (initialOptions.isTokenAutoRefreshEnabled ===
1501
- options.isTokenAutoRefreshEnabled &&
1502
- initialOptions.provider.isEqual(options.provider)) {
1512
+ let defaultProvider;
1513
+ /**
1514
+ * If user did not pass a provider, look for site key in project
1515
+ * config and create a default ReCaptchaEnterpriseProvider with it.
1516
+ */
1517
+ if (!options?.provider && app.options.recaptchaSiteKey) {
1518
+ defaultProvider = new ReCaptchaEnterpriseProvider(app.options.recaptchaSiteKey);
1519
+ }
1520
+ /**
1521
+ * If there's no passed provider and no siteKey in project config,
1522
+ * throw.
1523
+ */
1524
+ if (!options?.provider && !defaultProvider) {
1525
+ throw ERROR_FACTORY.create("no-provider" /* AppCheckError.NO_PROVIDER */);
1526
+ }
1527
+ const initOptions = {
1528
+ ...options,
1529
+ provider: options?.provider || defaultProvider
1530
+ };
1531
+ const componentProvider = _getProvider(app, 'app-check');
1532
+ if (componentProvider.isInitialized()) {
1533
+ const existingInstance = componentProvider.getImmediate();
1534
+ const existingOptions = componentProvider.getOptions();
1535
+ /**
1536
+ * Check if all AppCheckOptions previously passed to initializeAppCheck
1537
+ * (`isTokenAutoRefreshEnabled` and `provider`) match those being passed
1538
+ * now. If so, return previous existing instance. Otherwise throw error.
1539
+ */
1540
+ if (existingOptions.isTokenAutoRefreshEnabled ===
1541
+ initOptions.isTokenAutoRefreshEnabled &&
1542
+ existingOptions.provider?.isEqual(initOptions.provider)) {
1503
1543
  return existingInstance;
1504
1544
  }
1505
1545
  else {
1546
+ if (typeof getStateReference(app).internallyInitializedBy === 'string') {
1547
+ throw ERROR_FACTORY.create("already-internally-initialized" /* AppCheckError.ALREADY_INTERNALLY_INITIALIZED */, {
1548
+ initializerName: getStateReference(app)
1549
+ .internallyInitializedBy
1550
+ });
1551
+ }
1506
1552
  throw ERROR_FACTORY.create("already-initialized" /* AppCheckError.ALREADY_INITIALIZED */, {
1507
1553
  appName: app.name
1508
1554
  });
1509
1555
  }
1510
1556
  }
1511
- const appCheck = provider.initialize({ options });
1512
- _activate(app, options.provider, options.isTokenAutoRefreshEnabled);
1557
+ const appCheck = componentProvider.initialize({ options: initOptions });
1558
+ _activate(app, initOptions.provider, initOptions.isTokenAutoRefreshEnabled);
1513
1559
  // If isTokenAutoRefreshEnabled is false, do not send any requests to the
1514
1560
  // exchange endpoint without an explicit call from the user either directly
1515
1561
  // or through another Firebase library (storage, functions, etc.)
@@ -1523,6 +1569,29 @@ function initializeAppCheck(app = getApp(), options) {
1523
1569
  }
1524
1570
  return appCheck;
1525
1571
  }
1572
+ /**
1573
+ * Internal wrapper that sets a state variable flagging that this was
1574
+ * initialized under the hood by a product SDK.
1575
+ *
1576
+ * @internal
1577
+ */
1578
+ function _initializeAppCheckInternal(
1579
+ // String to be used in error message if there is a future conflict.
1580
+ // Example: "Firebase AI Logic"
1581
+ initializerName, app = getApp(), options) {
1582
+ const componentProvider = _getProvider(app, 'app-check');
1583
+ const previouslyInitialized = componentProvider.isInitialized();
1584
+ if (previouslyInitialized) {
1585
+ // Product SDKs should accept any previously initialized
1586
+ // App Check configuration without error.
1587
+ return componentProvider.getImmediate();
1588
+ }
1589
+ else {
1590
+ const appCheck = initializeAppCheck(app, options);
1591
+ getStateReference(app).internallyInitializedBy = initializerName;
1592
+ return appCheck;
1593
+ }
1594
+ }
1526
1595
  /**
1527
1596
  * Activate App Check
1528
1597
  * @param app - Firebase app to activate App Check for.
@@ -1690,5 +1759,5 @@ function registerAppCheck() {
1690
1759
  }
1691
1760
  registerAppCheck();
1692
1761
 
1693
- export { CustomProvider, ReCaptchaEnterpriseProvider, ReCaptchaV3Provider, getLimitedUseToken, getToken, initializeAppCheck, onTokenChanged, setTokenAutoRefreshEnabled };
1762
+ export { CustomProvider, ReCaptchaEnterpriseProvider, ReCaptchaV3Provider, _initializeAppCheckInternal, getLimitedUseToken, getToken, initializeAppCheck, onTokenChanged, setTokenAutoRefreshEnabled };
1694
1763
  //# sourceMappingURL=index.esm.js.map