not-node 6.3.53 → 6.3.54
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
|
};
|