Salesforce OAuth2 Web Server Authentication Flow
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.
npm install salesforce-oauth2
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 = expresscreateServerexpresslogger;appget"/"var uri = oauth2getAuthorizationUrlredirect_uri: callbackUrlclient_id: consumerKeyscope: 'api';return responseredirecturi;;appget'/oauth/callback'var authorizationCode = requestparam'code';oauth2authenticateredirect_uri: callbackUrlclient_id: consumerKeyclient_secret: consumerSecretcode: authorizationCode/*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 thenumber 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 obtaina fresh access token on expiry of the accesstoken in this response.instance_url Identifies the Salesforce instance to which APIcalls 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 requestsagainst the resource server (the Force.com instance specified by the instance URL)via the REST APIs, providing the access token as an HTTP header ineach request:Authorization: OAuth 00D50000000IZ3Z!AQ0AQDpEDKYsn7ioKug2aSmgCjgrPjG...*/;;applisten3000console.log"Listening on 3000";;