Skip to content

Commit

Permalink
fix: throw proper error when runtime doesn't support OKP
Browse files Browse the repository at this point in the history
closes #48
  • Loading branch information
panva committed Oct 4, 2019
1 parent 12ea8a6 commit 0a16efb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
9 changes: 9 additions & 0 deletions lib/jwk/key/okp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
THUMBPRINT_MATERIAL, JWK_MEMBERS, PUBLIC_MEMBERS,
PRIVATE_MEMBERS, KEY_MANAGEMENT_DECRYPT, KEY_MANAGEMENT_ENCRYPT, OKP_CURVES
} = require('../../help/consts')
const { edDSASupported } = require('../../help/runtime_support')
const errors = require('../../errors')

const Key = require('./base')
Expand Down Expand Up @@ -99,6 +100,10 @@ class OKPKey extends Key {
}

static async generate (crv = 'Ed25519', privat = true) {
if (!edDSASupported) {
throw new errors.JOSENotSupported('OKP keys are not supported in your Node.js runtime version')
}

if (!OKP_CURVES.has(crv)) {
throw new errors.JOSENotSupported(`unsupported OKP key curve: ${crv}`)
}
Expand All @@ -109,6 +114,10 @@ class OKPKey extends Key {
}

static generateSync (crv = 'Ed25519', privat = true) {
if (!edDSASupported) {
throw new errors.JOSENotSupported('OKP keys are not supported in your Node.js runtime version')
}

if (!OKP_CURVES.has(crv)) {
throw new errors.JOSENotSupported(`unsupported OKP key curve: ${crv}`)
}
Expand Down
34 changes: 24 additions & 10 deletions test/jwk/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,31 @@ test('fails to generate unsupported kty', async t => {
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported key type: foo' })
})

test('fails to generate unsupported OKP crv', async t => {
await t.throwsAsync(() => {
return generate('OKP', 'foo')
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
})
if (edDSASupported) {
test('fails to generate unsupported OKP crv', async t => {
await t.throwsAsync(() => {
return generate('OKP', 'foo')
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
})

test('fails to generateSync unsupported OKP crv', async t => {
await t.throws(() => {
return generateSync('OKP', 'foo')
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
})
test('fails to generateSync unsupported OKP crv', async t => {
await t.throws(() => {
return generateSync('OKP', 'foo')
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
})
} else {
test('fails to generate OKP when not supported', async t => {
await t.throwsAsync(() => {
return generate('OKP')
}, { instanceOf: errors.JOSENotSupported, message: 'OKP keys are not supported in your Node.js runtime version' })
})

test('fails to generateSync OKP when not supported', async t => {
await t.throws(() => {
return generateSync('OKP')
}, { instanceOf: errors.JOSENotSupported, message: 'OKP keys are not supported in your Node.js runtime version' })
})
}

test('fails to generateSync unsupported EC crv', t => {
t.throws(() => {
Expand Down

0 comments on commit 0a16efb

Please sign in to comment.