Skip to content

Commit

Permalink
starting migration to express 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed Nov 19, 2010
1 parent e5e1598 commit 4ed0ba8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 107 deletions.
75 changes: 37 additions & 38 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,25 @@ h3. /app.js
require.paths.unshift(__dirname + '/lib/support/express/lib')
require.paths.unshift(__dirname + '/lib/support/hashlib/build/default')

require('express')
require('express/plugins')

configure(function(){
use(MethodOverride)
use(ContentLength)
use(Cookie)
use(Session)
use(Logger)
use(require('facebook').Facebook, {
var exress = require('express')
var app = express.createServer();

app.configure(function(){
app.use(express.methodOverride())
app.use(ContentLength)
app.use(Cookie)
app.use(Session)
app.use(Logger)
app.use(require('facebook').Facebook(dx{
apiKey: 'e1249f7d4bc25b8f90e5c9c7523e3ee1',
apiSecret: '4ae45734dd66fa85c7b189fc2d7d5b4c'
})
set('root', __dirname)
app.set('root', __dirname)
})

// Called to get information about the current authenticated user
get('/fbSession', function(){
var fbSession = this.fbSession()
app.get('/fbSession', function(req, res){
var fbSession = req.fbSession()

if(fbSession) {
// Here would be a nice place to lookup userId in the database
Expand All @@ -95,30 +95,30 @@ get('/fbSession', function(){
})

// Called after a successful FB Connect
post('/fbSession', function() {
var fbSession = this.fbSession() // Will return null if verification was unsuccesful
app.post('/fbSession', function(req, res) {
var fbSession = req.fbSession() // Will return null if verification was unsuccesful

if(fbSession) {
// Now that we have a Facebook Session, we might want to store this new user in the db
// Also, in this.params there is additional information about the user (name, pic, first_name, etc)
// Note of warning: unlike fbSession, this additional information has not been verified
fbSession.first_name = this.params.post['first_name']
fbSession.first_name = req.params['first_name']
}

this.contentType('json')
this.halt(200, JSON.stringify(fbSession || {}))
})

// Called on Facebook logout
post('/fbLogout', function() {
this.fbLogout();
app.post('/fbLogout', function(req, res) {
req.fbLogout();
this.halt(200, JSON.stringify({}))
})

// Static files in ./public
get('/', function(file){ this.sendfile(__dirname + '/public/index.html') })
get('/xd_receiver.htm', function(file){ this.sendfile(__dirname + '/public/xd_receiver.htm') })
get('/javascripts/jquery.facebook.js', function(file){ this.sendfile(__dirname + '/public/javascripts/jquery.facebook.js') })
app.get('/', function(file){ this.sendfile(__dirname + '/public/index.html') })
app.get('/xd_receiver.htm', function(file){ this.sendfile(__dirname + '/public/xd_receiver.htm') })
app.get('/javascripts/jquery.facebook.js', function(file){ this.sendfile(__dirname + '/public/javascripts/jquery.facebook.js') })

run()

Expand Down Expand Up @@ -195,31 +195,30 @@ h3. /app.js
require.paths.unshift(__dirname + '/lib/support/express/lib')
require.paths.unshift(__dirname + '/lib/support/hashlib/build/default')

require('express')
require('express/plugins')
var express = require('express')
var app = express,createServer();

configure(function(){
use(MethodOverride)
use(ContentLength)
use(Cookie)
use(Session)
use(Logger)
use(require('facebook').Facebook, {
app.configure(function(){
app.use(express.methodOverride())
app.use(express.cookieDecoder())
app.use(express.session())
app.use(express.logger())
app.use(require('facebook').Facebook({
apiKey: 'e1249f7d4bc25b8f90e5c9c7523e3ee1',
apiSecret: '4ae45734dd66fa85c7b189fc2d7d5b4c'
})
set('root', __dirname)
express.staticProvider(__dirname + '/public')
})

// This is the canvas URL set in the Facebook Application settings
get('/iframe', function (){
var fbSession = this.fbSession() // Will create a session based on verified data from the GET params
app.get('/iframe', function(req, res) {
var fbSession = req.fbSession() // Will create a session based on verified data from the GET params
this.sendfile(__dirname + '/public/iframe.html')
})

// Called to get information about the current authenticated user
get('/fbSession', function(){
var fbSession = this.fbSession()
app.get('/fbSession', function(req, res){
var fbSession = req.fbSession()

if(fbSession) {
// Here would be a nice place to lookup userId in the database
Expand All @@ -232,10 +231,10 @@ get('/fbSession', function(){
})

// Static files in ./public
get('/xd_receiver.htm', function(file){ this.sendfile(__dirname + '/public/xd_receiver.htm') })
get('/javascripts/jquery.facebook.js', function(file){ this.sendfile(__dirname + '/public/javascripts/jquery.facebook.js') })
app.get('/xd_receiver.htm', function(file){ this.sendfile(__dirname + '/public/xd_receiver.htm') })
app.get('/javascripts/jquery.facebook.js', function(file){ this.sendfile(__dirname + '/public/javascripts/jquery.facebook.js') })

run()
app.listen(3000)

</code></pre>

Expand Down
120 changes: 51 additions & 69 deletions lib/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
sys = require('sys')
hashlib = require('hashlib')

exports.FBSession = Class({
init: function (userId) {
function FBSession(userId) {
this.userId = userId;
}
});
};

exports.getFingerprintForCookie = function (apiKey, cookies) {
var fields = ['expires', 'session_key', 'ss', 'user'];
Expand Down Expand Up @@ -38,85 +36,69 @@ exports.getFingerprintForParams = function (params) {

// --- Facebook

exports.Facebook = Plugin.extend({
extend: {

/**
* Initialize extensions.
*/

init: function(options) {
var apiKey = options['apiKey']
var apiSecret = options['apiSecret']

// --- Internal methods

Request.include({
exports.Facebook = function(options) {
options = options || {};
var apiKey = options['apiKey']
var apiSecret = options['apiSecret']

return function(req, res, next) {
/**
* Find or create Facebook session based on stored session, GET params or cookie
* Try authenticating by verifying Facebook data in GET params and cookie
*
* @param {hash} options
* @return {FBSession}
* @api public
*/
req.fbAuthenticate = function() {
var cookies = req.cookies;
var params = req.params;

// Get a fingerprint and signature
var fingerprint = null;
var signature = null;
if(cookies && cookies[apiKey]) {
fingerprint = exports.getFingerprintForCookie(apiKey, cookies)
signature = cookies[apiKey]
}
if(params && params['fb_sig']) {
fingerprint = exports.getFingerprintForParams(params)
signature = params['fb_sig']
}
if(!fingerprint)
return null;

// Verify signature using apiSecret
var expected_signature = hashlib.md5(fingerprint+apiSecret);
var valid = (expected_signature === signature)
if(!valid)
sys.puts("Warning, invalid signature: "+fingerprint)
return valid
};

fbSession: function(options) {
var session = this.session.fbSession;
if(session && session.userId)
return session;
if(this.fbAuthenticate()) {
var fbUserId = this.param('fb_sig_user') ? this.param('fb_sig_user') : this.cookie(apiKey + '_user')
this.session.fbSession = new exports.FBSession(fbUserId);
return this.session.fbSession;
}
return null;
},

/**
* Try authenticating by verifying Facebook data in GET params and cookie
*
* @param {hash} options
* Find or create Facebook session based on stored session, GET params or cookie
* @return {FBSession}
* @api public
*/

fbAuthenticate: function(options) {
var cookies = this.cookies;
var params = this.params.get;

// Get a fingerprint and signature
var fingerprint = null;
var signature = null;
if(cookies && cookies[apiKey]) {
fingerprint = exports.getFingerprintForCookie(apiKey, cookies)
signature = cookies[apiKey]
}
if(params && params['fb_sig']) {
fingerprint = exports.getFingerprintForParams(params)
signature = params['fb_sig']
}
if(!fingerprint)
req.fbSession = function() {
var session = req.session.fbSession;
if(session && session.userId)
return session;
if(req.fbAuthenticate()) {
var fbUserId = req.params['fb_sig_user'] ? req.params['fb_sig_user'] : req.cookies[apiKey + '_user']
req.session.fbSession = new FBSession(fbUserId);
return req.session.fbSession;
}
return null;

// Verify signature using apiSecret
var expected_signature = hashlib.md5(fingerprint+apiSecret);
var valid = (expected_signature === signature)
if(!valid)
sys.puts("Warning, invalid signature: "+fingerprint)
return valid
},

};
/**
* Logout
* @return null
* @api public
*/
fbLogout: function() {
this.session.fbSession = null
return null
},
})
}
}
})
req.fbLogout = function() {
req.session.fbSession = null
return null
};
next();
};
};

0 comments on commit 4ed0ba8

Please sign in to comment.