https://github.com/opencomponents/oc-plugin-jwt
https://github.com/opencomponents/oc-plugin-jwt
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/opencomponents/oc-plugin-jwt
- Owner: opencomponents
- License: mit
- Created: 2018-02-23T16:13:09.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-02-20T13:52:57.000Z (almost 6 years ago)
- Last Synced: 2025-01-22T07:37:57.232Z (about 1 year ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 3
- Watchers: 13
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# oc-plugin-jwt [](https://greenkeeper.io/) [](https://travis-ci.org/opencomponents/oc-plugin-jwt)
[OpenComponents](https://github.com/opentable/oc) plugin for validating [JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) inside OC components.
## Requirements
* Node version: min **6**
* [OC Registry](https://github.com/opentable/oc)
## Install
```bash
npm i oc-plugin-jwt --save
```
## Registry setup
More info about integrating OC plugins: [here](https://github.com/opentable/oc/wiki/Registry#plugins)
Registering using the simple in-memory keystore.
```js
const registry = oc.registry(configuration);
registry.register(
{
name: 'jwtVerify',
register: require('oc-plugin-jwt').verify,
options: {
keys: {
'key-id-1': {
publicKey: fs.readFileSync('certificate.pem')
},
'key-id-2': {
secret: 'super-secret-password'
}
}
}
}
);
registry.start(callback);
```
Or custom using a custom keystore
```js
const registry = oc.registry(configuration);
registry.register(
{
name: 'jwtVerify',
register: require('oc-plugin-jwt').verify,
options: {
keyStore: {
getSecretOrPublicKey(keyId, callback) {
// Get the public key or secret by some method
return callback(null, key);
}
}
}
}
);
registry.start(callback);
```
## Using it inside components
Example for a component's server.js:
```js
module.exports.data = (context, callback) => {
const exampleToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleS1pZC0yIn0.' +
'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.' +
'bQVxleqAX7NQzI_RkIPFVfTl44-iEY0UYPUBm10789o';
context.plugins.jwtVerify(exampleToken, (error, verifiedToken) => {
if (error) {
// Handle token verification errors
callback(error);
}
callback(null, { verifiedToken: verifiedToken });
});
};
```
## Generating Tokens
* [See Here](https://github.com/opencomponents/oc-plugin-jwt-examples)