Skip to content

Commit

Permalink
fix: remove clutter when tree shaking browser dist
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Oct 14, 2021
1 parent 373e0e4 commit 73ba370
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 65 deletions.
17 changes: 7 additions & 10 deletions src/jwe/flattened/decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { decode as base64url } from '../../runtime/base64url.js'
import decrypt from '../../runtime/decrypt.js'
import { inflate } from '../../runtime/zlib.js'
import random from '../../runtime/random.js'

import { JOSEAlgNotAllowed, JOSENotSupported, JWEInvalid } from '../../util/errors.js'
import isDisjoint from '../../lib/is_disjoint.js'
Expand All @@ -17,15 +16,10 @@ import type {
ResolvedKey,
} from '../../types.d'
import { encoder, decoder, concat } from '../../lib/buffer_utils.js'
import cekFactory from '../../lib/cek.js'
import generateCek from '../../lib/cek.js'
import validateCrit from '../../lib/validate_crit.js'
import validateAlgorithms from '../../lib/validate_algorithms.js'

const generateCek = cekFactory(random)
const checkExtensions = validateCrit.bind(undefined, JWEInvalid, new Map())
const checkAlgOption = validateAlgorithms.bind(undefined, 'keyManagementAlgorithms')
const checkEncOption = validateAlgorithms.bind(undefined, 'contentEncryptionAlgorithms')

