|
| 1 | +# oc-plugin-jwt [](https://greenkeeper.io/) [](https://travis-ci.org/opencomponents/oc-plugin-jwt) |
| 2 | + |
| 3 | + |
| 4 | +[OpenComponents](https://github.com/opentable/oc) plugin for validating [JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) inside OC components. |
| 5 | + |
| 6 | +## Requirements |
| 7 | +* Node version: min **6** |
| 8 | +* [OC Registry](https://github.com/opentable/oc) |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +```bash |
| 13 | +npm i oc-plugin-jwt --save |
| 14 | +``` |
| 15 | + |
| 16 | +## Registry setup |
| 17 | + |
| 18 | +More info about integrating OC plugins: [here](https://github.com/opentable/oc/wiki/Registry#plugins) |
| 19 | + |
| 20 | + |
| 21 | +Registering using the simple in-memory keystore. |
| 22 | + |
| 23 | +```js |
| 24 | +const registry = oc.registry(configuration); |
| 25 | + |
| 26 | +registry.register( |
| 27 | + { |
| 28 | + name: 'jwtVerify', |
| 29 | + register: require('oc-plugin-jwt').verify, |
| 30 | + options: { |
| 31 | + keys: { |
| 32 | + 'key-id-1': { |
| 33 | + publicKey: fs.readFileSync('certificate.pem') |
| 34 | + }, |
| 35 | + 'key-id-2': { |
| 36 | + secret: 'super-secret-password' |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + }, |
| 41 | + err => { |
| 42 | + if (err) { |
| 43 | + console.log('plugin initialisation failed:', err); |
| 44 | + } else { |
| 45 | + console.log('jwt verify now available'); |
| 46 | + } |
| 47 | + } |
| 48 | +); |
| 49 | + |
| 50 | +registry.start(callback); |
| 51 | +``` |
| 52 | + |
| 53 | +Or custom using a custom keystore |
| 54 | +```js |
| 55 | +const registry = oc.registry(configuration); |
| 56 | + |
| 57 | +registry.register( |
| 58 | + { |
| 59 | + name: 'jwtVerify', |
| 60 | + register: require('oc-plugin-jwt').verify, |
| 61 | + options: { |
| 62 | + keyStore: { |
| 63 | + getSecretOrPublicKey(keyId, callback) { |
| 64 | + // Get the public key or secret by some method |
| 65 | + return callback(null, key); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | + err => { |
| 71 | + if (err) { |
| 72 | + console.log('plugin initialisation failed:', err); |
| 73 | + } else { |
| 74 | + console.log('jwt verify now available'); |
| 75 | + } |
| 76 | + } |
| 77 | +); |
| 78 | + |
| 79 | +registry.start(callback); |
| 80 | +``` |
| 81 | + |
| 82 | +## Using it inside components |
| 83 | + |
| 84 | +Example for a component's server.js: |
| 85 | + |
| 86 | +```js |
| 87 | +module.exports.data = (context, callback) => { |
| 88 | + const exampleToken = |
| 89 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleS1pZC0yIn0.' + |
| 90 | + 'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.' + |
| 91 | + 'bQVxleqAX7NQzI_RkIPFVfTl44-iEY0UYPUBm10789o'; |
| 92 | + context.plugins.jwtVerify(exampleToken, (error, verifiedToken) => { |
| 93 | + if (error) { |
| 94 | + // Handle token verification errors |
| 95 | + callback(error); |
| 96 | + } |
| 97 | + callback(null, { verifiedToken: verifiedToken }); |
| 98 | + }); |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +## Generating Tokens |
| 103 | + |
| 104 | +* [See Here](https://github.com/opencomponents/oc-plugin-jwt-examples) |
0 commit comments