diff --git a/src/lib/check_key_type.ts b/src/lib/check_key_type.ts index ae70854195..1c2f83339e 100644 --- a/src/lib/check_key_type.ts +++ b/src/lib/check_key_type.ts @@ -1,11 +1,11 @@ -import invalidKeyInput from './invalid_key_input.js' +import { withAlg as invalidKeyInput } from './invalid_key_input.js' import isKeyLike, { types } from '../runtime/is_key_like.js' -const symmetricTypeCheck = (key: unknown) => { +const symmetricTypeCheck = (alg: string, key: unknown) => { if (key instanceof Uint8Array) return if (!isKeyLike(key)) { - throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array')) + throw new TypeError(invalidKeyInput(alg, key, ...types, 'Uint8Array')) } if (key.type !== 'secret') { @@ -15,9 +15,9 @@ const symmetricTypeCheck = (key: unknown) => { } } -const asymmetricTypeCheck = (key: unknown, usage: string) => { +const asymmetricTypeCheck = (alg: string, key: unknown, usage: string) => { if (!isKeyLike(key)) { - throw new TypeError(invalidKeyInput(key, ...types)) + throw new TypeError(invalidKeyInput(alg, key, ...types)) } if (key.type === 'secret') { @@ -65,9 +65,9 @@ const checkKeyType = (alg: string, key: unknown, usage: string): void => { /^A\d{3}(?:GCM)?KW$/.test(alg) if (symmetric) { - symmetricTypeCheck(key) + symmetricTypeCheck(alg, key) } else { - asymmetricTypeCheck(key, usage) + asymmetricTypeCheck(alg, key, usage) } } diff --git a/src/lib/invalid_key_input.ts b/src/lib/invalid_key_input.ts index dfcb74c9b8..2b99dd440c 100644 --- a/src/lib/invalid_key_input.ts +++ b/src/lib/invalid_key_input.ts @@ -1,6 +1,4 @@ -export default (actual: unknown, ...types: string[]) => { - let msg = 'Key must be ' - +function message(msg: string, actual: unknown, ...types: string[]) { if (types.length > 2) { const last = types.pop() msg += `one of type ${types.join(', ')}, or ${last}.` @@ -22,3 +20,11 @@ export default (actual: unknown, ...types: string[]) => { return msg } + +export default (actual: unknown, ...types: string[]) => { + return message('Key must be ', actual, ...types) +} + +export function withAlg(alg: string, actual: unknown, ...types: string[]) { + return message(`Key for the ${alg} algorithm must be `, actual, ...types) +} diff --git a/test/unit/check_key_type.test.mjs b/test/unit/check_key_type.test.mjs index 9975f6efda..1afe0c84df 100644 --- a/test/unit/check_key_type.test.mjs +++ b/test/unit/check_key_type.test.mjs @@ -17,7 +17,7 @@ const { generateKeyPair, generateSecret } = await import(keyRoot) test('lib/check_key_type.ts', async (t) => { const expected = { instanceOf: TypeError, - message: new RegExp(`^Key must be (?:one )?of type ${types}.`), + message: new RegExp(`^Key for the .+ algorithm must be (?:one )?of type ${types}\\.`), } t.throws(() => checkKeyType('HS256'), expected) @@ -33,7 +33,7 @@ test('lib/check_key_type.ts', async (t) => { t.throws(() => checkKeyType('PS256', new Uint8Array()), { ...expected, - message: new RegExp(`^Key must be (?:one )?of type ${asymmetricTypes}\.`), + message: new RegExp(`^Key for the .+ algorithm must be (?:one )?of type ${asymmetricTypes}\\.`), }) let secret = await generateSecret('HS256') t.throws(() => checkKeyType('PS256', secret), {