@edgeiq/edgeiq-api-js 1.7.5 → 1.7.6
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/OIDC/index.js +137 -238
- package/dist/OIDC/models.js +1 -2
- package/dist/__tests__/auth.test.js +56 -147
- package/dist/__tests__/command.test.js +80 -202
- package/dist/__tests__/companies.test.js +121 -269
- package/dist/__tests__/deviceErrors.test.js +16 -62
- package/dist/__tests__/deviceTypes.test.js +78 -200
- package/dist/__tests__/devices.test.js +84 -221
- package/dist/__tests__/discoveredDevice.test.js +57 -145
- package/dist/__tests__/files.test.js +58 -152
- package/dist/__tests__/ingestors.test.js +70 -188
- package/dist/__tests__/integrations.test.js +95 -235
- package/dist/__tests__/lwm2m.test.js +17 -62
- package/dist/__tests__/notifications.test.js +16 -62
- package/dist/__tests__/pollableAttributes.test.js +60 -162
- package/dist/__tests__/rules.test.js +60 -162
- package/dist/__tests__/scheduledJobs.test.js +60 -162
- package/dist/__tests__/translators.test.js +60 -162
- package/dist/__tests__/users.test.js +77 -197
- package/dist/auth/index.js +94 -167
- package/dist/bulkResponses/index.js +44 -110
- package/dist/bulkResponses/models.js +1 -2
- package/dist/commandExecutions/index.js +39 -85
- package/dist/commandExecutions/models.js +1 -2
- package/dist/commands/index.js +196 -342
- package/dist/commands/models.js +2 -5
- package/dist/companies/index.js +150 -272
- package/dist/companies/models.js +1 -2
- package/dist/configurations/index.js +172 -310
- package/dist/configurations/models.js +1 -2
- package/dist/constants.js +69 -85
- package/dist/core/ModelClass.js +11 -18
- package/dist/core/handleResponseError.js +77 -81
- package/dist/core/index.js +30 -38
- package/dist/deviceConfigs/index.js +103 -201
- package/dist/deviceConfigs/models.js +1 -2
- package/dist/deviceErrors/index.js +44 -110
- package/dist/deviceErrors/models.js +1 -2
- package/dist/deviceLocationObservations/index.js +82 -164
- package/dist/deviceLocationObservations/models.js +1 -2
- package/dist/deviceTemplates/index.js +105 -203
- package/dist/deviceTemplates/models.js +1 -2
- package/dist/deviceTransferRequests/index.js +116 -222
- package/dist/deviceTransferRequests/models.js +1 -2
- package/dist/deviceTypes/index.js +328 -554
- package/dist/deviceTypes/models.js +1 -2
- package/dist/devices/index.js +630 -971
- package/dist/devices/models.js +1 -2
- package/dist/discoveredDevices/index.js +190 -328
- package/dist/discoveredDevices/models.js +1 -2
- package/dist/downstreamSystems/index.js +87 -177
- package/dist/downstreamSystems/models.js +1 -2
- package/dist/escrowDevices/index.js +103 -201
- package/dist/escrowDevices/models.js +1 -2
- package/dist/files/index.js +137 -251
- package/dist/files/models.js +1 -2
- package/dist/filtersParser.js +12 -17
- package/dist/gatewayCommands/index.js +103 -201
- package/dist/gatewayCommands/models.js +1 -2
- package/dist/helpers.js +10 -16
- package/dist/index.js +86 -142
- package/dist/ingestors/index.js +103 -201
- package/dist/ingestors/models.js +1 -2
- package/dist/integrations/index.js +159 -273
- package/dist/integrations/models.js +1 -2
- package/dist/log/models.js +1 -2
- package/dist/lwm2m/index.js +23 -72
- package/dist/lwm2m/models.js +1 -2
- package/dist/models/index.js +1 -2
- package/dist/models.js +1 -2
- package/dist/notifications/index.js +44 -110
- package/dist/notifications/models.js +1 -2
- package/dist/pollableAttributes/index.js +87 -177
- package/dist/pollableAttributes/models.js +1 -2
- package/dist/regions/index.js +72 -154
- package/dist/regions/models.js +1 -2
- package/dist/reports/index.js +88 -178
- package/dist/reports/models.js +1 -2
- package/dist/retryOptions/models.js +1 -2
- package/dist/rules/index.js +131 -245
- package/dist/rules/models.js +1 -2
- package/dist/scheduledJobs/index.js +183 -321
- package/dist/scheduledJobs/models.js +1 -2
- package/dist/secrets/index.js +88 -178
- package/dist/secrets/models.js +1 -2
- package/dist/settings/index.js +119 -227
- package/dist/settings/models.js +1 -2
- package/dist/settingsApplicationRecord/index.js +27 -65
- package/dist/settingsApplicationRecord/models.js +1 -2
- package/dist/softwareUpdates/index.js +194 -350
- package/dist/softwareUpdates/models.js +1 -2
- package/dist/stats/index.js +64 -126
- package/dist/stats/models.js +1 -2
- package/dist/systemCommandJobs/index.js +72 -154
- package/dist/systemCommandJobs/models.js +1 -2
- package/dist/systemCommands/index.js +87 -177
- package/dist/systemCommands/models.js +1 -2
- package/dist/testConstants.js +57 -71
- package/dist/translators/index.js +103 -201
- package/dist/translators/models.js +1 -2
- package/dist/userTypes/index.js +87 -177
- package/dist/userTypes/models.js +1 -2
- package/dist/users/index.js +149 -271
- package/dist/users/models.js +1 -2
- package/dist/workflowActions/index.js +132 -246
- package/dist/workflowActions/models.js +1 -2
- package/dist/workflowDefinitions/index.js +104 -202
- package/dist/workflowDefinitions/models.js +1 -2
- package/dist/workflowExecutions/index.js +27 -65
- package/dist/workflowExecutions/models.js +1 -2
- package/dist/workflows/index.js +39 -85
- package/dist/workflows/models.js +1 -2
- package/package.json +1 -1
package/dist/OIDC/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -23,249 +7,164 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
23
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
8
|
});
|
|
25
9
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
-
if (t[2]) _.ops.pop();
|
|
46
|
-
_.trys.pop(); continue;
|
|
47
|
-
}
|
|
48
|
-
op = body.call(thisArg, _);
|
|
49
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
54
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
55
|
-
};
|
|
56
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
-
exports.OIDC = void 0;
|
|
58
|
-
var jwt_decode_1 = require("jwt-decode");
|
|
59
|
-
var axios_1 = __importDefault(require("axios"));
|
|
60
|
-
var ModelClass_1 = require("../core/ModelClass");
|
|
61
|
-
var crypto_js_1 = require("crypto-js");
|
|
62
|
-
var ACCESS_TOKEN = 'EIQ_TOKEN';
|
|
63
|
-
var AUTH_CODE_VERIFIER = 'authCodeVerifier';
|
|
64
|
-
var AUTH_STATE = 'authState';
|
|
65
|
-
var REFRESH_TOKEN = 'refreshToken';
|
|
66
|
-
var OIDC = (function (_super) {
|
|
67
|
-
__extends(OIDC, _super);
|
|
68
|
-
function OIDC(options) {
|
|
69
|
-
var _this = _super.call(this) || this;
|
|
70
|
-
_this.authority = options.authority;
|
|
71
|
-
_this.client_id = options.client_id;
|
|
72
|
-
_this.redirect_uri = options.redirect_uri;
|
|
73
|
-
_this.response_type = options.response_type;
|
|
74
|
-
_this.scope = options.scope;
|
|
75
|
-
_this.post_logout_redirect_uri = options.post_logout_redirect_uri;
|
|
76
|
-
_this.api = axios_1.default.create({
|
|
10
|
+
import { jwtDecode } from 'jwt-decode';
|
|
11
|
+
import axios from 'axios';
|
|
12
|
+
import { BaseModelClass } from '../core/ModelClass';
|
|
13
|
+
import { SHA256, enc } from 'crypto-js';
|
|
14
|
+
const ACCESS_TOKEN = 'EIQ_TOKEN';
|
|
15
|
+
const AUTH_CODE_VERIFIER = 'authCodeVerifier';
|
|
16
|
+
const AUTH_STATE = 'authState';
|
|
17
|
+
const REFRESH_TOKEN = 'refreshToken';
|
|
18
|
+
export class OIDC extends BaseModelClass {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
super();
|
|
21
|
+
this.authority = options.authority;
|
|
22
|
+
this.client_id = options.client_id;
|
|
23
|
+
this.redirect_uri = options.redirect_uri;
|
|
24
|
+
this.response_type = options.response_type;
|
|
25
|
+
this.scope = options.scope;
|
|
26
|
+
this.post_logout_redirect_uri = options.post_logout_redirect_uri;
|
|
27
|
+
this.api = axios.create({
|
|
77
28
|
baseURL: options.authority,
|
|
78
29
|
});
|
|
79
|
-
|
|
80
|
-
return _this;
|
|
30
|
+
this.setupAxiosInterceptor();
|
|
81
31
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return String.fromCharCode(Math.floor(Math.random() * 94) + 33);
|
|
85
|
-
}).join('');
|
|
32
|
+
generateCodeVerifier() {
|
|
33
|
+
const codeVerifier = Array.from({ length: 128 }, () => String.fromCharCode(Math.floor(Math.random() * 94) + 33)).join('');
|
|
86
34
|
return codeVerifier;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
35
|
+
}
|
|
36
|
+
generateCodeChallenge(codeVerifier) {
|
|
37
|
+
const hashed = SHA256(codeVerifier);
|
|
38
|
+
const base64encoded = hashed.toString(enc.Base64);
|
|
39
|
+
const codeChallenge = base64encoded
|
|
92
40
|
.replace(/\+/g, '-')
|
|
93
41
|
.replace(/\//g, '_')
|
|
94
42
|
.replace(/=/g, '');
|
|
95
43
|
return codeChallenge;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
url = this.authority + "/oauth/v2/authorize?client_id=" + this.client_id + "&redirect_uri=" + encodeURIComponent(this.redirect_uri) + "&response_type=" + this.response_type + "&scope=" + this.scope + "&state=" + state + "&code_challenge=" + codeChallenge + "&code_challenge_method=S256";
|
|
109
|
-
window.location.href = url;
|
|
110
|
-
return [2];
|
|
111
|
-
});
|
|
44
|
+
}
|
|
45
|
+
login() {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
BaseModelClass.logAction('User logs in to OIDC');
|
|
48
|
+
const codeVerifier = this.generateCodeVerifier();
|
|
49
|
+
const codeChallenge = this.generateCodeChallenge(codeVerifier);
|
|
50
|
+
const state = Math.random().toString(36).substring(2, 15) +
|
|
51
|
+
Math.random().toString(36).substring(2, 15);
|
|
52
|
+
localStorage.setItem(AUTH_CODE_VERIFIER, codeVerifier);
|
|
53
|
+
localStorage.setItem(AUTH_STATE, state);
|
|
54
|
+
const url = `${this.authority}/oauth/v2/authorize?client_id=${this.client_id}&redirect_uri=${encodeURIComponent(this.redirect_uri)}&response_type=${this.response_type}&scope=${this.scope}&state=${state}&code_challenge=${codeChallenge}&code_challenge_method=S256`;
|
|
55
|
+
window.location.href = url;
|
|
112
56
|
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (!code) return [3, 2];
|
|
124
|
-
codeVerifier = localStorage.getItem(AUTH_CODE_VERIFIER);
|
|
125
|
-
if (!codeVerifier) {
|
|
126
|
-
throw new Error('Code verifier not found');
|
|
127
|
-
}
|
|
128
|
-
headers = {
|
|
129
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
130
|
-
};
|
|
131
|
-
formData = new URLSearchParams();
|
|
132
|
-
formData.append('code', code);
|
|
133
|
-
formData.append('redirect_uri', this.redirect_uri);
|
|
134
|
-
formData.append('client_id', this.client_id);
|
|
135
|
-
formData.append('grant_type', 'authorization_code');
|
|
136
|
-
formData.append('code_verifier', codeVerifier);
|
|
137
|
-
return [4, axios_1.default.post(this.authority + "/oauth/v2/token", formData.toString(), {
|
|
138
|
-
headers: headers,
|
|
139
|
-
})];
|
|
140
|
-
case 1:
|
|
141
|
-
tokenResponse = _a.sent();
|
|
142
|
-
localStorage.setItem(REFRESH_TOKEN, tokenResponse.data.refresh_token);
|
|
143
|
-
return [2, tokenResponse.data];
|
|
144
|
-
case 2:
|
|
145
|
-
ModelClass_1.BaseModelClass.logError('Authorization code not found');
|
|
146
|
-
throw new Error('Authorization code not found');
|
|
57
|
+
}
|
|
58
|
+
signInRedirectCallback() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
BaseModelClass.logAction('user redirected back to the callback url');
|
|
61
|
+
const params = new URLSearchParams(window.location.search);
|
|
62
|
+
const code = params.get('code');
|
|
63
|
+
if (code) {
|
|
64
|
+
const codeVerifier = localStorage.getItem(AUTH_CODE_VERIFIER);
|
|
65
|
+
if (!codeVerifier) {
|
|
66
|
+
throw new Error('Code verifier not found');
|
|
147
67
|
}
|
|
148
|
-
|
|
68
|
+
const headers = {
|
|
69
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
70
|
+
};
|
|
71
|
+
const formData = new URLSearchParams();
|
|
72
|
+
formData.append('code', code);
|
|
73
|
+
formData.append('redirect_uri', this.redirect_uri);
|
|
74
|
+
formData.append('client_id', this.client_id);
|
|
75
|
+
formData.append('grant_type', 'authorization_code');
|
|
76
|
+
formData.append('code_verifier', codeVerifier);
|
|
77
|
+
const tokenResponse = yield axios.post(`${this.authority}/oauth/v2/token`, formData.toString(), {
|
|
78
|
+
headers,
|
|
79
|
+
});
|
|
80
|
+
localStorage.setItem(REFRESH_TOKEN, tokenResponse.data.refresh_token);
|
|
81
|
+
return tokenResponse.data;
|
|
82
|
+
}
|
|
83
|
+
BaseModelClass.logError('Authorization code not found');
|
|
84
|
+
throw new Error('Authorization code not found');
|
|
149
85
|
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
url = endSessionEndpoint + "?post_logout_redirect_uri=" + encodeURIComponent(this.post_logout_redirect_uri) + "&client_id=" + clientId;
|
|
163
|
-
window.location.href = url;
|
|
164
|
-
return [2];
|
|
165
|
-
});
|
|
86
|
+
}
|
|
87
|
+
logout() {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
BaseModelClass.logAction('User logged out from OIDC');
|
|
90
|
+
localStorage.removeItem(AUTH_CODE_VERIFIER);
|
|
91
|
+
localStorage.removeItem(AUTH_STATE);
|
|
92
|
+
localStorage.removeItem(REFRESH_TOKEN);
|
|
93
|
+
localStorage.removeItem(ACCESS_TOKEN);
|
|
94
|
+
const endSessionEndpoint = `${this.authority}/oidc/v1/end_session`;
|
|
95
|
+
const clientId = encodeURIComponent(this.client_id);
|
|
96
|
+
const url = `${endSessionEndpoint}?post_logout_redirect_uri=${encodeURIComponent(this.post_logout_redirect_uri)}&client_id=${clientId}`;
|
|
97
|
+
window.location.href = url;
|
|
166
98
|
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
_a.label = 2;
|
|
181
|
-
case 2:
|
|
182
|
-
userInfoEndpoint = this.authority + "/oidc/v1/userinfo";
|
|
183
|
-
return [4, this.api.get(userInfoEndpoint, {
|
|
184
|
-
headers: {
|
|
185
|
-
Authorization: "Bearer " + accessToken,
|
|
186
|
-
},
|
|
187
|
-
})];
|
|
188
|
-
case 3:
|
|
189
|
-
userInfoResponse = _a.sent();
|
|
190
|
-
return [2, userInfoResponse.data];
|
|
191
|
-
}
|
|
99
|
+
}
|
|
100
|
+
getUser() {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
BaseModelClass.logAction('fetched user data from OIDC');
|
|
103
|
+
const accessToken = localStorage.getItem(ACCESS_TOKEN);
|
|
104
|
+
if (!accessToken) {
|
|
105
|
+
yield this.signInRedirectCallback();
|
|
106
|
+
}
|
|
107
|
+
const userInfoEndpoint = `${this.authority}/oidc/v1/userinfo`;
|
|
108
|
+
const userInfoResponse = yield this.api.get(userInfoEndpoint, {
|
|
109
|
+
headers: {
|
|
110
|
+
Authorization: `Bearer ${accessToken}`,
|
|
111
|
+
},
|
|
192
112
|
});
|
|
113
|
+
return userInfoResponse.data;
|
|
193
114
|
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
115
|
+
}
|
|
116
|
+
isAccessTokenExpired() {
|
|
117
|
+
const accessToken = localStorage.getItem(ACCESS_TOKEN);
|
|
197
118
|
if (!accessToken) {
|
|
198
119
|
throw new Error('Access token is not available.');
|
|
199
120
|
}
|
|
200
|
-
|
|
121
|
+
const decodedToken = jwtDecode(accessToken);
|
|
201
122
|
if (decodedToken.exp && typeof decodedToken.exp === 'number') {
|
|
202
|
-
|
|
203
|
-
|
|
123
|
+
const expirationTimeMs = decodedToken.exp * 1000;
|
|
124
|
+
const currentTimeMs = new Date().getTime();
|
|
204
125
|
return currentTimeMs >= expirationTimeMs;
|
|
205
126
|
}
|
|
206
127
|
throw new Error('Invalid access token format.');
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
skipInterceptor: true,
|
|
226
|
-
};
|
|
227
|
-
return [4, this.api.post('/oauth/v2/token', formData.toString(), {
|
|
228
|
-
headers: headers,
|
|
229
|
-
})];
|
|
230
|
-
case 1:
|
|
231
|
-
tokenResponse = _a.sent();
|
|
232
|
-
localStorage.setItem(ACCESS_TOKEN, tokenResponse.data.access_token);
|
|
233
|
-
localStorage.setItem(REFRESH_TOKEN, tokenResponse.data.refresh_token);
|
|
234
|
-
return [2, tokenResponse.data.access_token];
|
|
235
|
-
}
|
|
128
|
+
}
|
|
129
|
+
refreshAccessToken() {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
BaseModelClass.logAction('Fetch a new access token using the refresh token.');
|
|
132
|
+
const refreshToken = localStorage.getItem(REFRESH_TOKEN);
|
|
133
|
+
if (!refreshToken) {
|
|
134
|
+
throw new Error('Refresh token not found.');
|
|
135
|
+
}
|
|
136
|
+
const formData = new URLSearchParams();
|
|
137
|
+
formData.append('refresh_token', `${refreshToken}1213232`);
|
|
138
|
+
formData.append('grant_type', 'refresh_token');
|
|
139
|
+
formData.append('client_id', this.client_id);
|
|
140
|
+
const headers = {
|
|
141
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
142
|
+
skipInterceptor: true,
|
|
143
|
+
};
|
|
144
|
+
const tokenResponse = yield this.api.post('/oauth/v2/token', formData.toString(), {
|
|
145
|
+
headers,
|
|
236
146
|
});
|
|
147
|
+
localStorage.setItem(ACCESS_TOKEN, tokenResponse.data.access_token);
|
|
148
|
+
localStorage.setItem(REFRESH_TOKEN, tokenResponse.data.refresh_token);
|
|
149
|
+
return tokenResponse.data.access_token;
|
|
237
150
|
});
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
return [2, config];
|
|
248
|
-
}
|
|
249
|
-
if (!this.isAccessTokenExpired()) return [3, 5];
|
|
250
|
-
_a.label = 1;
|
|
251
|
-
case 1:
|
|
252
|
-
_a.trys.push([1, 3, , 5]);
|
|
253
|
-
return [4, this.refreshAccessToken()];
|
|
254
|
-
case 2:
|
|
255
|
-
_a.sent();
|
|
256
|
-
return [3, 5];
|
|
257
|
-
case 3:
|
|
258
|
-
error_1 = _a.sent();
|
|
259
|
-
ModelClass_1.BaseModelClass.logError('Failed to refresh access token');
|
|
260
|
-
return [4, this.logout()];
|
|
261
|
-
case 4:
|
|
262
|
-
_a.sent();
|
|
263
|
-
throw new Error('User logged out due to expired session.');
|
|
264
|
-
case 5: return [2, config];
|
|
151
|
+
}
|
|
152
|
+
setupAxiosInterceptor() {
|
|
153
|
+
this.api.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
if (config.headers.skipInterceptor) {
|
|
155
|
+
return config;
|
|
156
|
+
}
|
|
157
|
+
if (this.isAccessTokenExpired()) {
|
|
158
|
+
try {
|
|
159
|
+
yield this.refreshAccessToken();
|
|
265
160
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
161
|
+
catch (error) {
|
|
162
|
+
BaseModelClass.logError('Failed to refresh access token');
|
|
163
|
+
yield this.logout();
|
|
164
|
+
throw new Error('User logged out due to expired session.');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return config;
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
}
|
package/dist/OIDC/models.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,152 +7,62 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
10
|
+
import { EdgeIQAPI } from '..';
|
|
11
|
+
import { Authentication } from '..';
|
|
12
|
+
import { ResponseErrorsMessages } from '../constants';
|
|
13
|
+
import { isApiError } from '../helpers';
|
|
14
|
+
import { stageURL, TestAuth } from '../testConstants';
|
|
15
|
+
describe('Authentication', () => {
|
|
16
|
+
EdgeIQAPI.init(stageURL);
|
|
17
|
+
it('Wrong user', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
try {
|
|
19
|
+
yield Authentication.login(TestAuth.noUser, TestAuth.noUser);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (isApiError(error)) {
|
|
23
|
+
expect(error.message).toBe(ResponseErrorsMessages.noUser);
|
|
32
24
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
var helpers_1 = require("../helpers");
|
|
43
|
-
var testConstants_1 = require("../testConstants");
|
|
44
|
-
describe('Authentication', function () {
|
|
45
|
-
__1.EdgeIQAPI.init(testConstants_1.stageURL);
|
|
46
|
-
it('Wrong user', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
47
|
-
var error_1;
|
|
48
|
-
return __generator(this, function (_a) {
|
|
49
|
-
switch (_a.label) {
|
|
50
|
-
case 0:
|
|
51
|
-
_a.trys.push([0, 2, , 3]);
|
|
52
|
-
return [4, __2.Authentication.login(testConstants_1.TestAuth.noUser, testConstants_1.TestAuth.noUser)];
|
|
53
|
-
case 1:
|
|
54
|
-
_a.sent();
|
|
55
|
-
return [3, 3];
|
|
56
|
-
case 2:
|
|
57
|
-
error_1 = _a.sent();
|
|
58
|
-
if ((0, helpers_1.isApiError)(error_1)) {
|
|
59
|
-
expect(error_1.message).toBe(constants_1.ResponseErrorsMessages.noUser);
|
|
60
|
-
}
|
|
61
|
-
return [3, 3];
|
|
62
|
-
case 3: return [2];
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}); });
|
|
66
|
-
it('Wrong password', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
67
|
-
var error_2;
|
|
68
|
-
return __generator(this, function (_a) {
|
|
69
|
-
switch (_a.label) {
|
|
70
|
-
case 0:
|
|
71
|
-
_a.trys.push([0, 2, , 3]);
|
|
72
|
-
return [4, __2.Authentication.login(testConstants_1.TestAuth.username, testConstants_1.TestAuth.noUser)];
|
|
73
|
-
case 1:
|
|
74
|
-
_a.sent();
|
|
75
|
-
return [3, 3];
|
|
76
|
-
case 2:
|
|
77
|
-
error_2 = _a.sent();
|
|
78
|
-
if ((0, helpers_1.isApiError)(error_2)) {
|
|
79
|
-
expect(error_2.message).toBe(constants_1.ResponseErrorsMessages.invalidPassword);
|
|
80
|
-
}
|
|
81
|
-
return [3, 3];
|
|
82
|
-
case 3: return [2];
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}); });
|
|
86
|
-
it('Correct user and password', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
87
|
-
var loggedUser;
|
|
88
|
-
return __generator(this, function (_a) {
|
|
89
|
-
switch (_a.label) {
|
|
90
|
-
case 0: return [4, __2.Authentication.login(testConstants_1.TestAuth.username, testConstants_1.TestAuth.pass)];
|
|
91
|
-
case 1:
|
|
92
|
-
loggedUser = _a.sent();
|
|
93
|
-
expect(loggedUser.session_token).not.toBeNull();
|
|
94
|
-
return [2];
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}); });
|
|
98
|
-
it('Gets logged user data', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
99
|
-
var result;
|
|
100
|
-
return __generator(this, function (_a) {
|
|
101
|
-
switch (_a.label) {
|
|
102
|
-
case 0: return [4, __2.Authentication.me()];
|
|
103
|
-
case 1:
|
|
104
|
-
result = _a.sent();
|
|
105
|
-
expect(result._id).not.toBeNull();
|
|
106
|
-
return [2];
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}); });
|
|
110
|
-
it("Doesn't allow token expiration to be set longer than a week", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
111
|
-
var oneWeekOneSecond, error_3, err, error_4, err;
|
|
112
|
-
return __generator(this, function (_a) {
|
|
113
|
-
switch (_a.label) {
|
|
114
|
-
case 0:
|
|
115
|
-
oneWeekOneSecond = 604801000;
|
|
116
|
-
_a.label = 1;
|
|
117
|
-
case 1:
|
|
118
|
-
_a.trys.push([1, 3, , 4]);
|
|
119
|
-
return [4, __2.Authentication.login(testConstants_1.TestAuth.username, testConstants_1.TestAuth.noUser, oneWeekOneSecond)];
|
|
120
|
-
case 2:
|
|
121
|
-
_a.sent();
|
|
122
|
-
return [3, 4];
|
|
123
|
-
case 3:
|
|
124
|
-
error_3 = _a.sent();
|
|
125
|
-
err = error_3;
|
|
126
|
-
expect(err.message).toBe(constants_1.ResponseErrorsMessages.invalidTokenExpiration);
|
|
127
|
-
return [3, 4];
|
|
128
|
-
case 4:
|
|
129
|
-
_a.trys.push([4, 6, , 7]);
|
|
130
|
-
return [4, __2.Authentication.login(testConstants_1.TestAuth.username, testConstants_1.TestAuth.noUser, 0)];
|
|
131
|
-
case 5:
|
|
132
|
-
_a.sent();
|
|
133
|
-
return [3, 7];
|
|
134
|
-
case 6:
|
|
135
|
-
error_4 = _a.sent();
|
|
136
|
-
err = error_4;
|
|
137
|
-
expect(err.message).toBe(constants_1.ResponseErrorsMessages.invalidTokenExpiration);
|
|
138
|
-
return [3, 7];
|
|
139
|
-
case 7: return [2];
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}); });
|
|
143
|
-
it('Resets user API token', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
144
|
-
var loggedUser, result;
|
|
145
|
-
return __generator(this, function (_a) {
|
|
146
|
-
switch (_a.label) {
|
|
147
|
-
case 0: return [4, __2.Authentication.login(testConstants_1.TestAuth.username, testConstants_1.TestAuth.pass)];
|
|
148
|
-
case 1:
|
|
149
|
-
loggedUser = _a.sent();
|
|
150
|
-
expect(loggedUser.session_token).not.toBeNull();
|
|
151
|
-
return [4, __2.Authentication.tokenReset(testConstants_1.TestAuth.username, testConstants_1.TestAuth.pass)];
|
|
152
|
-
case 2:
|
|
153
|
-
result = _a.sent();
|
|
154
|
-
expect(result.api_token).not.toEqual(loggedUser.session_token);
|
|
155
|
-
return [2];
|
|
25
|
+
}
|
|
26
|
+
}));
|
|
27
|
+
it('Wrong password', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
+
try {
|
|
29
|
+
yield Authentication.login(TestAuth.username, TestAuth.noUser);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (isApiError(error)) {
|
|
33
|
+
expect(error.message).toBe(ResponseErrorsMessages.invalidPassword);
|
|
156
34
|
}
|
|
157
|
-
}
|
|
158
|
-
})
|
|
35
|
+
}
|
|
36
|
+
}));
|
|
37
|
+
it('Correct user and password', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
+
const loggedUser = yield Authentication.login(TestAuth.username, TestAuth.pass);
|
|
39
|
+
expect(loggedUser.session_token).not.toBeNull();
|
|
40
|
+
}));
|
|
41
|
+
it('Gets logged user data', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
const result = yield Authentication.me();
|
|
43
|
+
expect(result._id).not.toBeNull();
|
|
44
|
+
}));
|
|
45
|
+
it("Doesn't allow token expiration to be set longer than a week", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
46
|
+
const oneWeekOneSecond = 604801000;
|
|
47
|
+
try {
|
|
48
|
+
yield Authentication.login(TestAuth.username, TestAuth.noUser, oneWeekOneSecond);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
const err = error;
|
|
52
|
+
expect(err.message).toBe(ResponseErrorsMessages.invalidTokenExpiration);
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
yield Authentication.login(TestAuth.username, TestAuth.noUser, 0);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
const err = error;
|
|
59
|
+
expect(err.message).toBe(ResponseErrorsMessages.invalidTokenExpiration);
|
|
60
|
+
}
|
|
61
|
+
}));
|
|
62
|
+
it('Resets user API token', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
const loggedUser = yield Authentication.login(TestAuth.username, TestAuth.pass);
|
|
64
|
+
expect(loggedUser.session_token).not.toBeNull();
|
|
65
|
+
const result = yield Authentication.tokenReset(TestAuth.username, TestAuth.pass);
|
|
66
|
+
expect(result.api_token).not.toEqual(loggedUser.session_token);
|
|
67
|
+
}));
|
|
159
68
|
});
|