Skip to content

Commit 0a16efb

Browse files
committedOct 4, 2019
fix: throw proper error when runtime doesn't support OKP
closes #48
1 parent 12ea8a6 commit 0a16efb

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed
 

‎lib/jwk/key/okp.js

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
THUMBPRINT_MATERIAL, JWK_MEMBERS, PUBLIC_MEMBERS,
66
PRIVATE_MEMBERS, KEY_MANAGEMENT_DECRYPT, KEY_MANAGEMENT_ENCRYPT, OKP_CURVES
77
} = require('../../help/consts')
8+
const { edDSASupported } = require('../../help/runtime_support')
89
const errors = require('../../errors')
910

1011
const Key = require('./base')
@@ -99,6 +100,10 @@ class OKPKey extends Key {
99100
}
100101

101102
static async generate (crv = 'Ed25519', privat = true) {
103+
if (!edDSASupported) {
104+
throw new errors.JOSENotSupported('OKP keys are not supported in your Node.js runtime version')
105+
}
106+
102107
if (!OKP_CURVES.has(crv)) {
103108
throw new errors.JOSENotSupported(`unsupported OKP key curve: ${crv}`)
104109
}
@@ -109,6 +114,10 @@ class OKPKey extends Key {
109114
}
110115

111116
static generateSync (crv = 'Ed25519', privat = true) {
117+
if (!edDSASupported) {
118+
throw new errors.JOSENotSupported('OKP keys are not supported in your Node.js runtime version')
119+
}
120+
112121
if (!OKP_CURVES.has(crv)) {
113122
throw new errors.JOSENotSupported(`unsupported OKP key curve: ${crv}`)
114123
}

‎test/jwk/generate.test.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,31 @@ test('fails to generate unsupported kty', async t => {
151151
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported key type: foo' })
152152
})
153153

154-
test('fails to generate unsupported OKP crv', async t => {
155-
await t.throwsAsync(() => {
156-
return generate('OKP', 'foo')
157-
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
158-
})
154+
if (edDSASupported) {
155+
test('fails to generate unsupported OKP crv', async t => {
156+
await t.throwsAsync(() => {
157+
return generate('OKP', 'foo')
158+
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
159+
})
159160

160-
test('fails to generateSync unsupported OKP crv', async t => {
161-
await t.throws(() => {
162-
return generateSync('OKP', 'foo')
163-
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
164-
})
161+
test('fails to generateSync unsupported OKP crv', async t => {
162+
await t.throws(() => {
163+
return generateSync('OKP', 'foo')
164+
}, { instanceOf: errors.JOSENotSupported, message: 'unsupported OKP key curve: foo' })
165+
})
166+
} else {
167+
test('fails to generate OKP when not supported', async t => {
168+
await t.throwsAsync(() => {
169+
return generate('OKP')
170+
}, { instanceOf: errors.JOSENotSupported, message: 'OKP keys are not supported in your Node.js runtime version' })
171+
})
172+
173+
test('fails to generateSync OKP when not supported', async t => {
174+
await t.throws(() => {
175+
return generateSync('OKP')
176+
}, { instanceOf: errors.JOSENotSupported, message: 'OKP keys are not supported in your Node.js runtime version' })
177+
})
178+
}
165179

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

0 commit comments

Comments
 (0)