unjwt 0.3.0 → 0.4.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/LICENSE +21 -21
- package/README.md +348 -48
- package/dist/index.d.mts +5 -79
- package/dist/index.d.ts +5 -79
- package/dist/index.mjs +5 -141
- package/dist/jwe.d.mts +3 -0
- package/dist/jwe.d.ts +3 -0
- package/dist/jwe.mjs +3 -0
- package/dist/jwk.d.mts +2 -0
- package/dist/jwk.d.ts +2 -0
- package/dist/jwk.mjs +2 -0
- package/dist/jws.d.mts +3 -0
- package/dist/jws.d.ts +3 -0
- package/dist/jws.mjs +4 -0
- package/dist/shared/unjwt.4uzJv0cA.d.mts +110 -0
- package/dist/shared/unjwt.A7HKFx3n.d.mts +52 -0
- package/dist/shared/unjwt.BdjvpIUY.d.mts +79 -0
- package/dist/shared/unjwt.BmjmfFWo.d.ts +79 -0
- package/dist/shared/unjwt.C9ELsKeK.mjs +120 -0
- package/dist/shared/unjwt.CVUxLNoy.d.ts +52 -0
- package/dist/shared/unjwt.ChP7VhqI.d.ts +110 -0
- package/dist/shared/unjwt.DD0p18Gd.mjs +81 -0
- package/dist/shared/unjwt.DGsIvgyn.d.ts +106 -0
- package/dist/shared/unjwt.DPTwlVpK.mjs +28 -0
- package/dist/shared/unjwt.Dr-mxtq9.d.mts +106 -0
- package/dist/shared/unjwt.KOd0vXyx.mjs +90 -0
- package/dist/shared/unjwt._GivNhs2.mjs +213 -0
- package/dist/shared/unjwt.zb2wBNCJ.d.mts +82 -0
- package/dist/shared/unjwt.zb2wBNCJ.d.ts +82 -0
- package/package.json +40 -84
- package/dist/crypto/node.cjs +0 -19
- package/dist/crypto/node.d.cts +0 -5
- package/dist/crypto/node.d.mts +0 -5
- package/dist/crypto/node.d.ts +0 -5
- package/dist/crypto/node.mjs +0 -11
- package/dist/crypto/web.cjs +0 -14
- package/dist/crypto/web.d.cts +0 -5
- package/dist/crypto/web.d.mts +0 -5
- package/dist/crypto/web.d.ts +0 -5
- package/dist/crypto/web.mjs +0 -10
- package/dist/index.cjs +0 -146
- package/dist/index.d.cts +0 -79
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 };
|