Created
November 27, 2010 00:36
-
-
Save jonastemplestein/717404 to your computer and use it in GitHub Desktop.
Node middleware that decodes FB cookies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
crypto = require 'crypto' | |
class FBSession | |
constructor: (@app_id, @app_secret) -> | |
@state = 'logged_out' | |
initialize: (req) => | |
@req = req | |
req.fbSession = () => if @state is 'logged_in' then this else null | |
getSignature: (params) => | |
hash = crypto.createHash 'md5' | |
keys = Object.keys(params).sort() | |
payload = "" | |
payload += "#{key}=#{value}" for key, value of params | |
payload += @app_secret | |
hash.update payload | |
return hash.digest 'hex' | |
verifyFBSession: (session) => | |
verify_signature = session.sig | |
delete session.sig | |
return verify_signature is this.getSignature(session) | |
eatCookie: (cookie) => | |
return unless cookie | |
params = require('querystring').parse cookie | |
if this.verifyFBSession(params) | |
@state = 'logged_in' | |
@params = params | |
# Hook up this middleware and you're set | |
module.exports.cookieMiddleware = (fb_app_id, fb_app_secret) -> | |
return ((req, res, next) -> | |
fb_session = new FBSession(fb_app_id, fb_app_secret) | |
fb_session.initialize req | |
fb_session.eatCookie req.cookies['fbs_'+fb_app_id] | |
next() | |
) | |
module.exports.FBSession = FBSession |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment