-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow JWK.asKey inputs for sign/verify/encrypt/decrypt operations
- Loading branch information
Showing
16 changed files
with
237 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const errors = require('../errors') | ||
const Key = require('../jwk/key/base') | ||
const importKey = require('../jwk/import') | ||
const { KeyStore } = require('../jwks/keystore') | ||
|
||
module.exports = (input, keyStoreAllowed = false) => { | ||
if (input instanceof KeyStore) { | ||
if (!keyStoreAllowed) { | ||
throw new TypeError('key argument for this operation must not be a JWKS.KeyStore instance') | ||
} | ||
|
||
return input | ||
} | ||
|
||
if (input instanceof Key) { | ||
return input | ||
} | ||
|
||
try { | ||
return importKey(input) | ||
} catch (err) { | ||
if (err instanceof errors.JOSEError && !(err instanceof errors.JWKImportFailed)) { | ||
throw err | ||
} | ||
|
||
let msg | ||
if (keyStoreAllowed) { | ||
msg = 'key must be an instance of a key instantiated by JWK.asKey, a valid JWK.asKey input, or a JWKS.KeyStore instance' | ||
} else { | ||
msg = 'key must be an instance of a key instantiated by JWK.asKey, or a valid JWK.asKey input' | ||
} | ||
|
||
throw new TypeError(msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const test = require('ava') | ||
|
||
const { createSecretKey, generateKeyPairSync } = require('crypto') | ||
|
||
const { keyObjectSupported } = require('../../lib/help/runtime_support') | ||
const errors = require('../../lib/errors') | ||
const getKey = require('../../lib/help/get_key') | ||
const { JWKS, JWK } = require('../..') | ||
|
||
test('key must not be a KeyStore instance unless keyStoreAllowed is true', t => { | ||
const ks = new JWKS.KeyStore() | ||
t.throws(() => { | ||
getKey(ks) | ||
}, { instanceOf: TypeError, message: 'key argument for this operation must not be a JWKS.KeyStore instance' }) | ||
t.is(getKey(ks, true), ks) | ||
}) | ||
|
||
test('Key instances are passed through', async t => { | ||
const k = await JWK.generate('oct') | ||
t.is(getKey(k, true), k) | ||
}) | ||
|
||
test('JWK is instantiated', async t => { | ||
const jwk = (await JWK.generate('RSA')).toJWK() | ||
const key = getKey(jwk) | ||
t.truthy(key) | ||
t.true(JWK.isKey(key)) | ||
}) | ||
|
||
if (keyObjectSupported) { | ||
test('KeyObject is instantiated', async t => { | ||
const key = getKey(createSecretKey(Buffer.from('foo'))) | ||
t.truthy(key) | ||
t.true(JWK.isKey(key)) | ||
}) | ||
} | ||
|
||
test('Buffer is instantiated', async t => { | ||
const key = getKey(Buffer.from('foo')) | ||
t.truthy(key) | ||
t.true(JWK.isKey(key)) | ||
t.is(key.kty, 'oct') | ||
}) | ||
|
||
test('oct tring is instantiated', async t => { | ||
const key = getKey(Buffer.from('foo')) | ||
t.truthy(key) | ||
t.true(JWK.isKey(key)) | ||
t.is(key.kty, 'oct') | ||
}) | ||
|
||
test('PEM key is instantiated', async t => { | ||
const pem = (await JWK.generate('RSA')).toPEM() | ||
const key = getKey(pem) | ||
t.truthy(key) | ||
t.true(JWK.isKey(key)) | ||
t.is(key.kty, 'RSA') | ||
}) | ||
|
||
test('invalid inputs throw TypeError', t => { | ||
;[{}, new Object(), false, null, Infinity, 0, 1].forEach((val) => { // eslint-disable-line no-new-object | ||
t.throws(() => { | ||
getKey(val) | ||
}, { instanceOf: TypeError, message: 'key must be an instance of a key instantiated by JWK.asKey, or a valid JWK.asKey input' }) | ||
t.throws(() => { | ||
getKey(val, true) | ||
}, { instanceOf: TypeError, message: 'key must be an instance of a key instantiated by JWK.asKey, a valid JWK.asKey input, or a JWKS.KeyStore instance' }) | ||
}) | ||
|
||
if (keyObjectSupported && !('electron' in process.versions)) { | ||
const { privateKey, publicKey } = generateKeyPairSync('dsa', { modulusLength: 1024 }) | ||
;[privateKey, publicKey].forEach((val) => { | ||
t.throws(() => { | ||
getKey(val) | ||
}, { instanceOf: errors.JOSENotSupported, code: 'ERR_JOSE_NOT_SUPPORTED', message: 'only RSA, EC and OKP asymmetric keys are supported' }) | ||
}) | ||
} | ||
}) |
Oops, something went wrong.