From 18eb7788902d253bacb5e424f4af9f6a5d062d01 Mon Sep 17 00:00:00 2001 From: Ron Sim Date: Sun, 4 Oct 2015 11:33:50 -0700 Subject: [PATCH] added the OAuth --- node_modules/passport/README.md | 22 ++-- node_modules/passport/init.js | 46 ------- node_modules/passport/lib/authenticator.js | 18 ++- node_modules/passport/lib/http/request.js | 7 -- .../passport/lib/middleware/initialize.js | 19 ++- .../passport/lib/strategies/session.js | 8 +- node_modules/passport/login.js | 80 ------------ node_modules/passport/package.json | 16 +-- node_modules/passport/signup.js | 116 ------------------ 9 files changed, 48 insertions(+), 284 deletions(-) delete mode 100644 node_modules/passport/init.js delete mode 100644 node_modules/passport/login.js delete mode 100644 node_modules/passport/signup.js diff --git a/node_modules/passport/README.md b/node_modules/passport/README.md index c739064..d95b7d0 100644 --- a/node_modules/passport/README.md +++ b/node_modules/passport/README.md @@ -14,7 +14,7 @@ middleware for [Node.js](http://nodejs.org/). Passport's sole purpose is to authenticate requests, which it does through an extensible set of plugins known as _strategies_. Passport does not mount -routes or assume any particular database schema, which maximizes flexibility and +routes or assume any particular database schema, which maximizes flexiblity and allows application-level decisions to be made by the developer. The API is simple: you provide Passport a request to authenticate, and Passport provides hooks for controlling what occurs when authentication succeeds or fails. @@ -78,13 +78,15 @@ with the required `passport.initialize()` middleware. If your application uses persistent login sessions (recommended, but not required), `passport.session()` middleware must also be used. - var app = express(); - app.use(require('serve-static')(__dirname + '/../../public')); - app.use(require('cookie-parser')()); - app.use(require('body-parser').urlencoded({ extended: true })); - app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true })); - app.use(passport.initialize()); - app.use(passport.session()); + app.configure(function() { + app.use(express.static(__dirname + '/../../public')); + app.use(express.cookieParser()); + app.use(express.bodyParser()); + app.use(express.session({ secret: 'keyboard cat' })); + app.use(passport.initialize()); + app.use(passport.session()); + app.use(app.router); + }); #### Authenticate Requests @@ -120,8 +122,8 @@ The following table lists commonly used strategies: ## Examples -- For a complete, working example, refer to the [example](https://github.com/passport/express-4.x-local-example) -that uses [passport-local](https://github.com/jaredhanson/passport-local). +- For a complete, working example, refer to the [login example](https://github.com/jaredhanson/passport-local/tree/master/examples/login) +included in [passport-local](https://github.com/jaredhanson/passport-local). - **Local Strategy**: Refer to the following tutorials for setting up user authentication via LocalStrategy (`passport-local`) - Express v3x - [Tutorial](http://mherman.org/blog/2013/11/11/user-authentication-with-passport-dot-js/) / [working example](https://github.com/mjhea0/passport-local) - Express v4x - [Tutorial](http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/) / [working example](https://github.com/mjhea0/passport-local-express4) diff --git a/node_modules/passport/init.js b/node_modules/passport/init.js deleted file mode 100644 index 13c94ee..0000000 --- a/node_modules/passport/init.js +++ /dev/null @@ -1,46 +0,0 @@ -var login = require('./login'); -var signup = require('./signup'); -var OrgUser = require('../models/organisation'); -var Sponsor = require('../models/sponsor'); - -module.exports = function(passport){ - - // Passport needs to be able to serialize and deserialize users to support persistent login sessions - passport.serializeUser(function(orguser, done) { - console.log('serializing orguser: ');console.log(orguser); - done(null, orguser._id); - }); - - passport.deserializeUser(function(id, done) { - OrgUser.findById(id, function(err, orguser) { - console.log('deserializing orguser:',orguser); - done(err, orguser); - }); - }); - - // Setting up Passport Strategies for Login and SignUp/Registration - login(passport); - signup(passport); - -} - -module.exports = function(passport){ - - // Passport needs to be able to serialize and deserialize users to support persistent login sessions - passport.serializeSponsor(function(sponsor, done) { - console.log('serializing sponsor: ');console.log(sponsor); - done(null, sponsor._id); - }); - - passport.deserializeSponsor(function(id, done) { - Sponsor.findById(id, function(err, sponsor) { - console.log('deserializing sponsor:',sponsor); - done(err, sponsor); - }); - }); - - // Setting up Passport Strategies for Login and SignUp/Registration - login(passport); - signup(passport); - -} \ No newline at end of file diff --git a/node_modules/passport/lib/authenticator.js b/node_modules/passport/lib/authenticator.js index 43b870b..6bcce55 100644 --- a/node_modules/passport/lib/authenticator.js +++ b/node_modules/passport/lib/authenticator.js @@ -116,9 +116,13 @@ Authenticator.prototype.framework = function(fw) { * * Examples: * - * app.use(passport.initialize()); + * app.configure(function() { + * app.use(passport.initialize()); + * }); * - * app.use(passport.initialize({ userProperty: 'currentUser' })); + * app.configure(function() { + * app.use(passport.initialize({ userProperty: 'currentUser' })); + * }); * * @param {Object} options * @return {Function} middleware @@ -211,10 +215,12 @@ Authenticator.prototype.authorize = function(strategy, options, callback) { * * Examples: * - * app.use(connect.cookieParser()); - * app.use(connect.session({ secret: 'keyboard cat' })); - * app.use(passport.initialize()); - * app.use(passport.session()); + * app.configure(function() { + * app.use(connect.cookieParser()); + * app.use(connect.session({ secret: 'keyboard cat' })); + * app.use(passport.initialize()); + * app.use(passport.session()); + * }); * * Options: * - `pauseStream` Pause the request stream before deserializing the user diff --git a/node_modules/passport/lib/http/request.js b/node_modules/passport/lib/http/request.js index 757424c..4e97901 100644 --- a/node_modules/passport/lib/http/request.js +++ b/node_modules/passport/lib/http/request.js @@ -47,14 +47,7 @@ req.logIn = function(user, options, done) { var self = this; this._passport.instance.serializeUser(user, this, function(err, obj) { if (err) { self[property] = null; return done(err); } - if (!self._passport.session) { - self._passport.session = {}; - } self._passport.session.user = obj; - if (!self.session) { - self.session = {}; - } - self.session[self._passport.instance._key] = self._passport.session; done(); }); } else { diff --git a/node_modules/passport/lib/middleware/initialize.js b/node_modules/passport/lib/middleware/initialize.js index 53ce3d8..0826955 100644 --- a/node_modules/passport/lib/middleware/initialize.js +++ b/node_modules/passport/lib/middleware/initialize.js @@ -21,10 +21,12 @@ * * Examples: * - * app.use(connect.cookieParser()); - * app.use(connect.session({ secret: 'keyboard cat' })); - * app.use(passport.initialize()); - * app.use(passport.session()); + * app.configure(function() { + * app.use(connect.cookieParser()); + * app.use(connect.session({ secret: 'keyboard cat' })); + * app.use(passport.initialize()); + * app.use(passport.session()); + * }); * * passport.serializeUser(function(user, done) { * done(null, user.id); @@ -48,8 +50,15 @@ module.exports = function initialize(passport) { if (req.session && req.session[passport._key]) { // load data from existing session req._passport.session = req.session[passport._key]; + } else if (req.session) { + // initialize new session + req.session[passport._key] = {}; + req._passport.session = req.session[passport._key]; + } else { + // no session is available + req._passport.session = {}; } - + next(); }; }; diff --git a/node_modules/passport/lib/strategies/session.js b/node_modules/passport/lib/strategies/session.js index f2db338..a14063b 100644 --- a/node_modules/passport/lib/strategies/session.js +++ b/node_modules/passport/lib/strategies/session.js @@ -38,12 +38,8 @@ SessionStrategy.prototype.authenticate = function(req, options) { if (!req._passport) { return this.error(new Error('passport.initialize() middleware not in use')); } options = options || {}; - var self = this, - su; - if (req._passport.session) { - su = req._passport.session.user; - } - + var self = this + , su = req._passport.session.user; if (su || su === 0) { // NOTE: Stream pausing is desirable in the case where later middleware is // listening for events emitted from request. For discussion on the diff --git a/node_modules/passport/login.js b/node_modules/passport/login.js deleted file mode 100644 index be02863..0000000 --- a/node_modules/passport/login.js +++ /dev/null @@ -1,80 +0,0 @@ -var LocalStrategy = require('passport-local').Strategy; -var OrgUser = require('../models/organisation'); -var Sponsor = require('../models/sponsor'); -var bCrypt = require('bcrypt-nodejs'); - -module.exports = function(passport){ - - passport.use('login', new LocalStrategy({ - passReqToCallback : true - }, - function(req, username, password, done) { - // check in mongo if a user with username exists or not - OrgUser.findOne({ 'username' : username }, - function(err, user) { - // In case of any error, return using the done method - if (err) - return done(err); - // Username does not exist, log the error and redirect back - if (!orguser){ - console.log('Organisation Not Found with username '+username); - return done(null, false, req.flash('message', 'Organisation Not found.')); - } - // User exists but wrong password, log the error - if (!isValidPassword(orguser, password)){ - console.log('Invalid Password'); - return done(null, false, req.flash('message', 'Invalid Password')); // redirect back to login page - } - // User and password both match, return user from done method - // which will be treated like success - return done(null, orguser); - } - ); - - }) - ); - - - var isValidPassword = function(orguser, password){ - return bCrypt.compareSync(password, orguser.password); - } - -} - -module.exports = function(passport){ - - passport.use('login', new LocalStrategy({ - passReqToCallback : true - }, - function(req, username, password, done) { - // check in mongo if a user with username exists or not - Sponsor.findOne({ 'username' : username }, - function(err, user) { - // In case of any error, return using the done method - if (err) - return done(err); - // Username does not exist, log the error and redirect back - if (!sponsor){ - console.log('Sponsor Not Found with username '+username); - return done(null, false, req.flash('message', 'Sponsor Not found.')); - } - // User exists but wrong password, log the error - if (!isValidPassword(sponsor, password)){ - console.log('Invalid Password'); - return done(null, false, req.flash('message', 'Invalid Password')); // redirect back to login page - } - // User and password both match, return user from done method - // which will be treated like success - return done(null, sponsor); - } - ); - - }) - ); - - - var isValidPassword = function(sponsor, password){ - return bCrypt.compareSync(password, sponsor.password); - } - -} \ No newline at end of file diff --git a/node_modules/passport/package.json b/node_modules/passport/package.json index d522d0c..2d08bba 100644 --- a/node_modules/passport/package.json +++ b/node_modules/passport/package.json @@ -1,6 +1,6 @@ { "name": "passport", - "version": "0.3.0", + "version": "0.2.2", "description": "Simple, unobtrusive authentication for Node.js.", "keywords": [ "express", @@ -47,10 +47,10 @@ "scripts": { "test": "mocha --reporter spec --require test/bootstrap/node test/*.test.js test/**/*.test.js" }, - "gitHead": "aa7420756c2c4d430835c3a694c0281343133bb9", - "_id": "passport@0.3.0", - "_shasum": "14c151b0eb6795aa9335239827f548d5f94c7046", - "_from": "passport@*", + "gitHead": "bc68044826ce97e9ac1afa97cf71bce64033858f", + "_id": "passport@0.2.2", + "_shasum": "9c38f17beb929f3d81af7b8838e8430db8703f2b", + "_from": "passport@>=0.2.0 <0.3.0", "_npmVersion": "1.4.23", "_npmUser": { "name": "jaredhanson", @@ -63,10 +63,10 @@ } ], "dist": { - "shasum": "14c151b0eb6795aa9335239827f548d5f94c7046", - "tarball": "http://registry.npmjs.org/passport/-/passport-0.3.0.tgz" + "shasum": "9c38f17beb929f3d81af7b8838e8430db8703f2b", + "tarball": "http://registry.npmjs.org/passport/-/passport-0.2.2.tgz" }, "directories": {}, - "_resolved": "https://registry.npmjs.org/passport/-/passport-0.3.0.tgz", + "_resolved": "https://registry.npmjs.org/passport/-/passport-0.2.2.tgz", "readme": "ERROR: No README data found!" } diff --git a/node_modules/passport/signup.js b/node_modules/passport/signup.js deleted file mode 100644 index 2217aa1..0000000 --- a/node_modules/passport/signup.js +++ /dev/null @@ -1,116 +0,0 @@ -var LocalStrategy = require('passport-local').Strategy; -var OrgUser = require('../models/organisation'); -var Sponsor = require('../models/sponsor'); -var bCrypt = require('bcrypt-nodejs'); - -module.exports = function(passport){ - - passport.use('signup', new LocalStrategy({ - passReqToCallback : true // allows us to pass back the entire request to the callback - }, - function(req, username, password, done) { - - findOrCreateOrgUser = function(){ - // find a user in Mongo with provided username - OrgUser.findOne({ 'username' : username }, function(err, orguser) { - // In case of any error, return using the done method - if (err){ - console.log('Error in SignUp: '+err); - return done(err); - } - // already exists - if (orguser) { - console.log('Organisation already exists with username: '+username); - return done(null, false, req.flash('message','User Already Exists')); - } else { - // if there is no user with that email - // create the user - var newOrgUser = new OrgUser(); - - // set the user's local credentials - newOrgUser.username = username; - newOrgUser.password = createHash(password); - newOrgUser.email = req.param('email'); - newOrgUser.firstName = req.param('firstName'); - newOrgUser.lastName = req.param('lastName'); - - // save the user - newOrgUser.save(function(err) { - if (err){ - console.log('Error in Saving organisation: '+err); - throw err; - } - console.log('Organisation Registration succesful'); - return done(null, newOrgUser); - }); - } - }); - }; - // Delay the execution of findOrCreateUser and execute the method - // in the next tick of the event loop - process.nextTick(findOrCreateOrgUser); - }) - ); - - // Generates hash using bCrypt - var createHash = function(password){ - return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null); - } - -} - -module.exports = function(passport){ - - passport.use('signup', new LocalStrategy({ - passReqToCallback : true // allows us to pass back the entire request to the callback - }, - function(req, username, password, done) { - - findOrCreateSponsor = function(){ - // find a user in Mongo with provided username - Sponsor.findOne({ 'username' : username }, function(err, user) { - // In case of any error, return using the done method - if (err){ - console.log('Error in SignUp: '+err); - return done(err); - } - // already exists - if (sponsor) { - console.log('User already exists with username: '+username); - return done(null, false, req.flash('message','Sponsor Already Exists')); - } else { - // if there is no user with that email - // create the user - var newSponsor = new Sponsor(); - - // set the user's local credentials - newSponsor.username = username; - newSponsor.password = createHash(password); - newSponsor.email = req.param('email'); - newSponsor.firstName = req.param('firstName'); - newSponsor.lastName = req.param('lastName'); - - // save the user - newSponsor.save(function(err) { - if (err){ - console.log('Error in Saving user: '+err); - throw err; - } - console.log('User Registration succesful'); - return done(null, newSponsor); - }); - } - }); - }; - // Delay the execution of findOrCreateUser and execute the method - // in the next tick of the event loop - process.nextTick(findOrCreateSponsor); - }) - ); - - // Generates hash using bCrypt - var createHash = function(password){ - return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null); - } - -} \ No newline at end of file