passport-linkedin-oauth2

Passport for LinkedIn Oauth2

A simple Passport strategy for LinkedIn OAuth2.

npm install passport-linkedin-oauth2

Register the strategy

var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
 
passport.use(new LinkedInStrategy({
  clientID: LINKEDIN_KEY,
  clientSecret: LINKEDIN_SECRET,
  callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
  scope: ['r_emailaddress', 'r_basicprofile'],
}, function(accessTokenrefreshTokenprofiledone) {
  // asynchronous verification, for effect... 
  process.nextTick(function () {
    // To keep the example simple, the user's LinkedIn profile is returned to 
    // represent the logged-in user. In a typical application, you would want 
    // to associate the LinkedIn account with a user record in your database, 
    // and return that user instead. 
    return done(null, profile);
  });
}));

and then authenticate as:

app.get('/auth/linkedin',
  passport.authenticate('linkedin', { state: 'SOME STATE'  }),
  function(reqres){
    // The request will be redirected to LinkedIn for authentication, so this 
    // function will not be called. 
  });

the login callback:

app.get('/auth/linkedin/callback', passport.authenticate('linkedin', {
  successRedirect: '/',
  failureRedirect: '/login'
}));

See this for details on LinkedIn API.

The state param is used to prevent CSRF attacks, and is required by the LinkedIn API. You can ask Passport to handle the sending and validating of the state parameter by passing state: true as an option to the strategy:

var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
 
passport.use(new LinkedInStrategy({
  clientID: LINKEDIN_KEY,
  clientSecret: LINKEDIN_SECRET,
  callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
  scope: ['r_emailaddress', 'r_basicprofile'],
  state: true
}, function(accessTokenrefreshTokenprofiledone) {
  // asynchronous verification, for effect... 
  process.nextTick(function () {
    // To keep the example simple, the user's LinkedIn profile is returned to 
    // represent the logged-in user. In a typical application, you would want 
    // to associate the LinkedIn account with a user record in your database, 
    // and return that user instead. 
    return done(null, profile);
  });
}));

and then authenticate as:

app.get('/auth/linkedin',
  passport.authenticate('linkedin'),
  function(reqres){
    // The request will be redirected to LinkedIn for authentication, so this 
    // function will not be called. 
  });

MIT - 2014 - AUTH0