@arcblock/jwt 1.17.0 → 1.17.3
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/index.d.ts +9 -19
- package/lib/index.js +7 -24
- package/package.json +5 -5
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { BytesType } from '@ocap/util';
|
|
2
2
|
export declare type JwtBody = {
|
|
3
|
-
iss
|
|
4
|
-
iat
|
|
5
|
-
nbf
|
|
6
|
-
exp
|
|
7
|
-
version
|
|
3
|
+
iss: string;
|
|
4
|
+
iat: string;
|
|
5
|
+
nbf: string;
|
|
6
|
+
exp: string;
|
|
7
|
+
version: string;
|
|
8
8
|
[key: string]: any;
|
|
9
9
|
};
|
|
10
10
|
export declare type JwtHeader = {
|
|
@@ -31,21 +31,11 @@ export declare type JwtVerifyOptions = Partial<{
|
|
|
31
31
|
* @param {string} [version='1.0.0']
|
|
32
32
|
* @return {*} {string} - hex encoded signature
|
|
33
33
|
*/
|
|
34
|
-
export declare function sign(signer: string, sk:
|
|
34
|
+
export declare function sign(signer: string, sk: BytesType, payload?: {}, doSign?: boolean, version?: string): string;
|
|
35
|
+
export declare function decode(token: string, bodyOnly?: true): JwtBody;
|
|
36
|
+
export declare function decode(token: string, bodyOnly?: false): JwtToken;
|
|
35
37
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @param {string} token - jwt string
|
|
39
|
-
* @param {boolean} [payloadOnly=true]
|
|
40
|
-
* @return {*} {{
|
|
41
|
-
* header: any;
|
|
42
|
-
* body: any;
|
|
43
|
-
* signature: string;
|
|
44
|
-
* }}
|
|
45
|
-
*/
|
|
46
|
-
export declare function decode(token: string, payloadOnly?: boolean): JwtToken;
|
|
47
|
-
/**
|
|
48
|
-
*
|
|
38
|
+
* Verify a jwt token
|
|
49
39
|
*
|
|
50
40
|
* @param {string} token - the jwt token
|
|
51
41
|
* @param {string} signerPk - signer public key
|
package/lib/index.js
CHANGED
|
@@ -50,19 +50,13 @@ function sign(signer, sk, payload = {}, doSign = true, version = '1.0.0') {
|
|
|
50
50
|
const headerB64 = (0, util_1.toBase64)((0, json_stable_stringify_1.default)(header));
|
|
51
51
|
// make body
|
|
52
52
|
const now = Math.floor(Date.now() / 1000);
|
|
53
|
-
|
|
53
|
+
const body = Object.assign({ iss: (0, did_1.toDid)(signer), iat: String(now), nbf: String(now), exp: String(now + 5 * 60), version }, (payload || {}));
|
|
54
54
|
// remove empty keys
|
|
55
|
-
|
|
56
|
-
.filter((x) => {
|
|
55
|
+
Object.keys(body).forEach((x) => {
|
|
57
56
|
if (typeof body[x] === 'undefined' || body[x] == null || body[x] === '') {
|
|
58
|
-
|
|
57
|
+
delete body[x];
|
|
59
58
|
}
|
|
60
|
-
|
|
61
|
-
})
|
|
62
|
-
.reduce((acc, x) => {
|
|
63
|
-
acc[x] = body[x];
|
|
64
|
-
return acc;
|
|
65
|
-
}, {});
|
|
59
|
+
});
|
|
66
60
|
const bodyB64 = (0, util_1.toBase64)((0, json_stable_stringify_1.default)(body));
|
|
67
61
|
debug('sign.body', body);
|
|
68
62
|
// istanbul ignore if
|
|
@@ -79,30 +73,19 @@ function sign(signer, sk, payload = {}, doSign = true, version = '1.0.0') {
|
|
|
79
73
|
return [headerB64, bodyB64, sigB64].join('.');
|
|
80
74
|
}
|
|
81
75
|
exports.sign = sign;
|
|
82
|
-
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* @param {string} token - jwt string
|
|
86
|
-
* @param {boolean} [payloadOnly=true]
|
|
87
|
-
* @return {*} {{
|
|
88
|
-
* header: any;
|
|
89
|
-
* body: any;
|
|
90
|
-
* signature: string;
|
|
91
|
-
* }}
|
|
92
|
-
*/
|
|
93
|
-
function decode(token, payloadOnly = true) {
|
|
76
|
+
function decode(token, bodyOnly = true) {
|
|
94
77
|
const [headerB64, bodyB64, sigB64] = token.split('.');
|
|
95
78
|
const header = JSON.parse((0, util_1.fromBase64)(headerB64).toString());
|
|
96
79
|
const body = JSON.parse((0, util_1.fromBase64)(bodyB64).toString());
|
|
97
80
|
const sig = Buffer.from((0, util_1.fromBase64)(sigB64)).toString('hex');
|
|
98
|
-
if (
|
|
81
|
+
if (bodyOnly) {
|
|
99
82
|
return body;
|
|
100
83
|
}
|
|
101
84
|
return { header, body, signature: `0x${(0, did_1.toStrictHex)(sig)}` };
|
|
102
85
|
}
|
|
103
86
|
exports.decode = decode;
|
|
104
87
|
/**
|
|
105
|
-
*
|
|
88
|
+
* Verify a jwt token
|
|
106
89
|
*
|
|
107
90
|
* @param {string} token - the jwt token
|
|
108
91
|
* @param {string} signerPk - signer public key
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/jwt",
|
|
3
3
|
"description": "JSON Web Token variant for arcblock DID solutions",
|
|
4
|
-
"version": "1.17.
|
|
4
|
+
"version": "1.17.3",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "wangshijun",
|
|
7
7
|
"email": "shijun@arcblock.io",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@arcblock/did": "1.17.
|
|
22
|
-
"@ocap/mcrypto": "1.17.
|
|
23
|
-
"@ocap/util": "1.17.
|
|
21
|
+
"@arcblock/did": "1.17.3",
|
|
22
|
+
"@ocap/mcrypto": "1.17.3",
|
|
23
|
+
"@ocap/util": "1.17.3",
|
|
24
24
|
"debug": "^4.3.3",
|
|
25
25
|
"json-stable-stringify": "^1.0.1",
|
|
26
26
|
"semver": "^7.3.4"
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"build:watch": "npm run build -- -w",
|
|
65
65
|
"build": "tsc"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "7a74e4e2b362a6e6ea8d14617f0480966c3363d5"
|
|
68
68
|
}
|