hapijs / bell

Third-party login plugin for hapi
Other
624 stars 210 forks source link

Cannot use bell with Facebook #46

Closed valvoline closed 9 years ago

valvoline commented 9 years ago

Hi all,

I cannot find a way to make bell usable with Facebook. Here is my server config:

server.register(Bell, function (err) {
   server.auth.strategy('facebook', 'bell', {
        provider: 'facebook',
        password: 'password',
        isSecure: false,
        // Make sure to set a "Callback URL" and
        // check the "Allow this application to be used to Sign in with Twitter"
        // on the "Settings" tab in your Twitter application
        clientId: 'myClientID',                                 // Set client id
        clientSecret: 'myClientSecret'            // Set client secret
    });
    server.route({
        method: '*',
        path: '/bell/facebook',
        config: {
            auth: {
                strategy: 'facebook',
                mode: 'try'
            },
            handler: function (request, reply) {
                if (!request.auth.isAuthenticated) {
                    return reply('Authentication failed due to: ' + request.auth.error.message);
                } else {
                reply('<pre>' + JSON.stringify(request.auth.credentials, null, 4) + '</pre>');
               }
            }
        }
    });
});

However i obtain always an Authentication failed due to: Failed obtaining facebook access token

I double checked my Facebook app developer page and on the basic settings I've configured a WebSite that point to http://thalion.local:3000/bell/facebook/ that's my server route for bell authenticatin

Also, on Advanced Settings I've included Valid OAuth Redirect URI to point to http://thalion.local:3000/bell/facebook/

I'm stucked without any idea. Any hints can be much appreciated!

valvoline commented 9 years ago

I found the issue. In oauth.js I added the following debug code:

// Obtain token
Wreck.post(settings.provider.token, requestOptions, function (err, res, payload) {
            console.log("ERR: ", err);
            console.log("payload: ", payload);
            console.log("res: ", res);

This give me a more in-deep analysis about my misconfiguration. Specifically, I found that the app was configured as a Desktop one. Changing the settings from Facebook developers app, Advanced Settings, solved the issue.

I also reverted the server configuration as follow:

server.register(Bell, function (err) {

   server.auth.strategy('facebook', 'bell', {
        provider: 'facebook',
        password: 'password',
        isSecure: false,
        // Make sure to set a "Callback URL" and
        // check the "Allow this application to be used to Sign in with Twitter"
        // on the "Settings" tab in your Twitter application
        clientId: '832516966762800',                                // Set client id
        clientSecret: '4256a8f3a381735507d0a2eb67770577'            // Set client secret
    });

    server.route({
        method: '*',
        path: '/bell/facebook',
        config: {
            auth: 'facebook',
            handler: function (request, reply) {
                reply('<pre>' + JSON.stringify(request.auth.credentials, null, 4) + '</pre>');
            }
        }
    });
});

now, It works like a charm.

geek commented 9 years ago

@valvoline glad it all worked out. Are you interested in adding a Facebook example to the /examples folder?

valvoline commented 9 years ago

Sure,

I have also just completed a provider for linkedin and it works like a charm. Are you interested?

regards, — c.

On 15 Dec 2014, at 18:32, Wyatt Preul notifications@github.com wrote:

@valvoline https://github.com/valvoline glad it all worked out. Are you interested in adding a Facebook example to the /examples folder?

— Reply to this email directly or view it on GitHub https://github.com/hapijs/bell/issues/46#issuecomment-67031878.

geek commented 9 years ago

@valvoline absolutely interested, thanks!

hzaheer commented 9 years ago

@valvoline can u provide the linkedin config? thanks a ton!

valvoline commented 9 years ago

Absolutely yes!

I have no time at the moment to fork a branch, so I attach here our working config.

on your server.js (or whatever you maintain your route / auth strategy) add the following:

    server.auth.strategy('linkedin', 'bell', {
        provider            : 'linkedin',
        password            : 'password',
        isSecure            : false,
        clientId            : '#yourClientID#',                                 
        clientSecret        : '#yourClientSecret#'            
    });

    server.route({
        method: '*',
        path: '/bell/linkedin',
        config: {
            auth: 'linkedin',
            handler: function (request, reply) {
                console.log('credentials: ' + request.auth.credentials);
            }
        }
    });

Also, under bell/providers folder, create a linkedin.js file with the following content:

//
//  linkedin plugin bell/hapijs
//
//  Created by valvoline on 15/12/14.
//  Copyright (c) 2014 Costantino Pistagna. All rights reserved.
//
// Load modules

var Crypto = require('crypto');

// Declare internals

var internals = {};

exports = module.exports = function (options) {

    return {
    protocol: 'oauth2',
    auth: 'https://www.linkedin.com/uas/oauth2/authorization',
    token: 'https://www.linkedin.com/uas/oauth2/accessToken',
    scope: ['r_fullprofile', 'r_emailaddress', 'r_network', 'r_contactinfo', 'rw_groups'],
    scopeSeparator: ',',
    profile: function (credentials, params, get, callback) {
        var query = {
        format: 'json',
        appsecret_proof: Crypto.createHmac('sha256', this.clientSecret).update(credentials.token).digest('hex')
        };
        get('https://api.linkedin.com/v1/people/~:(id,first-name,last-name,industry,associations,interests,publications,patents,languages,skills,educations,three-current-positions,num-recommenders,following,suggestions)', query, function (profile) {

            credentials.profile = profile;
            return callback();
            });
    }
    };
};

Also, be sure to modify your bell/lib/providers/index.js file accordingly:

exports = module.exports = {
    linkedin: require('./linkedin'),
...

That's all! Happy OAuth2

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.