@firebase/app-check 0.4.2-pr5646.6d9dd3616 → 0.5.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @firebase/app-check
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`61604979c`](https://github.com/firebase/firebase-js-sdk/commit/61604979cb35647610ea385a6ba0ca67cb03f5d1) [#5595](https://github.com/firebase/firebase-js-sdk/pull/5595) - Add ReCAPTCHA Enterprise as an attestation option for App Check.
8
+
9
+ ### Patch Changes
10
+
11
+ - [`2322b6023`](https://github.com/firebase/firebase-js-sdk/commit/2322b6023c628cd9f4f4172767c17d215dd91684) [#5693](https://github.com/firebase/firebase-js-sdk/pull/5693) - Add exports field to all packages
12
+
13
+ - Updated dependencies [[`2322b6023`](https://github.com/firebase/firebase-js-sdk/commit/2322b6023c628cd9f4f4172767c17d215dd91684)]:
14
+ - @firebase/component@0.5.8
15
+ - @firebase/logger@0.3.1
16
+ - @firebase/util@1.4.1
17
+
3
18
  ## 0.4.2
4
19
 
5
20
  ### Patch Changes
@@ -30,9 +30,9 @@ export declare interface AppCheck {
30
30
  */
31
31
  export declare interface AppCheckOptions {
32
32
  /**
33
- * reCAPTCHA provider or custom provider.
33
+ * A reCAPTCHA V3 provider, reCAPTCHA Enterprise provider, or custom provider.
34
34
  */
35
- provider: CustomProvider | ReCaptchaV3Provider;
35
+ provider: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider;
36
36
  /**
37
37
  * If set to true, enables automatic background refresh of App Check token.
38
38
  */
@@ -159,6 +159,26 @@ export declare function onTokenChanged(appCheckInstance: AppCheck, observer: Par
159
159
  export declare function onTokenChanged(appCheckInstance: AppCheck, onNext: (tokenResult: AppCheckTokenResult) => void, onError?: (error: Error) => void, onCompletion?: () => void): Unsubscribe;
160
160
  export { PartialObserver }
161
161
 
162
+ /**
163
+ * App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it
164
+ * for an App Check token.
165
+ *
166
+ * @public
167
+ */
168
+ export declare class ReCaptchaEnterpriseProvider implements AppCheckProvider {
169
+ private _siteKey;
170
+ private _app?;
171
+ private _platformLoggerProvider?;
172
+ /**
173
+ * Create a ReCaptchaEnterpriseProvider instance.
174
+ * @param siteKey - reCAPTCHA Enterprise score-based site key.
175
+ */
176
+ constructor(_siteKey: string);
177
+ /* Excluded from this release type: getToken */
178
+ /* Excluded from this release type: initialize */
179
+ /* Excluded from this release type: isEqual */
180
+ }
181
+
162
182
  /**
163
183
  * App Check provider that can obtain a reCAPTCHA V3 token and exchange it
164
184
  * for an App Check token.
@@ -36,9 +36,9 @@ export declare type _AppCheckInternalComponentName = 'app-check-internal';
36
36
  */
37
37
  export declare interface AppCheckOptions {
38
38
  /**
39
- * reCAPTCHA provider or custom provider.
39
+ * A reCAPTCHA V3 provider, reCAPTCHA Enterprise provider, or custom provider.
40
40
  */
41
- provider: CustomProvider | ReCaptchaV3Provider;
41
+ provider: CustomProvider | ReCaptchaV3Provider | ReCaptchaEnterpriseProvider;
42
42
  /**
43
43
  * If set to true, enables automatic background refresh of App Check token.
44
44
  */
@@ -181,6 +181,36 @@ export declare function onTokenChanged(appCheckInstance: AppCheck, observer: Par
181
181
  export declare function onTokenChanged(appCheckInstance: AppCheck, onNext: (tokenResult: AppCheckTokenResult) => void, onError?: (error: Error) => void, onCompletion?: () => void): Unsubscribe;
182
182
  export { PartialObserver }
183
183
 
184
+ /**
185
+ * App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it
186
+ * for an App Check token.
187
+ *
188
+ * @public
189
+ */
190
+ export declare class ReCaptchaEnterpriseProvider implements AppCheckProvider {
191
+ private _siteKey;
192
+ private _app?;
193
+ private _platformLoggerProvider?;
194
+ /**
195
+ * Create a ReCaptchaEnterpriseProvider instance.
196
+ * @param siteKey - reCAPTCHA Enterprise score-based site key.
197
+ */
198
+ constructor(_siteKey: string);
199
+ /**
200
+ * Returns an App Check token.
201
+ * @internal
202
+ */
203
+ getToken(): Promise<AppCheckTokenInternal>;
204
+ /**
205
+ * @internal
206
+ */
207
+ initialize(app: FirebaseApp): void;
208
+ /**
209
+ * @internal
210
+ */
211
+ isEqual(otherProvider: unknown): boolean;
212
+ }
213
+
184
214
  /**
185
215
  * App Check provider that can obtain a reCAPTCHA V3 token and exchange it
186
216
  * for an App Check token.
@@ -57,6 +57,7 @@ function getDebugState() {
57
57
  */
58
58
  var BASE_ENDPOINT = 'https://content-firebaseappcheck.googleapis.com/v1beta';
59
59
  var EXCHANGE_RECAPTCHA_TOKEN_METHOD = 'exchangeRecaptchaToken';
60
+ var EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD = 'exchangeRecaptchaEnterpriseToken';
60
61
  var EXCHANGE_DEBUG_TOKEN_METHOD = 'exchangeDebugToken';
61
62
  var TOKEN_REFRESH_TIME = {
62
63
  /**
@@ -256,7 +257,12 @@ var ERROR_FACTORY = new ErrorFactory('appCheck', 'AppCheck', ERRORS);
256
257
  * See the License for the specific language governing permissions and
257
258
  * limitations under the License.
258
259
  */
259
- function getRecaptcha() {
260
+ function getRecaptcha(isEnterprise) {
261
+ var _a;
262
+ if (isEnterprise === void 0) { isEnterprise = false; }
263
+ if (isEnterprise) {
264
+ return (_a = self.grecaptcha) === null || _a === void 0 ? void 0 : _a.enterprise;
265
+ }
260
266
  return self.grecaptcha;
261
267
  }
262
268
  function ensureActivated(app) {
@@ -363,13 +369,21 @@ function exchangeToken(_a, platformLoggerProvider) {
363
369
  });
364
370
  });
365
371
  }
366
- function getExchangeRecaptchaTokenRequest(app, reCAPTCHAToken) {
372
+ function getExchangeRecaptchaV3TokenRequest(app, reCAPTCHAToken) {
367
373
  var _a = app.options, projectId = _a.projectId, appId = _a.appId, apiKey = _a.apiKey;
368
374
  return {
369
375
  url: BASE_ENDPOINT + "/projects/" + projectId + "/apps/" + appId + ":" + EXCHANGE_RECAPTCHA_TOKEN_METHOD + "?key=" + apiKey,
370
376
  body: {
371
- // eslint-disable-next-line
372
- recaptcha_token: reCAPTCHAToken
377
+ 'recaptcha_token': reCAPTCHAToken
378
+ }
379
+ };
380
+ }
381
+ function getExchangeRecaptchaEnterpriseTokenRequest(app, reCAPTCHAToken) {
382
+ var _a = app.options, projectId = _a.projectId, appId = _a.appId, apiKey = _a.apiKey;
383
+ return {
384
+ url: BASE_ENDPOINT + "/projects/" + projectId + "/apps/" + appId + ":" + EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD + "?key=" + apiKey,
385
+ body: {
386
+ 'recaptcha_enterprise_token': reCAPTCHAToken
373
387
  }
374
388
  };
375
389
  }
@@ -996,7 +1010,7 @@ function internalFactory(appCheck) {
996
1010
  }
997
1011
 
998
1012
  var name = "@firebase/app-check";
999
- var version = "0.4.2-pr5646.6d9dd3616";
1013
+ var version = "0.5.0";
1000
1014
 
1001
1015
  /**
1002
1016
  * @license
@@ -1015,38 +1029,72 @@ var version = "0.4.2-pr5646.6d9dd3616";
1015
1029
  * limitations under the License.
1016
1030
  */
1017
1031
  var RECAPTCHA_URL = 'https://www.google.com/recaptcha/api.js';
1018
- function initialize(app, siteKey) {
1032
+ var RECAPTCHA_ENTERPRISE_URL = 'https://www.google.com/recaptcha/enterprise.js';
1033
+ function initializeV3(app, siteKey) {
1019
1034
  var state = getState(app);
1020
1035
  var initialized = new Deferred();
1021
1036
  setState(app, __assign(__assign({}, state), { reCAPTCHAState: { initialized: initialized } }));
1022
- var divId = "fire_app_check_" + app.name;
1023
- var invisibleDiv = document.createElement('div');
1024
- invisibleDiv.id = divId;
1025
- invisibleDiv.style.display = 'none';
1026
- document.body.appendChild(invisibleDiv);
1027
- var grecaptcha = getRecaptcha();
1037
+ var divId = makeDiv(app);
1038
+ var grecaptcha = getRecaptcha(false);
1028
1039
  if (!grecaptcha) {
1029
- loadReCAPTCHAScript(function () {
1030
- var grecaptcha = getRecaptcha();
1040
+ loadReCAPTCHAV3Script(function () {
1041
+ var grecaptcha = getRecaptcha(false);
1031
1042
  if (!grecaptcha) {
1032
1043
  // it shouldn't happen.
1033
1044
  throw new Error('no recaptcha');
1034
1045
  }
1035
- grecaptcha.ready(function () {
1036
- // Invisible widgets allow us to set a different siteKey for each widget, so we use them to support multiple apps
1037
- renderInvisibleWidget(app, siteKey, grecaptcha, divId);
1038
- initialized.resolve(grecaptcha);
1039
- });
1046
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1040
1047
  });
1041
1048
  }
1042
1049
  else {
1043
- grecaptcha.ready(function () {
1044
- renderInvisibleWidget(app, siteKey, grecaptcha, divId);
1045
- initialized.resolve(grecaptcha);
1050
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1051
+ }
1052
+ return initialized.promise;
1053
+ }
1054
+ function initializeEnterprise(app, siteKey) {
1055
+ var state = getState(app);
1056
+ var initialized = new Deferred();
1057
+ setState(app, __assign(__assign({}, state), { reCAPTCHAState: { initialized: initialized } }));
1058
+ var divId = makeDiv(app);
1059
+ var grecaptcha = getRecaptcha(true);
1060
+ if (!grecaptcha) {
1061
+ loadReCAPTCHAEnterpriseScript(function () {
1062
+ var grecaptcha = getRecaptcha(true);
1063
+ if (!grecaptcha) {
1064
+ // it shouldn't happen.
1065
+ throw new Error('no recaptcha');
1066
+ }
1067
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1046
1068
  });
1047
1069
  }
1070
+ else {
1071
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1072
+ }
1048
1073
  return initialized.promise;
1049
1074
  }
1075
+ /**
1076
+ * Add listener to render the widget and resolve the promise when
1077
+ * the grecaptcha.ready() event fires.
1078
+ */
1079
+ function queueWidgetRender(app, siteKey, grecaptcha, container, initialized) {
1080
+ grecaptcha.ready(function () {
1081
+ // Invisible widgets allow us to set a different siteKey for each widget,
1082
+ // so we use them to support multiple apps
1083
+ renderInvisibleWidget(app, siteKey, grecaptcha, container);
1084
+ initialized.resolve(grecaptcha);
1085
+ });
1086
+ }
1087
+ /**
1088
+ * Add invisible div to page.
1089
+ */
1090
+ function makeDiv(app) {
1091
+ var divId = "fire_app_check_" + app.name;
1092
+ var invisibleDiv = document.createElement('div');
1093
+ invisibleDiv.id = divId;
1094
+ invisibleDiv.style.display = 'none';
1095
+ document.body.appendChild(invisibleDiv);
1096
+ return divId;
1097
+ }
1050
1098
  function getToken$1(app) {
1051
1099
  return __awaiter(this, void 0, void 0, function () {
1052
1100
  var reCAPTCHAState, recaptcha;
@@ -1087,9 +1135,15 @@ function renderInvisibleWidget(app, siteKey, grecaptcha, container) {
1087
1135
  setState(app, __assign(__assign({}, state), { reCAPTCHAState: __assign(__assign({}, state.reCAPTCHAState), { // state.reCAPTCHAState is set in the initialize()
1088
1136
  widgetId: widgetId }) }));
1089
1137
  }
1090
- function loadReCAPTCHAScript(onload) {
1138
+ function loadReCAPTCHAV3Script(onload) {
1091
1139
  var script = document.createElement('script');
1092
- script.src = "" + RECAPTCHA_URL;
1140
+ script.src = RECAPTCHA_URL;
1141
+ script.onload = onload;
1142
+ document.head.appendChild(script);
1143
+ }
1144
+ function loadReCAPTCHAEnterpriseScript(onload) {
1145
+ var script = document.createElement('script');
1146
+ script.src = RECAPTCHA_ENTERPRISE_URL;
1093
1147
  script.onload = onload;
1094
1148
  document.head.appendChild(script);
1095
1149
  }
@@ -1133,22 +1187,13 @@ var ReCaptchaV3Provider = /** @class */ (function () {
1133
1187
  var attestedClaimsToken;
1134
1188
  return __generator(this, function (_a) {
1135
1189
  switch (_a.label) {
1136
- case 0:
1137
- if (!this._app || !this._platformLoggerProvider) {
1138
- // This should only occur if user has not called initializeAppCheck().
1139
- // We don't have an appName to provide if so.
1140
- // This should already be caught in the top level `getToken()` function.
1141
- throw ERROR_FACTORY.create("use-before-activation" /* USE_BEFORE_ACTIVATION */, {
1142
- appName: ''
1143
- });
1144
- }
1145
- return [4 /*yield*/, getToken$1(this._app).catch(function (_e) {
1146
- // reCaptcha.execute() throws null which is not very descriptive.
1147
- throw ERROR_FACTORY.create("recaptcha-error" /* RECAPTCHA_ERROR */);
1148
- })];
1190
+ case 0: return [4 /*yield*/, getToken$1(this._app).catch(function (_e) {
1191
+ // reCaptcha.execute() throws null which is not very descriptive.
1192
+ throw ERROR_FACTORY.create("recaptcha-error" /* RECAPTCHA_ERROR */);
1193
+ })];
1149
1194
  case 1:
1150
1195
  attestedClaimsToken = _a.sent();
1151
- return [2 /*return*/, exchangeToken(getExchangeRecaptchaTokenRequest(this._app, attestedClaimsToken), this._platformLoggerProvider)];
1196
+ return [2 /*return*/, exchangeToken(getExchangeRecaptchaV3TokenRequest(this._app, attestedClaimsToken), this._platformLoggerProvider)];
1152
1197
  }
1153
1198
  });
1154
1199
  });
@@ -1159,7 +1204,7 @@ var ReCaptchaV3Provider = /** @class */ (function () {
1159
1204
  ReCaptchaV3Provider.prototype.initialize = function (app) {
1160
1205
  this._app = app;
1161
1206
  this._platformLoggerProvider = _getProvider(app, 'platform-logger');
1162
- initialize(app, this._siteKey).catch(function () {
1207
+ initializeV3(app, this._siteKey).catch(function () {
1163
1208
  /* we don't care about the initialization result */
1164
1209
  });
1165
1210
  };
@@ -1176,6 +1221,63 @@ var ReCaptchaV3Provider = /** @class */ (function () {
1176
1221
  };
1177
1222
  return ReCaptchaV3Provider;
1178
1223
  }());
1224
+ /**
1225
+ * App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it
1226
+ * for an App Check token.
1227
+ *
1228
+ * @public
1229
+ */
1230
+ var ReCaptchaEnterpriseProvider = /** @class */ (function () {
1231
+ /**
1232
+ * Create a ReCaptchaEnterpriseProvider instance.
1233
+ * @param siteKey - reCAPTCHA Enterprise score-based site key.
1234
+ */
1235
+ function ReCaptchaEnterpriseProvider(_siteKey) {
1236
+ this._siteKey = _siteKey;
1237
+ }
1238
+ /**
1239
+ * Returns an App Check token.
1240
+ * @internal
1241
+ */
1242
+ ReCaptchaEnterpriseProvider.prototype.getToken = function () {
1243
+ return __awaiter(this, void 0, void 0, function () {
1244
+ var attestedClaimsToken;
1245
+ return __generator(this, function (_a) {
1246
+ switch (_a.label) {
1247
+ case 0: return [4 /*yield*/, getToken$1(this._app).catch(function (_e) {
1248
+ // reCaptcha.execute() throws null which is not very descriptive.
1249
+ throw ERROR_FACTORY.create("recaptcha-error" /* RECAPTCHA_ERROR */);
1250
+ })];
1251
+ case 1:
1252
+ attestedClaimsToken = _a.sent();
1253
+ return [2 /*return*/, exchangeToken(getExchangeRecaptchaEnterpriseTokenRequest(this._app, attestedClaimsToken), this._platformLoggerProvider)];
1254
+ }
1255
+ });
1256
+ });
1257
+ };
1258
+ /**
1259
+ * @internal
1260
+ */
1261
+ ReCaptchaEnterpriseProvider.prototype.initialize = function (app) {
1262
+ this._app = app;
1263
+ this._platformLoggerProvider = _getProvider(app, 'platform-logger');
1264
+ initializeEnterprise(app, this._siteKey).catch(function () {
1265
+ /* we don't care about the initialization result */
1266
+ });
1267
+ };
1268
+ /**
1269
+ * @internal
1270
+ */
1271
+ ReCaptchaEnterpriseProvider.prototype.isEqual = function (otherProvider) {
1272
+ if (otherProvider instanceof ReCaptchaEnterpriseProvider) {
1273
+ return this._siteKey === otherProvider._siteKey;
1274
+ }
1275
+ else {
1276
+ return false;
1277
+ }
1278
+ };
1279
+ return ReCaptchaEnterpriseProvider;
1280
+ }());
1179
1281
  /**
1180
1282
  * Custom provider class.
1181
1283
  * @public
@@ -1192,16 +1294,7 @@ var CustomProvider = /** @class */ (function () {
1192
1294
  var customToken, issuedAtTimeSeconds, issuedAtTimeMillis;
1193
1295
  return __generator(this, function (_a) {
1194
1296
  switch (_a.label) {
1195
- case 0:
1196
- if (!this._app) {
1197
- // This should only occur if user has not called initializeAppCheck().
1198
- // We don't have an appName to provide if so.
1199
- // This should already be caught in the top level `getToken()` function.
1200
- throw ERROR_FACTORY.create("use-before-activation" /* USE_BEFORE_ACTIVATION */, {
1201
- appName: ''
1202
- });
1203
- }
1204
- return [4 /*yield*/, this._customProviderOptions.getToken()];
1297
+ case 0: return [4 /*yield*/, this._customProviderOptions.getToken()];
1205
1298
  case 1:
1206
1299
  customToken = _a.sent();
1207
1300
  issuedAtTimeSeconds = issuedAtTime(customToken.token);
@@ -1436,5 +1529,5 @@ function registerAppCheck() {
1436
1529
  }
1437
1530
  registerAppCheck();
1438
1531
 
1439
- export { CustomProvider, ReCaptchaV3Provider, getToken, initializeAppCheck, onTokenChanged, setTokenAutoRefreshEnabled };
1532
+ export { CustomProvider, ReCaptchaEnterpriseProvider, ReCaptchaV3Provider, getToken, initializeAppCheck, onTokenChanged, setTokenAutoRefreshEnabled };
1440
1533
  //# sourceMappingURL=index.esm.js.map