Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First implementation of authentication using Passport

  • Loading branch information...
commit 87b200bdcf11a7f313c47201c8e4410bd782818d 1 parent 846e27a
@JanVanRyswyck authored
View
4 application.coffee
@@ -1,9 +1,7 @@
-bootstrapper = require('./bootstrapper')
+bootstrapper = require('./configuration/bootstrapper')
express = require('express')
require('./extensions')
-# TODO: Test
-bootstrapper.bootstrapAuthentication()
application = express.createServer()
application.configure ->
View
103 bootstrapper.coffee
@@ -1,103 +0,0 @@
-express = require('express')
-cradle = require('cradle')
-everyauth = require('everyauth')
-_ = require('underscore')
-configuration = require('./configuration')
-viewHelpers = require('./views/viewhelpers')
-errors = require('./errors')
-errorHandler = require('./errorhandler')
-
-routes = require('./routes')
-routes.runs = _.extend(require('./routes/runs'),
- require('./routes/runs/new'),
- require('./routes/runs/edit'))
-routes.shoes = _.extend(require('./routes/shoes'),
- require('./routes/shoes/new'),
- require('./routes/shoes/edit'))
-routes.options = require('./routes/options')
-
-validators = {}
-validators.run = require('./validators/run')
-validators.shoes = require('./validators/shoes')
-validators.options = require('./validators/options')
-
-
-exports.bootstrap = (application) ->
- # bootstrapAuthentication()
- bootstrapExpress(application)
- bootstrapRoutes(application)
- # TODO: Test
- # bootstrapErrorHandler(application)
- bootstrapCouchDB()
-
-exports.bootstrapAuthentication = () ->
- configuration.authenticationSettings((error, authentication) ->
- if error
- throw new errors.ConfigurationError('An error occured while reading the configuration settings for authenticating users.', error)
-
- everyauth.twitter
- .consumerKey(authentication.twitter.consumerKey)
- .consumerSecret(authentication.twitter.consumerSecret)
- .findOrCreateUser(
- (session, accessToken, accessTokenSecret, twitterUser) ->
- console.log twitterUserData
- console.log accessToken
- console.log accessTokenSecret
-
- return twitterUser
- # TODO: find user and if cannot be found, do not authenticate
- )
- .redirectPath('/')
- )
-
-bootstrapExpress = (application) ->
- application.use(express.bodyParser())
- application.use(express.methodOverride())
- application.use(express.cookieParser())
- application.use(express.session( secret: '498f99f3bbee4ae3a075eada02499464' ))
- application.use(everyauth.middleware())
- application.use(application.router)
- application.use(express.static(__dirname + '/public'))
- application.use(express.errorHandler())
-
- everyauth.helpExpress(application)
- application.helpers(viewHelpers)
-
- application.set('view engine', 'jade')
- application.set('showStackTrace', application.settings.env == 'development')
-
-bootstrapRoutes = (application) ->
- application.get('/', routes.index)
-
- application.post('/runs', validators.run.validate, routes.runs.create)
- application.get('/runs/new', routes.runs.new)
- application.get('/runs/:year([0-9]{4})?', routes.runs.index)
- application.put('/runs/:id([a-z0-9]{32})', validators.run.validate, routes.runs.update)
- application.get('/runs/:id([a-z0-9]{32})', routes.runs.edit)
-
- application.get('/shoes', routes.shoes.index)
- application.post('/shoes', validators.shoes.validate, routes.shoes.create)
- application.get('/shoes/new', routes.shoes.new)
- application.put('/shoes/:id([a-z0-9]{32})', validators.shoes.validate, routes.shoes.update)
- application.get('/shoes/:id([a-z0-9]{32})', routes.shoes.edit)
-
- application.get('/options', routes.options.index)
- application.put('/options/:id([a-z0-9]{32})', validators.options.validate, routes.options.update)
-
-bootstrapErrorHandler = (application) ->
- errorHandler.bootstrap(application)
-
-bootstrapCouchDB = ->
- configuration.couchDBSettings((error, couchDB) ->
- if error
- throw new errors.ConfigurationError('An error occured while reading the configuration settings for the CouchDB database.', error)
-
- cradle.setup(
- host: couchDB.url
- port: couchDB.port
- auth:
- username: couchDB.userName
- password: couchDB.password
- )
- )
-
View
44 configuration/authentication.coffee
@@ -0,0 +1,44 @@
+passport = require('passport')
+configuration = require('./configuration')
+errors = require('../errors')
+TwitterStrategy = require('passport-twitter').Strategy
+Users = require('../data/users')
+
+exports.bootstrap = ->
+ configuration.authenticationSettings((error, authentication) ->
+ if error
+ throw new errors.ConfigurationError('An error occured while reading the configuration settings for authenticating users.', error)
+
+ passport.use(new TwitterStrategy(
+ consumerKey: authentication.twitter.consumerKey
+ consumerSecret: authentication.twitter.consumerSecret
+ callbackURL: authentication.twitter.callbackUrl
+ , verifyUser))
+ )
+
+verifyUser = (token, tokenSecret, profile, done) ->
+ users = new Users()
+ users.getByName(profile.username, "twitter",
+ (error, user) ->
+ if error
+ return done(error)
+
+ if !user
+ return done(null, false)
+
+ done(null, user)
+ )
+
+passport.serializeUser((user, done) ->
+ done(null, user.id)
+)
+
+passport.deserializeUser((id, done) ->
+ users = new Users()
+ user = users.getById(id, (error, user) ->
+ if(error)
+ return done(error)
+
+ done(null, user)
+ )
+)
View
5 configuration/bootstrapper.coffee
@@ -0,0 +1,5 @@
+exports.bootstrap = (application) ->
+ require('./authentication').bootstrap()
+ require('./middleware').bootstrap(application)
+ require('./routes').bootstrap(application)
+ require('./couchdb').bootstrap()
View
3  configuration.coffee → configuration/configuration.coffee
@@ -7,8 +7,9 @@ module.exports = class Configuration
if @settings
return process.nextTick(=> callback(null, @settings))
- fileSystem.readFile('./config.json', 'utf8', (error, data) ->
+ fileSystem.readFile(__dirname + '/config.json', 'utf8', (error, data) ->
if error
+ console.log error
return process.nextTick(-> callback(error))
@settings = JSON.parse(data)
View
17 configuration/couchdb.coffee
@@ -0,0 +1,17 @@
+cradle = require('cradle')
+configuration = require('./configuration')
+errors = require('../errors')
+
+exports.bootstrap = ->
+ configuration.couchDBSettings((error, couchDB) ->
+ if error
+ throw new errors.ConfigurationError('An error occured while reading the configuration settings for the CouchDB database.', error)
+
+ cradle.setup(
+ host: couchDB.url
+ port: couchDB.port
+ auth:
+ username: couchDB.userName
+ password: couchDB.password
+ )
+ )
View
19 configuration/middleware.coffee
@@ -0,0 +1,19 @@
+express = require('express')
+passport = require('passport')
+viewHelpers = require('../views/viewhelpers')
+
+exports.bootstrap = (application) ->
+ application.use(express.bodyParser())
+ application.use(express.methodOverride())
+ application.use(express.cookieParser())
+ application.use(express.session( secret: '498f99f3bbee4ae3a075eada02499464' ))
+ application.use(passport.initialize())
+ application.use(passport.session())
+ application.use(application.router)
+ application.use(express.static(__dirname + '/../public'))
+ application.use(express.errorHandler())
+
+ application.helpers(viewHelpers)
+
+ application.set('view engine', 'jade')
+ application.set('showStackTrace', application.settings.env == 'development')
View
48 configuration/routes.coffee
@@ -0,0 +1,48 @@
+passport = require('passport')
+_ = require('underscore')
+errorHandler = require('../errorhandler')
+
+routes = require('../routes')
+routes.runs = _.extend(require('../routes/runs'),
+ require('../routes/runs/new'),
+ require('../routes/runs/edit'))
+routes.shoes = _.extend(require('../routes/shoes'),
+ require('../routes/shoes/new'),
+ require('../routes/shoes/edit'))
+routes.options = require('../routes/options')
+
+validators = {}
+validators.run = require('../validators/run')
+validators.shoes = require('../validators/shoes')
+validators.options = require('../validators/options')
+
+exports.bootstrap = (application) ->
+ bootstrapRoutes(application)
+ bootstrapErrorHandler(application)
+
+bootstrapRoutes = (application) ->
+ application.get('/', routes.index)
+
+ application.post('/runs', validators.run.validate, routes.runs.create)
+ application.get('/runs/new', routes.runs.new)
+ application.get('/runs/:year([0-9]{4})?', routes.runs.index)
+ application.put('/runs/:id([a-z0-9]{32})', validators.run.validate, routes.runs.update)
+ application.get('/runs/:id([a-z0-9]{32})', routes.runs.edit)
+
+ application.get('/shoes', routes.shoes.index)
+ application.post('/shoes', validators.shoes.validate, routes.shoes.create)
+ application.get('/shoes/new', routes.shoes.new)
+ application.put('/shoes/:id([a-z0-9]{32})', validators.shoes.validate, routes.shoes.update)
+ application.get('/shoes/:id([a-z0-9]{32})', routes.shoes.edit)
+
+ application.get('/options', routes.options.index)
+ application.put('/options/:id([a-z0-9]{32})', validators.options.validate, routes.options.update)
+
+ application.get('/auth/twitter', passport.authenticate('twitter'))
+ application.get('/auth/twitter/callback',
+ passport.authenticate('twitter', { successRedirect: '/', failureRedirect: '/auth/twitter' }))
+
+bootstrapErrorHandler = (application) ->
+ errorHandler.bootstrap(application)
+
+
View
3  data/shoes.coffee
@@ -28,7 +28,8 @@ module.exports = class Shoes
)
getShoesInUse: (callback) ->
- _database.view('shoes/inUse', { descending: true }, (error, response) ->
+ _database.view('shoes/inUse', { descending: true },
+ (error, response) ->
if error
return callback(error)
View
26 data/users.coffee
@@ -8,4 +8,28 @@ module.exports = class Shoes
connection = connectionManager.getConnection()
_database = connection.database('trackmyrun')
-
+ getById: (id, callback) ->
+ _database.get(id, (error, response) ->
+ if error
+ return callback(error)
+
+ shoes = mapFrom(response)
+ callback(null, shoes)
+ )
+
+ getByName: (name, authority, callback) ->
+ _database.view('users/usersByName', { key: [name, authority] },
+ (error, response) ->
+ if error
+ return callback(error)
+
+ if(0 == response.length)
+ return callback(error, null)
+
+ user = mapFrom(response[0].value)
+ callback(null, user)
+ )
+
+ mapFrom = (document) ->
+ id: document._id
+ displayName: document.displayName
View
14 data/views.js
@@ -45,4 +45,18 @@
"map": "function(document) {\n if(document.type === 'options') \t\n emit(document._id, document);\n}"
}
}
+}
+
+// *********************************************************************
+// Users
+// *********************************************************************
+{
+ "_id": "_design/users",
+ "_rev": "1-dfd98898fdcf061822396d4fe419ae77",
+ "language": "javascript",
+ "views": {
+ "usersByName": {
+ "map": "function(document) {\n if(document.type === 'user')\n emit([document.name, document.authority], document);\n}"
+ }
+ }
}
View
3  package.json
@@ -16,6 +16,7 @@
"step": "0.0.5",
"underscore": "1.2.x",
"cradle": "0.5.x",
- "everyauth": "0.2.x"
+ "passport": "0.1.x",
+ "passport-twitter": "0.1.x"
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.