@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/dist/index.cjs.js CHANGED
@@ -61,6 +61,7 @@ function getDebugState() {
61
61
  */
62
62
  var BASE_ENDPOINT = 'https://content-firebaseappcheck.googleapis.com/v1beta';
63
63
  var EXCHANGE_RECAPTCHA_TOKEN_METHOD = 'exchangeRecaptchaToken';
64
+ var EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD = 'exchangeRecaptchaEnterpriseToken';
64
65
  var EXCHANGE_DEBUG_TOKEN_METHOD = 'exchangeDebugToken';
65
66
  var TOKEN_REFRESH_TIME = {
66
67
  /**
@@ -260,7 +261,12 @@ var ERROR_FACTORY = new util.ErrorFactory('appCheck', 'AppCheck', ERRORS);
260
261
  * See the License for the specific language governing permissions and
261
262
  * limitations under the License.
262
263
  */
263
- function getRecaptcha() {
264
+ function getRecaptcha(isEnterprise) {
265
+ var _a;
266
+ if (isEnterprise === void 0) { isEnterprise = false; }
267
+ if (isEnterprise) {
268
+ return (_a = self.grecaptcha) === null || _a === void 0 ? void 0 : _a.enterprise;
269
+ }
264
270
  return self.grecaptcha;
265
271
  }
266
272
  function ensureActivated(app) {
@@ -367,13 +373,21 @@ function exchangeToken(_a, platformLoggerProvider) {
367
373
  });
368
374
  });
369
375
  }
