oauthor

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i oauthor

Weekly Downloads

0

Version

1.0.1

License

Apache-2.0

Last publish

Collaborators

  • adrian2x