All Projects → Mastercard → client-encryption-nodejs

Mastercard / client-encryption-nodejs

Licence: MIT license
Library for Mastercard API compliant payload encryption/decryption.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to client-encryption-nodejs

client-encryption-java
Library for Mastercard API compliant payload encryption/decryption.
Stars: ✭ 55 (+175%)
Mutual labels:  openapi, fle, decryption, mastercard, field-level-encryption
oauth1-signer-ruby
Zero dependency library for generating a Mastercard API compliant OAuth signature.
Stars: ✭ 15 (-25%)
Mutual labels:  openapi, mastercard
oauth1-signer-nodejs
Zero dependency library for generating a Mastercard API compliant OAuth signature.
Stars: ✭ 26 (+30%)
Mutual labels:  openapi, mastercard
mastercard-api-client-tutorial
Generating and Configuring a Mastercard API Client
Stars: ✭ 25 (+25%)
Mutual labels:  openapi, mastercard
Api
The Up Banking API Specification
Stars: ✭ 248 (+1140%)
Mutual labels:  openapi
Flasgger
Easy OpenAPI specs and Swagger UI for your Flask API
Stars: ✭ 2,825 (+14025%)
Mutual labels:  openapi
Openapi Comment Parser
⚓️ JSDoc Comments for the OpenAPI Specification
Stars: ✭ 221 (+1005%)
Mutual labels:  openapi
Generators
API Generator - instantly generate REST and GraphQL APIs (openapi (OAS) 3.0.0)
Stars: ✭ 213 (+965%)
Mutual labels:  openapi
OpenAPI
A pharo implementation of OpenAPI 3.0.1
Stars: ✭ 20 (+0%)
Mutual labels:  openapi
openshift-json-schema
A set of JSON schemas for various OpenShift versions, extracted from the OpenAPI definitions
Stars: ✭ 23 (+15%)
Mutual labels:  openapi
Api Generator
PHP-code generator for Laravel framework, with complete support of JSON-API data format
Stars: ✭ 244 (+1120%)
Mutual labels:  openapi
Spot
Spot is a concise, developer-friendly way to describe your API contract.
Stars: ✭ 230 (+1050%)
Mutual labels:  openapi
Shins
Shins development continues at
Stars: ✭ 250 (+1150%)
Mutual labels:  openapi
Openapi Codegen
OpenAPI 3.0 CodeGen plus Node.js minus the Java and emojis
Stars: ✭ 224 (+1020%)
Mutual labels:  openapi
kmstool
Tool for using AWS Kms data keys to encrypt and decrypt large files.
Stars: ✭ 33 (+65%)
Mutual labels:  decryption
Openapi Backend
Build, Validate, Route, Authenticate and Mock using OpenAPI
Stars: ✭ 216 (+980%)
Mutual labels:  openapi
Grpc Swagger
Debugging gRPC application with swagger-ui.
Stars: ✭ 242 (+1110%)
Mutual labels:  openapi
ciphr
CLI crypto swiss-army knife for performing and composing encoding, decoding, encryption, decryption, hashing, and other various cryptographic operations on streams of data from the command line; mostly intended for ad hoc, infosec-related uses.
Stars: ✭ 100 (+400%)
Mutual labels:  decryption
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+17140%)
Mutual labels:  openapi
Kubernetes Json Schema
A set of JSON schemas for various Kubernetes versions, extracted from the OpenAPI definitions
Stars: ✭ 234 (+1070%)
Mutual labels:  openapi

client-encryption-nodejs

Table of Contents

Overview

NodeJS library for Mastercard API compliant payload encryption/decryption.

Compatibility

  • NodeJS 6.12.3+
  • NodeJS 13.14.0+ (JWE features)

There shouldn't be any Node compatibility issues with this package, but it's a good idea to keep your Node versions up-to-date. It is recommended that you use one of the LTS Node.js releases, or one of the more general recent releases. A Node version manager such as nvm (Mac and Linux) or nvm-windows is a good way to stay on top of this.

References

Encryption of sensitive data

Usage

Prerequisites

Before using this library, you will need to set up a project in the Mastercard Developers Portal.

As part of this set up, you'll receive:

  • A public request encryption certificate (aka Client Encryption Keys)
  • A private response decryption key (aka Mastercard Encryption Keys)

Installation

If you want to use mastercard-client-encryption with Node.js, it is available through npm:

Adding the library to your project:

npm install mastercard-client-encryption

You can then use it as a regular module:

const clientEncryption = require("mastercard-client-encryption");

Performing Field Level Encryption and Decryption

Introduction

The core methods responsible for payload encryption and decryption are encryptData and decryptData in the FieldLevelEncryption class.

  • encrypt() usage:
