Skip to content

Commit

Permalink
refactor: update key input validation error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Sep 15, 2022
1 parent 6741679 commit 2eac34a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/lib/check_key_type.ts
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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') {
Expand Down Expand Up @@ -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)
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/lib/invalid_key_input.ts
Original file line number Diff line number Diff line change
@@ -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}.`
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions test/unit/check_key_type.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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), {
Expand Down

0 comments on commit 2eac34a

Please sign in to comment.