Read logged in User details from SSO server in Blu

2019-03-06 03:57发布

问题:

I am working on Node.js application with Cloudant database. I was able to do IBM IDP authentication with SSO server on Blue mix via the SSO service.

My issue occurs after successful authentication, I am unable to get JSON object that can give me all the user attributes that I need, for example, is the logged in person a manager? if yes then his serial number, name etc

Does anyone know how to retrieve the information from IBM SSO service? Kindly let me know as soon as possible.

回答1:

You can check the request.user object returned after successful authentication. It returns some information about the logged in user, but each provider returns different data.

For example, for LinkedIn logged users it returns displayName, firstName, lastName and emailAddress.

The snippet code below prints the request.user JSON object in the application log, so you can see what is available and retrieve as needed.

app.get('/auth/sso/callback', function(req, res, next) { 

    var redirect_url = req.session.originalUrl;                
    passport.authenticate('openidconnect', {
        successRedirect: '/hello',                                
        failureRedirect: '/failure',                        
    })(req,res,next);
});

app.get('/hello', ensureAuthenticated, function(request, response) {

    response.send('Hello, '+ request.user['id'] + '!\n' + '<a href="/logout">Log Out</a>');
    console.log(JSON.stringify(request.user));     

});

After user logs in you can run:

cf logs <app-name> --recent

to see results from console.log code.