@gandalan/weblibs 1.3.6 → 1.3.7
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/api/RESTClient.js +2 -0
- package/api/authUtils.js +5 -1
- package/api/fluentAuthManager.js +12 -5
- package/api/fluentAuthUtils.js +1 -0
- package/api/fluentEnvUtils.js +1 -0
- package/api/fluentRestClient.js +22 -8
- package/package.json +1 -1
package/api/RESTClient.js
CHANGED
|
@@ -70,6 +70,7 @@ export class RESTClient
|
|
|
70
70
|
fileName = response.headers["content-disposition"].split(";")[1];
|
|
71
71
|
fileName = fileName.replace("filename=", "").trim();
|
|
72
72
|
}
|
|
73
|
+
|
|
73
74
|
this.lastError = "";
|
|
74
75
|
return { data: response.data, filename: fileName, contentType: "application/pdf" };
|
|
75
76
|
}
|
|
@@ -92,6 +93,7 @@ export class RESTClient
|
|
|
92
93
|
{
|
|
93
94
|
this.handleError(error);
|
|
94
95
|
}
|
|
96
|
+
|
|
95
97
|
return response;
|
|
96
98
|
}
|
|
97
99
|
|
package/api/authUtils.js
CHANGED
|
@@ -51,14 +51,17 @@ export async function initIDAS(appToken)
|
|
|
51
51
|
{
|
|
52
52
|
mandantGuid = urlParams.get("m");
|
|
53
53
|
}
|
|
54
|
+
|
|
54
55
|
if (urlParams.has("a"))
|
|
55
56
|
{
|
|
56
57
|
apiBaseurl = urlParams.get("a");
|
|
57
58
|
}
|
|
59
|
+
|
|
58
60
|
if (urlParams.has("j"))
|
|
59
61
|
{
|
|
60
62
|
jwtToken = urlParams.get("j");
|
|
61
63
|
}
|
|
64
|
+
|
|
62
65
|
if (urlParams.has("t"))
|
|
63
66
|
{
|
|
64
67
|
jwtRefreshToken = urlParams.get("t");
|
|
@@ -87,7 +90,7 @@ export async function initIDAS(appToken)
|
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
/**
|
|
90
|
-
* sets up authentication
|
|
93
|
+
* sets up authentication
|
|
91
94
|
*
|
|
92
95
|
* @export
|
|
93
96
|
* @async
|
|
@@ -121,6 +124,7 @@ export async function setup(settings)
|
|
|
121
124
|
currentRefreshToken = refreshToken;
|
|
122
125
|
startRefreshTimer(settings);
|
|
123
126
|
}
|
|
127
|
+
|
|
124
128
|
let mandantGuid = decoded["mandantGuid"] || "";
|
|
125
129
|
if (mandantGuid)
|
|
126
130
|
{
|
package/api/fluentAuthManager.js
CHANGED
|
@@ -93,8 +93,9 @@ export function createAuthManager() {
|
|
|
93
93
|
* @private
|
|
94
94
|
*/
|
|
95
95
|
async ensureAuthenticated() {
|
|
96
|
-
if (this.token && isTokenValid(this.token))
|
|
96
|
+
if (this.token && isTokenValid(this.token)) {
|
|
97
97
|
return;
|
|
98
|
+
}
|
|
98
99
|
|
|
99
100
|
try {
|
|
100
101
|
await this.authenticate();
|
|
@@ -114,11 +115,13 @@ export function createAuthManager() {
|
|
|
114
115
|
async authenticate() { // benutzt bei existierendem JWT oder RefreshToken, wenn keins vorhanden ERROR
|
|
115
116
|
console.log("authenticating:", this.token ? `token set, exp: ${jwtDecode(this.token).exp - (Date.now() / 1000)}` : "no token,", this.refreshToken, this.appToken);
|
|
116
117
|
|
|
117
|
-
if (this.token && isTokenValid(this.token))
|
|
118
|
+
if (this.token && isTokenValid(this.token)) {
|
|
118
119
|
return;
|
|
120
|
+
}
|
|
119
121
|
|
|
120
|
-
if (this.token && !this.refreshToken)
|
|
122
|
+
if (this.token && !this.refreshToken) {
|
|
121
123
|
this.refreshToken = getRefreshToken(this.token);
|
|
124
|
+
}
|
|
122
125
|
|
|
123
126
|
if (!this.refreshToken) {
|
|
124
127
|
throw new Error("not authenticated");
|
|
@@ -163,6 +166,7 @@ export function createAuthManager() {
|
|
|
163
166
|
this.redirectToLogin();
|
|
164
167
|
throw "Redirect to login...";
|
|
165
168
|
}
|
|
169
|
+
|
|
166
170
|
return this;
|
|
167
171
|
},
|
|
168
172
|
|
|
@@ -180,6 +184,7 @@ export function createAuthManager() {
|
|
|
180
184
|
this.updateUserSession((await res.json()));
|
|
181
185
|
return;
|
|
182
186
|
}
|
|
187
|
+
|
|
183
188
|
throw new Error("not authenticated");
|
|
184
189
|
},
|
|
185
190
|
|
|
@@ -281,15 +286,17 @@ export function getRefreshToken(token) {
|
|
|
281
286
|
* check if the token is still valid
|
|
282
287
|
* - checks the expiry date and the JWT_SAFE_RENEWAL buffer
|
|
283
288
|
*
|
|
284
|
-
*
|
|
289
|
+
* @export
|
|
285
290
|
* @param {string} token
|
|
286
291
|
* @returns {boolean}
|
|
287
292
|
*/
|
|
288
293
|
export function isTokenValid(token) {
|
|
289
294
|
try {
|
|
290
295
|
const decoded = jwtDecode(token);
|
|
291
|
-
if (!decoded || !decoded.exp)
|
|
296
|
+
if (!decoded || !decoded.exp) {
|
|
292
297
|
throw new Error("Invalid token");
|
|
298
|
+
}
|
|
299
|
+
|
|
293
300
|
return (decoded.exp - JWT_SAFE_RENEWAL > Date.now() / 1000);
|
|
294
301
|
}
|
|
295
302
|
catch {
|
package/api/fluentAuthUtils.js
CHANGED
package/api/fluentEnvUtils.js
CHANGED
package/api/fluentRestClient.js
CHANGED
|
@@ -51,8 +51,10 @@ export function restClient() {
|
|
|
51
51
|
const finalUrl = `${this.baseUrl}${url}`;
|
|
52
52
|
const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
|
|
53
53
|
const res = await fetch(finalUrl, { method: "GET", headers });
|
|
54
|
-
if (res.ok)
|
|
54
|
+
if (res.ok) {
|
|
55
55
|
return await this._parseReponse(res);
|
|
56
|
+
}
|
|
57
|
+
|
|
56
58
|
throw new Error(`GET ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
57
59
|
},
|
|
58
60
|
|
|
@@ -68,8 +70,10 @@ export function restClient() {
|
|
|
68
70
|
const finalUrl = `${this.baseUrl}${url}`;
|
|
69
71
|
const headers = this.token ? { "Authorization": `Bearer ${this.token}`, "Content-Type": "application/json" } : {};
|
|
70
72
|
const res = await fetch(finalUrl, { method: "PUT", body: JSON.stringify(payload), headers });
|
|
71
|
-
if (res.ok)
|
|
73
|
+
if (res.ok) {
|
|
72
74
|
return await this._parseReponse(res);
|
|
75
|
+
}
|
|
76
|
+
|
|
73
77
|
throw new Error(`PUT ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
74
78
|
},
|
|
75
79
|
|
|
@@ -95,8 +99,10 @@ export function restClient() {
|
|
|
95
99
|
}
|
|
96
100
|
|
|
97
101
|
const res = await fetch(finalUrl, { method: "POST", body, headers });
|
|
98
|
-
if (res.ok)
|
|
102
|
+
if (res.ok) {
|
|
99
103
|
return await this._parseReponse(res);
|
|
104
|
+
}
|
|
105
|
+
|
|
100
106
|
throw new Error(`POST ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
101
107
|
},
|
|
102
108
|
|
|
@@ -111,18 +117,26 @@ export function restClient() {
|
|
|
111
117
|
const finalUrl = `${this.baseUrl}${url}`;
|
|
112
118
|
const headers = this.token ? { "Authorization": `Bearer ${this.token}` } : {};
|
|
113
119
|
const res = await fetch(finalUrl, { method: "DELETE", headers });
|
|
114
|
-
if (res.ok)
|
|
120
|
+
if (res.ok) {
|
|
115
121
|
return await this._parseReponse(res);
|
|
122
|
+
}
|
|
123
|
+
|
|
116
124
|
throw new Error(`DELETE ${finalUrl} failed: ${res.status} ${res.statusText}`);
|
|
117
125
|
},
|
|
118
126
|
|
|
119
127
|
async _parseReponse(res) {
|
|
120
128
|
// check if repsonse is JSON, then return parsed JSON, otherwise return text
|
|
121
129
|
const contentType = res.headers.get("content-type");
|
|
122
|
-
if (contentType
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
130
|
+
if (contentType) {
|
|
131
|
+
if (contentType.includes("application/json")) {
|
|
132
|
+
return await res.json();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (contentType.includes("image") || contentType.includes("application/pdf")) {
|
|
136
|
+
return await res.blob();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
126
140
|
return await res.text();
|
|
127
141
|
}
|
|
128
142
|
};
|