unjwt 0.1.2 → 0.2.1
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/README.md +1 -1
- package/dist/crypto/node.d.mts +5 -0
- package/dist/crypto/node.d.ts +5 -0
- package/dist/crypto/node.mjs +11 -0
- package/dist/crypto/web.d.mts +5 -0
- package/dist/crypto/web.d.ts +5 -0
- package/dist/crypto/web.mjs +10 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +33 -28
- package/package.json +21 -1
package/README.md
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import nodeCrypto from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
const subtle = nodeCrypto.webcrypto?.subtle || {};
|
|
4
|
+
const randomUUID = () => {
|
|
5
|
+
return nodeCrypto.randomUUID();
|
|
6
|
+
};
|
|
7
|
+
const getRandomValues = (array) => {
|
|
8
|
+
return nodeCrypto.webcrypto.getRandomValues(array);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { getRandomValues, randomUUID, subtle };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const webCrypto = globalThis.crypto;
|
|
2
|
+
const subtle = webCrypto.subtle;
|
|
3
|
+
const randomUUID = () => {
|
|
4
|
+
return webCrypto.randomUUID();
|
|
5
|
+
};
|
|
6
|
+
const getRandomValues = (array) => {
|
|
7
|
+
return webCrypto.getRandomValues(array);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { getRandomValues, randomUUID, subtle };
|
package/dist/index.d.mts
CHANGED
|
@@ -47,7 +47,7 @@ interface JWTRegisteredClaims {
|
|
|
47
47
|
declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): (JWTRegisteredClaims & T) | undefined;
|
|
48
48
|
declare function _decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
|
|
49
49
|
|
|
50
|
-
declare function signJWT<T extends Record<string, any> = Record<string, any>>(
|
|
50
|
+
declare function signJWT<T extends Record<string, any> = Record<string, any>>(options: {
|
|
51
51
|
payload?: T;
|
|
52
52
|
secret: string;
|
|
53
53
|
issuer: string;
|
|
@@ -63,7 +63,7 @@ declare function signJWT<T extends Record<string, any> = Record<string, any>>({
|
|
|
63
63
|
hashMethod?: string;
|
|
64
64
|
}): Promise<string>;
|
|
65
65
|
|
|
66
|
-
declare function verifyJWT(
|
|
66
|
+
declare function verifyJWT(options: {
|
|
67
67
|
token: string;
|
|
68
68
|
secret: string;
|
|
69
69
|
issuer: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ interface JWTRegisteredClaims {
|
|
|
47
47
|
declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): (JWTRegisteredClaims & T) | undefined;
|
|
48
48
|
declare function _decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
|
|
49
49
|
|
|
50
|
-
declare function signJWT<T extends Record<string, any> = Record<string, any>>(
|
|
50
|
+
declare function signJWT<T extends Record<string, any> = Record<string, any>>(options: {
|
|
51
51
|
payload?: T;
|
|
52
52
|
secret: string;
|
|
53
53
|
issuer: string;
|
|
@@ -63,7 +63,7 @@ declare function signJWT<T extends Record<string, any> = Record<string, any>>({
|
|
|
63
63
|
hashMethod?: string;
|
|
64
64
|
}): Promise<string>;
|
|
65
65
|
|
|
66
|
-
declare function verifyJWT(
|
|
66
|
+
declare function verifyJWT(options: {
|
|
67
67
|
token: string;
|
|
68
68
|
secret: string;
|
|
69
69
|
issuer: string;
|
package/dist/index.mjs
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
+
import { subtle } from 'unjwt/crypto';
|
|
2
|
+
|
|
1
3
|
const DEFAULT_SIGNATURE_METHOD = "HMAC";
|
|
2
4
|
const DEFAULT_HASH_METHODS = "SHA-256";
|
|
3
5
|
const textEncoder = new TextEncoder();
|
|
4
6
|
async function createSignature(key, algorithm, data) {
|
|
5
7
|
const encoded = textEncoder.encode(data);
|
|
6
|
-
const signature = await
|
|
8
|
+
const signature = await subtle.sign(algorithm, key, encoded);
|
|
7
9
|
return encodeToBase64Url(signature);
|
|
8
10
|
}
|
|
9
|
-
async function importKey({
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
async function importKey(options) {
|
|
12
|
+
const {
|
|
13
|
+
secret,
|
|
14
|
+
usage = "sign",
|
|
15
|
+
name = DEFAULT_SIGNATURE_METHOD,
|
|
16
|
+
hash = DEFAULT_HASH_METHODS
|
|
17
|
+
} = { ...options };
|
|
15
18
|
const encodedKey = textEncoder.encode(secret);
|
|
16
|
-
return await
|
|
19
|
+
return await subtle.importKey(
|
|
17
20
|
"raw",
|
|
18
21
|
encodedKey,
|
|
19
22
|
{ name, hash },
|
|
@@ -61,15 +64,16 @@ const headerAlgMap = {
|
|
|
61
64
|
RSA: "RS",
|
|
62
65
|
ECDSA: "ES"
|
|
63
66
|
};
|
|
64
|
-
async function signJWT({
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
async function signJWT(options) {
|
|
68
|
+
const {
|
|
69
|
+
payload = {},
|
|
70
|
+
secret,
|
|
71
|
+
issuer,
|
|
72
|
+
audience,
|
|
73
|
+
expires = 30,
|
|
74
|
+
signatureMethod = DEFAULT_SIGNATURE_METHOD,
|
|
75
|
+
hashMethod = DEFAULT_HASH_METHODS
|
|
76
|
+
} = { ...options };
|
|
73
77
|
const key = await importKey({
|
|
74
78
|
secret,
|
|
75
79
|
name: signatureMethod,
|
|
@@ -98,23 +102,24 @@ async function signJWT({
|
|
|
98
102
|
)}.${signature}`;
|
|
99
103
|
}
|
|
100
104
|
|
|
101
|
-
async function verifyJWT({
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
async function verifyJWT(options) {
|
|
106
|
+
const {
|
|
107
|
+
token,
|
|
108
|
+
secret,
|
|
109
|
+
issuer,
|
|
110
|
+
audience,
|
|
111
|
+
leeway = 60,
|
|
112
|
+
signatureMethod = DEFAULT_SIGNATURE_METHOD,
|
|
113
|
+
hashMethod = DEFAULT_HASH_METHODS
|
|
114
|
+
} = { ...options };
|
|
115
|
+
const [header, payload, signature] = String(token).split(".");
|
|
111
116
|
const key = await importKey({
|
|
112
117
|
secret,
|
|
113
118
|
usage: "verify",
|
|
114
119
|
name: signatureMethod,
|
|
115
120
|
hash: hashMethod
|
|
116
121
|
});
|
|
117
|
-
const isValid = await
|
|
122
|
+
const isValid = await subtle.verify(
|
|
118
123
|
signatureMethod,
|
|
119
124
|
key,
|
|
120
125
|
decodeFromBase64UrlToBuffer(signature),
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unjwt",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"packageManager": "pnpm@8.6.12",
|
|
6
6
|
"description": "Easy creation and verification of JSON Web Tokens (JWT)",
|
|
7
7
|
"author": "Johann Schopplich <pkg@johannschopplich.com>",
|
|
@@ -26,6 +26,25 @@
|
|
|
26
26
|
".": {
|
|
27
27
|
"types": "./dist/index.d.ts",
|
|
28
28
|
"import": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./crypto": {
|
|
31
|
+
"browser": "./dist/crypto/web.mjs",
|
|
32
|
+
"bun": "./dist/crypto/web.mjs",
|
|
33
|
+
"deno": "./dist/crypto/web.mjs",
|
|
34
|
+
"edge-light": "./dist/crypto/web.mjs",
|
|
35
|
+
"edge-routine": "./dist/crypto/web.mjs",
|
|
36
|
+
"lagon": "./dist/crypto/web.mjs",
|
|
37
|
+
"netlify": "./dist/crypto/web.mjs",
|
|
38
|
+
"react-native": "./dist/crypto/web.mjs",
|
|
39
|
+
"wintercg": "./dist/crypto/web.mjs",
|
|
40
|
+
"worker": "./dist/crypto/web.mjs",
|
|
41
|
+
"workerd": "./dist/crypto/web.mjs",
|
|
42
|
+
"node": {
|
|
43
|
+
"types": "./dist/crypto/node.d.ts",
|
|
44
|
+
"import": "./dist/crypto/node.mjs"
|
|
45
|
+
},
|
|
46
|
+
"types": "./dist/crypto/web.d.ts",
|
|
47
|
+
"import": "./dist/crypto/web.mjs"
|
|
29
48
|
}
|
|
30
49
|
},
|
|
31
50
|
"types": "./dist/index.d.ts",
|
|
@@ -42,6 +61,7 @@
|
|
|
42
61
|
},
|
|
43
62
|
"devDependencies": {
|
|
44
63
|
"@antfu/eslint-config": "^0.40.2",
|
|
64
|
+
"@types/node": "^20.5.0",
|
|
45
65
|
"bumpp": "^9.1.1",
|
|
46
66
|
"eslint": "^8.47.0",
|
|
47
67
|
"typescript": "^5.1.6",
|