1.0.1 • Published 6 years ago

oc-plugin-jwt v1.0.1

Weekly downloads
12
License
MIT
Repository
github
Last release
6 years ago

oc-plugin-jwt Greenkeeper badge Build Status

OpenComponents plugin for validating JSON Web Token (JWT) inside OC components.

Requirements

Install

npm i oc-plugin-jwt --save

Registry setup

More info about integrating OC plugins: here

Registering using the simple in-memory keystore.

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

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:

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