unjwt 0.3.0 → 0.5.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.
Files changed (47) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +518 -48
  3. package/dist/index.d.mts +6 -79
  4. package/dist/index.d.ts +6 -79
  5. package/dist/index.mjs +5 -141
  6. package/dist/jwe.d.mts +3 -0
  7. package/dist/jwe.d.ts +3 -0
  8. package/dist/jwe.mjs +4 -0
  9. package/dist/jwk.d.mts +2 -0
  10. package/dist/jwk.d.ts +2 -0
  11. package/dist/jwk.mjs +2 -0
  12. package/dist/jws.d.mts +3 -0
  13. package/dist/jws.d.ts +3 -0
  14. package/dist/jws.mjs +4 -0
  15. package/dist/shared/unjwt.B4e_rwol.mjs +453 -0
  16. package/dist/shared/unjwt.B9uGngq-.d.mts +154 -0
  17. package/dist/shared/unjwt.BKG_8Y9G.d.ts +189 -0
  18. package/dist/shared/unjwt.BOEZHxi_.d.ts +154 -0
  19. package/dist/shared/unjwt.Bzcov22m.mjs +167 -0
  20. package/dist/shared/unjwt.C0bBN0AC.d.mts +200 -0
  21. package/dist/shared/unjwt.C0bBN0AC.d.ts +200 -0
  22. package/dist/shared/unjwt.CJ_ohCFC.d.mts +46 -0
  23. package/dist/shared/unjwt.CjHZv3F3.mjs +57 -0
  24. package/dist/shared/unjwt.D90BbcE1.d.mts +52 -0
  25. package/dist/shared/unjwt.DKq5VF7t.mjs +1186 -0
  26. package/dist/shared/unjwt.DYFIh1YE.d.mts +100 -0
  27. package/dist/shared/unjwt.DoVzOnty.d.ts +52 -0
  28. package/dist/shared/unjwt.Dz5Oe9BG.d.mts +189 -0
  29. package/dist/shared/unjwt.mc4AEAT7.d.ts +46 -0
  30. package/dist/shared/unjwt.ppm5Y0k7.d.ts +100 -0
  31. package/dist/shared/unjwt.sux3_Z19.mjs +101 -0
  32. package/dist/utils.d.mts +2 -0
  33. package/dist/utils.d.ts +2 -0
  34. package/dist/utils.mjs +1 -0
  35. package/package.json +44 -84
  36. package/dist/crypto/node.cjs +0 -19
  37. package/dist/crypto/node.d.cts +0 -5
  38. package/dist/crypto/node.d.mts +0 -5
  39. package/dist/crypto/node.d.ts +0 -5
  40. package/dist/crypto/node.mjs +0 -11
  41. package/dist/crypto/web.cjs +0 -14
  42. package/dist/crypto/web.d.cts +0 -5
  43. package/dist/crypto/web.d.mts +0 -5
  44. package/dist/crypto/web.d.ts +0 -5
  45. package/dist/crypto/web.mjs +0 -10
  46. package/dist/index.cjs +0 -146
  47. package/dist/index.d.cts +0 -79
package/package.json CHANGED
@@ -1,104 +1,64 @@
1
1
  {
2
2
  "name": "unjwt",
3
- "type": "module",
4
- "version": "0.3.0",
5
- "packageManager": "pnpm@8.8.0",
6
- "description": "Easy creation and verification of JSON Web Tokens (JWT)",
7
- "author": "Johann Schopplich <pkg@johannschopplich.com>",
3
+ "version": "0.5.0",
4
+ "description": "Low-level JWT utilities",
5
+ "repository": "sandros94/unjwt",
8
6
  "license": "MIT",
9
- "homepage": "https://github.com/johannschopplich/unjwt#readme",
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/johannschopplich/unjwt.git"
13
- },
14
- "bugs": {
15
- "url": "https://github.com/johannschopplich/unjwt/issues"
16
- },
17
- "keywords": [
18
- "cloudflare",
19
- "jwt",
20
- "serverless",
21
- "token",
22
- "worker"
23
- ],
24
7
  "sideEffects": false,
