0.2.0 • Published 7 years ago

cimpress-auth0-client-request-promise v0.2.0

Weekly downloads
60
License
Apache-2.0
Repository
github
Last release
7 years ago

cimpress-auth0-client-request-promise

Build Status

NPM

A module for handling generation of OAuth Bearer tokens issued by Auth0 by integrating credential management into request-promise.js.

This is a promise-request port from the original callback request library here

Installation

npm i cimpress-auth0-client-request-promise --save

OR

yarn add cimpress-auth0-client-request-promise

Usage

This module exposes a few methods, but the most important one is:

module.exports = (options) => { return Promise; }

This works as a drop-in replacement for request-promise. Adopting this flow is as simple as these two surgical incisions:

var request = require('cimpress-auth0-client-request-promise');
// Note the set of 6 possible new options that can be passed in the request.js options.auth object.
// Every other property in the request options object works as normal, and you can call all of the
// convenience methods exposed by request.js.
var options = {
  auth: {
    clientId: 'see below',
    clientSecret: 'see below',
    refreshToken: 'see below',
    targetId: 'see below',
    bearer: 'see below',
  },
  keyGen: (options) => `${options.uri}-${options.method}-${options.auth.bearer}`,
  timesToRetry: 2,
  cacheAll: true,
};
return request(options).then(
  (response) => {},
  (error) => {}
);

Here's how you should use those 5 auth parameters + 2 new parameters:

PropertyDescriptionV1 RequiredV2 Required
clientIdThe client id you wish to use to request client credential grants (https://auth0.com/docs/api-auth/grant/client-credentials).N/AY
clientSecretThe client secret you wish to use to request client credential grants (https://auth0.com/docs/api-auth/grant/client-credentials).N/AY
refreshTokenA refresh token for use in delegation flows, retrieved from developer.cimpress.io.YN/A
targetIdThe client id for which you are trying to retrieve a delegated token. Note, if you don't know this, you can rely on a 401 with a Www-Authenticate to provide the client id. If you don't provide this config, and the service doesn't provide that header, your call will fail with a 401.NN/A
bearerIf you've already generated an access token you can specify it here and the library will attempt to use it. If the call fails with a 401 the library will immediately returnNN
authorizationServerOPTIONAL The server to call to request client credential grants (https://auth0.com/docs/api-auth/grant/client-credentials). This defaults to https://cimpress-dev.auth0.com/oauth/token.YY
audienceThe audience to send when requesting client credential grants (https://auth0.com/docs/api-auth/grant/client-credentials). This defaults to https://api.cimpress.io/YY
keyGenOPTIONAL A function that returns a string to be used when caching responses. Takes in the options object and must return a promise. If not specified a default function is usedN/AN/A
timesToRetryOPTIONAL The number of times to retry when receiving a non-2XX response. Otherwise keeps trying foreverN/AN/A
cacheAllOPTIONAL By default the library only caches GET, HEAD, and OPTIONS requests. Set this to true if you want to also cache all other request typesN/AN/A

Some other exposes methods:

// specify a cache that will be used for the generated auth token as well as responses that have a cache-control header. By default there is no caching.
module.exports.setCredentialCache(altcache);  

// specify an alternative logger.
// by default the library only logs to console.log if the enviroment variable NODE_DEBUG is set equal to/includes 'cimpress-auth0-client-request-promise'
// otherwise the library does not log
module.exports.setLogger(altLogger); 

Note that the alternative caching method you use must return promises and have the following function definitions:

  • get(key)
  • set(key, value, ttl)

Events for Requests/Responses

The library has a custom event emitter that emits on outgoing requests and incoming responses. This means that events will be emited for both requests to the specified url as well as requests made when generating an auth token, if needed.

const request = require('cimpress-auth0-client-request-promise');
const re = request.requestEmitter;

// Tip: Make sure the auth token has been removed before printing values out
re.on(re.requestSentEvent, options => console.log(options));
re.on(re.responseReceivedEvent, response => console.log(response));

Wrapper

A helpful wrapper has been added that allows you to set some values constant between request (clientId, secret, etc..) as well as setting up the cache.

This wrapper returns a function with a simplified signature of:

return (method, url, body, headers, accessToken) => {};

Note that the accessToken must be a bearer token.

const wrapper = require('cimpress-auth0-client-request-promise/wrapper');
const cache = require('some-caching-library');

const config = {
  audience: 'https://audience.com',  // default is same as main library if not specified
  clientId: 'asd',
  clientSecret: 'fgh',
  refreshToken: 'lkj',
  keyGen: () => 'a',  // see main library for details
};

const primedWrapper = wrapper(config, cache);

// making a call
return primedWrapper(
  'GET', // method (required)
  'https://1234.com',  // url (required)
  { message: 'Hi I\'m a body!' }, // body
  { customHeader: '5689' }, // custom headers (set to 'content-type' = 'application/json' by default)
  'qazwsx');  // accessToken

You can also access the requestEmitter via wrapper.requestEmitter. See above for how to use it.

Tests

You might also want to look at our tests to see some examples of usage.

You can run tests via npm test.

Development

Getting Started from Source

# From Root
npm install

OR

# From Root
yarn

Code Formatting

We use eslint for formatting and mostly follow the airbnb standards

Making Changes

Contributing

To contribute to this library fork the repo and create a pull-request back to the master branch.

All changes require tests to be added or updated as part of the PR.

Once Travis successfully runs, including running the linter and all tests, your PR will require approval before merging.

Publishing

To publish a new version of this library make a pull-request to master updating the version in package.json. Once the update merges a new version of the package should be published shortly!

0.2.0

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago