Search code examples
javascriptnode.jsdrupal-7passport.js

Passport.js Module, undefined callbackURL


After setting up the Drupal as this guide says: Drupal-passport I created a simple simple node app to test how it works.

It doesn't, I get the InternalOAuthError: Failed to obtain request token error.

Going through the strategy.js, I saw that my callbackURL is logging out undefined not exactly sure why. The callbackURL is set in my Drupal app

Also preforming a curl -i -XPOST http://extranet.local/rest/system/connect/ gives me exactly what I need

Here is my node.js code (keep in mind this is just supposed to test the drupal set up).

var express = require('express');
var passport = require('passport');
var dStrategy = require('passport-drupal').DrupalStrategy;
var passportDrupal = require('passport-drupal');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser')
var session = require('express-session');
var http = require('http');

var app = express();
var server = http.createServer(app);

app.use(cookieParser());
app.use(bodyParser());
app.use(session({ secret: 'SECRET' }));
app.use(passport.initialize());
app.use(passport.session());

passport.use(new dStrategy({
    consumerKey: "emDVp7P2LZFLPcN3cNCjLmrjrhQLnNv6",
    consumerSecret: "mkbc3UYEuUQLNQRwLWo3B8zEk4ZrErKa",
    providerURL: "http://extranet.local",
    resourceEndpoint: "rest/system/connect", // <---- optional. Defaults to `rest/system/connect`
    callbackURL: 'http://33.33.33.40:8888/auth/drupal/callback'
  },
  function(token, tokenSecret, profile, done) {
    profile.oauth = { token: token, token_secret: tokenSecret };
    done(null, profile);
  }
));
app.get('/', function(req, res) {
  res.writeHead(200);
  res.end("This is root");
});
app.get('/auth/drupal',
  passport.authenticate('drupal'),
  function(req, res) {
    // The request will be redirected to the Drupal website for
    // authentication, so this function will not be called.
});
app.get('/auth/drupal/callback',
  passport.authenticate('drupal', { failureRedirect: '/error' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/signedin');
});

app.get('/error', function(req, res) {
  res.writeHead(200);
  res.end("Could not sign in");
});
app.get('/signedin', function(req, res) {
  res.writeHead(200);
  res.end("signed in");
});

server.listen(8888, '33.33.33.40');

Any clues as to why or ideas are greatly appreciated


Solution

  • If you look into the strategy.js code of the library passport-drupal, you will see that the DrupalStrategy constructor does not expect a callbackURL property in the options parameter object and it also does not pass it further into the OAuthStrategy.

    This is the code snippet that creates the parameter for the oauth strategy:

      // Determine all necessary OAuth options
      var oauthOptions = {
        requestTokenURL: this._providerURL + '/oauth/request_token',
        accessTokenURL: this._providerURL + '/oauth/access_token',
        userAuthorizationURL: this._providerURL + '/oauth/authorize',
        consumerKey: options.consumerKey,
        consumerSecret: options.consumerSecret
      };
    
      OAuthStrategy.call(this, oauthOptions, verify);
    

    It should be modified to pass the callbackURL, for example like this:

      // Determine all necessary OAuth options
      var oauthOptions = {
        requestTokenURL: this._providerURL + '/oauth/request_token',
        accessTokenURL: this._providerURL + '/oauth/access_token',
        userAuthorizationURL: this._providerURL + '/oauth/authorize',
        consumerKey: options.consumerKey,
        consumerSecret: options.consumerSecret,
        callbackURL: options.callbackURL// <==== THIS LINE WAS ADDED
      };
    
      OAuthStrategy.call(this, oauthOptions, verify);
    

    I'm not sure that will solve your issue though. But I made a pull request