npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oniyi-http-plugin-credentials

v1.1.0

Published

A plugin for oniyi-http-client for automatic attachment of user credentials

Downloads

24

Readme

oniyi-http-plugin-credentials NPM version Dependency Status

An async plugin for oniyi-http-client to resolve and attach credentials to request params

resolve endpoint specific credentials asynchronusly and inject them into request options before making the actual request to endpoint

Installation

$ npm install --save oniyi-http-plugin-credentials

Usage

const OniyiHttpClient = require('oniyi-http-client');
const oniyiHttpPluginCredentials = require('oniyi-http-plugin-credentials');

const httpClientParams = {
  requestPhases: ['initial','credentials', 'final'],
};

const pluginOptions = {
  providerName: 'my-auth-provider', // Name of the provider that credentials should be resolved for
  removeUserProp: true, // should plugin remove `user` prop from `reqParams`
  userPropName: 'user', // name of the `reqParams` property that holds the `user` object
  credentialsMethodName: 'getCredentialsForProvider', // name of the method on `user` object that resolves credentials for `providerName`
};
const plugin = oniyiHttpPluginCredentials(pluginOptions);
const phaseMapOptions = {
  requestPhaseMap: {
    credentials: 'newCredentialsPhase',
  },
  responsePhaseMap: {
    final: 'end',
  },
};

const httpClient = OniyiHttpClient
  .create(httpClientParams)       // create custom http client with defined phase lists
  .use(plugin, phaseMapOptions);  // mount a plugin

Plugin Options

The oniyi-http-plugin-credentials module exports a factory function that takes a single options argument.

available options are:

  • providerName: undefined (string, required) - is passed to getCredentialsForProvider to indicate which backend we need credentials for
  • removeUserProp: true (boolean, optional) - indicates if the user property should be removed from the request options
  • userPropName: user (string, optional) - name of the reqParams property that holds the user object
  • credentialsMethodName: getCredentialsForProvider (string, optional) - name of the method on user object that resolves credentials for providerName

How does it work?

This plugin relies on logic implemented in oniyi-http-client, which has extensive documentation on how phase lists work and what conventions must be followed when implementing a plugin.

Basically, we have implemented a phase list hook handler which gets invoked in request phase list of http client. Once credentials hook handler gets invoked, it receives a ctx and next params. Once we pull options from the context object, the following flow is applied:

copy options into reqParams. Depending on pluginOptions.removeUserProp, the original prop named pluginOptions.userPropName will be omitted or included. read prop named pluginOptions.userPropName from options into user. If user can not be found, abort flow and invoke next function so that next plugin in this phase list can do its operations. If user[pluginOptions.credentialsMethodName] is not a function, invoke next with Error.

Invoke user[pluginOptions.credentialsMethodName] with pluginOptions.providerName and reqParams as well as a callback function. Now user[pluginOptions.credentialsMethodName] is supposed to resolve credentials for user and the authentication provider. This resolution should happen async and results be passed to our local callback (which takes err and credentials arguments). If an error occurs, plugin flow is aborted and err passed to callback. If credentials is falsy, plugin flow is also aborted and next gets invoked.

At this point, we let user[pluginOptions.credentialsMethodName] resolve credentials for the auth provider that this plugin instance is configured for – and no errors occurred.

Now the plugin applies credentials to reqParams. For that, credentials.type is mapped against a list of supported credential types. If credentials.type is supported, that type specific implementation is invoked with reqParams and credentials.payload. Each credentials type expects a different layout of credentials.payload.

Finally, we update the ctx.options object with latest reqParamsWithCredentials changes, and invoke next function.

Credentials types

basic

Reads username, password and optionally sendImmediately (default: true) and authType (default: basic) from payload and injects them into reqParams.

Use this type when you have username and password at hand (plain)

bearer

Reads token and optionally sendImmediately (default: true) and authType (default: oauth) from payload and injects them into reqParams.

Use this type when you have e.g. an OAuth2 / OIDC access token at hand

cookie

Reads cookie and optionally authType (default: cookie) from payload and injects them into reqParams. The value of cookie is set into reqParams.headers.cookie. If reqParams.headers.cookie was not empty to begin with, value of cookie is appended.

Use this type when you have an authentication cookie (e.g. LtpaToken2 for IBM Websphere ApplicationServer) at hand.

header

Reads value and optionally name (default: authorization) and authType (default: undefined) from payload and injects them into reqParams. The value is set into reqParams.headers[name].

Use this type for any other form of credentials that are provided in a http header. E.g. if you only have basic credentials already base64 encoded or you're working with a custom TAI for IBM Websphere ApplicationServer where you simply pass an encrypted username to the remote host.

License

MIT © Benjamin Kroeger