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/dist/index.d.mts CHANGED
@@ -1,79 +1,6 @@
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 };
1
+ export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, m as JWK, A as JWKAlgorithm, J as JWKParameters, u as JWK_AES_CBC_HMAC, v as JWK_AES_GCM, t as JWK_AES_GCM_KW, s as JWK_AES_KW, z as JWK_Asymmetric_Algorithm, h as JWK_EC, x as JWK_ECDH_ES, q as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, n as JWK_HMAC, w as JWK_PBES2, k as JWK_RSA, r as JWK_RSA_ENC, p as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, o as JWK_RSA_SIGN, y as JWK_Symmetric_Algorithm, l as JWK_oct, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.C0bBN0AC.mjs';
2
+ export { b as JWT, a as JWTClaims, J as JoseHeaderParameters } from './shared/unjwt.D90BbcE1.mjs';
3
+ export { J as JWSAlgorithm, a as JWSHeaderParameters, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, j as jws } from './shared/unjwt.DYFIh1YE.mjs';
4
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, j as jwe } from './shared/unjwt.Dz5Oe9BG.mjs';
5
+ export { i as utils } from './shared/unjwt.CJ_ohCFC.mjs';
6
+ export { j as jwk } from './shared/unjwt.B9uGngq-.mjs';
package/dist/index.d.ts CHANGED
@@ -1,79 +1,6 @@
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 };
1
+ export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, m as JWK, A as JWKAlgorithm, J as JWKParameters, u as JWK_AES_CBC_HMAC, v as JWK_AES_GCM, t as JWK_AES_GCM_KW, s as JWK_AES_KW, z as JWK_Asymmetric_Algorithm, h as JWK_EC, x as JWK_ECDH_ES, q as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, n as JWK_HMAC, w as JWK_PBES2, k as JWK_RSA, r as JWK_RSA_ENC, p as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, o as JWK_RSA_SIGN, y as JWK_Symmetric_Algorithm, l as JWK_oct, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.C0bBN0AC.js';
2
+ export { b as JWT, a as JWTClaims, J as JoseHeaderParameters } from './shared/unjwt.DoVzOnty.js';
3
+ export { J as JWSAlgorithm, a as JWSHeaderParameters, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, j as jws } from './shared/unjwt.ppm5Y0k7.js';
4
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, j as jwe } from './shared/unjwt.BKG_8Y9G.js';
5
+ export { i as utils } from './shared/unjwt.mc4AEAT7.js';
6
+ export { j as jwk } from './shared/unjwt.BOEZHxi_.js';
package/dist/index.mjs CHANGED
@@ -1,141 +1,5 @@
1
- import { subtle } from 'unjwt/crypto';
2
-
3
- const DEFAULT_SIGNATURE_METHOD = "HMAC";
4
- const DEFAULT_HASH_METHODS = "SHA-256";
5
- const textEncoder = new TextEncoder();
6
- async function createSignature(key, algorithm, data) {
7
- const encoded = textEncoder.encode(data);
8
- const signature = await subtle.sign(algorithm, key, encoded);
9
- return encodeToBase64Url(signature);
10
- }
11
- async function importKey(options) {
12
- const {
13
- secret,
14
- usage = "sign",
15
- name = DEFAULT_SIGNATURE_METHOD,
16
- hash = DEFAULT_HASH_METHODS
17
- } = { ...options };
18
- const encodedKey = textEncoder.encode(secret);
19
- return await subtle.importKey(
20
- "raw",
21
- encodedKey,
22
- { name, hash },
23
- false,
24
- [usage]
25
- );
26
- }
27
- function encodeToBase64Url(input) {
28
- let data;
29
- if (typeof input === "string")
30
- data = textEncoder.encode(input);
31
- else if (input instanceof ArrayBuffer)
32
- data = new Uint8Array(input);
33
- else
34
- data = textEncoder.encode(JSON.stringify(input));
35
- const base64 = btoa(String.fromCodePoint(...data));
36
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
37
- }
38
- function decodeFromBase64Url(base64Url) {
39
- const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
40
- return atob(base64);
41
- }
42
- function decodeFromBase64UrlToBuffer(base64Url) {
43
- const bytes = decodeFromBase64Url(base64Url);
44
- const buffer = Uint8Array.from(bytes, (c) => c.codePointAt(0));
45
- return buffer.buffer;
46
- }
47
-
48
- function decodeJWT(token) {
49
- try {
50
- return _decodeJWT(token);
51
- } catch {
52
- }
53
- }
54
- function _decodeJWT(token) {
55
- const parts = String(token).split(".");
56
- if (parts.length !== 3)
57
- throw new Error("Invalid token format");
58
- const [, payload] = parts;
59
- return JSON.parse(decodeFromBase64Url(payload));
60
- }
61
-
62
- const headerAlgMap = {
63
- HMAC: "HS",
64
- RSA: "RS",
65
- ECDSA: "ES"
66
- };
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 };
77
- const key = await importKey({
78
- secret,
79
- name: signatureMethod,
80
- hash: hashMethod
81
- });
82
- const header = {
83
- typ: "JWT",
84
- alg: `${headerAlgMap[signatureMethod]}${hashMethod.split("-")[1]}`
85
- };
86
- const iat = Math.floor(Date.now() / 1e3);
87
- const exp = iat + expires * 24 * 60 * 60;
88
- const jwtPayload = {
89
- ...payload,
90
- iss: issuer,
91
- aud: audience,
92
- iat,
93
- exp
94
- };
95
- const signature = await createSignature(
96
- key,
97
- signatureMethod,
98
- `${encodeToBase64Url(header)}.${encodeToBase64Url(jwtPayload)}`
99
- );
100
- return `${encodeToBase64Url(header)}.${encodeToBase64Url(
101
- jwtPayload
102
- )}.${signature}`;
103
- }
104
-
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(".");
116
- const key = await importKey({
117
- secret,
118
- usage: "verify",
119
- name: signatureMethod,
120
- hash: hashMethod
121
- });
122
- const isValid = await subtle.verify(
123
- signatureMethod,
124
- key,
125
- decodeFromBase64UrlToBuffer(signature),
126
- textEncoder.encode(`${header}.${payload}`)
127
- );
128
- if (!isValid)
129
- throw new Error("Invalid JWT signature");
130
- let decodedPayload;
131
- try {
132
- decodedPayload = JSON.parse(decodeFromBase64Url(payload));
133
- } catch {
134
- throw new Error("Invalid JWT payload");
135
- }
136
- const now = Math.floor(Date.now() / 1e3);
137
- 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)
138
- throw new Error("Invalid JWT claims");
139
- }
140
-
141
- export { _decodeJWT, decodeJWT, signJWT, verifyJWT };
1
+ export { i as utils } from './shared/unjwt.CjHZv3F3.mjs';
2
+ export { j as jwe } from './shared/unjwt.B4e_rwol.mjs';
3
+ export { j as jwk } from './shared/unjwt.DKq5VF7t.mjs';
4
+ export { j as jws } from './shared/unjwt.Bzcov22m.mjs';
5
+ import './shared/unjwt.sux3_Z19.mjs';
package/dist/jwe.d.mts ADDED
@@ -0,0 +1,3 @@
1
+ import './shared/unjwt.C0bBN0AC.mjs';
2
+ import './shared/unjwt.D90BbcE1.mjs';
3
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, g as decrypt, f as encrypt } from './shared/unjwt.Dz5Oe9BG.mjs';
package/dist/jwe.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import './shared/unjwt.C0bBN0AC.js';
2
+ import './shared/unjwt.DoVzOnty.js';
3
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, g as decrypt, f as encrypt } from './shared/unjwt.BKG_8Y9G.js';
package/dist/jwe.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import './shared/unjwt.DKq5VF7t.mjs';
2
+ import './shared/unjwt.CjHZv3F3.mjs';
3
+ export { d as decrypt, e as encrypt } from './shared/unjwt.B4e_rwol.mjs';
4
+ import './shared/unjwt.sux3_Z19.mjs';
package/dist/jwk.d.mts ADDED
@@ -0,0 +1,2 @@
1
+ export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, m as JWK, A as JWKAlgorithm, J as JWKParameters, u as JWK_AES_CBC_HMAC, v as JWK_AES_GCM, t as JWK_AES_GCM_KW, s as JWK_AES_KW, z as JWK_Asymmetric_Algorithm, h as JWK_EC, x as JWK_ECDH_ES, q as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, n as JWK_HMAC, w as JWK_PBES2, k as JWK_RSA, r as JWK_RSA_ENC, p as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, o as JWK_RSA_SIGN, y as JWK_Symmetric_Algorithm, l as JWK_oct, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.C0bBN0AC.mjs';
2
+ export { d as deriveKeyFromPassword, e as exportKey, g as generateKey, i as importKey, u as unwrapKey, w as wrapKey } from './shared/unjwt.B9uGngq-.mjs';
package/dist/jwk.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, m as JWK, A as JWKAlgorithm, J as JWKParameters, u as JWK_AES_CBC_HMAC, v as JWK_AES_GCM, t as JWK_AES_GCM_KW, s as JWK_AES_KW, z as JWK_Asymmetric_Algorithm, h as JWK_EC, x as JWK_ECDH_ES, q as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, n as JWK_HMAC, w as JWK_PBES2, k as JWK_RSA, r as JWK_RSA_ENC, p as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, o as JWK_RSA_SIGN, y as JWK_Symmetric_Algorithm, l as JWK_oct, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.C0bBN0AC.js';
2
+ export { d as deriveKeyFromPassword, e as exportKey, g as generateKey, i as importKey, u as unwrapKey, w as wrapKey } from './shared/unjwt.BOEZHxi_.js';
package/dist/jwk.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import './shared/unjwt.CjHZv3F3.mjs';
2
+ export { o as deriveKeyFromPassword, p as exportKey, n as generateKey, i as importKey, l as unwrapKey, q as wrapKey } from './shared/unjwt.DKq5VF7t.mjs';
package/dist/jws.d.mts ADDED
@@ -0,0 +1,3 @@
1
+ import './shared/unjwt.C0bBN0AC.mjs';
2
+ import './shared/unjwt.D90BbcE1.mjs';
3
+ export { J as JWSAlgorithm, a as JWSHeaderParameters, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, s as sign, v as verify } from './shared/unjwt.DYFIh1YE.mjs';
package/dist/jws.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import './shared/unjwt.C0bBN0AC.js';
2
+ import './shared/unjwt.DoVzOnty.js';
3
+ export { J as JWSAlgorithm, a as JWSHeaderParameters, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, s as sign, v as verify } from './shared/unjwt.ppm5Y0k7.js';
package/dist/jws.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import './shared/unjwt.DKq5VF7t.mjs';
2
+ import './shared/unjwt.CjHZv3F3.mjs';
3
+ import './shared/unjwt.sux3_Z19.mjs';
4
+ export { s as sign, v as verify } from './shared/unjwt.Bzcov22m.mjs';