@bee.js/node 0.0.45 → 0.0.47
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/lib/JWT/beeJWT.js +18 -10
- package/package.json +1 -1
- package/tools/hash.js +3 -4
package/lib/JWT/beeJWT.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
const
|
|
1
|
+
const CryptoJS = require("crypto-js");
|
|
2
2
|
const log = require("../beeHive/log");
|
|
3
3
|
|
|
4
|
+
function base64url(source) {
|
|
5
|
+
encodedSource = CryptoJS.enc.Base64.stringify(source);
|
|
6
|
+
encodedSource = encodedSource.replace(/=+$/, "");
|
|
7
|
+
encodedSource = encodedSource.replace(/\+/g, "-");
|
|
8
|
+
encodedSource = encodedSource.replace(/\//g, "_");
|
|
9
|
+
|
|
10
|
+
return encodedSource;
|
|
11
|
+
}
|
|
12
|
+
|
|
4
13
|
module.exports = function token(_payload = null, header = {}) {
|
|
5
14
|
if (_payload) {
|
|
6
15
|
let { jwt, ...payload } = _payload;
|
|
@@ -20,16 +29,13 @@ module.exports = function token(_payload = null, header = {}) {
|
|
|
20
29
|
|
|
21
30
|
payload = { ...payload, iat: iat, exp: exp };
|
|
22
31
|
|
|
23
|
-
header = JSON.stringify(header);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
payload = JSON.stringify(payload);
|
|
27
|
-
payload = new Buffer.from(payload).toString("base64");
|
|
32
|
+
header = base64url(CryptoJS.enc.Utf8.parse(JSON.stringify(header)));
|
|
33
|
+
payload = base64url(CryptoJS.enc.Utf8.parse(JSON.stringify(payload)));
|
|
28
34
|
|
|
29
|
-
const signature =
|
|
30
|
-
|
|
31
|
-
"base64"
|
|
35
|
+
const signature = base64url(
|
|
36
|
+
CryptoJS.HmacSHA256(`${header}.${payload}`, secret)
|
|
32
37
|
);
|
|
38
|
+
const token = `${header}.${payload}.${signature}`;
|
|
33
39
|
|
|
34
40
|
this.data.jwt =
|
|
35
41
|
this.data.jwt && typeof this.data.jwt !== "object"
|
|
@@ -53,7 +59,9 @@ module.exports = function token(_payload = null, header = {}) {
|
|
|
53
59
|
const header = array[0];
|
|
54
60
|
const payload = array[1];
|
|
55
61
|
|
|
56
|
-
const signature =
|
|
62
|
+
const signature = base64url(
|
|
63
|
+
CryptoJS.HmacSHA256(`${header}.${payload}`, secret)
|
|
64
|
+
);
|
|
57
65
|
|
|
58
66
|
return token === `${header}.${payload}.${signature}`
|
|
59
67
|
? new Buffer.from(payload, "base64").toString("ascii")
|
package/package.json
CHANGED
package/tools/hash.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
const md5 = require("crypto-js/md5");
|
|
1
|
+
const cryptoJS = require("crypto-js");
|
|
3
2
|
|
|
4
3
|
module.exports = {
|
|
5
|
-
md5,
|
|
6
|
-
|
|
4
|
+
md5: (string) => cryptoJS.MD5(string).toString(),
|
|
5
|
+
HmacMD5: (string, key = "") => CryptoJS.HmacMD5(string, key).toString(),
|
|
7
6
|
};
|