UNPKG

951 BJavaScriptView Raw
1const errors = require('../errors')
2const Key = require('../jwk/key/base')
3const importKey = require('../jwk/import')
4const { KeyStore } = require('../jwks/keystore')
5
6module.exports = (input, keyStoreAllowed = false) => {
7 if (input instanceof Key) {
8 return input
9 }
10
11 if (input instanceof KeyStore) {
12 if (!keyStoreAllowed) {
13 throw new TypeError('key argument for this operation must not be a JWKS.KeyStore instance')
14 }
15
16 return input
17 }
18
19 try {
20 return importKey(input)
21 } catch (err) {
22 if (err instanceof errors.JOSEError && !(err instanceof errors.JWKImportFailed)) {
23 throw err
24 }
25
26 let msg
27 if (keyStoreAllowed) {
28 msg = 'key must be an instance of a key instantiated by JWK.asKey, a valid JWK.asKey input, or a JWKS.KeyStore instance'
29 } else {
30 msg = 'key must be an instance of a key instantiated by JWK.asKey, or a valid JWK.asKey input'
31 }
32
33 throw new TypeError(msg)
34 }
35}