not-node 6.3.53 → 6.3.55
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/package.json
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const Log = require("not-log")(module, "Identity//Token");
|
|
2
|
+
const notRequestError = require("not-error/src/request.error.node.cjs");
|
|
3
|
+
const notCommon = require("../../common");
|
|
4
|
+
const Request = require("http").IncomingMessage;
|
|
5
|
+
|
|
3
6
|
const CONST = require("../../auth/const");
|
|
4
7
|
const ROLES = require("../../auth/roles");
|
|
5
8
|
const { objHas } = require("../../common");
|
|
@@ -7,14 +10,22 @@ const phrase = require("not-locale").modulePhrase("not-node");
|
|
|
7
10
|
|
|
8
11
|
const JWT = require("jsonwebtoken");
|
|
9
12
|
|
|
13
|
+
const TOKEN_OBJECT_REQUIRED_PROPERTIES = ["_id", "role", "active", "username"];
|
|
14
|
+
|
|
10
15
|
module.exports = class IdentityProviderToken {
|
|
16
|
+
/**
|
|
17
|
+
* @type {null|object}
|
|
18
|
+
*/
|
|
11
19
|
#tokenContent = null;
|
|
20
|
+
/**
|
|
21
|
+
* @type {null|string}
|
|
22
|
+
*/
|
|
12
23
|
#token = null;
|
|
13
24
|
|
|
14
25
|
static #options = {};
|
|
15
26
|
|
|
16
27
|
static setOptions(options = {}) {
|
|
17
|
-
this.#options = options;
|
|
28
|
+
this.#options = { ...this.#options, ...options };
|
|
18
29
|
}
|
|
19
30
|
|
|
20
31
|
static #getOptions() {
|
|
@@ -30,9 +41,12 @@ module.exports = class IdentityProviderToken {
|
|
|
30
41
|
}
|
|
31
42
|
|
|
32
43
|
constructor(req) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
44
|
+
if (IdentityProviderToken.sourceIsRequest(req)) {
|
|
45
|
+
this.#extractToken(req);
|
|
46
|
+
this.#extractTokenContent();
|
|
47
|
+
} else if (IdentityProviderToken.sourceIsTokenContent(req)) {
|
|
48
|
+
this.#tokenContent = IdentityProviderToken.copyTokenContent(req);
|
|
49
|
+
}
|
|
36
50
|
return this;
|
|
37
51
|
}
|
|
38
52
|
|
|
@@ -70,20 +84,20 @@ module.exports = class IdentityProviderToken {
|
|
|
70
84
|
}
|
|
71
85
|
return null;
|
|
72
86
|
} catch (e) {
|
|
73
|
-
error(e.message);
|
|
87
|
+
Log && Log.error(e.message);
|
|
74
88
|
return null;
|
|
75
89
|
}
|
|
76
90
|
}
|
|
77
91
|
|
|
78
92
|
#encodeTokenContent() {
|
|
79
93
|
try {
|
|
80
|
-
if (this.#token) {
|
|
94
|
+
if (this.#token && this.#tokenContent) {
|
|
81
95
|
const secret = IdentityProviderToken.#getOptions().secret;
|
|
82
96
|
return JWT.sign(this.#tokenContent, secret);
|
|
83
97
|
}
|
|
84
98
|
return null;
|
|
85
99
|
} catch (e) {
|
|
86
|
-
error(e.message);
|
|
100
|
+
Log && Log.error(e.message);
|
|
87
101
|
return null;
|
|
88
102
|
}
|
|
89
103
|
}
|
|
@@ -119,7 +133,7 @@ module.exports = class IdentityProviderToken {
|
|
|
119
133
|
|
|
120
134
|
static #validateTTLForToken(tokenTTL) {
|
|
121
135
|
if (tokenTTL <= 0 || isNaN(tokenTTL)) {
|
|
122
|
-
log(phrase("user_token_ttl_not_set"));
|
|
136
|
+
Log && Log.log(phrase("user_token_ttl_not_set"));
|
|
123
137
|
tokenTTL = CONST.TOKEN_TTL;
|
|
124
138
|
}
|
|
125
139
|
return tokenTTL;
|
|
@@ -193,7 +207,7 @@ module.exports = class IdentityProviderToken {
|
|
|
193
207
|
const roles = this.getRole();
|
|
194
208
|
for (let role of roles) {
|
|
195
209
|
if (
|
|
196
|
-
IdentityProviderToken.#getOptions()
|
|
210
|
+
IdentityProviderToken.#getOptions()?.primaryRoles.includes(role)
|
|
197
211
|
) {
|
|
198
212
|
return role;
|
|
199
213
|
}
|
|
@@ -298,7 +312,29 @@ module.exports = class IdentityProviderToken {
|
|
|
298
312
|
this.setGuest();
|
|
299
313
|
}
|
|
300
314
|
|
|
301
|
-
static test(
|
|
302
|
-
|
|
315
|
+
static test(some) {
|
|
316
|
+
if (this.sourceIsRequest(some)) {
|
|
317
|
+
return !!this.getTokenFromRequest(some);
|
|
318
|
+
} else if (this.sourceIsTokenContent(some)) {
|
|
319
|
+
return !!this.copyTokenContent(some);
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
static sourceIsRequest(some) {
|
|
325
|
+
return some instanceof Request;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
static sourceIsTokenContent(some) {
|
|
329
|
+
return (
|
|
330
|
+
typeof some === "object" &&
|
|
331
|
+
notCommon.objHas(some, TOKEN_OBJECT_REQUIRED_PROPERTIES)
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
static copyTokenContent(obj) {
|
|
336
|
+
return {
|
|
337
|
+
...obj,
|
|
338
|
+
};
|
|
303
339
|
}
|
|
304
340
|
};
|
package/src/manifest/route.js
CHANGED
|
@@ -208,12 +208,15 @@ class notRoute {
|
|
|
208
208
|
|
|
209
209
|
async executeRoute(modRoute, actionName, { req, res, next }) {
|
|
210
210
|
try {
|
|
211
|
+
let prepared = undefined;
|
|
211
212
|
//waiting preparation
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
213
|
+
if (modRoute[CONST_AFTER_ACTION]) {
|
|
214
|
+
prepared = await this.executeFunction(
|
|
215
|
+
modRoute,
|
|
216
|
+
CONST_BEFORE_ACTION,
|
|
217
|
+
[req, res, next]
|
|
218
|
+
);
|
|
219
|
+
}
|
|
217
220
|
//waiting results
|
|
218
221
|
let result = await this.executeFunction(modRoute, actionName, [
|
|
219
222
|
req,
|
|
@@ -234,12 +237,16 @@ class notRoute {
|
|
|
234
237
|
notManifestRouteResultFilter.filter(req.notRouteData, result);
|
|
235
238
|
}
|
|
236
239
|
//run after with results, continue without waiting when it finished
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
240
|
+
if (modRoute[CONST_AFTER_ACTION]) {
|
|
241
|
+
return this.executeFunction(modRoute, CONST_AFTER_ACTION, [
|
|
242
|
+
req,
|
|
243
|
+
res,
|
|
244
|
+
next,
|
|
245
|
+
result,
|
|
246
|
+
]);
|
|
247
|
+
} else {
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
243
250
|
} catch (e) {
|
|
244
251
|
next(e);
|
|
245
252
|
}
|