@sambath999/localize-token 12.3.9 → 12.4.1

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.
@@ -575,8 +575,13 @@
575
575
  this.isRevokingTokenSubject = new rxjs.BehaviorSubject(false);
576
576
  this.defaultConfig = {
577
577
  mainDomain: extractMainDomain(),
578
- authTokenName: 'auth-token',
579
- refreshTokenName: '_lze_rftkp_',
578
+ authToken: {
579
+ name: 'auth-token',
580
+ },
581
+ refreshToken: {
582
+ name: 'refresh-token',
583
+ requestUrl: '/api/token/revoke'
584
+ },
580
585
  };
581
586
  this.isInitialized = false;
582
587
  this.decodeToken = function (token) { return jwt_decode__namespace.jwtDecode(token); };
@@ -601,15 +606,21 @@
601
606
  Object.defineProperty(LocalizeTokenService.prototype, "authToken", {
602
607
  get: function () { return this.storageGet(); },
603
608
  set: function (value) {
609
+ var _a;
604
610
  value
605
611
  ? this.storageSet(value)
606
- : LocalizeToken.storage.delete(this.config.authTokenName);
612
+ : LocalizeToken.storage.delete((_a = this.config.authToken) === null || _a === void 0 ? void 0 : _a.name);
607
613
  },
608
614
  enumerable: false,
609
615
  configurable: true
610
616
  });
617
+ Object.defineProperty(LocalizeTokenService.prototype, "tenantToken", {
618
+ get: function () { var _a; return LocalizeToken.storage.get(((_a = this.config.tenantToken) === null || _a === void 0 ? void 0 : _a.name) || ''); },
619
+ enumerable: false,
620
+ configurable: true
621
+ });
611
622
  Object.defineProperty(LocalizeTokenService.prototype, "refreshToken", {
612
- get: function () { return LocalizeToken.storage.get(this.config.refreshTokenName); },
623
+ get: function () { var _a; return LocalizeToken.storage.get(((_a = this.config.refreshToken) === null || _a === void 0 ? void 0 : _a.name) || ''); },
613
624
  enumerable: false,
614
625
  configurable: true
615
626
  });
@@ -630,33 +641,30 @@
630
641
  enumerable: false,
631
642
  configurable: true
632
643
  });
633
- LocalizeTokenService.prototype.tenantToken = function (name) {
634
- if (!name)
635
- return undefined;
636
- return LocalizeToken.storage.get(name);
637
- };
638
644
  LocalizeTokenService.prototype.storageGet = function () {
645
+ var _a;
639
646
  try {
640
- var encoded = LocalizeToken.storage.get(this.config.authTokenName);
647
+ var encoded = LocalizeToken.storage.get(((_a = this.config.authToken) === null || _a === void 0 ? void 0 : _a.name) || '');
641
648
  var decoded = atob(encoded || '');
642
649
  return JSON.parse(decoded);
643
650
  }
644
- catch (_a) {
651
+ catch (_b) {
645
652
  return undefined;
646
653
  }
647
654
  };
648
655
  LocalizeTokenService.prototype.storageSet = function (value) {
656
+ var _a;
649
657
  var base64 = btoa(JSON.stringify(value));
650
- LocalizeToken.storage.set(this.config.authTokenName, base64);
658
+ LocalizeToken.storage.set(((_a = this.config.authToken) === null || _a === void 0 ? void 0 : _a.name) || '', base64);
651
659
  };
652
- LocalizeTokenService.prototype.tokensValid = function (tenantTokenName) {
660
+ LocalizeTokenService.prototype.tokensValid = function () {
653
661
  var _a, _b;
654
662
  return !!((_a = this.refreshToken) === null || _a === void 0 ? void 0 : _a.length)
655
- && (!this.config.needTenant || !!((_b = this.tenantToken(tenantTokenName)) === null || _b === void 0 ? void 0 : _b.length));
663
+ && (!this.config.tenantToken || !!((_b = this.tenantToken) === null || _b === void 0 ? void 0 : _b.length));
656
664
  };
657
665
  Object.defineProperty(LocalizeTokenService.prototype, "decodeRefreshToken", {
658
666
  get: function () {
659
- var token = LocalizeToken.storage.get(this.config.refreshTokenName);
667
+ var token = this.refreshToken;
660
668
  return !token ? null : this.decodeToken(token);
661
669
  },
662
670
  enumerable: false,
@@ -930,7 +938,7 @@
930
938
  configurable: true
931
939
  });
932
940
  Object.defineProperty(LocalizeApiService.prototype, "needTenant", {
933
- get: function () { return this.localizeTokenService.config.needTenant; },
941
+ get: function () { return this.localizeTokenService.config.tenantToken !== undefined; },
934
942
  enumerable: false,
935
943
  configurable: true
936
944
  });
@@ -957,7 +965,7 @@
957
965
  configurable: true
958
966
  });
