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

salesforce-oauth2

v0.2.0

Published

Salesforce OAuth2 Web Server Authentication Flow

Downloads

2,563

Readme

salesforce-oauth2 -- Salesforce OAuth2 Web Server Authentication Flow

Abstract

A very lightweight implementation of the OAuth2 Web Server Authentication Flow for Salesforce for Node.js.

As Salesforce already provides a very robust REST API, the aim of this module is to provide a very thin wrapper for the authentication functionality only.

Install

npm install salesforce-oauth2

Usage

An example using the express web framework:

var express = require('express'),
	oauth2 = require('salesforce-oauth2');

var callbackUrl = "<your callback url>",
	consumerKey = "<your consumer key>",
	consumerSecret = "<your consumer secret>";

var app = express.createServer(express.logger());


app.get("/", function(request, response) {
	var uri = oauth2.getAuthorizationUrl({
		redirect_uri: callbackUrl,
		client_id: consumerKey,
		scope: 'api', // 'id api web refresh_token'
		// You can change loginUrl to connect to sandbox or prerelease env.
		//base_url: 'https://test.my.salesforce.com'
	});
	return response.redirect(uri);
});

app.get('/oauth/callback', function(request, response) {
	var authorizationCode = request.param('code');

	oauth2.authenticate({
		redirect_uri: callbackUrl,
		client_id: consumerKey,
		client_secret: consumerSecret,
		code: authorizationCode,
		// You can change loginUrl to connect to sandbox or prerelease env.
		//base_url: 'https://test.my.salesforce.com'
	}, function(error, payload) {
		/*

		The payload should contain the following fields:
		
		id 				A URL, representing the authenticated user,
						which can be used to access the Identity Service.
		
		issued_at		The time of token issue, represented as the 
						number of seconds since the Unix epoch
						(00:00:00 UTC on 1 January 1970).
		
		refresh_token	A long-lived token that may be used to obtain
						a fresh access token on expiry of the access 
						token in this response. 

		instance_url	Identifies the Salesforce instance to which API
						calls should be sent.
		
		access_token	The short-lived access token.


		The signature field will be verified automatically and can be ignored.

		At this point, the client application can use the access token to authorize requests 
		against the resource server (the Force.com instance specified by the instance URL) 
		via the REST APIs, providing the access token as an HTTP header in 
		each request:

		Authorization: OAuth 00D50000000IZ3Z!AQ0AQDpEDKYsn7ioKug2aSmgCjgrPjG...
		*/
	});	
});

app.listen(3000, function() {
	console.log("Listening on 3000");
});

Getting refresh_token

To get refresh_token you must pass 'refresh_token' word on scope. Remember to add a Available OAuth Scopes - refresh_token to Selected OAuth Scopes in your console developer Example:

	app.get("/", function(request, response) {
		var uri = oauth2.getAuthorizationUrl({
			redirect_uri: callbackUrl,
			client_id: consumerKey,
			scope: 'api refresh_token',
			// You can change loginUrl to connect to sandbox or prerelease env.
			//base_url: 'https://test.my.salesforce.com'
		});
		return response.redirect(uri);
	});

Util links to setup your connected app salesforce