Access User Profile in node.js after SSO authentication (BlueMix)
<p>I have searched and I have searched a lot, finally came here.</p>

<p><strong>Have:</strong>
I have deployed a simple node.js app on Bluemix that does nothing but display Hello! after authenticating the user through SSO SAML service bound to the app on Bluemix.</p>

<p><strong>Need:</strong>
What I need to do is get the user profile (firstName, lastName, displayName, emailId etc) so that I can store the user on to a database of my choice (say Cloudant) and further develop the app.</p>

<p><strong>Issue:</strong>
I couldn't find a way (example code) to retrieve the user profile. I have read that there is a token returned by the server which needs to be claimed/consumed, but nowhere is an example to be found on how to do that. </p>

<p><strong>Existing thread:</strong>
There is a <a href="https://stackoverflow.com/questions/33498572/read-logged-in-user-details-from-sso-server-in-blue-mix">thread</a> on stackoverflow which is similar to my issue, but the solution didn't work. My code below explains that.</p>

<p><strong>My code:</strong></p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code> Strategy = new OpenIDConnectStrategy({
      authorizationURL : authorization_url,
      tokenURL : token_url,
      clientID : client_id,
      scope: 'openid',
      response_type: 'code',
      clientSecret : client_secret,
      callbackURL : callback_url,
      skipUserProfile: false,
      issuer: issuer_id}, 
  function(iss, sub, profile, accessToken, refreshToken, params, done)  {
     process.nextTick(function() {
   profile.accessToken = accessToken;
   profile.refreshToken = refreshToken;
   done(null, profile);
    })
 }); 
 passport.use(Strategy); 
}

app.get('/',ensureAuthenticated, function(req, res){});
app.get('/login', passport.authenticate('openidconnect', {successRedirect: '/hello',failureRedirect: '/failure'})); 
app.get('/hello', function(req, res) {
 console.log("Pooshon1: ",JSON.stringify(req.user));  
 console.log("Pooshon3: ",JSON.stringify(req.profile));  
 res.send('Hello, ');
 //res.send('Hello, '+ req.user.displayName + '!');
 //res.send('Hello, '+ req.user['id'] + '!');
});
app.get('/failure', function(req, res) { 
 res.send('login failed'); 
});          </code></pre>
</div>
</div>
</p>

<p>I did not put the entire code, just what was relevant. So, passport returns <code>done(null, profile)</code>, and what I read on the internet is that this profile object is returned by the server and can be found in the request object. In my code above, under the <code>app.get("/hello"</code>.... the two console.log statements print <code>"Pooshon: undefined"</code>, which means there is nothing like <code>req.user</code> or <code>req.profile</code> hence the last two lines are commented, because it throws Internal Server Error (500).</p>

<p>If anyone has done something like this, please help.</p>