const fle = new clientEncryption.FieldLevelEncryption(config);
// …
let encryptedRequestPayload = fle.encrypt(endpoint, header, body);
  • decrypt() usage:
const fle = new clientEncryption.FieldLevelEncryption(config);
// …
let responsePayload = fle.decrypt(encryptedResponsePayload);

Configuring the Field Level Encryption

FieldLevelEncryption needs a config object to instruct how to decrypt/decrypt the payloads. Example:

const config = {
  paths: [
    {
      path: "/resource",
      toEncrypt: [
        {
          /* path to element to be encrypted in request json body */
          element: "path.to.foo",
          /* path to object where to store encryption fields in request json body */
          obj: "path.to.encryptedFoo",
        },
      ],
      toDecrypt: [
        {
          /* path to element where to store decrypted fields in response object */
          element: "path.to.encryptedFoo",
          /* path to object with encryption fields */
          obj: "path.to.foo",
        },
      ],
    },
  ],
  ivFieldName: "iv",
  encryptedKeyFieldName: "encryptedKey",
  encryptedValueFieldName: "encryptedData",
  dataEncoding: "hex",
  encryptionCertificate: "./path/to/public.cert",
  privateKey: "./path/to/your/private.key",
  oaepPaddingDigestAlgorithm: "SHA-256",
};

For all config options, please see:

We have a predefined set of configurations to use with Mastercard services:

Performing Encryption

Call FieldLevelEncryption.encrypt() with a JSON request payload, and optional header object.

Example using the configuration above:

const payload = {
  path: {
    to: {
      foo: {
        sensitive: "this is a secret!",
        sensitive2: "this is a super-secret!",
      },
    },
  },
};
const fle = new (require("mastercard-client-encryption").FieldLevelEncryption)(
  config
);
// …
let responsePayload = fle.encrypt("/resource1", header, payload);

Output:

{
  "path": {
    "to": {
      "encryptedFoo": {
        "iv": "7f1105fb0c684864a189fb3709ce3d28",
        "encryptedKey": "67f467d1b653d98411a0c6d3c…ffd4c09dd42f713a51bff2b48f937c8",
        "encryptedData": "b73aabd267517fc09ed72455c2…dffb5fa04bf6e6ce9ade1ff514ed6141",
        "publicKeyFingerprint": "80810fc13a8319fcf0e2e…82cc3ce671176343cfe8160c2279",
        "oaepHashingAlgorithm": "SHA256"
      }
    }
  }
}

Performing Decryption

Call FieldLevelEncryption.decrypt() with an (encrypted) response object with the following fields:

  • body: json payload
  • request.url: requesting url
  • header: optional, header object

Example using the configuration above:

const response = {};
response.request = { url: "/resource1" };
response.body = {
  path: {
    to: {
      encryptedFoo: {
        iv: "e5d313c056c411170bf07ac82ede78c9",
        encryptedKey:
          "e3a56746c0f9109d18b3a2652b76…f16d8afeff36b2479652f5c24ae7bd",
        encryptedData:
          "809a09d78257af5379df0c454dcdf…353ed59fe72fd4a7735c69da4080e74f",
        oaepHashingAlgorithm: "SHA256",
        publicKeyFingerprint: "80810fc13a8319fcf0e2e…3ce671176343cfe8160c2279",
      },
    },
  },
};
const fle = new (require("mastercard-client-encryption").FieldLevelEncryption)(
  config
);
let responsePayload = fle.decrypt(response);

Output:

{
  "path": {
    "to": {
      "foo": {
        "sensitive": "this is a secret",
        "sensitive2": "this is a super secret!"
      }
    }
  }
}

Performing JWE Encryption and Decryption

JWE Encryption and Decryption

• Introduction

This library uses JWE compact serialization for the encryption of sensitive data. The core methods responsible for payload encryption and decryption are encryptData and decryptData in the JweEncryption class.

  • encryptPayload usage:
const jwe = new clientEncryption.JweEncryption(config);
// …
let encryptedRequestPayload = jwe.encrypt(endpoint, header, body);
  • decryptPayload usage:
const jwe = new clientEncryption.JweEncryption(config);
// …
let responsePayload = jwe.decrypt(encryptedResponsePayload);
• Configuring the JWE Encryption

JweEncryption needs a config object to instruct how to decrypt/decrypt the payloads. Example:

const config = {
  paths: [
    {
      path: "/resource1",
      toEncrypt: [
        {
          /* path to element to be encrypted in request json body */
          element: "path.to.foo",
          /* path to object where to store encryption fields in request json body */
          obj: "path.to.encryptedFoo",
        },
      ],
      toDecrypt: [
        {
          /* path to element where to store decrypted fields in response object */
          element: "path.to.encryptedFoo",
          /* path to object with encryption fields */
          obj: "path.to.foo",
        },
      ],
    },
  ],
  mode: "JWE",
  encryptedValueFieldName: "encryptedData",
  encryptionCertificate: "./path/to/public.cert",
  privateKey: "./path/to/your/private.key",
};

