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