node package manager

oauthware

Connect.js middleware for OAuth 1.0a / 2.0

OAuthware

OAuthware is an authentication library for Node.js, Connect.js and Express.js.

OAuthware supports simple authentication of Twitter, Facebook, Google, and other many providers using OAuth 1.0a/2.0.

Installation

$ npm install oauthware

Usage

OAuthware.createServer()

OAuthware is middleware of Connect.js and Express.js. Using as handle of HTTP Server.

var connect = require('connect),
    oauthware = require('oauthware');

connect.createServer(
  connect.cookieParser(),
  connect.session({secret: 'oauthware'}),

  oauthware.createServer(
    oauthware.twitter({
      consumerKey: TWITTER_CONSUMER_KEY,
      consumerSecret: TWITTER_CONSUMER_SECRET
    }),
    oauthware.facebook({
      clientId: FACEBOOK_APP_ID,
      clientSecret: FACEBOOK_APP_SECRET
    }),
    oauthware.google({
      clientId: GOOGLE_APP_ID,
      clientSecret: GOOGLE_APP_SECRET
    }),
  )
);

The function createServer() of OAuthware will work just like connect.createServer() !

var oauth = oauthware.createServer()

oauth.use('/auth/twitter', oauthware.twitter({
  consumerKey: TWITTER_CONSUMER_KEY,
  consumerSecret: TWITTER_CONSUMER_SECRET
}))

oauth.use('/auth/facebook', oauthware.facebook({
  clientId: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET
});

The createServer() has more enhanced options than those of Connect/Express.

oauthware.createServer({
    host: 'http://oauthware.org:3000/',
    route: '/auth',
  },
  ['/twitter', oauthware.twitter(..)],
  ['/facebook', oauthware.facebook(..)]
);

The server of OAuthware is also a server of Connect. This uses is possible.

oauthware.createServer(

  connect.logger(),
  connect.cookieParser(),
  connect.session({key: 'oauthware'}),

  ['/twitter', oauthware.twitter(..)],
  ['/facebook', oauthware.facebook(..)],

  connect.static(__dirname + '/public'))

).listen(3000);

Middleware of OAuthware

OAuthware.middleware is lists of authentication providers.

Object.keys(require('oauthware').middleware)

=> ['facebook', 'twitter', 'linkedin', 'github', ...]

Middleware of OAuthware will create a handle of HTTP Server

var twitter = oauthware.twitter({
  consumerKey: TWITTER_CONSUMER_KEY,
  consumerSecret: TWITTER_CONSUMER_SECRET
});

var facebook = oauthware.facebook({
  clientId: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET
});

connect.createServer(
  connect.cookieParser(),
  connect.session({secret: 'oauthware'}),
  twitter,
  facebook
);

User can configure prefer path of URL

var twitter = oauthware.twitter({
  consumerKey: TWITTER_CONSUMER_KEY,
  consumerSecret: TWITTER_CONSUMER_SECRET
});

var handle = twitter.router(function (app) {
  // default paths
  // this.path['auth'] = '/twitter/auth';
  // this.path['login'] = '/twitter/login';
  // this.path['logout'] = '/twitter/logout';
  // this.path['api'] = '/twitter/api';

  app.get(this.path['auth'], this.handle['auth']);
  app.get(this.path['login'], this.handle['login']);
  app.get(this.path['logout'], this.handle['logout']);
  app.get(this.path['api'], this.handle['api']);
  app.put(this.path['api'], this.handle['api']);
  app.post(this.path['api'], this.handle['api']);
  app.delete(this.path['api'], this.handle['api']);
});

Default routing path is added when it is created.

// jQuery, client side JavaScript
$(function() {

  $('#login').click(function() {
    windows.location = '/twitter/login';
  });

  $('#logout').click(function() {
    windows.location = '/twitter/logout';
  });

  $('#me').click(function() {
    $.get('/twitter/api/me', function(res) {
      $('#me-info').text(JSON.stringify(res));
    }, 'json');
  });

});

Authentication API

var twitter = oauthware.twitter({
  consumerKey: TWITTER_CONSUMER_KEY,
  consumerSecret: TWITTER_CONSUMER_SECRET
});

connect.router(function (app) {
  app.get('/twitter/auth', twitter.authenticate);
  app.get('/twitter/signin', twitter.signIn);
  app.get('/twitter/signOut', twitter.signOut);
  app.get('/twitter/rest', twitter.rest);
});

Examples

Provider

ProviderDescriptionDeveloper
TwitterTwitter authenticationLuuvish
FacebookFacebook authenticationLuuvish
LinkedinLinkedin authenticationLuuvish
GithubGithub authenticationLuuvish

Credits

License

(The MIT License)

Copyright © 2011 CodeHolics Inc.
Copyright © 2011 Luuvish

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 OTHERS DEALINGS IN THE SOFTWARE.