Skip to content

Commit

Permalink
fix: keystore.toJWKS(true) does not throw on public keys
Browse files Browse the repository at this point in the history
closes #42
  • Loading branch information
panva committed Sep 16, 2019
1 parent 8c47ca7 commit 81abdfa
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ Synchronous version of `keystore.generate()`.

Exports the keystore to a JSON Web Key Set formatted object.

- `private`: `<boolean>` When true exports keys with their private components. **Default:** 'false'
- `private`: `<boolean>` When true exports private keys with their private components. **Default:** 'false'
- Returns: `<Object>`

---
Expand Down
6 changes: 5 additions & 1 deletion lib/jwks/keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ class KeyStore {
}

toJWKS (priv = false) {
return { keys: [...i(this).keys.values()].map(key => key.toJWK(priv)) }
return {
keys: [...i(this).keys.values()].map(
key => key.toJWK(priv && (key.private || (key.secret && key.k)))
)
}
}

async generate (...args) {
Expand Down
4 changes: 1 addition & 3 deletions test/cookbook/jwk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,5 @@ test('keystore .toJWKS()', t => {
t.deepEqual(ks.toJWKS(), { keys: [pubEc, pubRsa, pubOct] })
t.deepEqual(ks.toJWKS(false), { keys: [pubEc, pubRsa, pubOct] })
ks.add(asKey(pubRsa))
t.throws(() => {
ks.toJWKS(true)
}, { instanceOf: TypeError, message: 'public key cannot be exported as private' })
t.deepEqual(ks.toJWKS(true), { keys: [ec, rsa, oct, pubRsa] })
})

0 comments on commit 81abdfa

Please sign in to comment.