370
- function getExchangeRecaptchaTokenRequest(app, reCAPTCHAToken) {
376
+ function getExchangeRecaptchaV3TokenRequest(app, reCAPTCHAToken) {
371
377
  var _a = app.options, projectId = _a.projectId, appId = _a.appId, apiKey = _a.apiKey;
372
378
  return {
373
379
  url: BASE_ENDPOINT + "/projects/" + projectId + "/apps/" + appId + ":" + EXCHANGE_RECAPTCHA_TOKEN_METHOD + "?key=" + apiKey,
374
380
  body: {
375
- // eslint-disable-next-line
376
- recaptcha_token: reCAPTCHAToken
381
+ 'recaptcha_token': reCAPTCHAToken
382
+ }
383
+ };
384
+ }
385
+ function getExchangeRecaptchaEnterpriseTokenRequest(app, reCAPTCHAToken) {
386
+ var _a = app.options, projectId = _a.projectId, appId = _a.appId, apiKey = _a.apiKey;
387
+ return {
388
+ url: BASE_ENDPOINT + "/projects/" + projectId + "/apps/" + appId + ":" + EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD + "?key=" + apiKey,
389
+ body: {
390
+ 'recaptcha_enterprise_token': reCAPTCHAToken
377
391
  }
378
392
  };
379
393
  }
@@ -1000,7 +1014,7 @@ function internalFactory(appCheck) {
1000
1014
  }
1001
1015
 
1002
1016
  var name = "@firebase/app-check";
1003
- var version = "0.4.2-pr5646.6d9dd3616";
1017
+ var version = "0.5.0";
1004
1018
 
1005
1019
  /**
1006
1020
  * @license
@@ -1019,38 +1033,72 @@ var version = "0.4.2-pr5646.6d9dd3616";
1019
1033
  * limitations under the License.
1020
1034
  */
1021
1035
  var RECAPTCHA_URL = 'https://www.google.com/recaptcha/api.js';
1022
- function initialize(app, siteKey) {
1036
+ var RECAPTCHA_ENTERPRISE_URL = 'https://www.google.com/recaptcha/enterprise.js';
1037
+ function initializeV3(app, siteKey) {
1023
1038
  var state = getState(app);
1024
1039
  var initialized = new util.Deferred();
1025
1040
  setState(app, tslib.__assign(tslib.__assign({}, state), { reCAPTCHAState: { initialized: initialized } }));
1026
- var divId = "fire_app_check_" + app.name;
1027
- var invisibleDiv = document.createElement('div');
1028
- invisibleDiv.id = divId;
1029
- invisibleDiv.style.display = 'none';
1030
- document.body.appendChild(invisibleDiv);
1031
- var grecaptcha = getRecaptcha();
1041
+ var divId = makeDiv(app);
1042
+ var grecaptcha = getRecaptcha(false);
1032
1043
  if (!grecaptcha) {
1033
- loadReCAPTCHAScript(function () {
1034
- var grecaptcha = getRecaptcha();
1044
+ loadReCAPTCHAV3Script(function () {
1045
+ var grecaptcha = getRecaptcha(false);
1035
1046
  if (!grecaptcha) {
1036
1047
  // it shouldn't happen.
1037
1048
  throw new Error('no recaptcha');
1038
1049
  }
1039
- grecaptcha.ready(function () {
1040
- // Invisible widgets allow us to set a different siteKey for each widget, so we use them to support multiple apps
1041
- renderInvisibleWidget(app, siteKey, grecaptcha, divId);
1042
- initialized.resolve(grecaptcha);
1043
- });
1050
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1044
1051
  });
1045
1052
  }
1046
1053
  else {
1047
- grecaptcha.ready(function () {
1048
- renderInvisibleWidget(app, siteKey, grecaptcha, divId);
1049
- initialized.resolve(grecaptcha);
1054
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1055
+ }
1056
+ return initialized.promise;
1057
+ }
1058
+ function initializeEnterprise(app, siteKey) {
1059
+ var state = getState(app);
1060
+ var initialized = new util.Deferred();
1061
+ setState(app, tslib.__assign(tslib.__assign({}, state), { reCAPTCHAState: { initialized: initialized } }));
1062
+ var divId = makeDiv(app);
1063
+ var grecaptcha = getRecaptcha(true);
1064
+ if (!grecaptcha) {
1065
+ loadReCAPTCHAEnterpriseScript(function () {
1066
+ var grecaptcha = getRecaptcha(true);
1067
+ if (!grecaptcha) {
1068
+ // it shouldn't happen.
1069
+ throw new Error('no recaptcha');
1070
+ }
1071
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1050
1072
  });
1051
1073
  }
1074
+ else {
1075
+ queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
1076
+ }
1052
1077
  return initialized.promise;
1053
1078
  }
1079
+ /**
1080
+ * Add listener to render the widget and resolve the promise when
1081
+ * the grecaptcha.ready() event fires.
1082
+ */
1083
+ function queueWidgetRender(app, siteKey, grecaptcha, container, initialized) {
1084
+ grecaptcha.ready(function () {
1085
+ // Invisible widgets allow us to set a different siteKey for each widget,
1086
+ // so we use them to support multiple apps
1087
+ renderInvisibleWidget(app, siteKey, grecaptcha, container);
1088
+ initialized.resolve(grecaptcha);
1089
+ });
1090
+ }
1091
+ /**
1092
+ * Add invisible div to page.
1093
+ */
1094
+ function makeDiv(app) {
1095
+ var divId = "fire_app_check_" + app.name;
1096
+ var invisibleDiv = document.createElement('div');
1097
+ invisibleDiv.id = divId;
1098
+ invisibleDiv.style.display = 'none';
1099
+ document.body.appendChild(invisibleDiv);
1100
+ return divId;
1101
+ }
1054
1102
  function getToken$1(app) {
1055
1103
  return tslib.__awaiter(this, void 0, void 0, function () {
1056
1104
  var reCAPTCHAState, recaptcha;
@@ -1091,9 +1139,15 @@ function renderInvisibleWidget(app, siteKey, grecaptcha, container) {
1091
1139
  setState(app, tslib.__assign(tslib.__assign({}, state), { reCAPTCHAState: tslib.__assign(tslib.__assign({}, state.reCAPTCHAState), { // state.reCAPTCHAState is set in the initialize()
1092
1140
  widgetId: widgetId }) }));
1093
1141
  }
1094
- function loadReCAPTCHAScript(onload) {
1142
+ function loadReCAPTCHAV3Script(onload) {
1095
1143
  var script = document.createElement('script');
1096
- script.src = "" + RECAPTCHA_URL;
1144
+ script.src = RECAPTCHA_URL;
1145
+ script.onload = onload;
1146
+ document.head.appendChild(script);
1147
+ }
1148
+ function loadReCAPTCHAEnterpriseScript(onload) {
1149
+ var script = document.createElement('script');
1150
+ script.src = RECAPTCHA_ENTERPRISE_URL;
1097
1151
  script.onload = onload;
1098
1152
  document.head.appendChild(script);
1099
1153
  }
@@ -1137,22 +1191,13 @@ var ReCaptchaV3Provider = /** @class */ (function () {
1137
1191
  var attestedClaimsToken;
1138
1192
  return tslib.__generator(this, function (_a) {
1139
1193
  switch (_a.label) {
1140
- case 0:
1141
- if (!this._app || !this._platformLoggerProvider) {
1142
- // This should only occur if user has not called initializeAppCheck().
1143
- // We don't have an appName to provide if so.
1144
- // This should already be caught in the top level `getToken()` function.
1145
- throw ERROR_FACTORY.create("use-before-activation" /* USE_BEFORE_ACTIVATION */, {
1146
- appName: ''
1147
- });
1148
- }
1149
- return [4 /*yield*/, getToken$1(this._app).catch(function (_e) {
1150
- // reCaptcha.execute() throws null which is not very descriptive.
1151
- throw ERROR_FACTORY.create("recaptcha-error" /* RECAPTCHA_ERROR */);
1152
- })];
1194
+ case 0: return [4 /*yield*/, getToken$1(this._app).catch(function (_e) {
1195
+ // reCaptcha.execute() throws null which is not very descriptive.
1196
+ throw ERROR_FACTORY.create("recaptcha-error" /* RECAPTCHA_ERROR */);
1197
+ })];
1153
1198
  case 1:
1154
1199
  attestedClaimsToken = _a.sent();
1155
- return [2 /*return*/, exchangeToken(getExchangeRecaptchaTokenRequest(this._app, attestedClaimsToken), this._platformLoggerProvider)];
1200
+ return [2 /*return*/, exchangeToken(getExchangeRecaptchaV3TokenRequest(this._app, attestedClaimsToken), this._platformLoggerProvider)];
1156
1201
  }
1157
1202
  });
1158
1203
  });
@@ -1163,7 +1208,7 @@ var ReCaptchaV3Provider = /** @class */ (function () {
1163
1208
  ReCaptchaV3Provider.prototype.initialize = function (app$1) {
1164
1209
  this._app = app$1;
1165
1210
  this._platformLoggerProvider = app._getProvider(app$1, 'platform-logger');
1166
- initialize(app$1, this._siteKey).catch(function () {
1211
+ initializeV3(app$1, this._siteKey).catch(function () {
1167
1212
  /* we don't care about the initialization result */
1168
1213
  });
1169
1214
  };
@@ -1180,6 +1225,63 @@ var ReCaptchaV3Provider = /** @class */ (function () {
1180
1225
  };
1181
1226
  return ReCaptchaV3Provider;
1182
1227
  }());
1228
+ /**
1229
+ * App Check provider that can obtain a reCAPTCHA Enterprise token and exchange it
1230
+ * for an App Check token.
1231
+ *
1232
+ * @public
1233
+ */
1234
+ var ReCaptchaEnterpriseProvider = /** @class */ (function () {
1235
+ /**
1236
+ * Create a ReCaptchaEnterpriseProvider instance.
1237
+ * @param siteKey - reCAPTCHA Enterprise score-based site key.
1238
+ */
1239
+ function ReCaptchaEnterpriseProvider(_siteKey) {
1240
+ this._siteKey = _siteKey;
1241
+ }
1242
+ /**
1243
+ * Returns an App Check token.
1244
+ * @internal
1245
+ */
1246
+ ReCaptchaEnterpriseProvider.prototype.getToken = function () {
1247
+ return tslib.__awaiter(this, void 0, void 0, function () {
1248
+ var attestedClaimsToken;
1249
+ return tslib.__generator(this, function (_a) {
1250
+ switch (_a.label) {
1251
+ case 0: return [4 /*yield*/, getToken$1(this._app).catch(function (_e) {
1252
+ // reCaptcha.execute() throws null which is not very descriptive.
1253
+ throw ERROR_FACTORY.create("recaptcha-error" /* RECAPTCHA_ERROR */);
1254
+ })];
1255
+ case 1:
1256
+ attestedClaimsToken = _a.sent();
1257
+ return [2 /*return*/, exchangeToken(getExchangeRecaptchaEnterpriseTokenRequest(this._app, attestedClaimsToken), this._platformLoggerProvider)];
1258
+ }
1259
+ });
1260
+ });
1261
+ };
1262
+ /**
1263
+ * @internal
1264
+ */
1265
+ ReCaptchaEnterpriseProvider.prototype.initialize = function (app$1) {
1266
+ this._app = app$1;
1267
+ this._platformLoggerProvider = app._getProvider(app$1, 'platform-logger');
1268
+ initializeEnterprise(app$1, this._siteKey).catch(function () {
1269
+ /* we don't care about the initialization result */
1270
+ });
1271
+ };
1272
+ /**
1273
+ * @internal
1274
+ */
1275
+ ReCaptchaEnterpriseProvider.prototype.isEqual = function (otherProvider) {
1276
+ if (otherProvider instanceof ReCaptchaEnterpriseProvider) {
1277
+ return this._siteKey === otherProvider._siteKey;
1278
+ }
1279
+ else {
1280
+ return false;
1281
+ }
1282
+ };
1283
+ return ReCaptchaEnterpriseProvider;
1284
+ }());
1183
1285
  /**
1184
1286
  * Custom provider class.
1185
1287
  * @public
@@ -1196,16 +1298,7 @@ var CustomProvider = /** @class */ (function () {
1196
1298
  var customToken, issuedAtTimeSeconds, issuedAtTimeMillis;
1197
1299
  return tslib.__generator(this, function (_a) {
1198
1300
  switch (_a.label) {
1199
- case 0:
1200
- if (!this._app) {
1201
- // This should only occur if user has not called initializeAppCheck().
1202
- // We don't have an appName to provide if so.
1203
- // This should already be caught in the top level `getToken()` function.
1204
- throw ERROR_FACTORY.create("use-before-activation" /* USE_BEFORE_ACTIVATION */, {
1205
- appName: ''
1206
- });
1207
- }
1208
- return [4 /*yield*/, this._customProviderOptions.getToken()];
1301
+ case 0: return [4 /*yield*/, this._customProviderOptions.getToken()];
1209
1302
  case 1:
1210
1303
  customToken = _a.sent();
1211
1304
  issuedAtTimeSeconds = util.issuedAtTime(customToken.token);
@@ -1441,6 +1534,7 @@ function registerAppCheck() {
1441
1534
  registerAppCheck();
1442
1535
 
1443
1536
  exports.CustomProvider = CustomProvider;
1537
+ exports.ReCaptchaEnterpriseProvider = ReCaptchaEnterpriseProvider;
1444
1538
  exports.ReCaptchaV3Provider = ReCaptchaV3Provider;
1445
1539
  exports.getToken = getToken;
1446
1540
  exports.initializeAppCheck = initializeAppCheck;