@osimatic/helpers-js 1.0.63 → 1.0.66
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/.idea/helpers-js.iml +8 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/index.js +2 -2
- package/jwt.js +71 -1
- package/network.js +21 -8
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="WEB_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager">
|
|
4
|
+
<content url="file://$MODULE_DIR$" />
|
|
5
|
+
<orderEntry type="inheritedJdk" />
|
|
6
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
7
|
+
</component>
|
|
8
|
+
</module>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/helpers-js.iml" filepath="$PROJECT_DIR$/.idea/helpers-js.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
package/.idea/vcs.xml
ADDED
package/index.js
CHANGED
|
@@ -32,7 +32,7 @@ const { ShoppingCart } = require('./shopping_cart');
|
|
|
32
32
|
const { FlashMessage } = require('./flash_message');
|
|
33
33
|
const { CountDown } = require('./count_down');
|
|
34
34
|
const { ImportFromCsv } = require('./import_from_csv');
|
|
35
|
-
const { JwtToken, JwtSession } = require('./jwt');
|
|
35
|
+
const { JwtToken, JwtSession, ApiTokenSession } = require('./jwt');
|
|
36
36
|
const { ListBox } = require('./list_box');
|
|
37
37
|
const { WebRTC } = require('./web_rtc');
|
|
38
38
|
const { EventBus } = require('./event_bus');
|
|
@@ -47,7 +47,7 @@ const { WebSocket } = require('./web_socket');
|
|
|
47
47
|
module.exports = {
|
|
48
48
|
Array, Object, Number, String,
|
|
49
49
|
HTTPRequest, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, SocialNetwork,
|
|
50
|
-
Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ListBox, WebRTC, WebSocket, EventBus,
|
|
50
|
+
Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ApiTokenSession, ListBox, WebRTC, WebSocket, EventBus,
|
|
51
51
|
sleep, refresh, chr, ord, trim, empty,
|
|
52
52
|
GoogleCharts, GoogleRecaptcha, GoogleMap, OpenStreetMap
|
|
53
53
|
};
|
package/jwt.js
CHANGED
|
@@ -93,4 +93,74 @@ class JwtSession {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
class ApiTokenSession {
|
|
97
|
+
static denyAccessUnlessGranted(roles) {
|
|
98
|
+
let hasRole = false;
|
|
99
|
+
|
|
100
|
+
roles.forEach(role => {
|
|
101
|
+
if (ApiTokenSession.isGranted(role)) {
|
|
102
|
+
hasRole = true;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return hasRole;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
static getToken() {
|
|
110
|
+
return localStorage.getItem('api_token');
|
|
111
|
+
}
|
|
112
|
+
static setToken(token) {
|
|
113
|
+
localStorage.setItem('api_token', token);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static getTokenData() {
|
|
117
|
+
let tokenData = localStorage.getItem('token_data');
|
|
118
|
+
if (null == tokenData) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
return JSON.parse(tokenData);
|
|
122
|
+
}
|
|
123
|
+
static setTokenData(data) {
|
|
124
|
+
localStorage.setItem('token_data', JSON.stringify(data));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static logout() {
|
|
128
|
+
localStorage.removeItem('api_token');
|
|
129
|
+
localStorage.removeItem('token_data');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static getData(key) {
|
|
133
|
+
let tokenData = ApiTokenSession.getTokenData();
|
|
134
|
+
if (tokenData == null) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (typeof tokenData[key] != 'undefined') {
|
|
139
|
+
return tokenData[key];
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static isAnonymous() {
|
|
145
|
+
return ApiTokenSession.getToken() == null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static isGranted(role) {
|
|
149
|
+
if (ApiTokenSession.getToken() == null) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let roles = [];
|
|
154
|
+
if (null !== ApiTokenSession.getData('role')) {
|
|
155
|
+
roles = ApiTokenSession.getData('role');
|
|
156
|
+
}
|
|
157
|
+
if (null !== ApiTokenSession.getData('roles')) {
|
|
158
|
+
roles = ApiTokenSession.getData('roles');
|
|
159
|
+
}
|
|
160
|
+
roles = Array.isArray(roles) ? roles : [roles];
|
|
161
|
+
|
|
162
|
+
return roles.indexOf(role) !== -1;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
module.exports = { JwtToken, JwtSession, ApiTokenSession };
|
package/network.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const { JwtSession } = require('@osimatic/helpers-js/jwt');
|
|
2
|
-
|
|
3
1
|
class HTTPRequest {
|
|
4
2
|
static init() {
|
|
5
3
|
require('whatwg-fetch'); //fetch polyfill loaded in window.fetch
|
|
@@ -121,7 +119,22 @@ class HTTPRequest {
|
|
|
121
119
|
}
|
|
122
120
|
|
|
123
121
|
static logJqueryRequestFailure(jqxhr, status, errorThrown) {
|
|
124
|
-
console.error('Request failure. Status: '+status+' ; HTTP Code: '+jqxhr.
|
|
122
|
+
console.error('Request failure. Status: '+status+' ; HTTP Code: '+jqxhr.status+(null!=errorThrown && ''!==errorThrown ? ' ; Error message: '+errorThrown : ''), jqxhr.responseJSON);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static isExpiredToken(response, json) {
|
|
126
|
+
if (response.status !== 401) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
response.statusText === 'Expired JWT Token'
|
|
132
|
+
|| (typeof json['message'] != 'undefined' && json['message'] === 'Expired JWT Token')
|
|
133
|
+
|| (typeof json['error'] != 'undefined' && json['error'] === 'expired_token')
|
|
134
|
+
|| (typeof json['error'] != 'undefined' && json['error'] === 'authentification_failure')
|
|
135
|
+
|| (json === 'expired_token')
|
|
136
|
+
|| (json === 'authentification_failure')
|
|
137
|
+
);
|
|
125
138
|
}
|
|
126
139
|
|
|
127
140
|
static async get(url, data, successCallback, errorCallback) {
|
|
@@ -140,7 +153,7 @@ class HTTPRequest {
|
|
|
140
153
|
try {
|
|
141
154
|
jsonData = await response.json();
|
|
142
155
|
|
|
143
|
-
if (
|
|
156
|
+
if (HTTPRequest.isExpiredToken(response, jsonData)) {
|
|
144
157
|
HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
|
|
145
158
|
return;
|
|
146
159
|
}
|
|
@@ -178,7 +191,7 @@ class HTTPRequest {
|
|
|
178
191
|
}
|
|
179
192
|
},
|
|
180
193
|
error: (jqxhr, status, errorThrown) => {
|
|
181
|
-
if (
|
|
194
|
+
if (HTTPRequest.isExpiredToken(jqxhr, jqxhr.responseJSON)) {
|
|
182
195
|
HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
|
|
183
196
|
return;
|
|
184
197
|
}
|
|
@@ -265,7 +278,7 @@ class HTTPRequest {
|
|
|
265
278
|
$.ajax(Object.assign({...ajaxOptions}, {
|
|
266
279
|
success: (data, status, jqxhr) => File.download(data, jqxhr.getResponseHeader('Content-Type'), jqxhr.getResponseHeader('Content-Disposition')),
|
|
267
280
|
error: (jqxhr, status, errorThrown) => {
|
|
268
|
-
if (
|
|
281
|
+
if (HTTPRequest.isExpiredToken(jqxhr, jqxhr.responseJSON)) {
|
|
269
282
|
HTTPRequest.refreshToken(() => HTTPRequest.download(url, data, errorCallback, completeCallback, method));
|
|
270
283
|
return;
|
|
271
284
|
}
|
|
@@ -302,7 +315,7 @@ class HTTPRequest {
|
|
|
302
315
|
}
|
|
303
316
|
//console.log(url, jsonData);
|
|
304
317
|
|
|
305
|
-
if (
|
|
318
|
+
if (url !== HTTPRequest.refreshTokenUrl && HTTPRequest.isExpiredToken(response, jsonData)) {
|
|
306
319
|
HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
|
|
307
320
|
return;
|
|
308
321
|
}
|
|
@@ -350,7 +363,7 @@ class HTTPRequest {
|
|
|
350
363
|
}
|
|
351
364
|
},
|
|
352
365
|
error: (jqxhr, status, errorThrown) => {
|
|
353
|
-
if (url !== HTTPRequest.refreshTokenUrl &&
|
|
366
|
+
if (url !== HTTPRequest.refreshTokenUrl && HTTPRequest.isExpiredToken(jqxhr, jqxhr.responseJSON)) {
|
|
354
367
|
HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
|
|
355
368
|
return;
|
|
356
369
|
}
|