jose 6.0.2 → 6.0.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,9 +1,3 @@
1
- /**
2
- * JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other
3
- * Web-interoperable runtimes
4
- *
5
- * @module jose
6
- */
7
1
  export { compactDecrypt } from './jwe/compact/decrypt.js';
8
2
  export type { CompactDecryptGetKey } from './jwe/compact/decrypt.js';
9
3
  export { flattenedDecrypt } from './jwe/flattened/decrypt.js';
@@ -9,7 +9,7 @@ function isCloudflareWorkers() {
9
9
  let USER_AGENT;
10
10
  if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
11
11
  const NAME = 'jose';
12
- const VERSION = 'v6.0.2';
12
+ const VERSION = 'v6.0.4';
13
13
  USER_AGENT = `${NAME}/${VERSION}`;
14
14
  }
15
15
  export const customFetch = Symbol();
@@ -185,11 +185,17 @@ function spkiFromX509(buf) {
185
185
  const tbsCertificate = getElement(getElement(parseElement(buf).contents)[0].contents);
186
186
  return encodeBase64(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw);
187
187
  }
188
- let X509Certificate;
188
+ let createPublicKey;
189
189
  function getSPKI(x509) {
190
- if ((X509Certificate ||= globalThis.process?.getBuiltinModule?.('node:crypto')?.X509Certificate)) {
190
+ try {
191
+ createPublicKey ??= globalThis.process?.getBuiltinModule?.('node:crypto')?.createPublicKey;
192
+ }
193
+ catch {
194
+ createPublicKey = 0;
195
+ }
196
+ if (createPublicKey) {
191
197
  try {
192
- return new X509Certificate(x509).publicKey.export({ format: 'pem', type: 'spki' });
198
+ return new createPublicKey(x509).export({ format: 'pem', type: 'spki' });
193
199
  }
194
200
  catch { }
195
201
  }
@@ -1,26 +1,29 @@
1
- import { encoder, decoder, Buffer } from '../lib/buffer_utils.js';
1
+ import { encoder, decoder } from '../lib/buffer_utils.js';
2
2
  export function encodeBase64(input) {
3
- if (Buffer)
4
- return Buffer.from(input).toString('base64');
5
- let unencoded = input;
6
- if (typeof unencoded === 'string') {
7
- unencoded = encoder.encode(unencoded);
3
+ if (Uint8Array.prototype.toBase64) {
4
+ return input.toBase64();
8
5
  }
9
6
  const CHUNK_SIZE = 0x8000;
10
7
  const arr = [];
11
- for (let i = 0; i < unencoded.length; i += CHUNK_SIZE) {
12
- arr.push(String.fromCharCode.apply(null, unencoded.subarray(i, i + CHUNK_SIZE)));
8
+ for (let i = 0; i < input.length; i += CHUNK_SIZE) {
9
+ arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE)));
13
10
  }
14
11
  return btoa(arr.join(''));
15
12
  }
16
13
  export function encode(input) {
17
- if (Buffer)
18
- return Buffer.from(input).toString('base64url');
19
- return encodeBase64(input).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
14
+ let unencoded = input;
15
+ if (typeof unencoded === 'string') {
16
+ unencoded = encoder.encode(unencoded);
17
+ }
18
+ if (Uint8Array.prototype.toBase64) {
19
+ return unencoded.toBase64({ alphabet: 'base64url', omitPadding: true });
20
+ }
21
+ return encodeBase64(unencoded).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
20
22
  }
21
23
  export function decodeBase64(encoded) {
22
- if (Buffer)
23
- return Buffer.from(encoded, 'base64');
24
+ if (Uint8Array.fromBase64) {
25
+ return Uint8Array.fromBase64(encoded);
26
+ }
24
27
  const binary = atob(encoded);
25
28
  const bytes = new Uint8Array(binary.length);
26
29
  for (let i = 0; i < binary.length; i++) {
@@ -29,12 +32,15 @@ export function decodeBase64(encoded) {
29
32
  return bytes;
30
33
  }
31
34
  export function decode(input) {
35
+ if (Uint8Array.fromBase64) {
36
+ return Uint8Array.fromBase64(typeof input === 'string' ? input : decoder.decode(input), {
37
+ alphabet: 'base64url',
38
+ });
39
+ }
32
40
  let encoded = input;
33
41
  if (encoded instanceof Uint8Array) {
34
42
  encoded = decoder.decode(encoded);
35
43
  }
36
- if (Buffer)
37
- return Buffer.from(encoded, 'base64url');
38
44
  encoded = encoded.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, '');
39
45
  try {
40
46
  return decodeBase64(encoded);
@@ -1,11 +1,7 @@
1
1
  export const encoder = new TextEncoder();
2
2
  export const decoder = new TextDecoder();
3
3
  const MAX_INT32 = 2 ** 32;
4
- const Buffer = globalThis.process?.getBuiltinModule?.('node:buffer')?.Buffer;
5
- export { Buffer };
6
4
  export function concat(...buffers) {
7
- if (Buffer)
8
- return Buffer.concat(buffers);
9
5
  const size = buffers.reduce((acc, { length }) => acc + length, 0);
10
6
  const buf = new Uint8Array(size);
11
7
  let i = 0;
@@ -5,25 +5,24 @@ import { JOSENotSupported, JWEDecryptionFailed, JWEInvalid } from '../util/error
5
5
  import { checkEncCryptoKey } from './crypto_key.js';
6
6
  import invalidKeyInput from './invalid_key_input.js';
7
7
  import { isCryptoKey } from './is_key_like.js';
8
- let timingSafeEqual = globalThis.process?.getBuiltinModule?.('node:crypto')?.timingSafeEqual;
9
- timingSafeEqual ||= (a, b) => {
8
+ async function timingSafeEqual(a, b) {
10
9
  if (!(a instanceof Uint8Array)) {
11
10
  throw new TypeError('First argument must be a buffer');
12
11
  }
13
12
  if (!(b instanceof Uint8Array)) {
14
13
  throw new TypeError('Second argument must be a buffer');
15
14
  }
16
- if (a.length !== b.length) {
17
- throw new TypeError('Input buffers must have the same length');
18
- }
19
- const len = a.length;
15
+ const algorithm = { name: 'HMAC', hash: 'SHA-256' };
16
+ const key = (await crypto.subtle.generateKey(algorithm, false, ['sign']));
17
+ const aHmac = new Uint8Array(await crypto.subtle.sign(algorithm, key, a));
18
+ const bHmac = new Uint8Array(await crypto.subtle.sign(algorithm, key, b));
20
19
  let out = 0;
21
20
  let i = -1;
22
- while (++i < len) {
23
- out |= a[i] ^ b[i];
21
+ while (++i < 32) {
22
+ out |= aHmac[i] ^ bHmac[i];
24
23
  }
25
24
  return out === 0;
26
- };
25
+ }
27
26
  async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
28
27
  if (!(cek instanceof Uint8Array)) {
29
28
  throw new TypeError(invalidKeyInput(cek, 'Uint8Array'));
@@ -38,7 +37,7 @@ async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
38
37
  const expectedTag = new Uint8Array((await crypto.subtle.sign('HMAC', macKey, macData)).slice(0, keySize >> 3));
39
38
  let macCheckPassed;
40
39
  try {
41
- macCheckPassed = timingSafeEqual(tag, expectedTag);
40
+ macCheckPassed = await timingSafeEqual(tag, expectedTag);
42
41
  }
43
42
  catch {
44
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jose",
3
- "version": "6.0.2",
3
+ "version": "6.0.4",
4
4
  "description": "JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes",
5
5
  "keywords": [
6
6
  "browser",