Skip to content

Commit 3c545f3

Browse files
author
Frank Schmid
committed
Adjusted readme
1 parent af3f229 commit 3c545f3

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@ _will come_
1414

1515
_will come_
1616

17+
## Examples
18+
19+
### Creating a signed token with node-jwk and njwt
20+
21+
The example uses bluebird promises to be able to catch exceptions thrown in the
22+
key retrieval and lodash for convenience.
23+
24+
```
25+
const time = Math.floor(_.now() / 1000);
26+
27+
const claims = {
28+
iss: 'itsME',
29+
aud: 'myAudience',
30+
iat: time,
31+
exp: time + 3600
32+
};
33+
34+
return BbPromise.try(() => {
35+
const keySet = nodeJWK.JWKSet.fromObject(myPrivateKeySet);
36+
const jwk = keySet.findKeyById(myKeyId);
37+
38+
if (!jwk) {
39+
return BbPromise.reject(new Error('Huh, my key is not there...'));
40+
}
41+
42+
const keyPEM = jwk.key.toPrivateKeyPEM();
43+
const jwt = njwt.create(claims, keyPEM, jwk.alg);
44+
45+
return BbPromise.resolve(jwt.compact());
46+
})
47+
.catch(err => {
48+
return BbPromise.reject(err);
49+
});
50+
```
51+
1752
## Supported key types
1853

1954
RSA, EC, oct

0 commit comments

Comments
 (0)