959
967
  Object.defineProperty(LocalizeApiService.prototype, "tenantToken", {
960
- get: function () { return this.localizeTokenService.tenantToken(this.config.tenantTokenName); },
968
+ get: function () { return this.localizeTokenService.tenantToken; },
961
969
  enumerable: false,
962
970
  configurable: true
963
971
  });
@@ -994,27 +1002,27 @@
994
1002
  return __awaiter(this, void 0, void 0, function () {
995
1003
  var apiOptions, error_1;
996
1004
  var _this = this;
997
- return __generator(this, function (_b) {
998
- switch (_b.label) {
1005
+ return __generator(this, function (_e) {
1006
+ switch (_e.label) {
999
1007
  case 0: return [4 /*yield*/, waitUntil(function () { return !_this.isResolvingStartup; }, 500)];
1000
1008
  case 1:
1001
- _b.sent();
1009
+ _e.sent();
1002
1010
  return [4 /*yield*/, ApiHelper.invokeHook(this.config.onPrepareRequest)];
1003
1011
  case 2:
1004
- _b.sent();
1012
+ _e.sent();
1005
1013
  apiOptions = this.buildApiOptions(baseUrl, path, method, reqBody, reqHeaders);
1006
- _b.label = 3;
1014
+ _e.label = 3;
1007
1015
  case 3:
1008
- _b.trys.push([3, 6, , 8]);
1016
+ _e.trys.push([3, 6, , 8]);
1009
1017
  return [4 /*yield*/, this.toWaitForPreviousRequest()];
1010
1018
  case 4:
1011
- _b.sent();
1019
+ _e.sent();
1012
1020
  return [4 /*yield*/, ApiHelper.performRequestWithRetry(apiOptions, this.config, this.performRequest.bind(this))];
1013
- case 5: return [2 /*return*/, _b.sent()];
1021
+ case 5: return [2 /*return*/, _e.sent()];
1014
1022
  case 6:
1015
- error_1 = _b.sent();
1023
+ error_1 = _e.sent();
1016
1024
  return [4 /*yield*/, this.handleOnRequestError(error_1, apiOptions)];
1017
- case 7: return [2 /*return*/, _b.sent()];
1025
+ case 7: return [2 /*return*/, _e.sent()];
1018
1026
  case 8: return [2 /*return*/];
1019
1027
  }
1020
1028
  });
@@ -1023,36 +1031,36 @@
1023
1031
  LocalizeApiService.prototype.handleOnRequestError = function (error, options) {
1024
1032
  return __awaiter(this, void 0, void 0, function () {
1025
1033
  var _this = this;
1026
- return __generator(this, function (_b) {
1027
- switch (_b.label) {
1034
+ return __generator(this, function (_e) {
1035
+ switch (_e.label) {
1028
1036
  case 0:
1029
1037
  if (error.status !== 401)
1030
1038
  throw error;
1031
1039
  return [4 /*yield*/, waitUntil(function () { return !_this.isResolvingStartup; }, 500)];
1032
1040
  case 1:
1033
- _b.sent();
1041
+ _e.sent();
1034
1042
  return [4 /*yield*/, ApiHelper.performRetry({
1035
1043
  maxRetries: function () { return 1000; },
1036
1044
  delay: 500,
1037
1045
  retryUnless: function (error) { return error.status === 401 || ApiHelper.isConnectionError(error); },
1038
1046
  callback: function () { return __awaiter(_this, void 0, void 0, function () {
1039
- return __generator(this, function (_b) {
1040
- switch (_b.label) {
1047
+ return __generator(this, function (_e) {
1048
+ switch (_e.label) {
1041
1049
  case 0:
1042
1050
  // Only handle 401 Unauthorized errors
1043
1051
  return [4 /*yield*/, this.revokeToken()];
1044
1052
  case 1:
1045
1053
  // Only handle 401 Unauthorized errors
1046
- _b.sent();
1054
+ _e.sent();
1047
1055
  return [4 /*yield*/, this.performRequest(options)];
1048
1056
  case 2:
1049
1057
  // Retry the request with the new access token
1050
- return [2 /*return*/, _b.sent()];
1058
+ return [2 /*return*/, _e.sent()];
1051
1059
  }
1052
1060
  });
1053
1061
  }); }
1054
1062
  })];
1055
- case 2: return [2 /*return*/, _b.sent()];
1063
+ case 2: return [2 /*return*/, _e.sent()];
1056
1064
  }
1057
1065
  });
1058
1066
  });
@@ -1061,8 +1069,8 @@
1061
1069
  return __awaiter(this, void 0, void 0, function () {
1062
1070
  var buildOptions, request$, response;
1063
1071
  var _this = this;
1064
- return __generator(this, function (_b) {
1065
- switch (_b.label) {
1072
+ return __generator(this, function (_e) {
1073
+ switch (_e.label) {
1066
1074
  case 0:
1067
1075
  buildOptions = { headers: this.buildHeaderOptions(options) };
1068
1076
  request$ = ApiHelper.createRequest({
@@ -1073,7 +1081,7 @@
1073
1081
  this.isRequestingSubject.next(true);
1074
1082
  return [4 /*yield*/, new Promise(function (resolve, reject) { return request$.subscribe({ next: function (res) { return resolve(res.body); }, error: reject }); })];
1075
1083
  case 1:
1076
- response = _b.sent();
1084
+ response = _e.sent();
1077
1085
  // Reset the isRequesting state after the request completes
1078
1086
  this.isRequestingSubject.next(false);
1079
1087
  return [2 /*return*/, response];
@@ -1082,32 +1090,33 @@
1082
1090
  });
1083
1091
  };
1084
1092
  LocalizeApiService.prototype.revokeToken = function () {
1093
+ var _a;
1085
1094
  return __awaiter(this, void 0, void 0, function () {
1086
1095
  var apiOptions, revokeToken, error_2;
1087
- return __generator(this, function (_b) {
1088
- switch (_b.label) {
1096
+ return __generator(this, function (_e) {
1097
+ switch (_e.label) {
1089
1098
  case 0:
1090
- _b.trys.push([0, 4, 6, 7]);
1099
+ _e.trys.push([0, 4, 6, 7]);
1091
1100
  return [4 /*yield*/, this.interceptRevokeToken()];
1092
1101
  case 1:
1093
- if (_b.sent())
1102
+ if (_e.sent())
1094
1103
  return [2 /*return*/];
1095
1104
  this.isRevokingToken = true;
1096
- apiOptions = Object.assign(Object.assign({}, this.buildApiOptions(this.localizeTokenService.config.revokeTokenUrl)), { refreshToken: true });
1105
+ apiOptions = Object.assign(Object.assign({}, this.buildApiOptions(((_a = this.localizeTokenService.config.refreshToken) === null || _a === void 0 ? void 0 : _a.requestUrl) || '')), { refreshToken: true });
1097
1106
  return [4 /*yield*/, ApiHelper.performRequestWithRetry(apiOptions, this.config, this.performRequest.bind(this))];
1098
1107
  case 2:
1099
- revokeToken = _b.sent();
1108
+ revokeToken = _e.sent();
1100
1109
  return [4 /*yield*/, this.handleOnTokenRevoked(revokeToken)];
1101
1110
  case 3:
1102
- _b.sent();
1111
+ _e.sent();
1103
1112
  return [3 /*break*/, 7];
1104
1113
  case 4:
1105
- error_2 = _b.sent();
1114
+ error_2 = _e.sent();
1106
1115
  // Handle the error, log it
1107
1116
  return [4 /*yield*/, ApiHelper.invokeHook(this.config.onAutoLogout)];
1108
1117
  case 5:
1109
1118
  // Handle the error, log it
1110
- _b.sent();
1119
+ _e.sent();
1111
1120
  return [3 /*break*/, 7];
1112
1121
  case 6:
1113
1122
  // Reset the revoking token state
@@ -1120,8 +1129,8 @@
1120
1129
  };
1121
1130
  /** default http request options */
1122
1131
  LocalizeApiService.prototype.buildHeaderOptions = function (options) {
1123
- var _b, _c, _d, _e;
1124
- var headers = Object.assign(Object.assign(Object.assign(Object.assign({}, (options.refreshToken && (_b = {}, _b[SCHEMES.X_REFRESH_TOKEN] = "" + this.refreshToken, _b))), (!options.isFormData && (_c = {}, _c[SCHEMES.CONTENT_TYPE] = 'application/json', _c))), (_d = {}, _d[SCHEMES.AUTHORIZATION] = "Bearer " + this.accessToken, _d)), (this.needTenant && (_e = {}, _e[SCHEMES.X_TENANT] = "" + this.tenantToken, _e)));
1132
+ var _e, _f, _g, _h;
1133
+ var headers = Object.assign(Object.assign(Object.assign(Object.assign({}, (options.refreshToken && (_e = {}, _e[SCHEMES.X_REFRESH_TOKEN] = "" + this.refreshToken, _e))), (!options.isFormData && (_f = {}, _f[SCHEMES.CONTENT_TYPE] = 'application/json', _f))), (_g = {}, _g[SCHEMES.AUTHORIZATION] = "Bearer " + this.accessToken, _g)), (this.needTenant && (_h = {}, _h[SCHEMES.X_TENANT] = "" + this.tenantToken, _h)));
1125
1134
  return new i1.HttpHeaders(Object.assign(Object.assign({}, headers), options.headers));
1126
1135
  };
1127
1136
  LocalizeApiService.prototype.buildApiOptions = function (baseUrl, path, method, requestBody, headers) {
@@ -1135,31 +1144,31 @@
1135
1144
  LocalizeApiService.prototype.toWaitForPreviousRequest = function () {
1136
1145
  var _a;
1137
1146
  return __awaiter(this, void 0, void 0, function () {
1138
- var _b, _c;
1147
+ var _e, _f;
1139
1148
  var _this = this;
1140
- return __generator(this, function (_d) {
1141
- switch (_d.label) {
1149
+ return __generator(this, function (_g) {
1150
+ switch (_g.label) {
1142
1151
  case 0:
1143
- _b = this.isRevokingToken;
1144
- if (!_b) return [3 /*break*/, 2];
1152
+ _e = this.isRevokingToken;
1153
+ if (!_e) return [3 /*break*/, 2];
1145
1154
  return [4 /*yield*/, waitUntil(function () { return !_this.isRevokingToken; })];
1146
1155
  case 1:
1147
- _b = (_d.sent());
1148
- _d.label = 2;
1156
+ _e = (_g.sent());
1157
+ _g.label = 2;
1149
1158
  case 2:
1150
- _b;
1159
+ _e;
1151
1160
  // to wait for each request in 50ms, even if the request is not completed
1152
- _c = ((_a = this.config.waitEachRequest) === null || _a === void 0 ? void 0 : _a.milliseconds);
1153
- if (!_c)
1161
+ _f = ((_a = this.config.waitEachRequest) === null || _a === void 0 ? void 0 : _a.milliseconds);
1162
+ if (!_f)
1154
1163
  // to wait for each request in 50ms, even if the request is not completed
1155
1164
  return [3 /*break*/, 4];
1156
1165
  return [4 /*yield*/, waitFor(this.config.waitEachRequest.milliseconds, this.isRequesting)];
1157
1166
  case 3:
1158
- _c = (_d.sent());
1159
- _d.label = 4;
1167
+ _f = (_g.sent());
1168
+ _g.label = 4;
1160
1169
  case 4:
1161
1170
  // to wait for each request in 50ms, even if the request is not completed
1162
- _c;
1171
+ _f;
1163
1172
  return [2 /*return*/];
1164
1173
  }
1165
1174
  });
@@ -1167,8 +1176,8 @@
1167
1176
  };
1168
1177
  LocalizeApiService.prototype.handleOnTokenRevoked = function (response) {
1169
1178
  return __awaiter(this, void 0, void 0, function () {
1170
- return __generator(this, function (_b) {
1171
- switch (_b.label) {
1179
+ return __generator(this, function (_e) {
1180
+ switch (_e.label) {
1172
1181
  case 0:
1173
1182
  if (!(response === null || response === void 0 ? void 0 : response.status)) return [3 /*break*/, 1];
1174
1183
  // If the response is successful, update the access token
@@ -1179,8 +1188,8 @@
1179
1188
  console.warn('Token revocation failed, refresh token is expired.', response.message);
1180
1189
  return [4 /*yield*/, ApiHelper.invokeHook(this.config.onRevokeUnauthorized)];
1181
1190
  case 2:
1182
- _b.sent();
1183
- _b.label = 3;
1191
+ _e.sent();
1192
+ _e.label = 3;
1184
1193
  case 3: return [2 /*return*/];
1185
1194
  }
1186
1195
  });
@@ -1189,14 +1198,14 @@
1189
1198
  LocalizeApiService.prototype.interceptRevokeToken = function () {
1190
1199
  return __awaiter(this, void 0, void 0, function () {
1191
1200
  var _this = this;
1192
- return __generator(this, function (_b) {
1193
- switch (_b.label) {
1201
+ return __generator(this, function (_e) {
1202
+ switch (_e.label) {
1194
1203
  case 0:
1195
1204
  if (!this.isRevokingToken) return [3 /*break*/, 2];
1196
1205
  console.warn('Token is already being revoked. Waiting for the current operation to complete...');
1197
1206
  return [4 /*yield*/, waitUntil(function () { return !_this.isRevokingToken; })];
1198
1207
  case 1:
1199
- _b.sent();
1208
+ _e.sent();
1200
1209
  return [2 /*return*/, true];
1201
1210
  case 2:
1202
1211
  if (!this.refreshToken) {
@@ -1209,12 +1218,12 @@
1209
1218
  });
1210
1219
  };
1211
1220
  LocalizeApiService.prototype.validateConfig = function () {
1212
- var _a;
1213
- var config = this.configSubject.value;
1214
- if (this.localizeTokenService.config.needTenant && !((_a = config.tenantTokenName) === null || _a === void 0 ? void 0 : _a.trim().length)) {
1221
+ var _a, _b, _c, _d;
1222
+ if (this.localizeTokenService.config.tenantToken
1223
+ && !((_b = (_a = this.localizeTokenService.config.tenantToken) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.trim().length)) {
1215
1224
  throw Error('Tenant token is required but tenantTokenName is not configured');
1216
1225
  }
1217
- if (!this.localizeTokenService.config.revokeTokenUrl.trim().length) {
1226
+ if (!((_d = (_c = this.localizeTokenService.config.refreshToken) === null || _c === void 0 ? void 0 : _c.requestUrl) === null || _d === void 0 ? void 0 : _d.trim().length)) {
1218
1227
  throw Error('Revoke token URL is not configured - token refresh will not work');
1219
1228
  }
1220
1229
  };
@@ -1282,7 +1291,7 @@
1282
1291
  this.cdt.detectChanges();
1283
1292
  };
1284
1293
  LocalizeLogindlgComponent.prototype.clickLogin = function () {
1285
- var _a, _b;
1294
+ var _a, _b, _c;
1286
1295
  return __awaiter(this, void 0, void 0, function () {
1287
1296
  var loginRes, cookieOptions;
1288
1297
  var _this = this;
@@ -1301,7 +1310,7 @@
1301
1310
  }
1302
1311
  this.tokenService.accessToken = loginRes.tokens.accessToken;
1303
1312
  cookieOptions = { expires: (_b = this.loginConfig.expire) !== null && _b !== void 0 ? _b : 365 };
1304
- LocalizeToken.storage.set(this.config.refreshTokenName, loginRes.tokens.refreshToken, cookieOptions);
1313
+ LocalizeToken.storage.set(((_c = this.config.refreshToken) === null || _c === void 0 ? void 0 : _c.name) || '', loginRes.tokens.refreshToken, cookieOptions);
1305
1314
  this.success = true;
1306
1315
  setTimeout(function () {
1307
1316
  _this.dlgConfig.dismissableMask = true;
@@ -1347,8 +1356,8 @@
1347
1356
  var _d;
1348
1357
  var _a, _b;
1349
1358
  return _d = {},
1350
- _d[LocalizeToken.httpHeaders.X_REFRESH_TOKEN] = (_a = LocalizeToken.storage.get(this.config.refreshTokenName)) !== null && _a !== void 0 ? _a : "",
1351
- _d[LocalizeToken.httpHeaders.X_TENANT] = (_b = LocalizeToken.storage.get(this.loginConfig.tenantTokenName)) !== null && _b !== void 0 ? _b : "",
1359
+ _d[LocalizeToken.httpHeaders.X_REFRESH_TOKEN] = (_a = this.tokenService.refreshToken) !== null && _a !== void 0 ? _a : "",
1360
+ _d[LocalizeToken.httpHeaders.X_TENANT] = (_b = this.tokenService.tenantToken) !== null && _b !== void 0 ? _b : "",
1352
1361
  _d;
1353
1362
  };
1354
1363
  Object.defineProperty(LocalizeLogindlgComponent.prototype, "isValidPassword", {