From 12eb2cb1c93debe9e9f348eec57966392d5e18bc Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Thu, 16 Mar 2017 20:23:24 +0100 Subject: [PATCH] More documentation. bumped to 0.1.0 --- README.md | 79 ++++++++++++++++++++++++++++++++++++++++-- lib/keytypes/BinKey.js | 16 +++++++++ package.json | 3 +- 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1adfa7e..8ce22c6 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,84 @@ enable the direct use of JWK formatted keys with other node modules like ## Usage -_will come_ +The module offers the classes `JWK` and `JWKSet` to work with JWK encoded keys +or key sets. -## Documentation +You can instantiate either of the objects from a stringified JSON or an object. +``` +const njwk = require('node-jwk'); + +const myKey = njwk.JWK.fromJSON(myJSONString); +const myKeySet = njwk.JWKSet.fromObject(myKeySet); +``` + +### Keysets (JWKSet) + +Keysets can contain a number of different keys which are unique by their _kid_. + +#### JWKSet.findKeyById(kid) + +The JWKSet class offers the `findKeyById` method that will let you grab a key +by its id and returns it wrapped in a JWK object. + +#### JWKSet.findKeysByUse(use) + +There might be cases where you want to use a key designated for encoding/decoding or +signing/verification. With `findKeysByUse` you can retrieve an array of all +contained keys that match the use given. + +But remember that the use property is specified as OPTIONAL, so is the content of +it. Be prepared that keys you get from 3rd party could miss it. + +#### JWKSet.keys + +Returns all keys as an array of JWK objects. + +#### JWKSet.fromObject(object) JWKSet.fromJSON(string) + +Factory to instantiate JWKSet objects. This method will throw on invalid +keysets (the keyset structure or invalid JSON). According to the specification +(RFC) invalid keys contained in a valid set are ignored. + + +### Keys (JWK) + +All standard JWK properties are exposed by the JWK object. Be aware that per +specification all properties but `kty` and `kid` are optional. Here's a list: +``` + kid + kty + use + key_ops + alg +``` + +#### JWK.key + +Through the key property you can access the key algorithm specific functionality. + +##### JWK.key.hasPrivateKey + +Returns true if the key contains a private key part. + +##### JWK.key.toPublicKeyPEM() => String + +Generates a PEM that contains the public key of the JWK. This can be used +directly as key in OpenSSL or other node modules and works for EC as well as +RSA keys. + +##### JWK.key.toPrivateKeyPEM() => String + +Generates a PEM that contains the private key of the JWK. This can be used +directly as key in OpenSSL or other node modules and works for EC as well as +RSA keys. + +#### JWK.fromObject(object) JWK.fromJSON(string) + +Factory to instantiate JWK objects. This method will throw on invalid +keys (the keyset structure or invalid JSON). +Normally you should use keysets to manage your keys instead of single keys. -_will come_ ## Examples diff --git a/lib/keytypes/BinKey.js b/lib/keytypes/BinKey.js index 78410d6..298e730 100644 --- a/lib/keytypes/BinKey.js +++ b/lib/keytypes/BinKey.js @@ -12,6 +12,22 @@ class BinKey { this._k = k; } + get hasPrivateKey() { + return true; + } + + get raw() { + return this._k; + } + + toPublicKeyPEM() { + return null; + } + + toPrivateKeyPEM() { + return null; + } + static validate(key) { // @see RFC-7517 par. 6.3 return _.has(key, 'k'); diff --git a/package.json b/package.json index 09f1c9d..74d4d1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-jwk", - "version": "0.0.1", + "version": "0.1.0", "description": "JWK support", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", @@ -19,6 +19,7 @@ "jwk", "jws", "jwt", + "jwkset", "security", "token", "convert",