8
+ "type": "module",
9
+ "author": {
10
+ "name": "Sandro Circi",
11
+ "email": "sandro.circi@digitoolmedia.com"
12
+ },
25
13
  "exports": {
26
14
  ".": {
27
15
  "types": "./dist/index.d.mts",
28
- "import": {
29
- "types": "./dist/index.d.mts",
30
- "default": "./dist/index.mjs"
31
- },
32
- "require": {
33
- "types": "./dist/index.d.cts",
34
- "default": "./dist/index.cjs"
35
- },
36
16
  "default": "./dist/index.mjs"
37
17
  },
38
- "./crypto": {
39
- "browser": "./dist/crypto/web.mjs",
40
- "bun": "./dist/crypto/web.mjs",
41
- "deno": "./dist/crypto/web.mjs",
42
- "edge-light": "./dist/crypto/web.mjs",
43
- "edge-routine": "./dist/crypto/web.mjs",
44
- "lagon": "./dist/crypto/web.mjs",
45
- "netlify": "./dist/crypto/web.mjs",
46
- "react-native": "./dist/crypto/web.mjs",
47
- "wintercg": "./dist/crypto/web.mjs",
48
- "worker": "./dist/crypto/web.mjs",
49
- "workerd": "./dist/crypto/web.mjs",
50
- "node": {
51
- "import": {
52
- "types": "./dist/crypto/node.d.mts",
53
- "default": "./dist/crypto/node.mjs"
54
- },
55
- "require": {
56
- "types": "./dist/crypto/node.d.cts",
57
- "default": "./dist/crypto/node.cjs"
58
- }
59
- },
60
- "types": "./dist/crypto/web.d.mts",
61
- "import": {
62
- "types": "./dist/crypto/web.d.mts",
63
- "default": "./dist/crypto/web.mjs"
64
- },
65
- "require": {
66
- "types": "./dist/crypto/web.d.cts",
67
- "default": "./dist/crypto/web.cjs"
68
- },
69
- "default": "./dist/crypto/web.mjs"
18
+ "./jwe": {
19
+ "types": "./dist/jwe.d.mts",
20
+ "default": "./dist/jwe.mjs"
21
+ },
22
+ "./jwk": {
23
+ "types": "./dist/jwk.d.mts",
24
+ "default": "./dist/jwk.mjs"
25
+ },
26
+ "./jws": {
27
+ "types": "./dist/jws.d.mts",
28
+ "default": "./dist/jws.mjs"
29
+ },
30
+ "./utils": {
31
+ "types": "./dist/utils.d.mts",
32
+ "default": "./dist/utils.mjs"
70
33
  }
71
34
  },
72
- "main": "./dist/index.cjs",
35
+ "main": "./dist/index.mjs",
73
36
  "module": "./dist/index.mjs",
74
- "types": "./dist/index.d.ts",
75
- "typesVersions": {
76
- "*": {
77
- "*": [
78
- "*",
79
- "dist/*",
80
- "dist/*.d.ts",
81
- "dist/*/index.d.ts"
82
- ]
83
- }
84
- },
37
+ "types": "./dist/index.d.mts",
85
38
  "files": [
86
39
  "dist"
87
40
  ],
88
41
  "scripts": {
89
42
  "build": "unbuild",
90
- "dev": "unbuild --stub",
91
- "lint": "eslint .",
92
- "lint:fix": "eslint . --fix",
93
- "release": "bumpp --commit --push --tag",
94
- "test:types": "tsc --noEmit"
43
+ "dev": "vitest dev",
44
+ "lint": "eslint . && prettier -c .",
45
+ "lint:fix": "automd && eslint . --fix && prettier -w .",
46
+ "prepack": "pnpm build",
47
+ "release": "pnpm test && pnpm prepack && changelogen --release --push --publish",
48
+ "test": "pnpm lint && pnpm test:types && vitest run --coverage --slowTestThreshold=500",
49
+ "test:types": "tsc --noEmit --skipLibCheck"
95
50
  },
96
51
  "devDependencies": {
97
- "@antfu/eslint-config": "^1.0.0-beta.17",
98
- "@types/node": "^20.7.1",
99
- "bumpp": "^9.2.0",
100
- "eslint": "^8.50.0",
101
- "typescript": "^5.2.2",
102
- "unbuild": "^2.0.0"
103
- }
52
+ "@types/node": "^22.13.13",
53
+ "@vitest/coverage-v8": "^3.1.2",
54
+ "automd": "^0.4.0",
55
+ "changelogen": "^0.6.1",
56
+ "eslint": "^9.23.0",
57
+ "eslint-config-unjs": "^0.4.2",
58
+ "prettier": "^3.5.3",
59
+ "typescript": "^5.8.2",
60
+ "unbuild": "^3.5.0",
61
+ "vitest": "^3.1.2"
62
+ },
63
+ "packageManager": "pnpm@10.7.0"
104
64
  }
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- const nodeCrypto = require('node:crypto');
4
-
5
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
-
7
- const nodeCrypto__default = /*#__PURE__*/_interopDefaultCompat(nodeCrypto);
8
-
9
- const subtle = nodeCrypto__default.webcrypto?.subtle || {};
10
- const randomUUID = () => {
11
- return nodeCrypto__default.randomUUID();
12
- };
13
- const getRandomValues = (array) => {
14
- return nodeCrypto__default.webcrypto.getRandomValues(array);
15
- };
16
-
17
- exports.getRandomValues = getRandomValues;
18
- exports.randomUUID = randomUUID;
19
- exports.subtle = subtle;
@@ -1,5 +0,0 @@
1
- declare const subtle: Crypto['subtle'];
2
- declare const randomUUID: Crypto['randomUUID'];
3
- declare const getRandomValues: Crypto['getRandomValues'];
4
-
5
- export { getRandomValues, randomUUID, subtle };
@@ -1,5 +0,0 @@
1
- declare const subtle: Crypto['subtle'];
2
- declare const randomUUID: Crypto['randomUUID'];
3
- declare const getRandomValues: Crypto['getRandomValues'];
4
-
5
- export { getRandomValues, randomUUID, subtle };
@@ -1,5 +0,0 @@
1
- declare const subtle: Crypto['subtle'];
2
- declare const randomUUID: Crypto['randomUUID'];
3
- declare const getRandomValues: Crypto['getRandomValues'];
4
-
5
- export { getRandomValues, randomUUID, subtle };
@@ -1,11 +0,0 @@
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 };
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- const webCrypto = globalThis.crypto;
4
- const subtle = webCrypto.subtle;
5
- const randomUUID = () => {
6
- return webCrypto.randomUUID();
7
- };
8
- const getRandomValues = (array) => {
9
- return webCrypto.getRandomValues(array);
10
- };
11
-
12
- exports.getRandomValues = getRandomValues;
13
- exports.randomUUID = randomUUID;
14
- exports.subtle = subtle;
@@ -1,5 +0,0 @@
1
- declare const subtle: Crypto['subtle'];
2
- declare const randomUUID: Crypto['randomUUID'];
3
- declare const getRandomValues: Crypto['getRandomValues'];
4
-
5
- export { getRandomValues, randomUUID, subtle };
@@ -1,5 +0,0 @@
1
- declare const subtle: Crypto['subtle'];
2
- declare const randomUUID: Crypto['randomUUID'];
3
- declare const getRandomValues: Crypto['getRandomValues'];
4
-
5
- export { getRandomValues, randomUUID, subtle };
@@ -1,5 +0,0 @@
1
- declare const subtle: Crypto['subtle'];
2
- declare const randomUUID: Crypto['randomUUID'];
3
- declare const getRandomValues: Crypto['getRandomValues'];
4
-
5
- export { getRandomValues, randomUUID, subtle };
@@ -1,10 +0,0 @@
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.cjs DELETED
@@ -1,146 +0,0 @@
1
- 'use strict';
2
-
3
- const crypto = require('unjwt/crypto');
4
-
5
- const DEFAULT_SIGNATURE_METHOD = "HMAC";
6
- const DEFAULT_HASH_METHODS = "SHA-256";
7
- const textEncoder = new TextEncoder();
8
- async function createSignature(key, algorithm, data) {
9
- const encoded = textEncoder.encode(data);
10
- const signature = await crypto.subtle.sign(algorithm, key, encoded);
11
- return encodeToBase64Url(signature);
12
- }
13
- async function importKey(options) {
14
- const {
15
- secret,
16
- usage = "sign",
17
- name = DEFAULT_SIGNATURE_METHOD,
18
- hash = DEFAULT_HASH_METHODS
19
- } = { ...options };
20
- const encodedKey = textEncoder.encode(secret);
21
- return await crypto.subtle.importKey(
22
- "raw",
23
- encodedKey,
24
- { name, hash },
25
- false,
26
- [usage]
27
- );
28
- }
29
- function encodeToBase64Url(input) {
30
- let data;
31
- if (typeof input === "string")
32
- data = textEncoder.encode(input);
33
- else if (input instanceof ArrayBuffer)
34
- data = new Uint8Array(input);
35
- else
36
- data = textEncoder.encode(JSON.stringify(input));
37
- const base64 = btoa(String.fromCodePoint(...data));
38
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
39
- }
40
- function decodeFromBase64Url(base64Url) {
41
- const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
42
- return atob(base64);
43
- }
44
- function decodeFromBase64UrlToBuffer(base64Url) {
45
- const bytes = decodeFromBase64Url(base64Url);
46
- const buffer = Uint8Array.from(bytes, (c) => c.codePointAt(0));
47
- return buffer.buffer;
48
- }
49
-
50
- function decodeJWT(token) {
51
- try {
52
- return _decodeJWT(token);
53
- } catch {
54
- }
55
- }
56
- function _decodeJWT(token) {
57
- const parts = String(token).split(".");
58
- if (parts.length !== 3)
59
- throw new Error("Invalid token format");
60
- const [, payload] = parts;
61
- return JSON.parse(decodeFromBase64Url(payload));
62
- }
63
-
64
- const headerAlgMap = {
65
- HMAC: "HS",
66
- RSA: "RS",
67
- ECDSA: "ES"
68
- };
69
- async function signJWT(options) {
70
- const {
71
- payload = {},
72
- secret,
73
- issuer,
74
- audience,
75
- expires = 30,
76
- signatureMethod = DEFAULT_SIGNATURE_METHOD,
77
- hashMethod = DEFAULT_HASH_METHODS
78
- } = { ...options };
79
- const key = await importKey({
80
- secret,
81
- name: signatureMethod,
82
- hash: hashMethod
83
- });
84
- const header = {
85
- typ: "JWT",
86
- alg: `${headerAlgMap[signatureMethod]}${hashMethod.split("-")[1]}`
87
- };
88
- const iat = Math.floor(Date.now() / 1e3);
89
- const exp = iat + expires * 24 * 60 * 60;
90
- const jwtPayload = {
91
- ...payload,
92
- iss: issuer,
93
- aud: audience,
94
- iat,
95
- exp
96
- };
97
- const signature = await createSignature(
98
- key,
99
- signatureMethod,
100
- `${encodeToBase64Url(header)}.${encodeToBase64Url(jwtPayload)}`
101
- );
102
- return `${encodeToBase64Url(header)}.${encodeToBase64Url(
103
- jwtPayload
104
- )}.${signature}`;
105
- }
106
-
107
- async function verifyJWT(options) {
108
- const {
109
- token,
110
- secret,
111
- issuer,
112
- audience,
113
- leeway = 60,
114
- signatureMethod = DEFAULT_SIGNATURE_METHOD,
115
- hashMethod = DEFAULT_HASH_METHODS
116
- } = { ...options };
117
- const [header, payload, signature] = String(token).split(".");
118
- const key = await importKey({
119
- secret,
120
- usage: "verify",
121
- name: signatureMethod,
122
- hash: hashMethod
123
- });
124
- const isValid = await crypto.subtle.verify(
125
- signatureMethod,
126
- key,
127
- decodeFromBase64UrlToBuffer(signature),
128
- textEncoder.encode(`${header}.${payload}`)
129
- );
130
- if (!isValid)
131
- throw new Error("Invalid JWT signature");
132
- let decodedPayload;
133
- try {
134
- decodedPayload = JSON.parse(decodeFromBase64Url(payload));
135
- } catch {
136
- throw new Error("Invalid JWT payload");
137
- }
138
- const now = Math.floor(Date.now() / 1e3);
139
- if (issuer !== decodedPayload.iss || (Array.isArray(decodedPayload.aud) ? !decodedPayload.aud.includes(audience) : audience !== decodedPayload.aud) || decodedPayload.exp && now > decodedPayload.exp + leeway || decodedPayload.nbf && now < decodedPayload.nbf - leeway)
140
- throw new Error("Invalid JWT claims");
141
- }
142
-
143
- exports._decodeJWT = _decodeJWT;
144
- exports.decodeJWT = decodeJWT;
145
- exports.signJWT = signJWT;
146
- exports.verifyJWT = verifyJWT;
package/dist/index.d.cts DELETED
@@ -1,79 +0,0 @@
1
- /** Recognized JWT Claims Set members */
2
- interface JWTRegisteredClaims {
3
- /**
4
- * JWT Issuer
5
- *
6
- * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.1 RFC7519#section-4.1.1}
7
- */
8
- iss?: string;
9
- /**
10
- * JWT Subject
11
- *
12
- * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.2 RFC7519#section-4.1.2}
13
- */
14
- sub?: string;
15
- /**
16
- * JWT Audience
17
- *
18
- * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.3 RFC7519#section-4.1.3}
19
- */
20
- aud?: string | string[];
21
- /**
22
- * JWT ID
23
- *
24
- * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.7 RFC7519#section-4.1.7}
25
- */
26
- jti?: string;
27
- /**
28
- * JWT Not Before
29
- *
30
- * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.5 RFC7519#section-4.1.5}
31
- */
32
- nbf?: number;
33
- /**
34
- * JWT Expiration Time
35
- *
36
- * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4 RFC7519#section-4.1.4}
37
- */
38
- exp?: number;
39
- /**
40
- * JWT Issued At
41
- *
42
- * @see {@link https://www.rfc-editor.org/rfc/rfc7519#section-4.1.6 RFC7519#section-4.1.6}
43
- */
44
- iat?: number;
45
- }
46
-
47
- declare function decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): (JWTRegisteredClaims & T) | undefined;
48
- declare function _decodeJWT<T extends Record<string, any> = Record<string, any>>(token: string): JWTRegisteredClaims & T;
49
-
50
- declare function signJWT<T extends Record<string, any> = Record<string, any>>(options: {
51
- payload?: T;
52
- secret: string;
53
- issuer: string;
54
- audience: string;
55
- /**
56
- * Expiration time in days
57
- * @default 30
58
- */
59
- expires?: number;
60
- /** @default 'HMAC' */
61
- signatureMethod?: string;
62
- /** @default 'SHA-256' */
63
- hashMethod?: string;
64
- }): Promise<string>;
65
-
66
- declare function verifyJWT(options: {
67
- token: string;
68
- secret: string;
69
- issuer: string;
70
- audience: string;
71
- /** @default 60 */
72
- leeway?: number;
73
- /** @default 'HMAC' */
74
- signatureMethod?: string;
75
- /** @default 'SHA-256' */
76
- hashMethod?: string;
77
- }): Promise<void>;
78
-
79
- export { type JWTRegisteredClaims, _decodeJWT, decodeJWT, signJWT, verifyJWT };