Skip to content

Commit 12eb2cb

Browse files
author
Frank Schmid
committed
More documentation. bumped to 0.1.0
1 parent 3c545f3 commit 12eb2cb

3 files changed

Lines changed: 94 additions & 4 deletions

File tree

README.md

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,84 @@ enable the direct use of JWK formatted keys with other node modules like
88

99
## Usage
1010

11-
_will come_
11+
The module offers the classes `JWK` and `JWKSet` to work with JWK encoded keys
12+
or key sets.
1213

13-
## Documentation
14+
You can instantiate either of the objects from a stringified JSON or an object.
15+
```
16+
const njwk = require('node-jwk');
17+
18+
const myKey = njwk.JWK.fromJSON(myJSONString);
19+
const myKeySet = njwk.JWKSet.fromObject(myKeySet);
20+
```
21+
22+
### Keysets (JWKSet)
23+
24+
Keysets can contain a number of different keys which are unique by their _kid_.
25+
26+
#### JWKSet.findKeyById(kid)
27+
28+
The JWKSet class offers the `findKeyById` method that will let you grab a key
29+
by its id and returns it wrapped in a JWK object.
30+
31+
#### JWKSet.findKeysByUse(use)
32+
33+
There might be cases where you want to use a key designated for encoding/decoding or
34+
signing/verification. With `findKeysByUse` you can retrieve an array of all
35+
contained keys that match the use given.
36+
37+
But remember that the use property is specified as OPTIONAL, so is the content of
38+
it. Be prepared that keys you get from 3rd party could miss it.
39+
40+
#### JWKSet.keys
41+
42+
Returns all keys as an array of JWK objects.
43+
44+
#### JWKSet.fromObject(object) JWKSet.fromJSON(string)
45+
46+
Factory to instantiate JWKSet objects. This method will throw on invalid
47+
keysets (the keyset structure or invalid JSON). According to the specification
48+
(RFC) invalid keys contained in a valid set are ignored.
49+
50+
51+
### Keys (JWK)
52+
53+
All standard JWK properties are exposed by the JWK object. Be aware that per
54+
specification all properties but `kty` and `kid` are optional. Here's a list:
55+
```
56+
kid
57+
kty
58+
use
59+
key_ops
60+
alg
61+
```
62+
63+
#### JWK.key
64+
65+
Through the key property you can access the key algorithm specific functionality.
66+
67+
##### JWK.key.hasPrivateKey
68+
69+
Returns true if the key contains a private key part.
70+
71+
##### JWK.key.toPublicKeyPEM() => String
72+
73+
Generates a PEM that contains the public key of the JWK. This can be used
74+
directly as key in OpenSSL or other node modules and works for EC as well as
75+
RSA keys.
76+
77+
##### JWK.key.toPrivateKeyPEM() => String
78+
79+
Generates a PEM that contains the private key of the JWK. This can be used
80+
directly as key in OpenSSL or other node modules and works for EC as well as
81+
RSA keys.
82+
83+
#### JWK.fromObject(object) JWK.fromJSON(string)
84+
85+
Factory to instantiate JWK objects. This method will throw on invalid
86+
keys (the keyset structure or invalid JSON).
87+
Normally you should use keysets to manage your keys instead of single keys.
1488

15-
_will come_
1689

1790
## Examples
1891

lib/keytypes/BinKey.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ class BinKey {
1212
this._k = k;
1313
}
1414

15+
get hasPrivateKey() {
16+
return true;
17+
}
18+
19+
get raw() {
20+
return this._k;
21+
}
22+
23+
toPublicKeyPEM() {
24+
return null;
25+
}
26+
27+
toPrivateKeyPEM() {
28+
return null;
29+
}
30+
1531
static validate(key) {
1632
// @see RFC-7517 par. 6.3
1733
return _.has(key, 'k');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-jwk",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "JWK support",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1",
@@ -19,6 +19,7 @@
1919
"jwk",
2020
"jws",
2121
"jwt",
22+
"jwkset",
2223
"security",
2324
"token",
2425
"convert",

0 commit comments

Comments
 (0)