Mode must be set to JWE to use JWE encryption

• Performing JWE Encryption

Call JweEncryption.encrypt() with a JSON request payload, and optional header object.

Example using the configuration above:

const payload = {
  path: {
    to: {
      foo: {
        sensitive: "this is a secret!",
        sensitive2: "this is a super-secret!",
      },
    },
  },
};
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
// …
let responsePayload = jwe.encrypt("/resource1", header, payload);

Output:

{
  "path": {
    "to": {
      "encryptedFoo": {
        "encryptedData": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw"
      }
    }
  }
}
• Performing JWE Decryption

Call JweEncryption.decrypt() with an (encrypted) response object with the following fields:

Example using the configuration above:

const response = {};
response.request = { url: "/resource1" };
response.body =
  "{" +
  '    "path": {' +
  '        "to": {' +
  '            "encryptedFoo": {' +
  '                "encryptedData": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw"' +
  "            }" +
  "        }" +
  "    }" +
  "}";
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
let responsePayload = jwe.decrypt(response);

Output:

{
  "path": {
    "to": {
      "foo": {
        "sensitive": "this is a secret",
        "sensitive2": "this is a super secret!"
      }
    }
  }
}
• Encrypting Entire Payloads

Entire payloads can be encrypted using the "$" operator as encryption path:

const config = {
  paths: [
    {
      path: "/resource1",
      toEncrypt: [
        {
          /* path to element to be encrypted in request json body */
          element: "$",
          /* path to object where to store encryption fields in request json body */
          obj: "$",
        },
      ],
      toDecrypt: [],
    },
  ],
  mode: "JWE",
  encryptedValueFieldName: "encryptedData",
  encryptionCertificate: "./path/to/public.cert",
  privateKey: "./path/to/your/private.key",
};

Example:

const payload =
  "{" +
  '    "sensitive": "this is a secret",' +
  '    "sensitive2": "this is a super secret!"' +
  "}";
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
// …
let responsePayload = jwe.encrypt("/resource1", header, payload);

Output:

{
  "encryptedData": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw"
}
• Decrypting Entire Payloads

Entire payloads can be decrypted using the "$" operator as decryption path:

const config = {
  paths: [
    {
      path: "/resource1",
      toEncrypt: [],
      toDecrypt: [
        {
          /* path to element where to store decrypted fields in response object */
          element: "$",
          /* path to object with encryption fields */
          obj: "$",
        },
      ],
    },
  ],
  mode: "JWE",
  encryptedValueFieldName: "encryptedData",
  encryptionCertificate: "./path/to/public.cert",
  privateKey: "./path/to/your/private.key",
};

Example:

const encryptedPayload =
  "{" +
  '  "encryptedData": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw"' +
  "}";
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
let responsePayload = jwe.decrypt(encryptedPayload);

Output:

{
  "sensitive": "this is a secret",
  "sensitive2": "this is a super secret!"
}

Integrating with OpenAPI Generator API Client Libraries

OpenAPI Generator generates API client libraries from OpenAPI Specs. It provides generators and library templates for supporting multiple languages and frameworks.

The client-encryption-nodejs library provides the Service decorator object you can use with the OpenAPI generated client. This class will take care of encrypting request and decrypting response payloads, but also of updating HTTP headers when needed, automatically, without manually calling encrypt()/decrypt() functions for each API request or response.

OpenAPI Generator

OpenAPI client can be generated, starting from your OpenAPI Spec / Swagger using the following command:

openapi-generator-cli generate -i openapi-spec.yaml -l javascript -o out

Client library will be generated in the out folder.

See also:

Usage of the mcapi-service:

To use it:

  1. Generate the OpenAPI client, as above

  2. Import the mastercard-client-encryption library

    const mcapi = require("mastercard-client-encryption");
  3. Import the OpenAPI Client using the Service decorator object:

    const openAPIClient = require("./path/to/generated/openapi/client");
    const config = {
      /* service configuration object */
    };
    
    const service = new mcapi.Service(openAPIClient, config);
  4. Use the service object as you are using the openAPIClient to make API requests.

    Example:

    let api = service.ServiceApi();
    let merchant =
      /* … */
      api.createMerchants(merchant, (error, data, response) => {
        // requests and responses will be automatically encrypted and decrypted
        // accordingly with the configuration used to instantiate the mcapi.Service.
        /* use response/data object here */
      });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].