unjwt 0.1.2 → 0.2.0
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 +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +33 -28
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -6,8 +6,8 @@ You may just use [Jose](https://github.com/panva/jose) or one of the other libra
|
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
9
|
+
- 📦 Works in Node, browser and workers
|
|
10
|
+
- 💨 Zero dependencies except [`uncrypto`](https://github.com/unjs/uncrypto) for conditional Web Crypto API support
|
|
11
11
|
|
|
12
12
|
## Installation
|
|
13
13
|
|
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 'uncrypto';
|
|
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.
|
|
4
|
+
"version": "0.2.0",
|
|
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>",
|
|
@@ -40,6 +40,9 @@
|
|
|
40
40
|
"release": "bumpp --commit --push --tag",
|
|
41
41
|
"test:types": "tsc --noEmit"
|
|
42
42
|
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"uncrypto": "^0.1.3"
|
|
45
|
+
},
|
|
43
46
|
"devDependencies": {
|
|
44
47
|
"@antfu/eslint-config": "^0.40.2",
|
|
45
48
|
"bumpp": "^9.1.1",
|