unjwt 0.5.37 → 0.5.39
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 +142 -663
- package/dist/_chunks/chunk.mjs +13 -0
- package/dist/_chunks/h3v1.mjs +598 -0
- package/dist/_chunks/index.d.mts +250 -0
- package/dist/_chunks/index2.d.mts +108 -0
- package/dist/_chunks/jwe.d.mts +43 -0
- package/dist/_chunks/jwe.mjs +132 -0
- package/dist/_chunks/jwk.d.mts +575 -0
- package/dist/_chunks/jwk.mjs +1592 -0
- package/dist/_chunks/jws.d.mts +46 -0
- package/dist/_chunks/jws.mjs +2069 -0
- package/dist/_chunks/utils.mjs +374 -0
- package/dist/adapters/h3.d.mts +3 -265
- package/dist/adapters/h3.mjs +3 -706
- package/dist/adapters/h3v1.d.mts +3 -8
- package/dist/adapters/h3v1.mjs +3 -7
- package/dist/adapters/h3v2.d.mts +145 -146
- package/dist/adapters/h3v2.mjs +505 -596
- package/dist/index.d.mts +6 -8
- package/dist/index.mjs +5 -4
- package/dist/jwe.d.mts +3 -4
- package/dist/jwe.mjs +4 -3
- package/dist/jwk.d.mts +3 -2
- package/dist/jwk.mjs +3 -2
- package/dist/jws.d.mts +3 -3
- package/dist/jws.mjs +4 -3
- package/dist/utils.d.mts +3 -3
- package/dist/utils.mjs +2 -1
- package/package.json +33 -31
- package/dist/shared/unjwt.4JQCoID_.d.mts +0 -161
- package/dist/shared/unjwt.B-v62plj.mjs +0 -249
- package/dist/shared/unjwt.BCi3FEWr.d.mts +0 -328
- package/dist/shared/unjwt.BYDTEWa-.d.mts +0 -269
- package/dist/shared/unjwt.Be8NxwFK.mjs +0 -438
- package/dist/shared/unjwt.BqdNUKDr.d.mts +0 -64
- package/dist/shared/unjwt.CMNtw8ek.d.mts +0 -155
- package/dist/shared/unjwt.Cy2MlkUe.d.mts +0 -174
- package/dist/shared/unjwt.D0_ccT7g.d.mts +0 -166
- package/dist/shared/unjwt.DdLN9vwI.mjs +0 -163
- package/dist/shared/unjwt.Dpl__ZAR.mjs +0 -2121
- package/dist/shared/unjwt.JdKqVANs.d.mts +0 -5
|
@@ -1,438 +0,0 @@
|
|
|
1
|
-
function applyTypCtyDefaults(header, payload) {
|
|
2
|
-
const isObjectPayload = typeof payload === "object" && payload !== null && !(payload instanceof Uint8Array);
|
|
3
|
-
if (header.typ === void 0 && isObjectPayload) {
|
|
4
|
-
header.typ = "JWT";
|
|
5
|
-
}
|
|
6
|
-
if (header.typ?.toLowerCase().includes("jwt") && isObjectPayload) {
|
|
7
|
-
header.cty ||= "application/json";
|
|
8
|
-
}
|
|
9
|
-
return header;
|
|
10
|
-
}
|
|
11
|
-
function isJWTContent(header) {
|
|
12
|
-
if (!header) return false;
|
|
13
|
-
if (header.typ?.toLowerCase().includes("jwt")) return true;
|
|
14
|
-
const cty = header.cty?.toLowerCase();
|
|
15
|
-
return cty === "json" || cty === "application/json" || !!cty && cty.endsWith("+json");
|
|
16
|
-
}
|
|
17
|
-
function decodeMaybeJWTString(decodedString, header) {
|
|
18
|
-
if (isJWTContent(header)) {
|
|
19
|
-
const looksLikeJson = decodedString.startsWith("{") && decodedString.endsWith("}") || decodedString.startsWith("[") && decodedString.endsWith("]");
|
|
20
|
-
if (looksLikeJson) {
|
|
21
|
-
try {
|
|
22
|
-
const obj = JSON.parse(decodedString);
|
|
23
|
-
return sanitizeObject(obj);
|
|
24
|
-
} catch {
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return decodedString;
|
|
29
|
-
}
|
|
30
|
-
function decodePayloadFromBytes(bytes, header, forceUint8Array) {
|
|
31
|
-
if (forceUint8Array) return bytes;
|
|
32
|
-
const decodedString = textDecoder.decode(bytes);
|
|
33
|
-
return decodeMaybeJWTString(decodedString, header);
|
|
34
|
-
}
|
|
35
|
-
function decodePayloadFromB64UrlSegment(payloadEncoded, header, forceUint8Array) {
|
|
36
|
-
if (forceUint8Array) return base64UrlDecode(payloadEncoded, false);
|
|
37
|
-
const decodedString = base64UrlDecode(payloadEncoded);
|
|
38
|
-
return decodeMaybeJWTString(decodedString, header);
|
|
39
|
-
}
|
|
40
|
-
function getPlaintextBytes(payload) {
|
|
41
|
-
if (payload instanceof Uint8Array) return payload;
|
|
42
|
-
if (typeof payload === "string") return textEncoder.encode(payload);
|
|
43
|
-
if (typeof payload === "object" && payload !== null)
|
|
44
|
-
return textEncoder.encode(JSON.stringify(payload));
|
|
45
|
-
throw new TypeError(
|
|
46
|
-
"Plaintext must be a string, Uint8Array, or a JSON-serializable object."
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
const TIME_CONSTANTS = Object.freeze({
|
|
50
|
-
s: 1,
|
|
51
|
-
second: 1,
|
|
52
|
-
seconds: 1,
|
|
53
|
-
m: 60,
|
|
54
|
-
minute: 60,
|
|
55
|
-
minutes: 60,
|
|
56
|
-
h: 3600,
|
|
57
|
-
hour: 3600,
|
|
58
|
-
hours: 3600,
|
|
59
|
-
D: 86400,
|
|
60
|
-
day: 86400,
|
|
61
|
-
days: 86400,
|
|
62
|
-
W: 604800,
|
|
63
|
-
week: 604800,
|
|
64
|
-
weeks: 604800,
|
|
65
|
-
M: 2592e3,
|
|
66
|
-
month: 2592e3,
|
|
67
|
-
months: 2592e3,
|
|
68
|
-
Y: 31536e3,
|
|
69
|
-
year: 31536e3,
|
|
70
|
-
years: 31536e3
|
|
71
|
-
});
|
|
72
|
-
const EXPIRES_IN_REGEX = /^(\d+)(s|second|seconds|m|minute|minutes|h|hour|hours|D|day|days|W|week|weeks|M|month|months|Y|year|years)?$/;
|
|
73
|
-
function computeExpiresInSeconds(expiresIn) {
|
|
74
|
-
if (typeof expiresIn === "number") {
|
|
75
|
-
if (!Number.isInteger(expiresIn) || expiresIn <= 0) {
|
|
76
|
-
throw new TypeError(
|
|
77
|
-
"If 'expiresIn' is a number, it must be a positive integer representing seconds."
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
return expiresIn;
|
|
81
|
-
}
|
|
82
|
-
if (typeof expiresIn === "string") {
|
|
83
|
-
const match = expiresIn.match(EXPIRES_IN_REGEX);
|
|
84
|
-
if (!match || !match[1]) {
|
|
85
|
-
throw new TypeError(
|
|
86
|
-
"Invalid 'expiresIn' format. Must be a positive integer or a string like '10m', '2h', '7D', etc."
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
const value = Number.parseInt(match[1], 10);
|
|
90
|
-
const unit = match[2] || "s";
|
|
91
|
-
return value * TIME_CONSTANTS[unit];
|
|
92
|
-
}
|
|
93
|
-
throw new TypeError(
|
|
94
|
-
"'expiresIn' must be a number or a string representing time duration."
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
const computeMaxTokenAgeSeconds = computeExpiresInSeconds;
|
|
98
|
-
function computeJwtTimeClaims(payload, headerTyp, expiresIn, currentDate = /* @__PURE__ */ new Date()) {
|
|
99
|
-
if (expiresIn === void 0 || !headerTyp?.toLowerCase().includes("jwt") || !(payload && typeof payload === "object") || payload instanceof Uint8Array || payload.exp) {
|
|
100
|
-
return void 0;
|
|
101
|
-
}
|
|
102
|
-
const now = Math.round(currentDate.getTime() / 1e3);
|
|
103
|
-
const claims = { ...payload };
|
|
104
|
-
claims.iat ||= now;
|
|
105
|
-
claims.exp = claims.iat + computeExpiresInSeconds(expiresIn);
|
|
106
|
-
return claims;
|
|
107
|
-
}
|
|
108
|
-
function validateJwtClaims(jwtClaims, options = {}) {
|
|
109
|
-
const clockTolerance = options.clockTolerance ?? 0;
|
|
110
|
-
const currentTime = Math.round(
|
|
111
|
-
(options.currentDate ?? /* @__PURE__ */ new Date()).getTime() / 1e3
|
|
112
|
-
);
|
|
113
|
-
const allRequiredClaims = new Set(options.requiredClaims || []);
|
|
114
|
-
const missingClaims = /* @__PURE__ */ new Set();
|
|
115
|
-
if (options.issuer) allRequiredClaims.add("iss");
|
|
116
|
-
if (options.audience) allRequiredClaims.add("aud");
|
|
117
|
-
if (options.subject) allRequiredClaims.add("sub");
|
|
118
|
-
if (options.maxTokenAge) allRequiredClaims.add("iat");
|
|
119
|
-
for (const claimName of allRequiredClaims) {
|
|
120
|
-
if (!(claimName in jwtClaims)) missingClaims.add(claimName);
|
|
121
|
-
}
|
|
122
|
-
if (missingClaims.size > 0) {
|
|
123
|
-
throw new Error(
|
|
124
|
-
`Missing required JWT Claims: ${[...missingClaims].join(", ")}`
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
if (options.issuer) {
|
|
128
|
-
const expectedIssuers = maybeArray(options.issuer);
|
|
129
|
-
if (!jwtClaims.iss || !expectedIssuers.includes(jwtClaims.iss)) {
|
|
130
|
-
throw new Error(
|
|
131
|
-
`Invalid JWT "iss" (Issuer) Claim: Expected ${expectedIssuers.join(" or ")}, got ${jwtClaims.iss}`
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (options.subject && jwtClaims.sub !== options.subject) {
|
|
136
|
-
throw new Error(
|
|
137
|
-
`Invalid JWT "sub" (Subject) Claim: Expected ${options.subject}, got ${jwtClaims.sub}`
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
if (options.audience) {
|
|
141
|
-
const expectedAudiences = maybeArray(options.audience);
|
|
142
|
-
const claimAudience = maybeArray(jwtClaims.aud || []);
|
|
143
|
-
if (!claimAudience.some((aud) => expectedAudiences.includes(aud))) {
|
|
144
|
-
throw new Error(
|
|
145
|
-
`Invalid JWT "aud" (Audience) Claim: Expected ${expectedAudiences.join(" or ")}, got ${claimAudience.join(", ")}`
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (typeof jwtClaims.nbf === "number" && jwtClaims.nbf > currentTime + clockTolerance) {
|
|
150
|
-
throw new Error(
|
|
151
|
-
`JWT "nbf" (Not Before) Claim validation failed: Token is not yet valid (nbf: ${new Date(jwtClaims.nbf * 1e3).toISOString()})`
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
if (typeof jwtClaims.exp === "number" && jwtClaims.exp <= currentTime - clockTolerance) {
|
|
155
|
-
throw new Error(
|
|
156
|
-
`JWT "exp" (Expiration Time) Claim validation failed: Token has expired (exp: ${new Date(jwtClaims.exp * 1e3).toISOString()})`
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
if (options.maxTokenAge) {
|
|
160
|
-
if (typeof jwtClaims.iat !== "number") {
|
|
161
|
-
throw new TypeError(
|
|
162
|
-
'JWT "iat" (Issued At) Claim must be a number when maxTokenAge is set.'
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
if (jwtClaims.iat > currentTime + clockTolerance) {
|
|
166
|
-
throw new Error(
|
|
167
|
-
`JWT "iat" (Issued At) Claim validation failed: Token was issued in the future (iat: ${new Date(jwtClaims.iat * 1e3).toISOString()})`
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
if (jwtClaims.iat < currentTime - computeMaxTokenAgeSeconds(options.maxTokenAge) - clockTolerance) {
|
|
171
|
-
throw new Error(
|
|
172
|
-
`JWT "iat" (Issued At) Claim validation failed: Token is too old (maxTokenAge: ${options.maxTokenAge}s, iat: ${new Date(jwtClaims.iat * 1e3).toISOString()})`
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
function validateCriticalHeadersJWS(protectedHeader, requiredHeaders = []) {
|
|
178
|
-
if (!protectedHeader.crit) return;
|
|
179
|
-
const missingHeaderParams = /* @__PURE__ */ new Set();
|
|
180
|
-
const recognizedParams = /* @__PURE__ */ new Set([
|
|
181
|
-
...requiredHeaders,
|
|
182
|
-
"alg",
|
|
183
|
-
"typ",
|
|
184
|
-
"cty",
|
|
185
|
-
"kid",
|
|
186
|
-
"jwk",
|
|
187
|
-
"jku",
|
|
188
|
-
"x5c",
|
|
189
|
-
"x5t",
|
|
190
|
-
"x5u",
|
|
191
|
-
"b64"
|
|
192
|
-
]);
|
|
193
|
-
for (const param of protectedHeader.crit) {
|
|
194
|
-
if (recognizedParams.has(param) && (param in protectedHeader || param === "b64")) {
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
missingHeaderParams.add(param);
|
|
198
|
-
}
|
|
199
|
-
if (missingHeaderParams.size > 0) {
|
|
200
|
-
throw new Error(
|
|
201
|
-
`Missing critical header parameters: ${[...missingHeaderParams].join(", ")}`
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
function validateCriticalHeadersJWE(protectedHeader, understoodFromOptions) {
|
|
206
|
-
if (protectedHeader.crit && !understoodFromOptions) {
|
|
207
|
-
throw new Error(
|
|
208
|
-
`Unprocessed critical header parameters: ${protectedHeader.crit.join(", ")}`
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
if (!protectedHeader.crit) return;
|
|
212
|
-
const understoodParams = /* @__PURE__ */ new Set([
|
|
213
|
-
...understoodFromOptions || [],
|
|
214
|
-
// JWE specific standard headers:
|
|
215
|
-
"alg",
|
|
216
|
-
"enc",
|
|
217
|
-
"typ",
|
|
218
|
-
"cty",
|
|
219
|
-
"kid",
|
|
220
|
-
"jwk",
|
|
221
|
-
"jku",
|
|
222
|
-
"x5c",
|
|
223
|
-
"x5t",
|
|
224
|
-
"x5u",
|
|
225
|
-
"iv",
|
|
226
|
-
"tag",
|
|
227
|
-
"p2s",
|
|
228
|
-
"p2c",
|
|
229
|
-
"epk",
|
|
230
|
-
"apu",
|
|
231
|
-
"apv"
|
|
232
|
-
]);
|
|
233
|
-
for (const critParam of protectedHeader.crit) {
|
|
234
|
-
if (!Object.prototype.hasOwnProperty.call(protectedHeader, critParam)) {
|
|
235
|
-
throw new Error(
|
|
236
|
-
`Critical header parameter "${critParam}" listed in "crit" but not present in the protected header.`
|
|
237
|
-
);
|
|
238
|
-
}
|
|
239
|
-
if (!understoodParams.has(critParam)) {
|
|
240
|
-
throw new Error(`Unprocessed critical header parameters: ${critParam}`);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const DANGEROUS_KEYS = ["__proto__", "prototype", "constructor"];
|
|
246
|
-
function sanitizeObject(obj) {
|
|
247
|
-
if (!obj || typeof obj !== "object") return obj;
|
|
248
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
249
|
-
visit(obj, seen);
|
|
250
|
-
return obj;
|
|
251
|
-
}
|
|
252
|
-
function visit(current, seen) {
|
|
253
|
-
if (!current || typeof current !== "object") return;
|
|
254
|
-
if (seen.has(current)) return;
|
|
255
|
-
seen.add(current);
|
|
256
|
-
for (const key of DANGEROUS_KEYS) {
|
|
257
|
-
if (Object.prototype.hasOwnProperty.call(current, key)) {
|
|
258
|
-
try {
|
|
259
|
-
delete current[key];
|
|
260
|
-
} catch {
|
|
261
|
-
}
|
|
262
|
-
if (Object.prototype.hasOwnProperty.call(current, key)) {
|
|
263
|
-
try {
|
|
264
|
-
current[key] = void 0;
|
|
265
|
-
delete current[key];
|
|
266
|
-
} catch {
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
if (Object.prototype.hasOwnProperty.call(current, key)) {
|
|
270
|
-
try {
|
|
271
|
-
Object.defineProperty(current, key, {
|
|
272
|
-
value: void 0,
|
|
273
|
-
configurable: true,
|
|
274
|
-
enumerable: false,
|
|
275
|
-
writable: true
|
|
276
|
-
});
|
|
277
|
-
delete current[key];
|
|
278
|
-
} catch {
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
delete current["__proto__"];
|
|
284
|
-
delete current["prototype"];
|
|
285
|
-
delete current["constructor"];
|
|
286
|
-
if (Array.isArray(current)) {
|
|
287
|
-
for (const value of current) visit(value, seen);
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
for (const k of Object.keys(current))
|
|
291
|
-
visit(current[k], seen);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const textEncoder = /* @__PURE__ */ new TextEncoder();
|
|
295
|
-
const textDecoder = /* @__PURE__ */ new TextDecoder();
|
|
296
|
-
function base64Encode(data) {
|
|
297
|
-
const encodedData = data instanceof Uint8Array ? data : textEncoder.encode(data);
|
|
298
|
-
if (Uint8Array.prototype.toBase64) {
|
|
299
|
-
return encodedData.toBase64();
|
|
300
|
-
}
|
|
301
|
-
return btoa(String.fromCodePoint(...encodedData));
|
|
302
|
-
}
|
|
303
|
-
function base64UrlEncode(data) {
|
|
304
|
-
const encodedData = data instanceof Uint8Array ? data : textEncoder.encode(data);
|
|
305
|
-
if (Uint8Array.prototype.toBase64) {
|
|
306
|
-
return encodedData.toBase64({ alphabet: "base64url", omitPadding: true });
|
|
307
|
-
}
|
|
308
|
-
return btoa(String.fromCodePoint(...encodedData)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
309
|
-
}
|
|
310
|
-
function base64Decode(str, toString) {
|
|
311
|
-
const decodeToString = toString !== false;
|
|
312
|
-
if (!str) {
|
|
313
|
-
return decodeToString ? "" : new Uint8Array(0);
|
|
314
|
-
}
|
|
315
|
-
const data = Uint8Array.fromBase64 ? (
|
|
316
|
-
// @ts-expect-error
|
|
317
|
-
Uint8Array.fromBase64(str)
|
|
318
|
-
) : Uint8Array.from(atob(str), (b) => b.codePointAt(0));
|
|
319
|
-
return decodeToString ? textDecoder.decode(data) : data;
|
|
320
|
-
}
|
|
321
|
-
function base64UrlDecode(str, toString) {
|
|
322
|
-
const decodeToString = toString !== false;
|
|
323
|
-
if (!str) {
|
|
324
|
-
return decodeToString ? "" : new Uint8Array(0);
|
|
325
|
-
}
|
|
326
|
-
let data;
|
|
327
|
-
if (Uint8Array.fromBase64) {
|
|
328
|
-
data = Uint8Array.fromBase64(str, { alphabet: "base64url" });
|
|
329
|
-
} else {
|
|
330
|
-
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
331
|
-
while (str.length % 4) str += "=";
|
|
332
|
-
data = Uint8Array.from(atob(str), (b) => b.codePointAt(0));
|
|
333
|
-
}
|
|
334
|
-
return decodeToString ? textDecoder.decode(data) : data;
|
|
335
|
-
}
|
|
336
|
-
function randomBytes(length) {
|
|
337
|
-
return crypto.getRandomValues(new Uint8Array(length));
|
|
338
|
-
}
|
|
339
|
-
function concatUint8Arrays(...arrays) {
|
|
340
|
-
const totalLength = arrays.reduce((length, arr) => length + arr.length, 0);
|
|
341
|
-
const result = new Uint8Array(totalLength);
|
|
342
|
-
let offset = 0;
|
|
343
|
-
for (const arr of arrays) {
|
|
344
|
-
result.set(arr, offset);
|
|
345
|
-
offset += arr.length;
|
|
346
|
-
}
|
|
347
|
-
return result;
|
|
348
|
-
}
|
|
349
|
-
function maybeArray(item) {
|
|
350
|
-
return Array.isArray(item) ? item : [item];
|
|
351
|
-
}
|
|
352
|
-
function isJWK(key) {
|
|
353
|
-
return typeof key === "object" && key !== null && "kty" in key && typeof key.kty === "string";
|
|
354
|
-
}
|
|
355
|
-
function isJWKSet(key) {
|
|
356
|
-
return key && typeof key === "object" && "keys" in key && Array.isArray(key.keys);
|
|
357
|
-
}
|
|
358
|
-
function assertCryptoKey(key) {
|
|
359
|
-
if (!isCryptoKey(key)) {
|
|
360
|
-
throw new Error("CryptoKey instance expected");
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
function isCryptoKey(key) {
|
|
364
|
-
return key?.[Symbol.toStringTag] === "CryptoKey";
|
|
365
|
-
}
|
|
366
|
-
const isCryptoKeyPair = (key) => key && typeof key === "object" && isCryptoKey(key.publicKey) && isCryptoKey(key.privateKey);
|
|
367
|
-
function isSymmetricJWK(key) {
|
|
368
|
-
return isJWK(key) && key.kty === "oct" && typeof key.k === "string";
|
|
369
|
-
}
|
|
370
|
-
function isAsymmetricJWK(key) {
|
|
371
|
-
return !isSymmetricJWK(key);
|
|
372
|
-
}
|
|
373
|
-
function isPrivateJWK(key) {
|
|
374
|
-
if (!isJWK(key)) return false;
|
|
375
|
-
if (key.kty === "EC") {
|
|
376
|
-
return typeof key.d === "string";
|
|
377
|
-
}
|
|
378
|
-
if (key.kty === "OKP") {
|
|
379
|
-
return typeof key.d === "string";
|
|
380
|
-
}
|
|
381
|
-
if (key.kty === "RSA") {
|
|
382
|
-
return typeof key.d === "string";
|
|
383
|
-
}
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
function isPublicJWK(key) {
|
|
387
|
-
if (!isJWK(key)) return false;
|
|
388
|
-
if (key.kty === "EC") {
|
|
389
|
-
const ec = key;
|
|
390
|
-
return typeof ec.x === "string" && typeof ec.y === "string" && typeof ec.d !== "string";
|
|
391
|
-
}
|
|
392
|
-
if (key.kty === "OKP") {
|
|
393
|
-
const okp = key;
|
|
394
|
-
return typeof okp.x === "string" && typeof okp.d !== "string";
|
|
395
|
-
}
|
|
396
|
-
if (key.kty === "RSA") {
|
|
397
|
-
const rsa = key;
|
|
398
|
-
return typeof rsa.n === "string" && typeof rsa.e === "string" && typeof rsa.d !== "string";
|
|
399
|
-
}
|
|
400
|
-
return false;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
const index = {
|
|
404
|
-
__proto__: null,
|
|
405
|
-
applyTypCtyDefaults: applyTypCtyDefaults,
|
|
406
|
-
assertCryptoKey: assertCryptoKey,
|
|
407
|
-
base64Decode: base64Decode,
|
|
408
|
-
base64Encode: base64Encode,
|
|
409
|
-
base64UrlDecode: base64UrlDecode,
|
|
410
|
-
base64UrlEncode: base64UrlEncode,
|
|
411
|
-
computeExpiresInSeconds: computeExpiresInSeconds,
|
|
412
|
-
computeJwtTimeClaims: computeJwtTimeClaims,
|
|
413
|
-
computeMaxTokenAgeSeconds: computeMaxTokenAgeSeconds,
|
|
414
|
-
concatUint8Arrays: concatUint8Arrays,
|
|
415
|
-
decodeMaybeJWTString: decodeMaybeJWTString,
|
|
416
|
-
decodePayloadFromB64UrlSegment: decodePayloadFromB64UrlSegment,
|
|
417
|
-
decodePayloadFromBytes: decodePayloadFromBytes,
|
|
418
|
-
getPlaintextBytes: getPlaintextBytes,
|
|
419
|
-
isAsymmetricJWK: isAsymmetricJWK,
|
|
420
|
-
isCryptoKey: isCryptoKey,
|
|
421
|
-
isCryptoKeyPair: isCryptoKeyPair,
|
|
422
|
-
isJWK: isJWK,
|
|
423
|
-
isJWKSet: isJWKSet,
|
|
424
|
-
isJWTContent: isJWTContent,
|
|
425
|
-
isPrivateJWK: isPrivateJWK,
|
|
426
|
-
isPublicJWK: isPublicJWK,
|
|
427
|
-
isSymmetricJWK: isSymmetricJWK,
|
|
428
|
-
maybeArray: maybeArray,
|
|
429
|
-
randomBytes: randomBytes,
|
|
430
|
-
sanitizeObject: sanitizeObject,
|
|
431
|
-
textDecoder: textDecoder,
|
|
432
|
-
textEncoder: textEncoder,
|
|
433
|
-
validateCriticalHeadersJWE: validateCriticalHeadersJWE,
|
|
434
|
-
validateCriticalHeadersJWS: validateCriticalHeadersJWS,
|
|
435
|
-
validateJwtClaims: validateJwtClaims
|
|
436
|
-
};
|
|
437
|
-
|
|
438
|
-
export { assertCryptoKey as A, isAsymmetricJWK as B, maybeArray as C, isJWTContent as D, decodeMaybeJWTString as E, computeMaxTokenAgeSeconds as F, isJWK as a, applyTypCtyDefaults as b, computeJwtTimeClaims as c, base64UrlEncode as d, base64UrlDecode as e, decodePayloadFromBytes as f, getPlaintextBytes as g, validateJwtClaims as h, index as i, isCryptoKeyPair as j, isCryptoKey as k, textDecoder as l, isJWKSet as m, decodePayloadFromB64UrlSegment as n, validateCriticalHeadersJWS as o, computeExpiresInSeconds as p, isSymmetricJWK as q, randomBytes as r, sanitizeObject as s, textEncoder as t, isPrivateJWK as u, validateCriticalHeadersJWE as v, isPublicJWK as w, concatUint8Arrays as x, base64Decode as y, base64Encode as z };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { x as JWK_HMAC, y as JWK_RSA_SIGN, z as JWK_RSA_PSS, A as JWK_ECDSA, V as JoseHeaderParameters, E as ExpiresIn, v as JWK, w as JWKSet, Z as JWTClaimValidationOptions, X as JWTClaims } from './unjwt.BCi3FEWr.mjs';
|
|
2
|
-
|
|
3
|
-
type JWS_SIGN_EXTRA = "Ed25519" | "EdDSA";
|
|
4
|
-
/** JWS Signing Algorithm Identifier. */
|
|
5
|
-
type JWSAlgorithm = JWK_HMAC | JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWS_SIGN_EXTRA;
|
|
6
|
-
/** Recognized JWS Header Parameters, any other Header Members may also be present. */
|
|
7
|
-
interface JWSHeaderParameters extends JoseHeaderParameters {
|
|
8
|
-
/** JWS "alg" (Algorithm) Header Parameter */
|
|
9
|
-
alg?: JWSAlgorithm;
|
|
10
|
-
/**
|
|
11
|
-
* This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing
|
|
12
|
-
* Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
|
|
13
|
-
* If `false`, the payload is used directly without Base64URL encoding.
|
|
14
|
-
* Defaults to `true`.
|
|
15
|
-
*/
|
|
16
|
-
b64?: boolean;
|
|
17
|
-
}
|
|
18
|
-
/** JWS Protected Header */
|
|
19
|
-
interface JWSProtectedHeader extends JWSHeaderParameters {
|
|
20
|
-
alg: JWSAlgorithm;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* JWS (JSON Web Signature) options for signing
|
|
24
|
-
*/
|
|
25
|
-
interface JWSSignOptions {
|
|
26
|
-
/** The JWS Algorithm to use. Must be provided. */
|
|
27
|
-
alg?: JWSAlgorithm;
|
|
28
|
-
/**
|
|
29
|
-
* Additional protected header parameters. `alg` is automatically included.
|
|
30
|
-
* `typ` defaults to "JWT" if not provided and payload is an object.
|
|
31
|
-
*/
|
|
32
|
-
protectedHeader?: JWSHeaderParameters;
|
|
33
|
-
/** Date to use when comparing NumericDate claims, defaults to `new Date()`. */
|
|
34
|
-
currentDate?: Date;
|
|
35
|
-
/**
|
|
36
|
-
* Time at which the JWS should expire, if no `exp` was already provided.
|
|
37
|
-
*/
|
|
38
|
-
expiresIn?: ExpiresIn;
|
|
39
|
-
}
|
|
40
|
-
/** Result of JWS verification */
|
|
41
|
-
interface JWSVerifyResult<T extends JWTClaims | Uint8Array<ArrayBuffer> | string = JWTClaims | Uint8Array<ArrayBuffer> | string> {
|
|
42
|
-
/** The decoded and verified payload. */
|
|
43
|
-
payload: T;
|
|
44
|
-
/** The JWS Protected Header. */
|
|
45
|
-
protectedHeader: JWSProtectedHeader;
|
|
46
|
-
}
|
|
47
|
-
/** Options for JWS verification */
|
|
48
|
-
interface JWSVerifyOptions extends JWTClaimValidationOptions {
|
|
49
|
-
/** List of allowed algorithms. If provided, the JWS `alg` must be in this list. */
|
|
50
|
-
algorithms?: JWSAlgorithm[];
|
|
51
|
-
/** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred. */
|
|
52
|
-
forceUint8Array?: boolean;
|
|
53
|
-
/** Unless false, will parse payload as JWT and validate claims if applicable (typ "JWT"). */
|
|
54
|
-
validateJWT?: boolean;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Key lookup function for JWS verification.
|
|
58
|
-
* @param header The JWS Protected Header.
|
|
59
|
-
* @param token The JWS token.
|
|
60
|
-
* @returns The key material (CryptoKey, JWK, or raw Uint8Array) or a Promise resolving to it.
|
|
61
|
-
*/
|
|
62
|
-
type JWSKeyLookupFunction = (header: JWSProtectedHeader, token: string) => CryptoKey | JWK | JWKSet | Uint8Array<ArrayBuffer> | Promise<CryptoKey | JWK | JWKSet | Uint8Array<ArrayBuffer>>;
|
|
63
|
-
|
|
64
|
-
export type { JWSAlgorithm as J, JWSHeaderParameters as a, JWSProtectedHeader as b, JWSSignOptions as c, JWSVerifyResult as d, JWSVerifyOptions as e, JWSKeyLookupFunction as f };
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { K as KeyManagementAlgorithm, f as ContentEncryptionAlgorithm, E as ExpiresIn, V as JoseHeaderParameters, h as JWK_EC_Public, i as JWK_EC_Private, v as JWK, Z as JWTClaimValidationOptions, X as JWTClaims } from './unjwt.BCi3FEWr.mjs';
|
|
2
|
-
import { S as StrictOmit } from './unjwt.JdKqVANs.mjs';
|
|
3
|
-
|
|
4
|
-
/** Recognized JWE Header Parameters, any other Header Members may also be present. */
|
|
5
|
-
interface JWEHeaderParameters extends JoseHeaderParameters {
|
|
6
|
-
/**
|
|
7
|
-
* `alg` (Algorithm): Header Parameter - Key Management Algorithm
|
|
8
|
-
*/
|
|
9
|
-
alg?: string;
|
|
10
|
-
/**
|
|
11
|
-
* `enc` (Encryption Algorithm): Header Parameter - Content Encryption Algorithm
|
|
12
|
-
*/
|
|
13
|
-
enc?: string;
|
|
14
|
-
/**
|
|
15
|
-
* `p2c` (PBES2 Count): Header Parameter
|
|
16
|
-
*/
|
|
17
|
-
p2c?: number;
|
|
18
|
-
/**
|
|
19
|
-
* `p2s` (PBES2 Salt): Header Parameter. Base64URL encoded.
|
|
20
|
-
*/
|
|
21
|
-
p2s?: string;
|
|
22
|
-
/**
|
|
23
|
-
* "iv" (Initialization Vector) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
|
|
24
|
-
*/
|
|
25
|
-
iv?: string;
|
|
26
|
-
/**
|
|
27
|
-
* "tag" (Authentication Tag) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
|
|
28
|
-
*/
|
|
29
|
-
tag?: string;
|
|
30
|
-
/**
|
|
31
|
-
* "epk" (Ephemeral Public Key) Header Parameter for ECDH-ES.
|
|
32
|
-
*/
|
|
33
|
-
epk?: JWK_EC_Public;
|
|
34
|
-
/**
|
|
35
|
-
* "apu" (Agreement PartyUInfo) Header Parameter for ECDH-ES. Base64URL encoded.
|
|
36
|
-
*/
|
|
37
|
-
apu?: string;
|
|
38
|
-
/**
|
|
39
|
-
* "apv" (Agreement PartyVInfo) Header Parameter for ECDH-ES. Base64URL encoded.
|
|
40
|
-
*/
|
|
41
|
-
apv?: string;
|
|
42
|
-
/** Any other JWE Header member. */
|
|
43
|
-
[propName: string]: unknown;
|
|
44
|
-
}
|
|
45
|
-
/** Recognized JWE Key Management-related Header Parameters. */
|
|
46
|
-
interface JWEKeyManagementHeaderParameters {
|
|
47
|
-
apu?: Uint8Array<ArrayBuffer>;
|
|
48
|
-
apv?: Uint8Array<ArrayBuffer>;
|
|
49
|
-
/**
|
|
50
|
-
* For Internal use only
|
|
51
|
-
*/
|
|
52
|
-
p2c?: number;
|
|
53
|
-
/**
|
|
54
|
-
* For Internal use only
|
|
55
|
-
*/
|
|
56
|
-
p2s?: Uint8Array<ArrayBuffer>;
|
|
57
|
-
/**
|
|
58
|
-
* For Internal use only
|
|
59
|
-
*/
|
|
60
|
-
iv?: Uint8Array<ArrayBuffer>;
|
|
61
|
-
/**
|
|
62
|
-
* For Internal use only
|
|
63
|
-
*/
|
|
64
|
-
epk?: CryptoKey | JWK_EC_Public;
|
|
65
|
-
/**
|
|
66
|
-
* For Internal use only
|
|
67
|
-
*/
|
|
68
|
-
epkPrivateKey?: CryptoKey | JWK_EC_Private;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* JWE (JSON Web Encryption) encryption options
|
|
72
|
-
*/
|
|
73
|
-
interface JWEEncryptOptions {
|
|
74
|
-
/** JWE "alg" (Algorithm) Header Parameter. Key Management Algorithm. */
|
|
75
|
-
alg?: KeyManagementAlgorithm;
|
|
76
|
-
/** JWE "enc" (Encryption Algorithm) Header Parameter. Content Encryption Algorithm. */
|
|
77
|
-
enc?: ContentEncryptionAlgorithm;
|
|
78
|
-
/** Date to use when computing NumericDate claims, defaults to `new Date()`. */
|
|
79
|
-
currentDate?: Date;
|
|
80
|
-
/** Time at which the JWT should expire, if no `exp` was already provided (only when typ is "JWT" or explicitly parsing JWT). */
|
|
81
|
-
expiresIn?: ExpiresIn;
|
|
82
|
-
/** Additional JWE Protected Header parameters. */
|
|
83
|
-
protectedHeader?: StrictOmit<JWEHeaderParameters, "alg" | "enc" | "iv" | "tag" | "p2s" | "p2c" | "epk" | "apu" | "apv">;
|
|
84
|
-
/** Initialization Vector for AES-GCMKW key wrapping. Generated if not provided. */
|
|
85
|
-
keyManagementIV?: Uint8Array<ArrayBuffer>;
|
|
86
|
-
/** PBES2 Salt value (p2s). Required for PBES2 algorithms. */
|
|
87
|
-
p2s?: Uint8Array<ArrayBuffer>;
|
|
88
|
-
/** PBES2 Iteration count (p2c). Required for PBES2 algorithms. */
|
|
89
|
-
p2c?: number;
|
|
90
|
-
/** ECDH-ES specific options. */
|
|
91
|
-
ecdh?: {
|
|
92
|
-
/**
|
|
93
|
-
* ECDH-ES Ephemeral key material. Provide a private key (CryptoKey or JWK),
|
|
94
|
-
* a CryptoKeyPair, or an object exposing both public and private parts.
|
|
95
|
-
*/
|
|
96
|
-
ephemeralKey?: CryptoKey | JWK_EC_Private | CryptoKeyPair | {
|
|
97
|
-
publicKey: CryptoKey | JWK_EC_Public;
|
|
98
|
-
privateKey: CryptoKey | JWK_EC_Private;
|
|
99
|
-
};
|
|
100
|
-
/** ECDH-ES Agreement PartyUInfo. */
|
|
101
|
-
partyUInfo?: Uint8Array<ArrayBuffer>;
|
|
102
|
-
/** ECDH-ES Agreement PartyVInfo. */
|
|
103
|
-
partyVInfo?: Uint8Array<ArrayBuffer>;
|
|
104
|
-
};
|
|
105
|
-
/**
|
|
106
|
-
* Content Encryption Key (CEK) to use.
|
|
107
|
-
* If provided, it will be used directly. Otherwise, a CEK will be generated.
|
|
108
|
-
*/
|
|
109
|
-
cek?: Uint8Array<ArrayBuffer>;
|
|
110
|
-
/**
|
|
111
|
-
* Initialization Vector for content encryption.
|
|
112
|
-
* If provided, it will be used. Otherwise, one will be generated.
|
|
113
|
-
*/
|
|
114
|
-
contentEncryptionIV?: Uint8Array<ArrayBuffer>;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Key lookup function for JWE decryption.
|
|
118
|
-
* @param header The JWE Protected Header.
|
|
119
|
-
* @returns The key material (CryptoKey, JWK, password string, or raw Uint8Array) or a Promise resolving to it.
|
|
120
|
-
*/
|
|
121
|
-
type JWEKeyLookupFunction = (header: JWEHeaderParameters, token: string) => CryptoKey | JWK | string | Uint8Array<ArrayBuffer> | Promise<CryptoKey | JWK | string | Uint8Array<ArrayBuffer>>;
|
|
122
|
-
/**
|
|
123
|
-
* JWE (JSON Web Encryption) decryption options
|
|
124
|
-
*/
|
|
125
|
-
interface JWEDecryptOptions extends JWTClaimValidationOptions {
|
|
126
|
-
/** A list of allowed JWE "alg" (Algorithm) Header Parameter values for key management. */
|
|
127
|
-
algorithms?: KeyManagementAlgorithm[];
|
|
128
|
-
/** A list of allowed JWE "enc" (Encryption Algorithm) Header Parameter values for content encryption. */
|
|
129
|
-
encryptionAlgorithms?: ContentEncryptionAlgorithm[];
|
|
130
|
-
/** Algorithm to import the unwrapped CEK as (e.g., { name: 'AES-GCM' }). Defaults based on 'enc'. */
|
|
131
|
-
unwrappedKeyAlgorithm?: Parameters<typeof crypto.subtle.importKey>[2];
|
|
132
|
-
/** Key usages for the unwrapped CEK. Defaults based on 'enc' (typically ['encrypt', 'decrypt']). */
|
|
133
|
-
keyUsage?: KeyUsage[];
|
|
134
|
-
/** Mark the unwrapped CEK as extractable. Defaults to true. */
|
|
135
|
-
extractable?: boolean;
|
|
136
|
-
/** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred based on JWE headers. */
|
|
137
|
-
forceUint8Array?: boolean;
|
|
138
|
-
/** Unless false, will parse payload as JWT and validate claims if applicable (typ "JWT"). */
|
|
139
|
-
validateJWT?: boolean;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Result of a JWE decryption operation.
|
|
143
|
-
*/
|
|
144
|
-
interface JWEDecryptResult<T extends JWTClaims | Uint8Array<ArrayBuffer> | string = JWTClaims | Uint8Array<ArrayBuffer> | string> {
|
|
145
|
-
/** The decrypted payload. */
|
|
146
|
-
payload: T;
|
|
147
|
-
/** The JWE Protected Header. */
|
|
148
|
-
protectedHeader: JWEHeaderParameters;
|
|
149
|
-
/** The Content Encryption Key (CEK) used for decryption, as Uint8Array. */
|
|
150
|
-
cek: Uint8Array<ArrayBuffer>;
|
|
151
|
-
/** The Additional Authenticated Data (AAD) used, as Uint8Array. */
|
|
152
|
-
aad: Uint8Array<ArrayBuffer>;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export type { JWEHeaderParameters as J, JWEKeyManagementHeaderParameters as a, JWEEncryptOptions as b, JWEKeyLookupFunction as c, JWEDecryptOptions as d, JWEDecryptResult as e };
|