12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778 |
- 'use strict';
-
- Object.defineProperty(exports, '__esModule', { value: true });
-
- var app = require('@firebase/app');
- var component = require('@firebase/component');
- var tslib = require('tslib');
- var util = require('@firebase/util');
- var logger$1 = require('@firebase/logger');
-
-
- var APP_CHECK_STATES = new Map();
- var DEFAULT_STATE = {
- activated: false,
- tokenObservers: []
- };
- var DEBUG_STATE = {
- initialized: false,
- enabled: false
- };
-
- function getStateReference(app) {
- return APP_CHECK_STATES.get(app) || tslib.__assign({}, DEFAULT_STATE);
- }
-
- function setInitialState(app, state) {
- APP_CHECK_STATES.set(app, state);
- return APP_CHECK_STATES.get(app);
- }
- function getDebugState() {
- return DEBUG_STATE;
- }
-
-
- var BASE_ENDPOINT = 'https://content-firebaseappcheck.googleapis.com/v1';
- var EXCHANGE_RECAPTCHA_TOKEN_METHOD = 'exchangeRecaptchaV3Token';
- var EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD = 'exchangeRecaptchaEnterpriseToken';
- var EXCHANGE_DEBUG_TOKEN_METHOD = 'exchangeDebugToken';
- var TOKEN_REFRESH_TIME = {
-
-
- OFFSET_DURATION: 5 * 60 * 1000,
-
-
- RETRIAL_MIN_WAIT: 30 * 1000,
-
-
- RETRIAL_MAX_WAIT: 16 * 60 * 1000
- };
-
- var ONE_DAY = 24 * 60 * 60 * 1000;
-
-
-
-
-
- var Refresher = (function () {
- function Refresher(operation, retryPolicy, getWaitDuration, lowerBound, upperBound) {
- this.operation = operation;
- this.retryPolicy = retryPolicy;
- this.getWaitDuration = getWaitDuration;
- this.lowerBound = lowerBound;
- this.upperBound = upperBound;
- this.pending = null;
- this.nextErrorWaitInterval = lowerBound;
- if (lowerBound > upperBound) {
- throw new Error('Proactive refresh lower bound greater than upper bound!');
- }
- }
- Refresher.prototype.start = function () {
- this.nextErrorWaitInterval = this.lowerBound;
- this.process(true).catch(function () {
-
- });
- };
- Refresher.prototype.stop = function () {
- if (this.pending) {
- this.pending.reject('cancelled');
- this.pending = null;
- }
- };
- Refresher.prototype.isRunning = function () {
- return !!this.pending;
- };
- Refresher.prototype.process = function (hasSucceeded) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var error_1;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- this.stop();
- _a.label = 1;
- case 1:
- _a.trys.push([1, 6, , 7]);
- this.pending = new util.Deferred();
- return [4 , sleep(this.getNextRun(hasSucceeded))];
- case 2:
- _a.sent();
-
-
-
-
-
- this.pending.resolve();
- return [4 , this.pending.promise];
- case 3:
- _a.sent();
- this.pending = new util.Deferred();
- return [4 , this.operation()];
- case 4:
- _a.sent();
- this.pending.resolve();
- return [4 , this.pending.promise];
- case 5:
- _a.sent();
- this.process(true).catch(function () {
-
- });
- return [3 , 7];
- case 6:
- error_1 = _a.sent();
- if (this.retryPolicy(error_1)) {
- this.process(false).catch(function () {
-
- });
- }
- else {
- this.stop();
- }
- return [3 , 7];
- case 7: return [2 ];
- }
- });
- });
- };
- Refresher.prototype.getNextRun = function (hasSucceeded) {
- if (hasSucceeded) {
-
-
- this.nextErrorWaitInterval = this.lowerBound;
-
- return this.getWaitDuration();
- }
- else {
-
- var currentErrorWaitInterval = this.nextErrorWaitInterval;
-
- this.nextErrorWaitInterval *= 2;
-
- if (this.nextErrorWaitInterval > this.upperBound) {
- this.nextErrorWaitInterval = this.upperBound;
- }
- return currentErrorWaitInterval;
- }
- };
- return Refresher;
- }());
- function sleep(ms) {
- return new Promise(function (resolve) {
- setTimeout(resolve, ms);
- });
- }
-
-
- var _a;
- var ERRORS = (_a = {},
- _a["already-initialized" ] = 'You have already called initializeAppCheck() for FirebaseApp {$appName} with ' +
- 'different options. To avoid this error, call initializeAppCheck() with the ' +
- 'same options as when it was originally called. This will return the ' +
- 'already initialized instance.',
- _a["use-before-activation" ] = 'App Check is being used before initializeAppCheck() is called for FirebaseApp {$appName}. ' +
- 'Call initializeAppCheck() before instantiating other Firebase services.',
- _a["fetch-network-error" ] = 'Fetch failed to connect to a network. Check Internet connection. ' +
- 'Original error: {$originalErrorMessage}.',
- _a["fetch-parse-error" ] = 'Fetch client could not parse response.' +
- ' Original error: {$originalErrorMessage}.',
- _a["fetch-status-error" ] = 'Fetch server returned an HTTP error status. HTTP status: {$httpStatus}.',
- _a["storage-open" ] = 'Error thrown when opening storage. Original error: {$originalErrorMessage}.',
- _a["storage-get" ] = 'Error thrown when reading from storage. Original error: {$originalErrorMessage}.',
- _a["storage-set" ] = 'Error thrown when writing to storage. Original error: {$originalErrorMessage}.',
- _a["recaptcha-error" ] = 'ReCAPTCHA error.',
- _a["throttled" ] = "Requests throttled due to {$httpStatus} error. Attempts allowed again after {$time}",
- _a);
- var ERROR_FACTORY = new util.ErrorFactory('appCheck', 'AppCheck', ERRORS);
-
-
- function getRecaptcha(isEnterprise) {
- var _a;
- if (isEnterprise === void 0) { isEnterprise = false; }
- if (isEnterprise) {
- return (_a = self.grecaptcha) === null || _a === void 0 ? void 0 : _a.enterprise;
- }
- return self.grecaptcha;
- }
- function ensureActivated(app) {
- if (!getStateReference(app).activated) {
- throw ERROR_FACTORY.create("use-before-activation" , {
- appName: app.name
- });
- }
- }
- function getDurationString(durationInMillis) {
- var totalSeconds = Math.round(durationInMillis / 1000);
- var days = Math.floor(totalSeconds / (3600 * 24));
- var hours = Math.floor((totalSeconds - days * 3600 * 24) / 3600);
- var minutes = Math.floor((totalSeconds - days * 3600 * 24 - hours * 3600) / 60);
- var seconds = totalSeconds - days * 3600 * 24 - hours * 3600 - minutes * 60;
- var result = '';
- if (days) {
- result += pad(days) + 'd:';
- }
- if (hours) {
- result += pad(hours) + 'h:';
- }
- result += pad(minutes) + 'm:' + pad(seconds) + 's';
- return result;
- }
- function pad(value) {
- if (value === 0) {
- return '00';
- }
- return value >= 10 ? value.toString() : '0' + value;
- }
-
-
- function exchangeToken(_a, heartbeatServiceProvider) {
- var url = _a.url, body = _a.body;
- return tslib.__awaiter(this, void 0, void 0, function () {
- var headers, heartbeatService, heartbeatsHeader, options, response, originalError_1, responseBody, originalError_2, match, timeToLiveAsNumber, now;
- return tslib.__generator(this, function (_b) {
- switch (_b.label) {
- case 0:
- headers = {
- 'Content-Type': 'application/json'
- };
- heartbeatService = heartbeatServiceProvider.getImmediate({
- optional: true
- });
- if (!heartbeatService) return [3 , 2];
- return [4 , heartbeatService.getHeartbeatsHeader()];
- case 1:
- heartbeatsHeader = _b.sent();
- if (heartbeatsHeader) {
- headers['X-Firebase-Client'] = heartbeatsHeader;
- }
- _b.label = 2;
- case 2:
- options = {
- method: 'POST',
- body: JSON.stringify(body),
- headers: headers
- };
- _b.label = 3;
- case 3:
- _b.trys.push([3, 5, , 6]);
- return [4 , fetch(url, options)];
- case 4:
- response = _b.sent();
- return [3 , 6];
- case 5:
- originalError_1 = _b.sent();
- throw ERROR_FACTORY.create("fetch-network-error" , {
- originalErrorMessage: originalError_1 === null || originalError_1 === void 0 ? void 0 : originalError_1.message
- });
- case 6:
- if (response.status !== 200) {
- throw ERROR_FACTORY.create("fetch-status-error" , {
- httpStatus: response.status
- });
- }
- _b.label = 7;
- case 7:
- _b.trys.push([7, 9, , 10]);
- return [4 , response.json()];
- case 8:
-
- responseBody = _b.sent();
- return [3 , 10];
- case 9:
- originalError_2 = _b.sent();
- throw ERROR_FACTORY.create("fetch-parse-error" , {
- originalErrorMessage: originalError_2 === null || originalError_2 === void 0 ? void 0 : originalError_2.message
- });
- case 10:
- match = responseBody.ttl.match(/^([\d.]+)(s)$/);
- if (!match || !match[2] || isNaN(Number(match[1]))) {
- throw ERROR_FACTORY.create("fetch-parse-error" , {
- originalErrorMessage: "ttl field (timeToLive) is not in standard Protobuf Duration " +
- "format: ".concat(responseBody.ttl)
- });
- }
- timeToLiveAsNumber = Number(match[1]) * 1000;
- now = Date.now();
- return [2 , {
- token: responseBody.token,
- expireTimeMillis: now + timeToLiveAsNumber,
- issuedAtTimeMillis: now
- }];
- }
- });
- });
- }
- function getExchangeRecaptchaV3TokenRequest(app, reCAPTCHAToken) {
- var _a = app.options, projectId = _a.projectId, appId = _a.appId, apiKey = _a.apiKey;
- return {
- url: "".concat(BASE_ENDPOINT, "/projects/").concat(projectId, "/apps/").concat(appId, ":").concat(EXCHANGE_RECAPTCHA_TOKEN_METHOD, "?key=").concat(apiKey),
- body: {
- 'recaptcha_v3_token': reCAPTCHAToken
- }
- };
- }
- function getExchangeRecaptchaEnterpriseTokenRequest(app, reCAPTCHAToken) {
- var _a = app.options, projectId = _a.projectId, appId = _a.appId, apiKey = _a.apiKey;
- return {
- url: "".concat(BASE_ENDPOINT, "/projects/").concat(projectId, "/apps/").concat(appId, ":").concat(EXCHANGE_RECAPTCHA_ENTERPRISE_TOKEN_METHOD, "?key=").concat(apiKey),
- body: {
- 'recaptcha_enterprise_token': reCAPTCHAToken
- }
- };
- }
- function getExchangeDebugTokenRequest(app, debugToken) {
- var _a = app.options, projectId = _a.projectId, appId = _a.appId, apiKey = _a.apiKey;
- return {
- url: "".concat(BASE_ENDPOINT, "/projects/").concat(projectId, "/apps/").concat(appId, ":").concat(EXCHANGE_DEBUG_TOKEN_METHOD, "?key=").concat(apiKey),
- body: {
-
- debug_token: debugToken
- }
- };
- }
-
-
- var DB_NAME = 'firebase-app-check-database';
- var DB_VERSION = 1;
- var STORE_NAME = 'firebase-app-check-store';
- var DEBUG_TOKEN_KEY = 'debug-token';
- var dbPromise = null;
- function getDBPromise() {
- if (dbPromise) {
- return dbPromise;
- }
- dbPromise = new Promise(function (resolve, reject) {
- try {
- var request = indexedDB.open(DB_NAME, DB_VERSION);
- request.onsuccess = function (event) {
- resolve(event.target.result);
- };
- request.onerror = function (event) {
- var _a;
- reject(ERROR_FACTORY.create("storage-open" , {
- originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
- }));
- };
- request.onupgradeneeded = function (event) {
- var db = event.target.result;
-
-
-
-
-
- switch (event.oldVersion) {
- case 0:
- db.createObjectStore(STORE_NAME, {
- keyPath: 'compositeKey'
- });
- }
- };
- }
- catch (e) {
- reject(ERROR_FACTORY.create("storage-open" , {
- originalErrorMessage: e === null || e === void 0 ? void 0 : e.message
- }));
- }
- });
- return dbPromise;
- }
- function readTokenFromIndexedDB(app) {
- return read(computeKey(app));
- }
- function writeTokenToIndexedDB(app, token) {
- return write(computeKey(app), token);
- }
- function writeDebugTokenToIndexedDB(token) {
- return write(DEBUG_TOKEN_KEY, token);
- }
- function readDebugTokenFromIndexedDB() {
- return read(DEBUG_TOKEN_KEY);
- }
- function write(key, value) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var db, transaction, store, request;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0: return [4 , getDBPromise()];
- case 1:
- db = _a.sent();
- transaction = db.transaction(STORE_NAME, 'readwrite');
- store = transaction.objectStore(STORE_NAME);
- request = store.put({
- compositeKey: key,
- value: value
- });
- return [2 , new Promise(function (resolve, reject) {
- request.onsuccess = function (_event) {
- resolve();
- };
- transaction.onerror = function (event) {
- var _a;
- reject(ERROR_FACTORY.create("storage-set" , {
- originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
- }));
- };
- })];
- }
- });
- });
- }
- function read(key) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var db, transaction, store, request;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0: return [4 , getDBPromise()];
- case 1:
- db = _a.sent();
- transaction = db.transaction(STORE_NAME, 'readonly');
- store = transaction.objectStore(STORE_NAME);
- request = store.get(key);
- return [2 , new Promise(function (resolve, reject) {
- request.onsuccess = function (event) {
- var result = event.target.result;
- if (result) {
- resolve(result.value);
- }
- else {
- resolve(undefined);
- }
- };
- transaction.onerror = function (event) {
- var _a;
- reject(ERROR_FACTORY.create("storage-get" , {
- originalErrorMessage: (_a = event.target.error) === null || _a === void 0 ? void 0 : _a.message
- }));
- };
- })];
- }
- });
- });
- }
- function computeKey(app) {
- return "".concat(app.options.appId, "-").concat(app.name);
- }
-
-
- var logger = new logger$1.Logger('@firebase/app-check');
-
-
-
- function readTokenFromStorage(app) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var token, e_1;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- if (!util.isIndexedDBAvailable()) return [3 , 5];
- token = undefined;
- _a.label = 1;
- case 1:
- _a.trys.push([1, 3, , 4]);
- return [4 , readTokenFromIndexedDB(app)];
- case 2:
- token = _a.sent();
- return [3 , 4];
- case 3:
- e_1 = _a.sent();
-
- logger.warn("Failed to read token from IndexedDB. Error: ".concat(e_1));
- return [3 , 4];
- case 4: return [2 , token];
- case 5: return [2 , undefined];
- }
- });
- });
- }
-
- function writeTokenToStorage(app, token) {
- if (util.isIndexedDBAvailable()) {
- return writeTokenToIndexedDB(app, token).catch(function (e) {
-
- logger.warn("Failed to write token to IndexedDB. Error: ".concat(e));
- });
- }
- return Promise.resolve();
- }
- function readOrCreateDebugTokenFromStorage() {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var existingDebugToken, newToken;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- existingDebugToken = undefined;
- _a.label = 1;
- case 1:
- _a.trys.push([1, 3, , 4]);
- return [4 , readDebugTokenFromIndexedDB()];
- case 2:
- existingDebugToken = _a.sent();
- return [3 , 4];
- case 3:
- _a.sent();
- return [3 , 4];
- case 4:
- if (!existingDebugToken) {
- newToken = util.uuidv4();
-
-
-
-
-
- writeDebugTokenToIndexedDB(newToken).catch(function (e) {
- return logger.warn("Failed to persist debug token to IndexedDB. Error: ".concat(e));
- });
- return [2 , newToken];
- }
- else {
- return [2 , existingDebugToken];
- }
- }
- });
- });
- }
-
-
- function isDebugMode() {
- var debugState = getDebugState();
- return debugState.enabled;
- }
- function getDebugToken() {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var state;
- return tslib.__generator(this, function (_a) {
- state = getDebugState();
- if (state.enabled && state.token) {
- return [2 , state.token.promise];
- }
- else {
-
- throw Error("\n Can't get debug token in production mode.\n ");
- }
- });
- });
- }
- function initializeDebugMode() {
- var globals = util.getGlobal();
- var debugState = getDebugState();
-
-
- debugState.initialized = true;
- if (typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== 'string' &&
- globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== true) {
- return;
- }
- debugState.enabled = true;
- var deferredToken = new util.Deferred();
- debugState.token = deferredToken;
- if (typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN === 'string') {
- deferredToken.resolve(globals.FIREBASE_APPCHECK_DEBUG_TOKEN);
- }
- else {
- deferredToken.resolve(readOrCreateDebugTokenFromStorage());
- }
- }
-
-
-
-
- var defaultTokenErrorData = { error: 'UNKNOWN_ERROR' };
-
- function formatDummyToken(tokenErrorData) {
- return util.base64.encodeString(JSON.stringify(tokenErrorData),
- false);
- }
-
- function getToken$2(appCheck, forceRefresh) {
- if (forceRefresh === void 0) { forceRefresh = false; }
- return tslib.__awaiter(this, void 0, void 0, function () {
- var app, state, token, error, cachedToken, shouldCallListeners, _a, _b, _c, _d, tokenFromDebugExchange, e_1, interopTokenResult;
- return tslib.__generator(this, function (_e) {
- switch (_e.label) {
- case 0:
- app = appCheck.app;
- ensureActivated(app);
- state = getStateReference(app);
- token = state.token;
- error = undefined;
-
-
- if (token && !isValid(token)) {
- state.token = undefined;
- token = undefined;
- }
- if (!!token) return [3 , 4];
- return [4 , state.cachedTokenPromise];
- case 1:
- cachedToken = _e.sent();
- if (!cachedToken) return [3 , 4];
- if (!isValid(cachedToken)) return [3 , 2];
- token = cachedToken;
- return [3 , 4];
- case 2:
-
- return [4 , writeTokenToStorage(app, undefined)];
- case 3:
-
- _e.sent();
- _e.label = 4;
- case 4:
-
- if (!forceRefresh && token && isValid(token)) {
- return [2 , {
- token: token.token
- }];
- }
- shouldCallListeners = false;
- if (!isDebugMode()) return [3 , 9];
- if (!!state.exchangeTokenPromise) return [3 , 6];
- _a = state;
- _b = exchangeToken;
- _c = getExchangeDebugTokenRequest;
- _d = [app];
- return [4 , getDebugToken()];
- case 5:
- _a.exchangeTokenPromise = _b.apply(void 0, [_c.apply(void 0, _d.concat([_e.sent()])),
- appCheck.heartbeatServiceProvider]).finally(function () {
-
- state.exchangeTokenPromise = undefined;
- });
- shouldCallListeners = true;
- _e.label = 6;
- case 6: return [4 , state.exchangeTokenPromise];
- case 7:
- tokenFromDebugExchange = _e.sent();
-
- return [4 , writeTokenToStorage(app, tokenFromDebugExchange)];
- case 8:
-
- _e.sent();
-
- state.token = tokenFromDebugExchange;
- return [2 , { token: tokenFromDebugExchange.token }];
- case 9:
- _e.trys.push([9, 11, , 12]);
-
- if (!state.exchangeTokenPromise) {
-
-
-
- state.exchangeTokenPromise = state.provider.getToken().finally(function () {
-
- state.exchangeTokenPromise = undefined;
- });
- shouldCallListeners = true;
- }
- return [4 , getStateReference(app).exchangeTokenPromise];
- case 10:
- token = _e.sent();
- return [3 , 12];
- case 11:
- e_1 = _e.sent();
- if (e_1.code === "appCheck/".concat("throttled" )) {
-
- logger.warn(e_1.message);
- }
- else {
-
- logger.error(e_1);
- }
-
- error = e_1;
- return [3 , 12];
- case 12:
- if (!!token) return [3 , 13];
-
-
- interopTokenResult = makeDummyTokenResult(error);
- return [3 , 16];
- case 13:
- if (!error) return [3 , 14];
- if (isValid(token)) {
-
-
-
-
-
-
-
- interopTokenResult = {
- token: token.token,
- internalError: error
- };
- }
- else {
-
-
- interopTokenResult = makeDummyTokenResult(error);
- }
- return [3 , 16];
- case 14:
- interopTokenResult = {
- token: token.token
- };
-
-
- state.token = token;
- return [4 , writeTokenToStorage(app, token)];
- case 15:
- _e.sent();
- _e.label = 16;
- case 16:
- if (shouldCallListeners) {
- notifyTokenListeners(app, interopTokenResult);
- }
- return [2 , interopTokenResult];
- }
- });
- });
- }
- function addTokenListener(appCheck, type, listener, onError) {
- var app = appCheck.app;
- var state = getStateReference(app);
- var tokenObserver = {
- next: listener,
- error: onError,
- type: type
- };
- state.tokenObservers = tslib.__spreadArray(tslib.__spreadArray([], state.tokenObservers, true), [tokenObserver], false);
-
-
- if (state.token && isValid(state.token)) {
- var validToken_1 = state.token;
- Promise.resolve()
- .then(function () {
- listener({ token: validToken_1.token });
- initTokenRefresher(appCheck);
- })
- .catch(function () {
-
- });
- }
-
-
-
- void state.cachedTokenPromise.then(function () { return initTokenRefresher(appCheck); });
- }
- function removeTokenListener(app, listener) {
- var state = getStateReference(app);
- var newObservers = state.tokenObservers.filter(function (tokenObserver) { return tokenObserver.next !== listener; });
- if (newObservers.length === 0 &&
- state.tokenRefresher &&
- state.tokenRefresher.isRunning()) {
- state.tokenRefresher.stop();
- }
- state.tokenObservers = newObservers;
- }
-
- function initTokenRefresher(appCheck) {
- var app = appCheck.app;
- var state = getStateReference(app);
-
-
- var refresher = state.tokenRefresher;
- if (!refresher) {
- refresher = createTokenRefresher(appCheck);
- state.tokenRefresher = refresher;
- }
- if (!refresher.isRunning() && state.isTokenAutoRefreshEnabled) {
- refresher.start();
- }
- }
- function createTokenRefresher(appCheck) {
- var _this = this;
- var app = appCheck.app;
- return new Refresher(
-
-
- function () { return tslib.__awaiter(_this, void 0, void 0, function () {
- var state, result;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- state = getStateReference(app);
- if (!!state.token) return [3 , 2];
- return [4 , getToken$2(appCheck)];
- case 1:
- result = _a.sent();
- return [3 , 4];
- case 2: return [4 , getToken$2(appCheck, true)];
- case 3:
- result = _a.sent();
- _a.label = 4;
- case 4:
-
-
- if (result.error) {
- throw result.error;
- }
-
-
- if (result.internalError) {
- throw result.internalError;
- }
- return [2 ];
- }
- });
- }); }, function () {
- return true;
- }, function () {
- var state = getStateReference(app);
- if (state.token) {
-
- var nextRefreshTimeMillis = state.token.issuedAtTimeMillis +
- (state.token.expireTimeMillis - state.token.issuedAtTimeMillis) *
- 0.5 +
- 5 * 60 * 1000;
-
- var latestAllowableRefresh = state.token.expireTimeMillis - 5 * 60 * 1000;
- nextRefreshTimeMillis = Math.min(nextRefreshTimeMillis, latestAllowableRefresh);
- return Math.max(0, nextRefreshTimeMillis - Date.now());
- }
- else {
- return 0;
- }
- }, TOKEN_REFRESH_TIME.RETRIAL_MIN_WAIT, TOKEN_REFRESH_TIME.RETRIAL_MAX_WAIT);
- }
- function notifyTokenListeners(app, token) {
- var observers = getStateReference(app).tokenObservers;
- for (var _i = 0, observers_1 = observers; _i < observers_1.length; _i++) {
- var observer = observers_1[_i];
- try {
- if (observer.type === "EXTERNAL" && token.error != null) {
-
-
-
- observer.error(token.error);
- }
- else {
-
-
-
- observer.next(token);
- }
- }
- catch (e) {
-
- }
- }
- }
- function isValid(token) {
- return token.expireTimeMillis - Date.now() > 0;
- }
- function makeDummyTokenResult(error) {
- return {
- token: formatDummyToken(defaultTokenErrorData),
- error: error
- };
- }
-
-
-
- var AppCheckService = (function () {
- function AppCheckService(app, heartbeatServiceProvider) {
- this.app = app;
- this.heartbeatServiceProvider = heartbeatServiceProvider;
- }
- AppCheckService.prototype._delete = function () {
- var tokenObservers = getStateReference(this.app).tokenObservers;
- for (var _i = 0, tokenObservers_1 = tokenObservers; _i < tokenObservers_1.length; _i++) {
- var tokenObserver = tokenObservers_1[_i];
- removeTokenListener(this.app, tokenObserver.next);
- }
- return Promise.resolve();
- };
- return AppCheckService;
- }());
- function factory(app, heartbeatServiceProvider) {
- return new AppCheckService(app, heartbeatServiceProvider);
- }
- function internalFactory(appCheck) {
- return {
- getToken: function (forceRefresh) { return getToken$2(appCheck, forceRefresh); },
- addTokenListener: function (listener) {
- return addTokenListener(appCheck, "INTERNAL" , listener);
- },
- removeTokenListener: function (listener) { return removeTokenListener(appCheck.app, listener); }
- };
- }
-
- var name = "@firebase/app-check";
- var version = "0.6.1";
-
-
- var RECAPTCHA_URL = 'https://www.google.com/recaptcha/api.js';
- var RECAPTCHA_ENTERPRISE_URL = 'https://www.google.com/recaptcha/enterprise.js';
- function initializeV3(app, siteKey) {
- var initialized = new util.Deferred();
- var state = getStateReference(app);
- state.reCAPTCHAState = { initialized: initialized };
- var divId = makeDiv(app);
- var grecaptcha = getRecaptcha(false);
- if (!grecaptcha) {
- loadReCAPTCHAV3Script(function () {
- var grecaptcha = getRecaptcha(false);
- if (!grecaptcha) {
-
- throw new Error('no recaptcha');
- }
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
- });
- }
- else {
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
- }
- return initialized.promise;
- }
- function initializeEnterprise(app, siteKey) {
- var initialized = new util.Deferred();
- var state = getStateReference(app);
- state.reCAPTCHAState = { initialized: initialized };
- var divId = makeDiv(app);
- var grecaptcha = getRecaptcha(true);
- if (!grecaptcha) {
- loadReCAPTCHAEnterpriseScript(function () {
- var grecaptcha = getRecaptcha(true);
- if (!grecaptcha) {
-
- throw new Error('no recaptcha');
- }
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
- });
- }
- else {
- queueWidgetRender(app, siteKey, grecaptcha, divId, initialized);
- }
- return initialized.promise;
- }
-
- function queueWidgetRender(app, siteKey, grecaptcha, container, initialized) {
- grecaptcha.ready(function () {
-
-
- renderInvisibleWidget(app, siteKey, grecaptcha, container);
- initialized.resolve(grecaptcha);
- });
- }
-
- function makeDiv(app) {
- var divId = "fire_app_check_".concat(app.name);
- var invisibleDiv = document.createElement('div');
- invisibleDiv.id = divId;
- invisibleDiv.style.display = 'none';
- document.body.appendChild(invisibleDiv);
- return divId;
- }
- function getToken$1(app) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var reCAPTCHAState, recaptcha;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- ensureActivated(app);
- reCAPTCHAState = getStateReference(app).reCAPTCHAState;
- return [4 , reCAPTCHAState.initialized.promise];
- case 1:
- recaptcha = _a.sent();
- return [2 , new Promise(function (resolve, _reject) {
-
- var reCAPTCHAState = getStateReference(app).reCAPTCHAState;
- recaptcha.ready(function () {
- resolve(
-
- recaptcha.execute(reCAPTCHAState.widgetId, {
- action: 'fire_app_check'
- }));
- });
- })];
- }
- });
- });
- }
-
- function renderInvisibleWidget(app, siteKey, grecaptcha, container) {
- var widgetId = grecaptcha.render(container, {
- sitekey: siteKey,
- size: 'invisible'
- });
- var state = getStateReference(app);
- state.reCAPTCHAState = tslib.__assign(tslib.__assign({}, state.reCAPTCHAState), {
- widgetId: widgetId });
- }
- function loadReCAPTCHAV3Script(onload) {
- var script = document.createElement('script');
- script.src = RECAPTCHA_URL;
- script.onload = onload;
- document.head.appendChild(script);
- }
- function loadReCAPTCHAEnterpriseScript(onload) {
- var script = document.createElement('script');
- script.src = RECAPTCHA_ENTERPRISE_URL;
- script.onload = onload;
- document.head.appendChild(script);
- }
-
-
-
- var ReCaptchaV3Provider = (function () {
-
-
- function ReCaptchaV3Provider(_siteKey) {
- this._siteKey = _siteKey;
-
-
- this._throttleData = null;
- }
-
-
- ReCaptchaV3Provider.prototype.getToken = function () {
- var _a, _b;
- return tslib.__awaiter(this, void 0, void 0, function () {
- var attestedClaimsToken, result, e_1;
- return tslib.__generator(this, function (_c) {
- switch (_c.label) {
- case 0:
- throwIfThrottled(this._throttleData);
- return [4 , getToken$1(this._app).catch(function (_e) {
-
- throw ERROR_FACTORY.create("recaptcha-error" );
- })];
- case 1:
- attestedClaimsToken = _c.sent();
- _c.label = 2;
- case 2:
- _c.trys.push([2, 4, , 5]);
- return [4 , exchangeToken(getExchangeRecaptchaV3TokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider)];
- case 3:
- result = _c.sent();
- return [3 , 5];
- case 4:
- e_1 = _c.sent();
- if ((_a = e_1.code) === null || _a === void 0 ? void 0 : _a.includes("fetch-status-error" )) {
- this._throttleData = setBackoff(Number((_b = e_1.customData) === null || _b === void 0 ? void 0 : _b.httpStatus), this._throttleData);
- throw ERROR_FACTORY.create("throttled" , {
- time: getDurationString(this._throttleData.allowRequestsAfter - Date.now()),
- httpStatus: this._throttleData.httpStatus
- });
- }
- else {
- throw e_1;
- }
- case 5:
-
- this._throttleData = null;
- return [2 , result];
- }
- });
- });
- };
-
-
- ReCaptchaV3Provider.prototype.initialize = function (app$1) {
- this._app = app$1;
- this._heartbeatServiceProvider = app._getProvider(app$1, 'heartbeat');
- initializeV3(app$1, this._siteKey).catch(function () {
-
- });
- };
-
-
- ReCaptchaV3Provider.prototype.isEqual = function (otherProvider) {
- if (otherProvider instanceof ReCaptchaV3Provider) {
- return this._siteKey === otherProvider._siteKey;
- }
- else {
- return false;
- }
- };
- return ReCaptchaV3Provider;
- }());
-
- var ReCaptchaEnterpriseProvider = (function () {
-
-
- function ReCaptchaEnterpriseProvider(_siteKey) {
- this._siteKey = _siteKey;
-
-
- this._throttleData = null;
- }
-
-
- ReCaptchaEnterpriseProvider.prototype.getToken = function () {
- var _a, _b;
- return tslib.__awaiter(this, void 0, void 0, function () {
- var attestedClaimsToken, result, e_2;
- return tslib.__generator(this, function (_c) {
- switch (_c.label) {
- case 0:
- throwIfThrottled(this._throttleData);
- return [4 , getToken$1(this._app).catch(function (_e) {
-
- throw ERROR_FACTORY.create("recaptcha-error" );
- })];
- case 1:
- attestedClaimsToken = _c.sent();
- _c.label = 2;
- case 2:
- _c.trys.push([2, 4, , 5]);
- return [4 , exchangeToken(getExchangeRecaptchaEnterpriseTokenRequest(this._app, attestedClaimsToken), this._heartbeatServiceProvider)];
- case 3:
- result = _c.sent();
- return [3 , 5];
- case 4:
- e_2 = _c.sent();
- if ((_a = e_2.code) === null || _a === void 0 ? void 0 : _a.includes("fetch-status-error" )) {
- this._throttleData = setBackoff(Number((_b = e_2.customData) === null || _b === void 0 ? void 0 : _b.httpStatus), this._throttleData);
- throw ERROR_FACTORY.create("throttled" , {
- time: getDurationString(this._throttleData.allowRequestsAfter - Date.now()),
- httpStatus: this._throttleData.httpStatus
- });
- }
- else {
- throw e_2;
- }
- case 5:
-
- this._throttleData = null;
- return [2 , result];
- }
- });
- });
- };
-
-
- ReCaptchaEnterpriseProvider.prototype.initialize = function (app$1) {
- this._app = app$1;
- this._heartbeatServiceProvider = app._getProvider(app$1, 'heartbeat');
- initializeEnterprise(app$1, this._siteKey).catch(function () {
-
- });
- };
-
-
- ReCaptchaEnterpriseProvider.prototype.isEqual = function (otherProvider) {
- if (otherProvider instanceof ReCaptchaEnterpriseProvider) {
- return this._siteKey === otherProvider._siteKey;
- }
- else {
- return false;
- }
- };
- return ReCaptchaEnterpriseProvider;
- }());
-
- var CustomProvider = (function () {
- function CustomProvider(_customProviderOptions) {
- this._customProviderOptions = _customProviderOptions;
- }
-
-
- CustomProvider.prototype.getToken = function () {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var customToken, issuedAtTimeSeconds, issuedAtTimeMillis;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0: return [4 , this._customProviderOptions.getToken()];
- case 1:
- customToken = _a.sent();
- issuedAtTimeSeconds = util.issuedAtTime(customToken.token);
- issuedAtTimeMillis = issuedAtTimeSeconds !== null &&
- issuedAtTimeSeconds < Date.now() &&
- issuedAtTimeSeconds > 0
- ? issuedAtTimeSeconds * 1000
- : Date.now();
- return [2 , tslib.__assign(tslib.__assign({}, customToken), { issuedAtTimeMillis: issuedAtTimeMillis })];
- }
- });
- });
- };
-
-
- CustomProvider.prototype.initialize = function (app) {
- this._app = app;
- };
-
-
- CustomProvider.prototype.isEqual = function (otherProvider) {
- if (otherProvider instanceof CustomProvider) {
- return (this._customProviderOptions.getToken.toString() ===
- otherProvider._customProviderOptions.getToken.toString());
- }
- else {
- return false;
- }
- };
- return CustomProvider;
- }());
-
- function setBackoff(httpStatus, throttleData) {
-
-
- if (httpStatus === 404 || httpStatus === 403) {
- return {
- backoffCount: 1,
- allowRequestsAfter: Date.now() + ONE_DAY,
- httpStatus: httpStatus
- };
- }
- else {
-
-
- var backoffCount = throttleData ? throttleData.backoffCount : 0;
- var backoffMillis = util.calculateBackoffMillis(backoffCount, 1000, 2);
- return {
- backoffCount: backoffCount + 1,
- allowRequestsAfter: Date.now() + backoffMillis,
- httpStatus: httpStatus
- };
- }
- }
- function throwIfThrottled(throttleData) {
- if (throttleData) {
- if (Date.now() - throttleData.allowRequestsAfter <= 0) {
-
- throw ERROR_FACTORY.create("throttled" , {
- time: getDurationString(throttleData.allowRequestsAfter - Date.now()),
- httpStatus: throttleData.httpStatus
- });
- }
- }
- }
-
-
-
- function initializeAppCheck(app$1, options) {
- if (app$1 === void 0) { app$1 = app.getApp(); }
- app$1 = util.getModularInstance(app$1);
- var provider = app._getProvider(app$1, 'app-check');
-
- if (!getDebugState().initialized) {
- initializeDebugMode();
- }
-
-
- if (isDebugMode()) {
-
- void getDebugToken().then(function (token) {
-
- return console.log("App Check debug token: ".concat(token, ". You will need to add it to your app's App Check settings in the Firebase console for it to work."));
- });
- }
- if (provider.isInitialized()) {
- var existingInstance = provider.getImmediate();
- var initialOptions = provider.getOptions();
- if (initialOptions.isTokenAutoRefreshEnabled ===
- options.isTokenAutoRefreshEnabled &&
- initialOptions.provider.isEqual(options.provider)) {
- return existingInstance;
- }
- else {
- throw ERROR_FACTORY.create("already-initialized" , {
- appName: app$1.name
- });
- }
- }
- var appCheck = provider.initialize({ options: options });
- _activate(app$1, options.provider, options.isTokenAutoRefreshEnabled);
-
-
-
- if (getStateReference(app$1).isTokenAutoRefreshEnabled) {
-
-
-
-
-
- addTokenListener(appCheck, "INTERNAL" , function () { });
- }
- return appCheck;
- }
-
- function _activate(app, provider, isTokenAutoRefreshEnabled) {
-
-
- var state = setInitialState(app, tslib.__assign({}, DEFAULT_STATE));
- state.activated = true;
- state.provider = provider;
- state.cachedTokenPromise = readTokenFromStorage(app).then(function (cachedToken) {
- if (cachedToken && isValid(cachedToken)) {
- state.token = cachedToken;
-
- notifyTokenListeners(app, { token: cachedToken.token });
- }
- return cachedToken;
- });
-
-
-
- state.isTokenAutoRefreshEnabled =
- isTokenAutoRefreshEnabled === undefined
- ? app.automaticDataCollectionEnabled
- : isTokenAutoRefreshEnabled;
- state.provider.initialize(app);
- }
-
- function setTokenAutoRefreshEnabled(appCheckInstance, isTokenAutoRefreshEnabled) {
- var app = appCheckInstance.app;
- var state = getStateReference(app);
-
-
- if (state.tokenRefresher) {
- if (isTokenAutoRefreshEnabled === true) {
- state.tokenRefresher.start();
- }
- else {
- state.tokenRefresher.stop();
- }
- }
- state.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabled;
- }
-
- function getToken(appCheckInstance, forceRefresh) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var result;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0: return [4 , getToken$2(appCheckInstance, forceRefresh)];
- case 1:
- result = _a.sent();
- if (result.error) {
- throw result.error;
- }
- return [2 , { token: result.token }];
- }
- });
- });
- }
-
- function onTokenChanged(appCheckInstance, onNextOrObserver, onError,
- /**
- * NOTE: Although an `onCompletion` callback can be provided, it will
- * never be called because the token stream is never-ending.
- * It is added only for API consistency with the observer pattern, which
- * we follow in JS APIs.
- */
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- onCompletion) {
- var nextFn = function () { };
- var errorFn = function () { };
- if (onNextOrObserver.next != null) {
- nextFn = onNextOrObserver.next.bind(onNextOrObserver);
- }
- else {
- nextFn = onNextOrObserver;
- }
- if (onNextOrObserver.error != null) {
- errorFn = onNextOrObserver.error.bind(onNextOrObserver);
- }
- else if (onError) {
- errorFn = onError;
- }
- addTokenListener(appCheckInstance, "EXTERNAL" /* ListenerType.EXTERNAL */, nextFn, errorFn);
- return function () { return removeTokenListener(appCheckInstance.app, nextFn); };
- }
-
- /**
- * Firebase App Check
- *
- * @packageDocumentation
- */
- var APP_CHECK_NAME = 'app-check';
- var APP_CHECK_NAME_INTERNAL = 'app-check-internal';
- function registerAppCheck() {
- // The public interface
- app._registerComponent(new component.Component(APP_CHECK_NAME, function (container) {
- // getImmediate for FirebaseApp will always succeed
- var app = container.getProvider('app').getImmediate();
- var heartbeatServiceProvider = container.getProvider('heartbeat');
- return factory(app, heartbeatServiceProvider);
- }, "PUBLIC" /* ComponentType.PUBLIC */)
- .setInstantiationMode("EXPLICIT" /* InstantiationMode.EXPLICIT */)
- /**
- * Initialize app-check-internal after app-check is initialized to make AppCheck available to
- * other Firebase SDKs
- */
- .setInstanceCreatedCallback(function (container, _identifier, _appcheckService) {
- container.getProvider(APP_CHECK_NAME_INTERNAL).initialize();
- }));
- // The internal interface used by other Firebase products
- app._registerComponent(new component.Component(APP_CHECK_NAME_INTERNAL, function (container) {
- var appCheck = container.getProvider('app-check').getImmediate();
- return internalFactory(appCheck);
- }, "PUBLIC" /* ComponentType.PUBLIC */).setInstantiationMode("EXPLICIT" /* InstantiationMode.EXPLICIT */));
- app.registerVersion(name, version);
- }
- registerAppCheck();
-
- exports.CustomProvider = CustomProvider;
- exports.ReCaptchaEnterpriseProvider = ReCaptchaEnterpriseProvider;
- exports.ReCaptchaV3Provider = ReCaptchaV3Provider;
- exports.getToken = getToken;
- exports.initializeAppCheck = initializeAppCheck;
- exports.onTokenChanged = onTokenChanged;
- exports.setTokenAutoRefreshEnabled = setTokenAutoRefreshEnabled;
- //# sourceMappingURL=index.cjs.js.map
|