@monocloud/auth-core 0.1.2 → 0.1.4

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.
@@ -1,475 +0,0 @@
1
-
2
- //#region src/utils/internal.ts
3
- /**
4
- * @ignore
5
- * Converts a string to a Base64URL encoded string.
6
- *
7
- * @param input - The string to encode.
8
- *
9
- * @returns The Base64URL encoded string.
10
- */
11
- const toB64Url = (input) => input.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
12
- /**
13
- * @ignore
14
- * Parses a string value into a boolean.
15
- *
16
- * @param value - The string value to parse.
17
- *
18
- * @returns `true` if "true", `false` if "false", otherwise `undefined`.
19
- */
20
- const getBoolean = (value) => {
21
- const v = value?.toLowerCase()?.trim();
22
- if (v === "true") return true;
23
- if (v === "false") return false;
24
- };
25
- /**
26
- * @ignore
27
- * Parses a string value into a number.
28
- *
29
- * @param value - The string value to parse.
30
- *
31
- * @returns The parsed number, or `undefined` if empty or invalid.
32
- */
33
- const getNumber = (value) => {
34
- const v = value?.trim();
35
- if (v === void 0 || v.length === 0) return;
36
- const p = parseInt(v, 10);
37
- return Number.isNaN(p) ? void 0 : p;
38
- };
39
- /**
40
- * @ignore
41
- * Ensures that a string has a leading forward slash.
42
- *
43
- * @param val - The string to check.
44
- *
45
- * @returns The string with a leading slash.
46
- */
47
- const ensureLeadingSlash = (val) => {
48
- const v = val?.trim();
49
- if (!v) return v;
50
- return v.startsWith("/") ? v : `/${v}`;
51
- };
52
- /**
53
- * @ignore
54
- * Removes a trailing forward slash from a string.
55
- *
56
- * @param val - The string to check.
57
- *
58
- * @returns The string without a trailing slash.
59
- */
60
- const removeTrailingSlash = (val) => {
61
- const v = val?.trim();
62
- if (!v) return v;
63
- return v.endsWith("/") ? v.substring(0, v.length - 1) : v;
64
- };
65
- /**
66
- * @ignore
67
- * Checks if a value is present (not null, undefined, or an empty string).
68
- *
69
- * @param value - The value to check.
70
- *
71
- * @returns `true` if the value is present, `false` otherwise.
72
- */
73
- const isPresent = (value) => {
74
- if (typeof value === "boolean" || typeof value === "number") return true;
75
- const v = value?.trim();
76
- return v !== void 0 && v !== null && v.length > 0;
77
- };
78
- /**
79
- * @ignore
80
- * Checks if a URL is an absolute URL (starts with http:// or https://).
81
- *
82
- * @param url - The URL to check.
83
- *
84
- * @returns `true` if absolute, `false` otherwise.
85
- */
86
- const isAbsoluteUrl = (url) => (url?.startsWith("http://") || url?.startsWith("https://")) ?? false;
87
- /**
88
- * @ignore
89
- * Checks if two URLs have the same origin (host and port).
90
- *
91
- * @param url - The first URL.
92
- * @param urlToCheck - The second URL to compare against.
93
- *
94
- * @returns `true` if they share the same origin, `false` otherwise.
95
- */
96
- const isSameHost = (url, urlToCheck) => {
97
- try {
98
- const u = new URL(url);
99
- const u2 = new URL(urlToCheck);
100
- return u.origin === u2.origin;
101
- } catch {
102
- return false;
103
- }
104
- };
105
- /**
106
- * @ignore
107
- * Converts a string to a Uint8Array using TextEncoder.
108
- *
109
- * @param str - The string to convert.
110
- *
111
- * @returns A Uint8Array representation of the string.
112
- */
113
- const stringToArrayBuffer = (str) => {
114
- return new TextEncoder().encode(str);
115
- };
116
- /**
117
- * @ignore
118
- * Converts an ArrayBuffer to a string using TextDecoder.
119
- *
120
- * @param buffer - The buffer to convert.
121
- *
122
- * @returns The decoded string.
123
- */
124
- const arrayBufferToString = (buffer) => {
125
- return new TextDecoder().decode(buffer);
126
- };
127
- /**
128
- * @ignore
129
- * Converts a Base64URL string back to a standard Base64 string with padding.
130
- *
131
- * @param input - The Base64URL string.
132
- *
133
- * @returns A standard Base64 string.
134
- */
135
- const fromB64Url = (input) => {
136
- let str = input;
137
- if (str.length % 4 !== 0) str += "===".slice(0, 4 - str.length % 4);
138
- str = str.replace(/-/g, "+").replace(/_/g, "/");
139
- return str;
140
- };
141
- /**
142
- * @ignore
143
- * Decodes a Base64URL encoded string.
144
- *
145
- * @param input - The Base64URL string to decode.
146
- *
147
- * @returns The decoded plaintext string.
148
- */
149
- const decodeBase64Url = (input) => atob(fromB64Url(input).replace(/\s/g, ""));
150
- /**
151
- * @ignore
152
- * Converts a Uint8Array to a Base64URL encoded string.
153
- *
154
- * @param buffer - The buffer to encode.
155
- *
156
- * @returns The Base64URL encoded string.
157
- */
158
- const arrayBufferToBase64 = (buffer) => {
159
- const binary = new Uint8Array(buffer).reduce((acc, byte) => acc + String.fromCharCode(byte), "");
160
- return btoa(binary).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
161
- };
162
- /**
163
- * @ignore
164
- * Gets the current Unix timestamp in seconds.
165
- *
166
- * @returns The current timestamp.
167
- */
168
- const now = () => Math.ceil(Date.now() / 1e3);
169
- const SUPPORTED_JWS_ALGS = [
170
- "RS256",
171
- "RS384",
172
- "RS512",
173
- "PS256",
174
- "PS384",
175
- "PS512",
176
- "ES256",
177
- "ES384",
178
- "ES512"
179
- ];
180
- /**
181
- * Retrieves a public CryptoKey from a JWK set based on the JWS header.
182
- *
183
- * @param jwks - The set of JSON Web Keys.
184
- * @param header - The JWS header containing the algorithm and key ID.
185
- *
186
- * @returns A promise that resolves to the CryptoKey.
187
- *
188
- * @throws If no applicable key or multiple keys are found or the algorithm is unsupported.
189
- */
190
- const getPublicSigKeyFromIssuerJwks = async (jwks, header) => {
191
- const { alg, kid } = header;
192
- if (!SUPPORTED_JWS_ALGS.includes(alg)) throw new Error("unsupported JWS \"alg\" identifier");
193
- let kty;
194
- switch (alg.slice(0, 2)) {
195
- case "RS":
196
- case "PS":
197
- kty = "RSA";
198
- break;
199
- case "ES":
200
- kty = "EC";
201
- break;
202
- }
203
- const { 0: jwk, length } = jwks.filter((jwk$1) => {
204
- if (jwk$1.kty !== kty) return false;
205
- if (kid !== void 0 && kid !== jwk$1.kid) return false;
206
- if (jwk$1.alg !== void 0 && alg !== jwk$1.alg) return false;
207
- if (jwk$1.use !== void 0 && jwk$1.use !== "sig") return false;
208
- if (jwk$1.key_ops?.includes("verify") === false) return false;
209
- switch (true) {
210
- case alg === "ES256" && jwk$1.crv !== "P-256":
211
- case alg === "ES384" && jwk$1.crv !== "P-384":
212
- case alg === "ES512" && jwk$1.crv !== "P-521": return false;
213
- }
214
- return true;
215
- });
216
- if (length !== 1) throw new Error("error when selecting a JWT verification key, multiple applicable keys found, a \"kid\" JWT Header Parameter is required");
217
- let algorithm;
218
- switch (alg) {
219
- case "PS256":
220
- case "PS384":
221
- case "PS512":
222
- algorithm = {
223
- name: "RSA-PSS",
224
- hash: `SHA-${alg.slice(-3)}`
225
- };
226
- break;
227
- case "RS256":
228
- case "RS384":
229
- case "RS512":
230
- algorithm = {
231
- name: "RSASSA-PKCS1-v1_5",
232
- hash: `SHA-${alg.slice(-3)}`
233
- };
234
- break;
235
- case "ES256":
236
- case "ES384":
237
- algorithm = {
238
- name: "ECDSA",
239
- namedCurve: `P-${alg.slice(-3)}`
240
- };
241
- break;
242
- case "ES512":
243
- algorithm = {
244
- name: "ECDSA",
245
- namedCurve: "P-521"
246
- };
247
- break;
248
- }
249
- const { ext, key_ops, use, ...k } = jwk;
250
- const key = await crypto.subtle.importKey("jwk", k, algorithm, true, ["verify"]);
251
- if (key.type !== "public") throw new Error("jwks_uri must only contain public keys");
252
- return key;
253
- };
254
- const CHUNK_SIZE = 32768;
255
- /**
256
- * @ignore
257
- * Encodes a Uint8Array or ArrayBuffer into a Base64URL string using chunked processing.
258
- *
259
- * @param input - The data to encode.
260
- *
261
- * @returns The Base64URL encoded string.
262
- */
263
- const encodeBase64Url = (input) => {
264
- if (input instanceof ArrayBuffer) input = new Uint8Array(input);
265
- const arr = [];
266
- for (let i = 0; i < input.byteLength; i += CHUNK_SIZE) arr.push(String.fromCharCode.apply(null, Array.from(new Uint8Array(input.slice(i, i + CHUNK_SIZE)))));
267
- return btoa(arr.join("")).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
268
- };
269
- /**
270
- * @ignore
271
- * Generates a random Base64URL encoded string.
272
- *
273
- * @param length - The number of random bytes to generate.
274
- *
275
- * @returns A random Base64URL string.
276
- */
277
- const randomBytes = (length = 32) => encodeBase64Url(crypto.getRandomValues(new Uint8Array(length)));
278
- /**
279
- * @ignore
280
- * Checks if a value is a non-null, non-array JSON object.
281
- *
282
- * @param input - The value to check.
283
- *
284
- * @returns `true` if the value is a JSON object.
285
- */
286
- const isJsonObject = (input) => {
287
- if (input === null || typeof input !== "object" || Array.isArray(input)) return false;
288
- return true;
289
- };
290
- /**
291
- * @ignore
292
- * Parses a space-separated string into an array of strings.
293
- *
294
- * @param s - The space-separated string.
295
- *
296
- * @returns An array of strings, or `undefined` if input is empty.
297
- */
298
- const parseSpaceSeparated = (s) => s?.split(/\s+/).map((x) => x.trim()).filter(Boolean);
299
- /**
300
- * @ignore
301
- * Parses a space-separated string into a Set of strings.
302
- *
303
- * @param s - The space-separated string.
304
- *
305
- * @returns A Set containing the unique strings.
306
- */
307
- const parseSpaceSeparatedSet = (s) => {
308
- if (!s) return /* @__PURE__ */ new Set();
309
- return new Set(parseSpaceSeparated(s));
310
- };
311
- /**
312
- * @ignore
313
- * Compares two Sets for equality.
314
- *
315
- * @param a - The first Set
316
- * @param b - The second Set
317
- * @param strict - If `true`, requires both sets to be the same size. @defaultValue true
318
- *
319
- * @returns `true` if the sets are equal
320
- */
321
- const setsEqual = (a, b, strict = true) => {
322
- if (strict && a.size !== b.size) return false;
323
- for (const v of a) if (!b.has(v)) return false;
324
- return true;
325
- };
326
- /**
327
- * Finds a specific access token in an array based on resource and scopes.
328
- *
329
- * @param tokens - The array of access tokens.
330
- * @param resource - Space-separated resource indicators.
331
- * @param scopes - Space-separated scopes.
332
- *
333
- * @returns The matching AccessToken, or `undefined` if not found.
334
- */
335
- const findToken = (tokens, resource, scopes) => {
336
- if (!Array.isArray(tokens) || tokens.length === 0) return;
337
- const desiredResource = parseSpaceSeparatedSet(resource);
338
- const desiredScopes = parseSpaceSeparatedSet(scopes);
339
- return tokens.find((t) => setsEqual(desiredResource, parseSpaceSeparatedSet(t.resource)) && setsEqual(desiredScopes, parseSpaceSeparatedSet(t.requestedScopes)));
340
- };
341
-
342
- //#endregion
343
- Object.defineProperty(exports, 'arrayBufferToBase64', {
344
- enumerable: true,
345
- get: function () {
346
- return arrayBufferToBase64;
347
- }
348
- });
349
- Object.defineProperty(exports, 'arrayBufferToString', {
350
- enumerable: true,
351
- get: function () {
352
- return arrayBufferToString;
353
- }
354
- });
355
- Object.defineProperty(exports, 'decodeBase64Url', {
356
- enumerable: true,
357
- get: function () {
358
- return decodeBase64Url;
359
- }
360
- });
361
- Object.defineProperty(exports, 'encodeBase64Url', {
362
- enumerable: true,
363
- get: function () {
364
- return encodeBase64Url;
365
- }
366
- });
367
- Object.defineProperty(exports, 'ensureLeadingSlash', {
368
- enumerable: true,
369
- get: function () {
370
- return ensureLeadingSlash;
371
- }
372
- });
373
- Object.defineProperty(exports, 'findToken', {
374
- enumerable: true,
375
- get: function () {
376
- return findToken;
377
- }
378
- });
379
- Object.defineProperty(exports, 'fromB64Url', {
380
- enumerable: true,
381
- get: function () {
382
- return fromB64Url;
383
- }
384
- });
385
- Object.defineProperty(exports, 'getBoolean', {
386
- enumerable: true,
387
- get: function () {
388
- return getBoolean;
389
- }
390
- });
391
- Object.defineProperty(exports, 'getNumber', {
392
- enumerable: true,
393
- get: function () {
394
- return getNumber;
395
- }
396
- });
397
- Object.defineProperty(exports, 'getPublicSigKeyFromIssuerJwks', {
398
- enumerable: true,
399
- get: function () {
400
- return getPublicSigKeyFromIssuerJwks;
401
- }
402
- });
403
- Object.defineProperty(exports, 'isAbsoluteUrl', {
404
- enumerable: true,
405
- get: function () {
406
- return isAbsoluteUrl;
407
- }
408
- });
409
- Object.defineProperty(exports, 'isJsonObject', {
410
- enumerable: true,
411
- get: function () {
412
- return isJsonObject;
413
- }
414
- });
415
- Object.defineProperty(exports, 'isPresent', {
416
- enumerable: true,
417
- get: function () {
418
- return isPresent;
419
- }
420
- });
421
- Object.defineProperty(exports, 'isSameHost', {
422
- enumerable: true,
423
- get: function () {
424
- return isSameHost;
425
- }
426
- });
427
- Object.defineProperty(exports, 'now', {
428
- enumerable: true,
429
- get: function () {
430
- return now;
431
- }
432
- });
433
- Object.defineProperty(exports, 'parseSpaceSeparated', {
434
- enumerable: true,
435
- get: function () {
436
- return parseSpaceSeparated;
437
- }
438
- });
439
- Object.defineProperty(exports, 'parseSpaceSeparatedSet', {
440
- enumerable: true,
441
- get: function () {
442
- return parseSpaceSeparatedSet;
443
- }
444
- });
445
- Object.defineProperty(exports, 'randomBytes', {
446
- enumerable: true,
447
- get: function () {
448
- return randomBytes;
449
- }
450
- });
451
- Object.defineProperty(exports, 'removeTrailingSlash', {
452
- enumerable: true,
453
- get: function () {
454
- return removeTrailingSlash;
455
- }
456
- });
457
- Object.defineProperty(exports, 'setsEqual', {
458
- enumerable: true,
459
- get: function () {
460
- return setsEqual;
461
- }
462
- });
463
- Object.defineProperty(exports, 'stringToArrayBuffer', {
464
- enumerable: true,
465
- get: function () {
466
- return stringToArrayBuffer;
467
- }
468
- });
469
- Object.defineProperty(exports, 'toB64Url', {
470
- enumerable: true,
471
- get: function () {
472
- return toB64Url;
473
- }
474
- });
475
- //# sourceMappingURL=internal-DytuO03E.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"internal-DytuO03E.cjs","names":["SUPPORTED_JWS_ALGS: JWSAlgorithm[]","kty: string","jwk","algorithm:\n | RsaHashedImportParams\n | EcKeyImportParams\n | AlgorithmIdentifier"],"sources":["../src/utils/internal.ts"],"sourcesContent":["import type {\n AccessToken,\n Jwk,\n JWSAlgorithm,\n JwsHeaderParameters,\n} from '../types';\n\n/**\n * @ignore\n * Converts a string to a Base64URL encoded string.\n *\n * @param input - The string to encode.\n *\n * @returns The Base64URL encoded string.\n */\nexport const toB64Url = (input: string): string =>\n input.replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '');\n\n/**\n * @ignore\n * Parses a string value into a boolean.\n *\n * @param value - The string value to parse.\n *\n * @returns `true` if \"true\", `false` if \"false\", otherwise `undefined`.\n */\nexport const getBoolean = (value?: string): boolean | undefined => {\n const v = value?.toLowerCase()?.trim();\n\n if (v === 'true') {\n return true;\n }\n\n if (v === 'false') {\n return false;\n }\n\n return undefined;\n};\n\n/**\n * @ignore\n * Parses a string value into a number.\n *\n * @param value - The string value to parse.\n *\n * @returns The parsed number, or `undefined` if empty or invalid.\n */\nexport const getNumber = (value?: string): number | undefined => {\n const v = value?.trim();\n\n if (v === undefined || v.length === 0) {\n return undefined;\n }\n\n const p = parseInt(v, 10);\n\n return Number.isNaN(p) ? undefined : p;\n};\n\n/**\n * @ignore\n * Ensures that a string has a leading forward slash.\n *\n * @param val - The string to check.\n *\n * @returns The string with a leading slash.\n */\nexport const ensureLeadingSlash = (val?: string): string => {\n const v = val?.trim();\n\n if (!v) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return v!;\n }\n\n return v.startsWith('/') ? v : `/${v}`;\n};\n\n/**\n * @ignore\n * Removes a trailing forward slash from a string.\n *\n * @param val - The string to check.\n *\n * @returns The string without a trailing slash.\n */\nexport const removeTrailingSlash = (val?: string): string => {\n const v = val?.trim();\n\n if (!v) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return v!;\n }\n\n return v.endsWith('/') ? v.substring(0, v.length - 1) : v;\n};\n\n/**\n * @ignore\n * Checks if a value is present (not null, undefined, or an empty string).\n *\n * @param value - The value to check.\n *\n * @returns `true` if the value is present, `false` otherwise.\n */\nexport const isPresent = (value?: string | number | boolean): boolean => {\n if (typeof value === 'boolean' || typeof value === 'number') {\n return true;\n }\n const v = value?.trim();\n return v !== undefined && v !== null && v.length > 0;\n};\n\n/**\n * @ignore\n * Checks if a URL is an absolute URL (starts with http:// or https://).\n *\n * @param url - The URL to check.\n *\n * @returns `true` if absolute, `false` otherwise.\n */\nexport const isAbsoluteUrl = (url: string): boolean =>\n (url?.startsWith('http://') || url?.startsWith('https://')) ?? false;\n\n/**\n * @ignore\n * Checks if two URLs have the same origin (host and port).\n *\n * @param url - The first URL.\n * @param urlToCheck - The second URL to compare against.\n *\n * @returns `true` if they share the same origin, `false` otherwise.\n */\nexport const isSameHost = (url: string, urlToCheck: string): boolean => {\n try {\n const u = new URL(url);\n const u2 = new URL(urlToCheck);\n\n return u.origin === u2.origin;\n } catch {\n return false;\n }\n};\n\n/**\n * @ignore\n * Converts a string to a Uint8Array using TextEncoder.\n *\n * @param str - The string to convert.\n *\n * @returns A Uint8Array representation of the string.\n */\nexport const stringToArrayBuffer = (str: string): Uint8Array => {\n const encoder = new TextEncoder();\n return encoder.encode(str);\n};\n\n/**\n * @ignore\n * Converts an ArrayBuffer to a string using TextDecoder.\n *\n * @param buffer - The buffer to convert.\n *\n * @returns The decoded string.\n */\nexport const arrayBufferToString = (buffer: ArrayBuffer): string => {\n const decoder = new TextDecoder();\n return decoder.decode(buffer);\n};\n\n/**\n * @ignore\n * Converts a Base64URL string back to a standard Base64 string with padding.\n *\n * @param input - The Base64URL string.\n *\n * @returns A standard Base64 string.\n */\nexport const fromB64Url = (input: string): string => {\n let str = input;\n if (str.length % 4 !== 0) {\n str += '==='.slice(0, 4 - (str.length % 4));\n }\n\n str = str.replace(/-/g, '+').replace(/_/g, '/');\n\n return str;\n};\n\n/**\n * @ignore\n * Decodes a Base64URL encoded string.\n *\n * @param input - The Base64URL string to decode.\n *\n * @returns The decoded plaintext string.\n */\nexport const decodeBase64Url = (input: string): string =>\n atob(fromB64Url(input).replace(/\\s/g, ''));\n\n/**\n * @ignore\n * Converts a Uint8Array to a Base64URL encoded string.\n *\n * @param buffer - The buffer to encode.\n *\n * @returns The Base64URL encoded string.\n */\nexport const arrayBufferToBase64 = (buffer: Uint8Array): string => {\n const bytes = new Uint8Array(buffer);\n const binary = bytes.reduce(\n (acc, byte) => acc + String.fromCharCode(byte),\n ''\n );\n return btoa(binary).replace(/=/g, '').replace(/\\+/g, '-').replace(/\\//g, '_');\n};\n\n/**\n * @ignore\n * Gets the current Unix timestamp in seconds.\n *\n * @returns The current timestamp.\n */\nexport const now = (): number => Math.ceil(Date.now() / 1000);\n\nconst SUPPORTED_JWS_ALGS: JWSAlgorithm[] = [\n 'RS256',\n 'RS384',\n 'RS512',\n 'PS256',\n 'PS384',\n 'PS512',\n 'ES256',\n 'ES384',\n 'ES512',\n];\n\n/**\n * Retrieves a public CryptoKey from a JWK set based on the JWS header.\n *\n * @param jwks - The set of JSON Web Keys.\n * @param header - The JWS header containing the algorithm and key ID.\n *\n * @returns A promise that resolves to the CryptoKey.\n *\n * @throws If no applicable key or multiple keys are found or the algorithm is unsupported.\n */\nexport const getPublicSigKeyFromIssuerJwks = async (\n jwks: Jwk[],\n header: JwsHeaderParameters\n): Promise<CryptoKey> => {\n const { alg, kid } = header;\n\n if (!SUPPORTED_JWS_ALGS.includes(alg)) {\n throw new Error('unsupported JWS \"alg\" identifier');\n }\n\n let kty: string;\n switch (alg.slice(0, 2)) {\n case 'RS': // Fall through\n case 'PS':\n kty = 'RSA';\n break;\n case 'ES':\n kty = 'EC';\n break;\n }\n\n const candidates = jwks.filter(jwk => {\n // filter keys based on the mapping of signature algorithms to Key Type\n if (jwk.kty !== kty) {\n return false;\n }\n\n // filter keys based on the JWK Key ID in the header\n if (kid !== undefined && kid !== jwk.kid) {\n return false;\n }\n\n // filter keys based on the key's declared Algorithm\n if (jwk.alg !== undefined && alg !== jwk.alg) {\n return false;\n }\n\n // filter keys based on the key's declared Public Key Use\n if (jwk.use !== undefined && jwk.use !== 'sig') {\n return false;\n }\n\n // filter keys based on the key's declared Key Operations\n if (jwk.key_ops?.includes('verify') === false) {\n return false;\n }\n\n // filter keys based on alg-specific key requirements\n switch (true) {\n case alg === 'ES256' && jwk.crv !== 'P-256': // Fall through\n case alg === 'ES384' && jwk.crv !== 'P-384': // Fall through\n case alg === 'ES512' && jwk.crv !== 'P-521': // Fall through\n return false;\n }\n\n return true;\n });\n\n const { 0: jwk, length } = candidates;\n\n if (length !== 1) {\n throw new Error(\n 'error when selecting a JWT verification key, multiple applicable keys found, a \"kid\" JWT Header Parameter is required'\n );\n }\n\n let algorithm:\n | RsaHashedImportParams\n | EcKeyImportParams\n | AlgorithmIdentifier;\n\n switch (alg) {\n case 'PS256': // Fall through\n case 'PS384': // Fall through\n case 'PS512':\n algorithm = { name: 'RSA-PSS', hash: `SHA-${alg.slice(-3)}` };\n break;\n case 'RS256': // Fall through\n case 'RS384': // Fall through\n case 'RS512':\n algorithm = { name: 'RSASSA-PKCS1-v1_5', hash: `SHA-${alg.slice(-3)}` };\n break;\n case 'ES256': // Fall through\n case 'ES384':\n algorithm = { name: 'ECDSA', namedCurve: `P-${alg.slice(-3)}` };\n break;\n case 'ES512':\n algorithm = { name: 'ECDSA', namedCurve: 'P-521' };\n break;\n }\n\n const { ext, key_ops, use, ...k } = jwk;\n\n const key = await crypto.subtle.importKey('jwk', k, algorithm, true, [\n 'verify',\n ]);\n\n if (key.type !== 'public') {\n throw new Error('jwks_uri must only contain public keys');\n }\n\n return key;\n};\n\nconst CHUNK_SIZE = 0x8000;\n\n/**\n * @ignore\n * Encodes a Uint8Array or ArrayBuffer into a Base64URL string using chunked processing.\n *\n * @param input - The data to encode.\n *\n * @returns The Base64URL encoded string.\n */\nexport const encodeBase64Url = (input: Uint8Array | ArrayBuffer): string => {\n if (input instanceof ArrayBuffer) {\n // eslint-disable-next-line no-param-reassign\n input = new Uint8Array(input);\n }\n\n const arr = [];\n for (let i = 0; i < input.byteLength; i += CHUNK_SIZE) {\n arr.push(\n String.fromCharCode.apply(\n null,\n Array.from(new Uint8Array(input.slice(i, i + CHUNK_SIZE)))\n )\n );\n }\n return btoa(arr.join(''))\n .replace(/=/g, '')\n .replace(/\\+/g, '-')\n .replace(/\\//g, '_');\n};\n\n/**\n * @ignore\n * Generates a random Base64URL encoded string.\n *\n * @param length - The number of random bytes to generate.\n *\n * @returns A random Base64URL string.\n */\nexport const randomBytes = (length = 32): string =>\n encodeBase64Url(crypto.getRandomValues(new Uint8Array(length)));\n\n/**\n * @ignore\n * Checks if a value is a non-null, non-array JSON object.\n *\n * @param input - The value to check.\n *\n * @returns `true` if the value is a JSON object.\n */\nexport const isJsonObject = <T>(input: unknown): input is T => {\n if (input === null || typeof input !== 'object' || Array.isArray(input)) {\n return false;\n }\n\n return true;\n};\n\n/**\n * @ignore\n * Parses a space-separated string into an array of strings.\n *\n * @param s - The space-separated string.\n *\n * @returns An array of strings, or `undefined` if input is empty.\n */\nexport const parseSpaceSeparated = (s?: string): string[] | undefined =>\n s\n ?.split(/\\s+/)\n .map(x => x.trim())\n .filter(Boolean);\n\n/**\n * @ignore\n * Parses a space-separated string into a Set of strings.\n *\n * @param s - The space-separated string.\n *\n * @returns A Set containing the unique strings.\n */\nexport const parseSpaceSeparatedSet = (s?: string): Set<string> => {\n if (!s) {\n return new Set();\n }\n\n return new Set(parseSpaceSeparated(s));\n};\n\n/**\n * @ignore\n * Compares two Sets for equality.\n *\n * @param a - The first Set\n * @param b - The second Set\n * @param strict - If `true`, requires both sets to be the same size. @defaultValue true\n *\n * @returns `true` if the sets are equal\n */\nexport const setsEqual = (\n a: Set<string>,\n b: Set<string>,\n strict = true\n): boolean => {\n if (strict && a.size !== b.size) {\n return false;\n }\n\n for (const v of a) {\n if (!b.has(v)) {\n return false;\n }\n }\n\n return true;\n};\n\n/**\n * Finds a specific access token in an array based on resource and scopes.\n *\n * @param tokens - The array of access tokens.\n * @param resource - Space-separated resource indicators.\n * @param scopes - Space-separated scopes.\n *\n * @returns The matching AccessToken, or `undefined` if not found.\n */\nexport const findToken = (\n tokens?: AccessToken[],\n resource?: string,\n scopes?: string\n): AccessToken | undefined => {\n if (!Array.isArray(tokens) || tokens.length === 0) {\n return undefined;\n }\n\n const desiredResource = parseSpaceSeparatedSet(resource);\n const desiredScopes = parseSpaceSeparatedSet(scopes);\n\n return tokens.find(\n t =>\n setsEqual(desiredResource, parseSpaceSeparatedSet(t.resource)) &&\n setsEqual(desiredScopes, parseSpaceSeparatedSet(t.requestedScopes))\n );\n};\n"],"mappings":";;;;;;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