passport-drupal

Drupal authentication strategy for Passport.

npm install passport-drupal
1 downloads in the last day
14 downloads in the last week
21 downloads in the last month

Passport-Drupal

Passport strategy for authenticating with Drupal websites that use the Services and OAuth modules.

This module lets you authenticate using a Drupal website in your Node.js applications. By plugging into Passport, Drupal authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Installation

$ npm install passport-drupal

Usage

Configure Strategy

The Drupal OAuth authentication strategy authenticates users using a Drupal account and OAuth tokens. The strategy requires a verify callback, which accepts these credentials and calls done providing a user, as well as options specifying a consumer key, consumer secret, and callback URL.

passport.use(new Strategy({
    consumerKey: DRUPAL_CONSUMER_KEY,
    consumerSecret: DRUPAL_CONSUMER_SECRET,
    providerURL: "http://drupal.example.com",
    resourceEndpoint: "oauthlogin/api/user/info" // <---- optional. Defaults to `rest/system/connect`
  },
  function(token, tokenSecret, profile, done) {
    profile.oauth = { token: token, token_secret: tokenSecret };
    done(null, profile);
  }
));

Authenticate Requests

Use passport.authenticate(), specifying the 'drupal' strategy, to authenticate requests.

For example, as route middleware in an Express application:

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: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
});

If you want to store access tokens locally (e.g. in a persistent session store), you can add something like the following to prevent requesting new ones every time:

app.get('/auth/drupal/login', function(req, res, next) {
  var tokens = req.session['my-session-key'];
  if (req.cookies['my-cookie-key'] && tokens) {
    // If all the data is there, load user profile
    strategy.userProfile(tokens.oauth_token, tokens.oauth_token_secret, {}, function(err, user) {
      if (err) return res.redirect('/auth/drupal');
      req.session.user = user;
      res.redirect('/');
    });
  } else {
    // If not, start OAuth authentication
    res.redirect('/auth/drupal');
  }
});

Drupal configuration

These instructions are for Drupal 7. For Drupal 6, refer to this issue: https://github.com/mixmarket/passport-drupal/issues/1

  • Install the following modules:
  • Go to admin/config/services/oauth and check on Enable the oauth provider.
  • On that same page, click on Add Context
    • Under Signature Methods, check HMAC-SHA1 only
    • Under Authorization Options, check on Disable authorization level selection.
    • Add an Authorization Level and check on Selected by default.
  • Go to admin/structure/services and click Add
    • Enter an endpoint path as both your machine name and path (rest is a good option).
    • Select REST as your Server.
    • Under Authentication, check OAuth authentication.
    • Save
  • On the resulting page, click to Edit your new Service
    • On the Server tab
      • Under Response formatters, select json.
      • Under Request parsing, select application/json and application/x-www-form-urlencoded.
    • On the Authentication tab
      • OAuth context: select context created above
      • Default required OAuth Authorization level: select level created above
      • Default required authentication: Consumer key and access token, also known as 3-legged OAuth
    • On the Resources tab
      • Check on system > connect
      • Select Default for the authentication options
  • Go to user/1/oauth/consumer and click Add consumer
  • Go back to user/1/oauth/consumer and click Edit on the new consumer to view the consumer Key and Secret.

Credits

This module is developed by Victor Kareh and is heavily based on work by Jared Hanson

License

(The MIT License)

Copyright (c) 2011 Victor Kareh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you