From 0a16efb961f9f59e3353ab61f0e15a83009eee7f Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 4 Oct 2019 12:20:44 +0200 Subject: [PATCH] fix: throw proper error when runtime doesn't support OKP closes #48 --- lib/jwk/key/okp.js | 9 +++++++++ test/jwk/generate.test.js | 34 ++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/jwk/key/okp.js b/lib/jwk/key/okp.js index 6f2810c0e9..61662f72a2 100644 --- a/lib/jwk/key/okp.js +++ b/lib/jwk/key/okp.js @@ -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') @@ -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}`) } @@ -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}`) } diff --git a/test/jwk/generate.test.js b/test/jwk/generate.test.js index 8f34475a27..54580670c5 100644 --- a/test/jwk/generate.test.js +++ b/test/jwk/generate.test.js @@ -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(() => {