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

oauthor

v1.0.1

Published

Oauth2 client for nodejs

Downloads

9

Readme

oauthor

OAuth2 client library for node

Travis npm

Get started

Install the oauthor package:

npm install --save oauthor

    // Step 1: Create oauth config
    var oauthor = require('oauthor');
    var oauth = oauthor(
        'https://provider.com/oauth',
        'https://provider.com/access_token',
        'yourclientid',
        'yourclientsecret'
    );

    // Step 2: Perform authorize request and handle redirect
    var code = redirect(oauth.authorizeUrl({redirect_uri: '...'}));

    // Step 3: Get code from the redirect and exchange for access token.
    oauth.requestAccessToken(code).then(response => {
        // The provider should have sent an access token in the response
        var accessToken = response.access_token;    
        // Sign all subsequent requests to the provider.
        var client = oauth.sign(accessToken);

        // Use `client` now to make authorized requests.
        client({url: 'https://provider.com./me'}).then(user => ...)
    });

Case study: Facebook

    // Step 1: Create oauth config
    var oauthor = require('oauthor');
    var oauth = oauthor(
        'https://www.facebook.com/dialog/oauth',
        'https://graph.facebook.com/oauth/access_token',
        facebook_client_id,
        facebook_client_secret, 
        {
            params: {redirect_uri: 'https://myapp.com/redirect/facebook'}
        }
    );

    // Step 2: Perform authorize request
    var code = redirect(oauth.authorizeUrl({
        scope: 'public_profile'
    }));

    // Step 3: Get code from the redirect and exchange for access token.
    oauth.requestAccessToken(code).then(response => {
        // The provider should have sent an access token in the response
        var accessToken = response.access_token;    
        // Sign all subsequent requests to the provider.
        var client = oauth.sign(accessToken);

        // Use `client` now to make authorized requests.
        client({
            url: 'https://graph.facebook.com/me',
            qs: {fields: 'id,name,email,link,gender,locale,timezone'}
        }).then(profile => ...)
    });

Use with generators

The oauthor package exposes a simple Promise api ready to be used with generators (using co and the likes).

    // Step 1: Create oauth config
    var oauthor = require('oauthor');
    var oauth = oauthor(
        'https://provider.com/oauth',
        'https://provider.com/access_token',
        'yourclientid',
        'yourclientsecret'
    );

    // Step 2: Perform authorize request and handle redirect
    var code = redirect(oauth.authorizeUrl({redirect_uri: '...'}));

    // Step 3: Get code from the redirect and exchange for access token.
    var response = yield oauth.requestAccessToken(code);

    // The provider should have sent an access token in the response
    var accessToken = response.access_token;    
    // Sign all subsequent requests to the provider.
    var client = oauth.sign(accessToken);

    // Use `client` now to make authorized requests.
    var user = yield client({url: 'https://provider.com./me'});

API

authorizeUrl

Creates the url that should be used for authorization, with the specified parameters as query string.

Parameters

  • params [Object] Parameters to send in the query string. (optional, default {})

Returns String The url that can be used to perform authorization.

requestAccessToken

Performs a request to the access token endpoint that was specified, with the code received from the authorization step, and optional parameters.

Parameters

  • code String The code obtained in the authorization step.
  • params [Object] Parameters that will be posted to the request. (optional, default {})

Returns Object The response from the provider.

sign

Specifies an access token that should be used to sign all subsequent requests. The return value is a function that can be used to make authorized requests. The function takes an object specifying the request options that will be passed to the request function. See request for more info on the supported options.

Parameters

  • accessToken String The access token obtained from the provider.

Examples

    // Sign all requests with `youraccesscode`.
    var client = oauth.sign('youraccesscode');
    
    // Suppose you have the endpoint `https://provider.com/me` that
    // returns your user profile when queried.
    client({url: 'https://provider.com/me'}).then((user) => ...)

Returns Function