@sambath999/localize-token 12.4.9 → 12.4.11

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.
@@ -334,6 +334,16 @@
334
334
  enumerable: false,
335
335
  configurable: true
336
336
  });
337
+ Object.defineProperty(LocalizeTokenService.prototype, "clientId", {
338
+ get: function () { var _a; return LocalizeToken.storage.get(((_a = this.config.thirdPartyConfig) === null || _a === void 0 ? void 0 : _a.clientId) || ''); },
339
+ enumerable: false,
340
+ configurable: true
341
+ });
342
+ Object.defineProperty(LocalizeTokenService.prototype, "clientSecret", {
343
+ get: function () { var _a; return LocalizeToken.storage.get(((_a = this.config.thirdPartyConfig) === null || _a === void 0 ? void 0 : _a.clientSecret) || ''); },
344
+ enumerable: false,
345
+ configurable: true
346
+ });
337
347
  LocalizeTokenService.prototype.storageGet = function () {
338
348
  var _a;
339
349
  try {
@@ -927,18 +937,8 @@
927
937
  enumerable: false,
928
938
  configurable: true
929
939
  });
930
- Object.defineProperty(LocalizeApiService.prototype, "isThirdPartyMode", {
931
- get: function () { return this.localizeTokenService.config.isThirdPartyMode; },
932
- enumerable: false,
933
- configurable: true
934
- });
935
- Object.defineProperty(LocalizeApiService.prototype, "clientId", {
936
- get: function () { var _a; return (_a = this.localizeTokenService.config.thirdPartyConfig) === null || _a === void 0 ? void 0 : _a.clientId; },
937
- enumerable: false,
938
- configurable: true
939
- });
940
- Object.defineProperty(LocalizeApiService.prototype, "clientSecret", {
941
- get: function () { var _a; return (_a = this.localizeTokenService.config.thirdPartyConfig) === null || _a === void 0 ? void 0 : _a.clientSecret; },
940
+ Object.defineProperty(LocalizeApiService.prototype, "thirdPartyConfig", {
941
+ get: function () { var _a; return (_a = this.localizeTokenService.config) === null || _a === void 0 ? void 0 : _a.thirdPartyConfig; },
942
942
  enumerable: false,
943
943
  configurable: true
944
944
  });
@@ -1016,9 +1016,11 @@
1016
1016
  callback: function () { return tslib.__awaiter(_this, void 0, void 0, function () {
1017
1017
  return tslib.__generator(this, function (_e) {
1018
1018
  switch (_e.label) {
1019
- case 0:
1020
- // Only handle 401 Unauthorized errors
1021
- return [4 /*yield*/, this.revokeToken()];
1019
+ case 0:
1020
+ // Handle third party API unauthorized error when request
1021
+ this.handleThirdPartyAuthError(error, options.requestUrl);
1022
+ // Only handle 401 Unauthorized errors
1023
+ return [4 /*yield*/, this.revokeToken()];
1022
1024
  case 1:
1023
1025
  // Only handle 401 Unauthorized errors
1024
1026
  _e.sent();
@@ -1035,6 +1037,21 @@
1035
1037
  });
1036
1038
  });
1037
1039
  };
1040
+ LocalizeApiService.prototype.handleThirdPartyAuthError = function (error, requestUrl) {
1041
+ var _a, _b, _c;
1042
+ if (!((_a = this.thirdPartyConfig) === null || _a === void 0 ? void 0 : _a.isThirdPartyMode))
1043
+ return;
1044
+ if (error.status === 401) {
1045
+ if (this.thirdPartyConfig.lastUnauthorizedUrl === requestUrl) {
1046
+ this.thirdPartyConfig.lastUnauthorizedUrl = "";
1047
+ throw (_c = (_b = this.config).onException) === null || _c === void 0 ? void 0 : _c.call(_b, Object.assign(Object.assign({}, error), { status: 405, message: "Unauthorized request url " + requestUrl + ". Please contact administrator." }));
1048
+ }
1049
+ this.thirdPartyConfig.lastUnauthorizedUrl = requestUrl;
1050
+ }
1051
+ else {
1052
+ this.thirdPartyConfig.lastUnauthorizedUrl = "";
1053
+ }
1054
+ };
1038
1055
  LocalizeApiService.prototype.performRequest = function (options) {
1039
1056
  var _a;
1040
1057
  return tslib.__awaiter(this, void 0, void 0, function () {
@@ -1061,46 +1078,35 @@
1061
1078
  });
1062
1079
  };
1063
1080
  LocalizeApiService.prototype.revokeToken = function () {
1064
- var _a;
1081
+ var _a, _b;
1065
1082
  return tslib.__awaiter(this, void 0, void 0, function () {
1066
- var apiOptions, thirdPartyConfig, clientSecret, revokeToken, error_2;
1067
- var _e;
1068
- return tslib.__generator(this, function (_f) {
1069
- switch (_f.label) {
1083
+ var defaultApiOptions, thirdParyApiOptions, apiOptions, revokeToken, error_2;
1084
+ return tslib.__generator(this, function (_e) {
1085
+ switch (_e.label) {
1070
1086
  case 0:
1071
- _f.trys.push([0, 4, 6, 7]);
1087
+ _e.trys.push([0, 4, 6, 7]);
1072
1088
  return [4 /*yield*/, this.interceptRevokeToken()];
1073
1089
  case 1:
1074
- if (_f.sent())
1090
+ if (_e.sent())
1075
1091
  return [2 /*return*/];
1076
1092
  this.isRevokingToken = true;
1077
- apiOptions = Object.assign(Object.assign({}, this.buildApiOptions(((_a = this.localizeTokenService.config.refreshToken) === null || _a === void 0 ? void 0 : _a.requestUrl) || '')), { refreshToken: true });
1078
- // If in third party mode, use the third party token refresh endpoint and options
1079
- if (this.isThirdPartyMode) {
1080
- thirdPartyConfig = this.localizeTokenService.config.thirdPartyConfig;
1081
- if (thirdPartyConfig) {
1082
- // Add the request URL, method and body
1083
- apiOptions.requestUrl = thirdPartyConfig.refreshTokenUrl;
1084
- apiOptions.method = (thirdPartyConfig === null || thirdPartyConfig === void 0 ? void 0 : thirdPartyConfig.method) || exports.EMethod.GET;
1085
- apiOptions.requestBody = (thirdPartyConfig === null || thirdPartyConfig === void 0 ? void 0 : thirdPartyConfig.body) || {};
1086
- clientSecret = (_e = {}, _e[SCHEMES.X_SECRET] = "" + this.clientSecret, _e);
1087
- apiOptions.headers = Object.assign(Object.assign({}, apiOptions.headers), clientSecret);
1088
- }
1089
- }
1093
+ defaultApiOptions = Object.assign(Object.assign({}, this.buildApiOptions(((_a = this.localizeTokenService.config.refreshToken) === null || _a === void 0 ? void 0 : _a.requestUrl) || '')), { refreshToken: true });
1094
+ thirdParyApiOptions = this.buildThirdPartyApiOption(defaultApiOptions);
1095
+ apiOptions = ((_b = this.thirdPartyConfig) === null || _b === void 0 ? void 0 : _b.isThirdPartyMode) ? thirdParyApiOptions : defaultApiOptions;
1090
1096
  return [4 /*yield*/, ApiHelper.performRequestWithRetry(apiOptions, this.config, this.performRequest.bind(this))];
1091
1097
  case 2:
1092
- revokeToken = _f.sent();
1098
+ revokeToken = _e.sent();
1093
1099
  return [4 /*yield*/, this.handleOnTokenRevoked(revokeToken)];
1094
1100
  case 3:
1095
- _f.sent();
1101
+ _e.sent();
1096
1102
  return [3 /*break*/, 7];
1097
1103
  case 4:
1098
- error_2 = _f.sent();
1104
+ error_2 = _e.sent();
1099
1105
  // Handle the error, log it
1100
1106
  return [4 /*yield*/, ApiHelper.invokeHook(this.config.onAutoLogout)];
1101
1107
  case 5:
1102
1108
  // Handle the error, log it
1103
- _f.sent();
1109
+ _e.sent();
1104
1110
  throw error_2;
1105
1111
  case 6:
1106
1112
  // Reset the revoking token state
@@ -1111,12 +1117,16 @@
1111
1117
  });
1112
1118
  });
1113
1119
  };
1114
- LocalizeApiService.prototype.apiOptionsForThirdParty = function () {
1120
+ LocalizeApiService.prototype.buildThirdPartyApiOption = function (apiOptions) {
1121
+ var _e;
1122
+ var _a, _b, _c;
1123
+ return Object.assign(Object.assign({}, apiOptions), { requestUrl: ((_a = this.thirdPartyConfig) === null || _a === void 0 ? void 0 : _a.refreshToken.requestUrl) || '', method: ((_b = this.thirdPartyConfig) === null || _b === void 0 ? void 0 : _b.refreshToken.method) || exports.EMethod.GET, requestBody: ((_c = this.thirdPartyConfig) === null || _c === void 0 ? void 0 : _c.refreshToken.body) || {}, headers: Object.assign(Object.assign({}, apiOptions.headers), (_e = {}, _e[SCHEMES.X_SECRET] = "" + this.localizeTokenService.clientSecret, _e)) });
1115
1124
  };
1116
1125
  /** default http request options */
1117
1126
  LocalizeApiService.prototype.buildHeaderOptions = function (options) {
1118
1127
  var _e, _f, _g, _h, _j;
1119
- var headers = Object.assign(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))), (this.isThirdPartyMode && (_j = {}, _j[SCHEMES.X_CLIENT] = "" + this.clientId, _j)));
1128
+ var _a;
1129
+ var headers = Object.assign(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))), (((_a = this.thirdPartyConfig) === null || _a === void 0 ? void 0 : _a.isThirdPartyMode) && (_j = {}, _j[SCHEMES.X_CLIENT] = "" + this.localizeTokenService.clientId, _j)));
1120
1130
  return new i1.HttpHeaders(Object.assign(Object.assign({}, headers), options.headers));
1121
1131
  };
1122
1132
  LocalizeApiService.prototype.buildApiOptions = function (baseUrl, path, method, requestBody, headers) {
@@ -1159,23 +1169,18 @@
1159
1169
  return tslib.__generator(this, function (_e) {
1160
1170
  switch (_e.label) {
1161
1171
  case 0:
1162
- if (!(this.isThirdPartyMode && (response === null || response === void 0 ? void 0 : response.token))) return [3 /*break*/, 1];
1163
- // If request third party token refresh, the new access token will be in response.token.
1164
- this.accessToken = response.token;
1165
- return [3 /*break*/, 4];
1166
- case 1:
1167
- if (!(response === null || response === void 0 ? void 0 : response.status)) return [3 /*break*/, 2];
1172
+ if (!((response === null || response === void 0 ? void 0 : response.status) || (response === null || response === void 0 ? void 0 : response.token))) return [3 /*break*/, 1];
1168
1173
  // If the response is successful, update the access token
1169
- this.accessToken = response.message;
1170
- return [3 /*break*/, 4];
1171
- case 2:
1174
+ this.accessToken = response.message || response.token; // response.token for third party revoke
1175
+ return [3 /*break*/, 3];
1176
+ case 1:
1172
1177
  // If the response indicates an error, invoke the onRevokeUnauthorized hook
1173
1178
  console.warn('Token revocation failed, refresh token is expired.', response.message);
1174
1179
  return [4 /*yield*/, ApiHelper.invokeHook(this.config.onRevokeUnauthorized)];
1175
- case 3:
1180
+ case 2:
1176
1181
  _e.sent();
1177
- _e.label = 4;
1178
- case 4: return [2 /*return*/];
1182
+ _e.label = 3;
1183
+ case 3: return [2 /*return*/];
1179
1184
  }
1180
1185
  });
1181
1186
  });