/**
* Interface for Flattened JWE Decryption dynamic key resolution.
* No token components have been verified at the time of this function call.
Expand Down Expand Up @@ -159,7 +153,7 @@ async function flattenedDecrypt(
...jwe.unprotected,
}

checkExtensions(options?.crit, parsedProt, joseHeader)
validateCrit(JWEInvalid, new Map(), options?.crit, parsedProt, joseHeader)

if (joseHeader.zip !== undefined) {
if (!parsedProt || !parsedProt.zip) {
Expand All @@ -183,8 +177,11 @@ async function flattenedDecrypt(
throw new JWEInvalid('missing JWE Encryption Algorithm (enc) in JWE Header')
}

const keyManagementAlgorithms = options && checkAlgOption(options.keyManagementAlgorithms)
const contentEncryptionAlgorithms = options && checkEncOption(options.contentEncryptionAlgorithms)
const keyManagementAlgorithms =
options && validateAlgorithms('keyManagementAlgorithms', options.keyManagementAlgorithms)
const contentEncryptionAlgorithms =
options &&
validateAlgorithms('contentEncryptionAlgorithms', options.contentEncryptionAlgorithms)

if (keyManagementAlgorithms && !keyManagementAlgorithms.has(alg)) {
throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed')
Expand Down
8 changes: 2 additions & 6 deletions src/jwe/flattened/encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { encode as base64url } from '../../runtime/base64url.js'
import random from '../../runtime/random.js'
import encrypt from '../../runtime/encrypt.js'
import { deflate } from '../../runtime/zlib.js'

Expand All @@ -10,16 +9,13 @@ import type {
JWEKeyManagementHeaderParameters,
EncryptOptions,
} from '../../types.d'
import ivFactory from '../../lib/iv.js'
import generateIv from '../../lib/iv.js'
import encryptKeyManagement from '../../lib/encrypt_key_management.js'
import { JOSENotSupported, JWEInvalid } from '../../util/errors.js'
import isDisjoint from '../../lib/is_disjoint.js'
import { encoder, decoder, concat } from '../../lib/buffer_utils.js'
import validateCrit from '../../lib/validate_crit.js'

const generateIv = ivFactory(random)
const checkExtensions = validateCrit.bind(undefined, JWEInvalid, new Map())

/**
* The FlattenedEncrypt class is a utility for creating Flattened JWE
* objects.
Expand Down Expand Up @@ -202,7 +198,7 @@ class FlattenedEncrypt {
...this._sharedUnprotectedHeader,
}

checkExtensions(options?.crit, this._protectedHeader, joseHeader)
validateCrit(JWEInvalid, new Map(), options?.crit, this._protectedHeader, joseHeader)

if (joseHeader.zip !== undefined) {
if (!this._protectedHeader || !this._protectedHeader.zip) {
Expand Down
10 changes: 7 additions & 3 deletions src/jws/flattened/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import type { KeyLike, FlattenedJWS, JWSHeaderParameters, SignOptions } from '..
import checkKeyType from '../../lib/check_key_type.js'
import validateCrit from '../../lib/validate_crit.js'

const checkExtensions = validateCrit.bind(undefined, JWSInvalid, new Map([['b64', true]]))

/**
* The FlattenedSign class is a utility for creating Flattened JWS objects.
*
Expand Down Expand Up @@ -105,7 +103,13 @@ class FlattenedSign {
...this._unprotectedHeader,
}

const extensions = checkExtensions(options?.crit, this._protectedHeader, joseHeader)
const extensions = validateCrit(
JWSInvalid,
new Map([['b64', true]]),
options?.crit,
this._protectedHeader,
joseHeader,
)

let b64: boolean = true
if (extensions.has('b64')) {
Expand Down
13 changes: 8 additions & 5 deletions src/jws/flattened/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import type {
ResolvedKey,
} from '../../types.d'

const checkExtensions = validateCrit.bind(undefined, JWSInvalid, new Map([['b64', true]]))
const checkAlgOption = validateAlgorithms.bind(undefined, 'algorithms')

/**
* Interface for Flattened JWS Verification dynamic key resolution.
* No token components have been verified at the time of this function call.
Expand Down Expand Up @@ -133,7 +130,13 @@ async function flattenedVerify(
...jws.header,
}

const extensions = checkExtensions(options?.crit, parsedProt, joseHeader)
const extensions = validateCrit(
JWSInvalid,
new Map([['b64', true]]),
options?.crit,
parsedProt,
joseHeader,
)

let b64: boolean = true
if (extensions.has('b64')) {
Expand All @@ -151,7 +154,7 @@ async function flattenedVerify(
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid')
}

const algorithms = options && checkAlgOption(options.algorithms)
const algorithms = options && validateAlgorithms('algorithms', options.algorithms)

if (algorithms && !algorithms.has(alg)) {
throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed')
Expand Down
19 changes: 9 additions & 10 deletions src/lib/cek.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JOSENotSupported } from '../util/errors.js'
import random from '../runtime/random.js'

const bitLengths = new Map<string, number>([
['A128CBC-HS256', 256],
Expand All @@ -9,16 +10,14 @@ const bitLengths = new Map<string, number>([
['A256GCM', 256],
])

const factory =
(random: (array: Uint8Array) => Uint8Array) =>
(alg: string): Uint8Array => {
const bitLength = bitLengths.get(alg)
if (!bitLength) {
throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`)
}

return random(new Uint8Array(bitLength >> 3))
const generateCek = (alg: string): Uint8Array => {
const bitLength = bitLengths.get(alg)
if (!bitLength) {
throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`)
}

export default factory
return random(new Uint8Array(bitLength >> 3))
}

export default generateCek
export { bitLengths }
5 changes: 1 addition & 4 deletions src/lib/encrypt_key_management.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random from '../runtime/random.js'
import { wrap as aesKw } from '../runtime/aeskw.js'
import * as ECDH from '../runtime/ecdhes.js'
import { encrypt as pbes2Kw } from '../runtime/pbes2kw.js'
Expand All @@ -7,13 +6,11 @@ import { wrap as aesGcmKw } from '../runtime/aesgcmkw.js'
import { encode as base64url } from '../runtime/base64url.js'

import type { KeyLike, JWEKeyManagementHeaderParameters, JWEHeaderParameters } from '../types.d'
import cekFactory, { bitLengths as cekLengths } from '../lib/cek.js'
import generateCek, { bitLengths as cekLengths } from '../lib/cek.js'
import { JOSENotSupported } from '../util/errors.js'
import { exportJWK } from '../key/export.js'
import checkKeyType from './check_key_type.js'

const generateCek = cekFactory(random)

async function encryptKeyManagement(
alg: string,
enc: string,
Expand Down
19 changes: 9 additions & 10 deletions src/lib/iv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JOSENotSupported } from '../util/errors.js'
import random from '../runtime/random.js'

const bitLengths = new Map<string, number>([
['A128CBC-HS256', 128],
Expand All @@ -12,16 +13,14 @@ const bitLengths = new Map<string, number>([
['A256GCMKW', 96],
])

const factory =
(random: (array: Uint8Array) => Uint8Array) =>
(alg: string): Uint8Array => {
const bitLength = bitLengths.get(alg)
if (!bitLength) {
throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`)
}

return random(new Uint8Array(bitLength >> 3))
const generateIv = (alg: string): Uint8Array => {
const bitLength = bitLengths.get(alg)
if (!bitLength) {
throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`)
}

export default factory
return random(new Uint8Array(bitLength >> 3))
}

export default generateIv
export { bitLengths }
5 changes: 1 addition & 4 deletions src/runtime/browser/aesgcmkw.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { AesGcmKwUnwrapFunction, AesGcmKwWrapFunction } from '../interfaces.d'
import encrypt from './encrypt.js'
import decrypt from './decrypt.js'
import ivFactory from '../../lib/iv.js'
import random from './random.js'
import generateIv from '../../lib/iv.js'
import { encode as base64url } from './base64url.js'

const generateIv = ivFactory(random)

export const wrap: AesGcmKwWrapFunction = async (
alg: string,
key: unknown,
Expand Down
4 changes: 1 addition & 3 deletions src/runtime/browser/random.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import crypto from './webcrypto.js'

const random = crypto.getRandomValues.bind(crypto)

export default random
export default crypto.getRandomValues.bind(crypto)
5 changes: 1 addition & 4 deletions src/runtime/node/aesgcmkw.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { AesGcmKwWrapFunction, AesGcmKwUnwrapFunction } from '../interfaces.d'
import encrypt from './encrypt.js'
import decrypt from './decrypt.js'
import ivFactory from '../../lib/iv.js'
import random from './random.js'
import generateIv from '../../lib/iv.js'
import { encode as base64url } from './base64url.js'

const generateIv = ivFactory(random)

export const wrap: AesGcmKwWrapFunction = async (
alg: string,
key: unknown,
Expand Down
6 changes: 3 additions & 3 deletions test/unit/cek.test.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'ava';

const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto';
Promise.all([import(`${root}/lib/cek`), import(`${root}/util/random`)]).then(
([{ default: cek }, { default: random }]) => {
Promise.all([import(`${root}/lib/cek`)]).then(
([{ default: cek }]) => {
test('lib/cek.ts', (t) => {
t.throws(() => cek(random)('foo'), {
t.throws(() => cek('foo'), {
code: 'ERR_JOSE_NOT_SUPPORTED',
message: 'Unsupported JWE Algorithm: foo',
});
Expand Down
6 changes: 3 additions & 3 deletions test/unit/iv.test.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'ava';

const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto';
Promise.all([import(`${root}/lib/iv`), import(`${root}/util/random`)]).then(
([{ default: iv }, { default: random }]) => {
Promise.all([import(`${root}/lib/iv`)]).then(
([{ default: iv }]) => {
test('lib/iv.ts', (t) => {
t.throws(() => iv(random)('foo'), {
t.throws(() => iv('foo'), {
code: 'ERR_JOSE_NOT_SUPPORTED',
message: 'Unsupported JWE Algorithm: foo',
});
Expand Down

0 comments on commit 73ba370

Please sign in to comment.