Skip to content

Commit d4231f9

Browse files
committed
refactor: reorganize internals, less files, smaller footprint
1 parent 7b22ba8 commit d4231f9

50 files changed

Lines changed: 916 additions & 1022 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/jwe/flattened/decrypt.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
import type * as types from '../../types.d.ts'
88
import { decode as b64u } from '../../util/base64url.js'
9-
import { decrypt } from '../../lib/decrypt.js'
9+
import { decrypt } from '../../lib/content_encryption.js'
10+
import { decodeBase64url } from '../../lib/helpers.js'
1011
import { JOSEAlgNotAllowed, JOSENotSupported, JWEInvalid } from '../../util/errors.js'
11-
import { isDisjoint } from '../../lib/is_disjoint.js'
12-
import { isObject } from '../../lib/is_object.js'
13-
import { decryptKeyManagement } from '../../lib/decrypt_key_management.js'
12+
import { isDisjoint } from '../../lib/type_checks.js'
13+
import { isObject } from '../../lib/type_checks.js'
14+
import { decryptKeyManagement } from '../../lib/key_management.js'
1415
import { decoder, concat, encode } from '../../lib/buffer_utils.js'
15-
import { generateCek } from '../../lib/cek.js'
16+
import { generateCek } from '../../lib/content_encryption.js'
1617
import { validateCrit } from '../../lib/validate_crit.js'
1718
import { validateAlgorithms } from '../../lib/validate_algorithms.js'
1819
import { normalizeKey } from '../../lib/normalize_key.js'
@@ -186,11 +187,7 @@ export async function flattenedDecrypt(
186187

187188
let encryptedKey!: Uint8Array
188189
if (jwe.encrypted_key !== undefined) {
189-
try {
190-
encryptedKey = b64u(jwe.encrypted_key!)
191-
} catch {
192-
throw new JWEInvalid('Failed to base64url decode the encrypted_key')
193-
}
190+
encryptedKey = decodeBase64url(jwe.encrypted_key!, 'encrypted_key', JWEInvalid)
194191
}
195192

196193
let resolvedKey = false
@@ -221,18 +218,10 @@ export async function flattenedDecrypt(
221218
let iv: Uint8Array | undefined
222219
let tag: Uint8Array | undefined
223220
if (jwe.iv !== undefined) {
224-
try {
225-
iv = b64u(jwe.iv)
226-
} catch {
227-
throw new JWEInvalid('Failed to base64url decode the iv')
228-
}
221+
iv = decodeBase64url(jwe.iv, 'iv', JWEInvalid)
229222
}
230223
if (jwe.tag !== undefined) {
231-
try {
232-
tag = b64u(jwe.tag)
233-
} catch {
234-
throw new JWEInvalid('Failed to base64url decode the tag')
235-
}
224+
tag = decodeBase64url(jwe.tag, 'tag', JWEInvalid)
236225
}
237226

238227
const protectedHeader: Uint8Array =
@@ -245,12 +234,7 @@ export async function flattenedDecrypt(
245234
additionalData = protectedHeader
246235
}
247236

248-
let ciphertext: Uint8Array
249-
try {
250-
ciphertext = b64u(jwe.ciphertext)
251-
} catch {
252-
throw new JWEInvalid('Failed to base64url decode the ciphertext')
253-
}
237+
const ciphertext = decodeBase64url(jwe.ciphertext, 'ciphertext', JWEInvalid)
254238
const plaintext = await decrypt(enc, cek, ciphertext, iv, tag, additionalData)
255239

256240
const result: types.FlattenedDecryptResult = { plaintext }
@@ -276,11 +260,7 @@ export async function flattenedDecrypt(
276260
}
277261

278262
if (jwe.aad !== undefined) {
279-
try {
280-
result.additionalAuthenticatedData = b64u(jwe.aad!)
281-
} catch {
282-
throw new JWEInvalid('Failed to base64url decode the aad')
283-
}
263+
result.additionalAuthenticatedData = decodeBase64url(jwe.aad!, 'aad', JWEInvalid)
284264
}
285265

286266
if (jwe.unprotected !== undefined) {

src/jwe/flattened/encrypt.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66

77
import { encode as b64u } from '../../util/base64url.js'
8-
import { unprotected } from '../../lib/private_symbols.js'
9-
import { encrypt } from '../../lib/encrypt.js'
8+
import { unprotected, assertNotSet } from '../../lib/helpers.js'
9+
import { encrypt } from '../../lib/content_encryption.js'
1010
import type * as types from '../../types.d.ts'
11-
import { encryptKeyManagement } from '../../lib/encrypt_key_management.js'
11+
import { encryptKeyManagement } from '../../lib/key_management.js'
1212
import { JOSENotSupported, JWEInvalid } from '../../util/errors.js'
13-
import { isDisjoint } from '../../lib/is_disjoint.js'
13+
import { isDisjoint } from '../../lib/type_checks.js'
1414
import { concat, encode } from '../../lib/buffer_utils.js'
1515
import { validateCrit } from '../../lib/validate_crit.js'
1616
import { normalizeKey } from '../../lib/normalize_key.js'
@@ -74,9 +74,7 @@ export class FlattenedEncrypt {
7474
* @param parameters JWE Key Management parameters.
7575
*/
7676
setKeyManagementParameters(parameters: types.JWEKeyManagementHeaderParameters): this {
77-
if (this.#keyManagementParameters) {
78-
throw new TypeError('setKeyManagementParameters can only be called once')
79-
}
77+
assertNotSet(this.#keyManagementParameters, 'setKeyManagementParameters')
8078
this.#keyManagementParameters = parameters
8179
return this
8280
}
@@ -87,9 +85,7 @@ export class FlattenedEncrypt {
8785
* @param protectedHeader JWE Protected Header.
8886
*/
8987
setProtectedHeader(protectedHeader: types.JWEHeaderParameters): this {
90-
if (this.#protectedHeader) {
91-
throw new TypeError('setProtectedHeader can only be called once')
92-
}
88+
assertNotSet(this.#protectedHeader, 'setProtectedHeader')
9389
this.#protectedHeader = protectedHeader
9490
return this
9591
}
@@ -100,9 +96,7 @@ export class FlattenedEncrypt {
10096
* @param sharedUnprotectedHeader JWE Shared Unprotected Header.
10197
*/
10298
setSharedUnprotectedHeader(sharedUnprotectedHeader: types.JWEHeaderParameters): this {
103-
if (this.#sharedUnprotectedHeader) {
104-
throw new TypeError('setSharedUnprotectedHeader can only be called once')
105-
}
99+
assertNotSet(this.#sharedUnprotectedHeader, 'setSharedUnprotectedHeader')
106100
this.#sharedUnprotectedHeader = sharedUnprotectedHeader
107101
return this
108102
}
@@ -113,9 +107,7 @@ export class FlattenedEncrypt {
113107
* @param unprotectedHeader JWE Per-Recipient Unprotected Header.
114108
*/
115109
setUnprotectedHeader(unprotectedHeader: types.JWEHeaderParameters): this {
116-
if (this.#unprotectedHeader) {
117-
throw new TypeError('setUnprotectedHeader can only be called once')
118-
}
110+
assertNotSet(this.#unprotectedHeader, 'setUnprotectedHeader')
119111
this.#unprotectedHeader = unprotectedHeader
120112
return this
121113
}
@@ -140,9 +132,7 @@ export class FlattenedEncrypt {
140132
* @param cek JWE Content Encryption Key.
141133
*/
142134
setContentEncryptionKey(cek: Uint8Array): this {
143-
if (this.#cek) {
144-
throw new TypeError('setContentEncryptionKey can only be called once')
145-
}
135+
assertNotSet(this.#cek, 'setContentEncryptionKey')
146136
this.#cek = cek
147137
return this
148138
}
@@ -157,9 +147,7 @@ export class FlattenedEncrypt {
157147
* @param iv JWE Initialization Vector.
158148
*/
159149
setInitializationVector(iv: Uint8Array): this {
160-
if (this.#iv) {
161-
throw new TypeError('setInitializationVector can only be called once')
162-
}
150+
assertNotSet(this.#iv, 'setInitializationVector')
163151
this.#iv = iv
164152
return this
165153
}

src/jwe/general/decrypt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { flattenedDecrypt } from '../flattened/decrypt.js'
88
import { JWEDecryptionFailed, JWEInvalid } from '../../util/errors.js'
99
import type * as types from '../../types.d.ts'
10-
import { isObject } from '../../lib/is_object.js'
10+
import { isObject } from '../../lib/type_checks.js'
1111

1212
/**
1313
* Interface for General JWE Decryption dynamic key resolution. No token components have been

src/jwe/general/encrypt.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import type * as types from '../../types.d.ts'
88
import { FlattenedEncrypt } from '../flattened/encrypt.js'
9-
import { unprotected } from '../../lib/private_symbols.js'
9+
import { unprotected, assertNotSet } from '../../lib/helpers.js'
1010
import { JOSENotSupported, JWEInvalid } from '../../util/errors.js'
11-
import { generateCek } from '../../lib/cek.js'
12-
import { isDisjoint } from '../../lib/is_disjoint.js'
13-
import { encryptKeyManagement } from '../../lib/encrypt_key_management.js'
11+
import { generateCek } from '../../lib/content_encryption.js'
12+
import { isDisjoint } from '../../lib/type_checks.js'
13+
import { encryptKeyManagement } from '../../lib/key_management.js'
1414
import { encode as b64u } from '../../util/base64url.js'
1515
import { validateCrit } from '../../lib/validate_crit.js'
1616
import { normalizeKey } from '../../lib/normalize_key.js'
@@ -63,17 +63,13 @@ class IndividualRecipient implements Recipient {
6363
}
6464

6565
setUnprotectedHeader(unprotectedHeader: types.JWEHeaderParameters): this {
66-
if (this.unprotectedHeader) {
67-
throw new TypeError('setUnprotectedHeader can only be called once')
68-
}
66+
assertNotSet(this.unprotectedHeader, 'setUnprotectedHeader')
6967
this.unprotectedHeader = unprotectedHeader
7068
return this
7169
}
7270

7371
setKeyManagementParameters(parameters: types.JWEKeyManagementHeaderParameters): this {
74-
if (this.keyManagementParameters) {
75-
throw new TypeError('setKeyManagementParameters can only be called once')
76-
}
72+
assertNotSet(this.keyManagementParameters, 'setKeyManagementParameters')
7773
this.keyManagementParameters = parameters
7874
return this
7975
}
@@ -155,9 +151,7 @@ export class GeneralEncrypt {
155151
* @param protectedHeader JWE Protected Header object.
156152
*/
157153
setProtectedHeader(protectedHeader: types.JWEHeaderParameters): this {
158-
if (this.#protectedHeader) {
159-
throw new TypeError('setProtectedHeader can only be called once')
160-
}
154+
assertNotSet(this.#protectedHeader, 'setProtectedHeader')
161155
this.#protectedHeader = protectedHeader
162156
return this
163157
}
@@ -168,9 +162,7 @@ export class GeneralEncrypt {
168162
* @param sharedUnprotectedHeader JWE Shared Unprotected Header object.
169163
*/
170164
setSharedUnprotectedHeader(sharedUnprotectedHeader: types.JWEHeaderParameters): this {
171-
if (this.#unprotectedHeader) {
172-
throw new TypeError('setSharedUnprotectedHeader can only be called once')
173-
}
165+
assertNotSet(this.#unprotectedHeader, 'setSharedUnprotectedHeader')
174166
this.#unprotectedHeader = sharedUnprotectedHeader
175167
return this
176168
}

src/jwk/embedded.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import type * as types from '../types.d.ts'
88
import { importJWK } from '../key/import.js'
9-
import { isObject } from '../lib/is_object.js'
9+
import { isObject } from '../lib/type_checks.js'
1010
import { JWSInvalid } from '../util/errors.js'
1111

1212
/**

src/jwk/thumbprint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
* @module
55
*/
66

7-
import { digest } from '../lib/digest.js'
7+
import { digest } from '../lib/helpers.js'
88
import { encode as b64u } from '../util/base64url.js'
99

1010
import { JOSENotSupported, JWKInvalid } from '../util/errors.js'
1111
import { encode } from '../lib/buffer_utils.js'
1212
import type * as types from '../types.d.ts'
1313
import { isKeyLike } from '../lib/is_key_like.js'
14-
import { isJWK } from '../lib/is_jwk.js'
14+
import { isJWK } from '../lib/type_checks.js'
1515
import { exportJWK } from '../key/export.js'
1616
import { invalidKeyInput } from '../lib/invalid_key_input.js'
1717

src/jwks/local.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
JWKSNoMatchingKey,
1313
JWKSMultipleMatchingKeys,
1414
} from '../util/errors.js'
15-
import { isObject } from '../lib/is_object.js'
15+
import { isObject } from '../lib/type_checks.js'
1616

1717
function getKtyFromAlg(alg: unknown) {
1818
switch (typeof alg === 'string' && alg.slice(0, 2)) {

src/jwks/remote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type * as types from '../types.d.ts'
88
import { JOSEError, JWKSNoMatchingKey, JWKSTimeout } from '../util/errors.js'
99

1010
import { createLocalJWKSet } from './local.js'
11-
import { isObject } from '../lib/is_object.js'
11+
import { isObject } from '../lib/type_checks.js'
1212

1313
function isCloudflareWorkers() {
1414
return (

src/jws/flattened/sign.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
import type * as types from '../../types.d.ts'
88
import { encode as b64u } from '../../util/base64url.js'
9-
import { sign } from '../../lib/sign.js'
9+
import { sign } from '../../lib/signing.js'
1010

11-
import { isDisjoint } from '../../lib/is_disjoint.js'
11+
import { isDisjoint } from '../../lib/type_checks.js'
1212
import { JWSInvalid } from '../../util/errors.js'
1313
import { concat, encode } from '../../lib/buffer_utils.js'
1414
import { checkKeyType } from '../../lib/check_key_type.js'
1515
import { validateCrit } from '../../lib/validate_crit.js'
1616
import { normalizeKey } from '../../lib/normalize_key.js'
17+
import { assertNotSet } from '../../lib/helpers.js'
1718

1819
/**
1920
* The FlattenedSign class is used to build and sign Flattened JWS objects.
@@ -58,9 +59,7 @@ export class FlattenedSign {
5859
* @param protectedHeader JWS Protected Header.
5960
*/
6061
setProtectedHeader(protectedHeader: types.JWSHeaderParameters): this {
61-
if (this.#protectedHeader) {
62-
throw new TypeError('setProtectedHeader can only be called once')
63-
}
62+
assertNotSet(this.#protectedHeader, 'setProtectedHeader')
6463
this.#protectedHeader = protectedHeader
6564
return this
6665
}
@@ -71,9 +70,7 @@ export class FlattenedSign {
7170
* @param unprotectedHeader JWS Unprotected Header.
7271
*/
7372
setUnprotectedHeader(unprotectedHeader: types.JWSHeaderParameters): this {
74-
if (this.#unprotectedHeader) {
75-
throw new TypeError('setUnprotectedHeader can only be called once')
76-
}
73+
assertNotSet(this.#unprotectedHeader, 'setUnprotectedHeader')
7774
this.#unprotectedHeader = unprotectedHeader
7875
return this
7976
}

src/jws/flattened/verify.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
import type * as types from '../../types.d.ts'
88
import { decode as b64u } from '../../util/base64url.js'
9-
import { verify } from '../../lib/verify.js'
9+
import { verify } from '../../lib/signing.js'
1010

1111
import { JOSEAlgNotAllowed, JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js'
1212
import { concat, encoder, decoder, encode } from '../../lib/buffer_utils.js'
13-
import { isDisjoint } from '../../lib/is_disjoint.js'
14-
import { isObject } from '../../lib/is_object.js'
13+
import { decodeBase64url } from '../../lib/helpers.js'
14+
import { isDisjoint } from '../../lib/type_checks.js'
15+
import { isObject } from '../../lib/type_checks.js'
1516
import { checkKeyType } from '../../lib/check_key_type.js'
1617
import { validateCrit } from '../../lib/validate_crit.js'
1718
import { validateAlgorithms } from '../../lib/validate_algorithms.js'
@@ -177,12 +178,7 @@ export async function flattenedVerify(
177178
: encoder.encode(jws.payload)
178179
: jws.payload,
179180
)
180-
let signature: Uint8Array
181-
try {
182-
signature = b64u(jws.signature)
183-
} catch {
184-
throw new JWSInvalid('Failed to base64url decode the signature')
185-
}
181+
const signature = decodeBase64url(jws.signature, 'signature', JWSInvalid)
186182

187183
const k = await normalizeKey(key, alg)
188184
const verified = await verify(alg, k, signature, data)
@@ -193,11 +189,7 @@ export async function flattenedVerify(
193189

194190
let payload: Uint8Array
195191
if (b64) {
196-
try {
197-
payload = b64u(jws.payload)
198-
} catch {
199-
throw new JWSInvalid('Failed to base64url decode the payload')
200-
}
192+
payload = decodeBase64url(jws.payload as string, 'payload', JWSInvalid)
201193
} else if (typeof jws.payload === 'string') {
202194
payload = encoder.encode(jws.payload)
203195
} else {

0 commit comments

Comments
 (0)