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

oc-plugin-authenticate

v1.0.1

Published

OpenComponents Plugin for authenticating requests

Downloads

4

Readme

oc-plugin-authenticate

oc-plugin-authenticate allows any OC component to authenticate requests.

Authentication

An OC appication using this plugin may authenticate any request to help control application flow and protect sensitive data.

Usage:

module.exports.data = (context, callback) => {
  context.plugins.authenticate(context, (err, {user, access_token}) => {
    if (err) {
      const errorMessage = 'unable to authenticate user';
      return callback(errorMessage);
    }

    callback(null, {
      access_token,
      username: user.username
    })
  });
};

After authenticating, you will receive either an error or a value using the callback pattern. The value will contain a user object, and a token string.

  • The user object will contain any claims or details provided within your authentication token.

  • The token string will be the authentication token provided in the original request and can be used to ensure further interactions with your OC component (eg. via AJAX calls using getData) are authenticated (see: Persisting authentication).

The authenticate method also has a few overloads which allows you to influence the authenticate algorithm if necessary.

context.plugins.authenticate(context, strategy, callback);
context.plugins.authenticate(context, options, callback);
context.plugins.authenticate(context, strategy, options, callback);

strategy : string Allows you to specify what strategy to use when authenticating. The value must refer to one of the strategies provided when the plugin was registered within the oc-registry.

options : object Will override something or another UPDATE THIS PART PLEASE!

Persisting authentication

When making getData calls within your component, sending headers is not possible by design as it causes the browser to make a pre-flight request. Therefore, you must provide the token as a simple oc parameter to ensure your application can authenticate your XHR request.

We recommend that you reserve and use the access_token parameter to send the access_token because most oc-authentication-strategy implementations will expect this parameter by default.

React Template example:

  class MyComponent extends React.Component {
    constructor(props) {
      super(props);
    }

    doSomething(data) {
      const { access_token } = this.props;
      const params = {
        ...data,

        // we use access_token to ensure
        // that we can authenticate
        // our requests.
        access_token
      };

      this.props.getData(params, (err, data) => {
        this.setState({ data });
      });
    }

    render() {
      return <button onClick={() => this.doSomething({ name: 'bob' })}>GO!</button>
    }
  }

Registry setup

For more information about integrating OC plugins click here.

When registering, the plugin must provide at least one strategy and must specify the defaultStrategy. Optionally, you may provide default options that will be provided to your strategy function.

const configuration = { ... };
const registry = new oc.Registry(configuration);
const twitterAuth = require('some-twitter-oc-authentication-strategy');
const internalAuth = require('some-internal-oc-authentication-strategy');

registry.register(
  {
    name: 'authenticate',
    register: require('oc-authenticate'),
    options: {
      strategies: {
        twitter: {
          strategy: twitterAuth,
        }
        internal: {
          strategy: internalAuth
          options: { ... }
        }
      },
      defaultStrategy: 'twitter'
    }
  }
);

registry.start(callback);

Authentication Strategies

Creating an authentication strategy.