Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed May 5, 2012
0 parents commit 48604f2
Show file tree
Hide file tree
Showing 17 changed files with 547 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store
node_modules
8 changes: 8 additions & 0 deletions .npmignore
@@ -0,0 +1,8 @@
*.md
.DS_Store
.git*
Makefile
docs/
examples/
support/
test/
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: "node_js"
node_js:
- 0.4
- 0.6
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
(The MIT License)

Copyright (c) 2012 Jared Hanson

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.
19 changes: 19 additions & 0 deletions Makefile
@@ -0,0 +1,19 @@
NODE = node
TEST = ./node_modules/.bin/vows
TESTS ?= test/*-test.js

test:
@NODE_ENV=test NODE_PATH=lib $(TEST) $(TEST_FLAGS) $(TESTS)

docs: docs/api.html

docs/api.html: lib/passport-intuit-oauth/*.js
dox \
--title Passport-Intuit-OAuth \
--desc "Intuit (OAuth) authentication strategy for Passport" \
$(shell find lib/passport-intuit-oauth/* -type f) > $@

docclean:
rm -f docs/*.{1,html}

.PHONY: test docs docclean
95 changes: 95 additions & 0 deletions README.md
@@ -0,0 +1,95 @@
# Passport-Intuit

[Passport](http://passportjs.org/) strategy for authenticating with [Intuit](http://www.intuit.com/)
using the OAuth 1.0a API.

This module lets you authenticate using Intuit in your Node.js applications.
By plugging into Passport, Intuit authentication can be easily and
unobtrusively integrated into any application or framework that supports
[Connect](http://www.senchalabs.org/connect/)-style middleware, including
[Express](http://expressjs.com/).

## Installation

$ npm install passport-intuit-oauth

## Usage

#### Configure Strategy

The Intuit authentication strategy authenticates users using a Intuit
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 IntuitStrategy({
consumerKey: INTUIT_CONSUMER_KEY,
consumerSecret: INTUIT_CONSUMER_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/intuit/callback"
},
function(token, tokenSecret, profile, done) {
User.findOrCreate({ intuitId: profile.id }, function (err, user) {
return done(err, user);
});
}
));

#### Authenticate Requests

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

For example, as route middleware in an [Express](http://expressjs.com/)
application:

app.get('/auth/intuit',
passport.authenticate('intuit'),
function(req, res){
// The request will be redirected to Intuit for authentication, so
// this function will not be called.
});

app.get('/auth/intuit/callback',
passport.authenticate('intuit', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});

## Examples

For a complete, working example, refer to the [login example](https://github.com/jaredhanson/passport-intuit-oauth/tree/master/examples/login).

## Tests

$ npm install --dev
$ make test

[![Build Status](https://secure.travis-ci.org/jaredhanson/passport-intuit-oauth.png)](http://travis-ci.org/jaredhanson/passport-intuit-oauth)

## Credits

- [Jared Hanson](http://github.com/jaredhanson)

## License

(The MIT License)

Copyright (c) 2011 Jared Hanson

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.
122 changes: 122 additions & 0 deletions examples/login/app.js
@@ -0,0 +1,122 @@
var express = require('express')
, passport = require('passport')
, util = require('util')
, IntuitStrategy = require('passport-intuit-oauth').Strategy;

var INTUIT_CONSUMER_KEY = "--insert-intuit-consumer-key-here--"
var INTUIT_CONSUMER_SECRET = "--insert-intuit-consumer-secret-here--";


// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete Intuit profile is
// serialized and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});

passport.deserializeUser(function(obj, done) {
done(null, obj);
});


// Use the IntuitStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a token, tokenSecret, and Intuit profile), and
// invoke a callback with a user object.
passport.use(new IntuitStrategy({
consumerKey: INTUIT_CONSUMER_KEY,
consumerSecret: INTUIT_CONSUMER_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/intuit/callback"
},
function(token, tokenSecret, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {

// To keep the example simple, the user's Intuit profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Intuit account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}
));




var app = express.createServer();

// configure Express
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});


app.get('/', function(req, res){
res.render('index', { user: req.user });
});

app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});

app.get('/login', function(req, res){
res.render('login', { user: req.user });
});

// GET /auth/intuit
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Intuit authentication will involve
// redirecting the user to intuit.com. After authorization, Intuit will
// redirect the user back to this application at /auth/intuit/callback
app.get('/auth/intuit',
passport.authenticate('intuit'),
function(req, res){
// The request will be redirected to Intuit for authentication, so this
// function will not be called.
});

// GET /auth/intuit/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/intuit/callback',
passport.authenticate('intuit', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});

app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});

app.listen(3000);


// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
10 changes: 10 additions & 0 deletions examples/login/package.json
@@ -0,0 +1,10 @@
{
"name": "passport-intuit-examples-login",
"version": "0.0.0",
"dependencies": {
"express": ">= 0.0.0",
"ejs": ">= 0.0.0",
"passport": ">= 0.0.0",
"passport-intuit-oauth": ">= 0.0.0"
}
}
2 changes: 2 additions & 0 deletions examples/login/views/account.ejs
@@ -0,0 +1,2 @@
<p>realmId: <%= user.realmId %></p>
<p>dataSource: <%= user.dataSource %></p>
5 changes: 5 additions & 0 deletions examples/login/views/index.ejs
@@ -0,0 +1,5 @@
<% if (!user) { %>
<h2>Welcome! Please log in.</h2>
<% } else { %>
<h2>Hello, <%= user.realmId %>.</h2>
<% } %>
21 changes: 21 additions & 0 deletions examples/login/views/layout.ejs
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Passport-Intuit Example</title>
</head>
<body>
<% if (!user) { %>
<p>
<a href="/">Home</a> |
<a href="/login">Log In</a>
</p>
<% } else { %>
<p>
<a href="/">Home</a> |
<a href="/account">Account</a> |
<a href="/logout">Log Out</a>
</p>
<% } %>
<%- body %>
</body>
</html>
1 change: 1 addition & 0 deletions examples/login/views/login.ejs
@@ -0,0 +1 @@
<a href="/auth/intuit">Login with Intuit</a>
15 changes: 15 additions & 0 deletions lib/passport-intuit-oauth/index.js
@@ -0,0 +1,15 @@
/**
* Module dependencies.
*/
var Strategy = require('./strategy');


/**
* Framework version.
*/
require('pkginfo')(module, 'version');

/**
* Expose constructors.
*/
exports.Strategy = Strategy;

0 comments on commit 48604f2

Please sign in to comment.