@arcblock/jwt 1.29.22 → 1.29.23
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/esm/index.mjs +16 -5
- package/lib/index.cjs +16 -6
- package/package.json +6 -8
package/esm/index.mjs
CHANGED
|
@@ -3,12 +3,24 @@ import { Hasher, getSigner, types } from "@ocap/mcrypto";
|
|
|
3
3
|
import { fromBase64, scopeMatchAny, toBase64, toHex } from "@ocap/util";
|
|
4
4
|
import Debug from "debug";
|
|
5
5
|
import stringify from "json-stable-stringify";
|
|
6
|
-
import semver from "semver";
|
|
7
6
|
|
|
8
7
|
//#region src/index.ts
|
|
9
8
|
const debug = Debug("@arcblock/jwt");
|
|
10
9
|
const JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN = "1.1.0";
|
|
11
10
|
const hasher = Hasher.SHA3.hash256;
|
|
11
|
+
function coerceVersion(str) {
|
|
12
|
+
const m = str.match(/(\d+\.\d+\.\d+)/);
|
|
13
|
+
return m ? m[1] : null;
|
|
14
|
+
}
|
|
15
|
+
function semverGte(a, b) {
|
|
16
|
+
const pa = a.split(".").map(Number);
|
|
17
|
+
const pb = b.split(".").map(Number);
|
|
18
|
+
for (let i = 0; i < 3; i++) {
|
|
19
|
+
if ((pa[i] || 0) > (pb[i] || 0)) return true;
|
|
20
|
+
if ((pa[i] || 0) < (pb[i] || 0)) return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
12
24
|
/**
|
|
13
25
|
*
|
|
14
26
|
*
|
|
@@ -59,8 +71,8 @@ async function sign(signer, sk, payload = {}, doSign = true, version = "1.0.0")
|
|
|
59
71
|
const bodyB64 = toBase64(stringify(body));
|
|
60
72
|
debug("sign.body", body);
|
|
61
73
|
const msgHex = toHex(`${headerB64}.${bodyB64}`);
|
|
62
|
-
const coercedVersion =
|
|
63
|
-
const msgHash = coercedVersion &&
|
|
74
|
+
const coercedVersion = coerceVersion(version);
|
|
75
|
+
const msgHash = coercedVersion && semverGte(coercedVersion, JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN) ? hasher(msgHex) : msgHex;
|
|
64
76
|
// istanbul ignore if
|
|
65
77
|
if (!doSign) return `${headerB64}.${bodyB64}`;
|
|
66
78
|
return [
|
|
@@ -203,8 +215,7 @@ async function verify(token, signerPk, options) {
|
|
|
203
215
|
};
|
|
204
216
|
if (signers[alg]) {
|
|
205
217
|
const msgHex = toHex(`${headerB64}.${bodyB64}`);
|
|
206
|
-
const
|
|
207
|
-
const version = coercedBodyVersion ? coercedBodyVersion.version : "";
|
|
218
|
+
const version = (body.version ? coerceVersion(body.version) : null) || "";
|
|
208
219
|
if (version && version === JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN) return signers[alg].verify(hasher(msgHex), signature, signerPk);
|
|
209
220
|
return signers[alg].verify(msgHex, signature, signerPk);
|
|
210
221
|
}
|
package/lib/index.cjs
CHANGED
|
@@ -6,13 +6,24 @@ let debug = require("debug");
|
|
|
6
6
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
7
7
|
let json_stable_stringify = require("json-stable-stringify");
|
|
8
8
|
json_stable_stringify = require_rolldown_runtime.__toESM(json_stable_stringify);
|
|
9
|
-
let semver = require("semver");
|
|
10
|
-
semver = require_rolldown_runtime.__toESM(semver);
|
|
11
9
|
|
|
12
10
|
//#region src/index.ts
|
|
13
11
|
const debug$1 = (0, debug.default)("@arcblock/jwt");
|
|
14
12
|
const JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN = "1.1.0";
|
|
15
13
|
const hasher = _ocap_mcrypto.Hasher.SHA3.hash256;
|
|
14
|
+
function coerceVersion(str) {
|
|
15
|
+
const m = str.match(/(\d+\.\d+\.\d+)/);
|
|
16
|
+
return m ? m[1] : null;
|
|
17
|
+
}
|
|
18
|
+
function semverGte(a, b) {
|
|
19
|
+
const pa = a.split(".").map(Number);
|
|
20
|
+
const pb = b.split(".").map(Number);
|
|
21
|
+
for (let i = 0; i < 3; i++) {
|
|
22
|
+
if ((pa[i] || 0) > (pb[i] || 0)) return true;
|
|
23
|
+
if ((pa[i] || 0) < (pb[i] || 0)) return false;
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
16
27
|
/**
|
|
17
28
|
*
|
|
18
29
|
*
|
|
@@ -63,8 +74,8 @@ async function sign(signer, sk, payload = {}, doSign = true, version = "1.0.0")
|
|
|
63
74
|
const bodyB64 = (0, _ocap_util.toBase64)((0, json_stable_stringify.default)(body));
|
|
64
75
|
debug$1("sign.body", body);
|
|
65
76
|
const msgHex = (0, _ocap_util.toHex)(`${headerB64}.${bodyB64}`);
|
|
66
|
-
const coercedVersion =
|
|
67
|
-
const msgHash = coercedVersion &&
|
|
77
|
+
const coercedVersion = coerceVersion(version);
|
|
78
|
+
const msgHash = coercedVersion && semverGte(coercedVersion, JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN) ? hasher(msgHex) : msgHex;
|
|
68
79
|
// istanbul ignore if
|
|
69
80
|
if (!doSign) return `${headerB64}.${bodyB64}`;
|
|
70
81
|
return [
|
|
@@ -207,8 +218,7 @@ async function verify(token, signerPk, options) {
|
|
|
207
218
|
};
|
|
208
219
|
if (signers[alg]) {
|
|
209
220
|
const msgHex = (0, _ocap_util.toHex)(`${headerB64}.${bodyB64}`);
|
|
210
|
-
const
|
|
211
|
-
const version = coercedBodyVersion ? coercedBodyVersion.version : "";
|
|
221
|
+
const version = (body.version ? coerceVersion(body.version) : null) || "";
|
|
212
222
|
if (version && version === JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN) return signers[alg].verify(hasher(msgHex), signature, signerPk);
|
|
213
223
|
return signers[alg].verify(msgHex, signature, signerPk);
|
|
214
224
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@arcblock/jwt",
|
|
3
3
|
"description": "JSON Web Token variant for arcblock DID solutions",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.29.
|
|
5
|
+
"version": "1.29.23",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "wangshijun",
|
|
8
8
|
"email": "shijun@arcblock.io",
|
|
@@ -19,18 +19,16 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@arcblock/did": "1.29.
|
|
23
|
-
"@ocap/mcrypto": "1.29.
|
|
24
|
-
"@ocap/util": "1.29.
|
|
22
|
+
"@arcblock/did": "1.29.23",
|
|
23
|
+
"@ocap/mcrypto": "1.29.23",
|
|
24
|
+
"@ocap/util": "1.29.23",
|
|
25
25
|
"debug": "^4.4.3",
|
|
26
|
-
"json-stable-stringify": "^1.0.1"
|
|
27
|
-
"semver": "^7.6.3"
|
|
26
|
+
"json-stable-stringify": "^1.0.1"
|
|
28
27
|
},
|
|
29
28
|
"devDependencies": {
|
|
30
|
-
"@ocap/wallet": "1.29.
|
|
29
|
+
"@ocap/wallet": "1.29.23",
|
|
31
30
|
"@types/json-stable-stringify": "^1.0.36",
|
|
32
31
|
"@types/node": "^22.7.5",
|
|
33
|
-
"@types/semver": "^7.5.8",
|
|
34
32
|
"tsdown": "^0.18.4",
|
|
35
33
|
"tslib": "^2.4.0"
|
|
36
34
|
},
|