Skip to content

Commit

Permalink
fix: only import RSA, EC and oct successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Mar 2, 2019
1 parent f9707f3 commit e5e02fc
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/jwk/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ const importKey = (key, parameters) => {
}

if (key instanceof KeyObject) {
if (key.type === 'private') {
privateKey = key
} else if (key.type === 'public') {
publicKey = key
} else if (key.type === 'secret') {
secret = key
} else {
throw new errors.JOSENotSupported()
switch (key.type) {
case 'private':
privateKey = key
break
case 'public':
publicKey = key
break
case 'secret':
secret = key
break
default:
}
} else if ('kty' in key && key.kty === 'oct') { // symmetric key <Object>
try {
Expand Down Expand Up @@ -69,20 +72,23 @@ const importKey = (key, parameters) => {
} catch (err) {}
}

if (!privateKey && !publicKey && !secret) {
throw new errors.JWKImportFailed('import failed')
}

const keyObject = privateKey || publicKey || secret

switch (keyObject.asymmetricKeyType) {
case 'rsa':
return new RSAKey(keyObject, parameters)
case 'ec':
return new ECKey(keyObject, parameters)
default:
return new OctKey(keyObject, parameters)
if (privateKey || publicKey) {
switch (keyObject.asymmetricKeyType) {
case 'rsa':
return new RSAKey(keyObject, parameters)
case 'ec':
return new ECKey(keyObject, parameters)
default:
// TODO: test this branch once https://github.com/nodejs/node/pull/26319 is released
throw new errors.JOSENotSupported('only RSA ane EC asymmetric keys are supported')
}
} else if (secret) {
return new OctKey(keyObject, parameters)
}

throw new errors.JWKImportFailed('import failed')
}

module.exports = importKey

0 comments on commit e5e02fc

Please sign in to comment.