Permalink
Browse files

trying to get package.json to work

  • Loading branch information...
1 parent 43d1435 commit 27ca4fde813b4e83e57685a2d5e20d8baceadba7 Jonas Huckestein committed Jan 26, 2011
Showing with 78 additions and 1 deletion.
  1. +75 −0 _facebook-session-cookie.coffee
  2. +2 −0 facebook-session-cookie.js
  3. +1 −1 package.json
@@ -0,0 +1,75 @@
+crypto = require 'crypto'
+
+class FBSession
+
+ constructor: (@app_id, @app_secret, @domain) ->
+ @state = 'logged_out'
+
+ initialize: (req) =>
+ @req = req
+ @_eatCookie req
+ req.fb_session = if @isLoggedIn() then this else null
+
+ getId: => @params?.uid
+ getAccessToken: => @params?.access_token
+ getParams: => @params
+
+ # TODO this doesn't log the user out of facebook so
+ # for now let's keep the logout shit on the client side
+ # logout: () =>
+ # res.setCookie 'fbs_'+@app_id, '',
+ # domain: @domain,
+ # expires: new Date( new Date().getTime() - 30 * 24 * 60 * 60 * 1000 )
+
+ isLoggedIn: -> @state is 'logged_in'
+
+ _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 @_getSignature(session)
+
+ # taken from connect's cookieDecoder middleware.
+ _getCookies: (req) =>
+ cookies = {}
+ header = req.headers.cookie
+ return cookies unless header
+ pairs = header.split /[;,] */
+ for pair in pairs
+ eqlIndex = pair.indexOf '='
+ key = pair.substr(0, eqlIndex).trim().toLowerCase()
+ val = pair.substr(++eqlIndex, pair.length).trim()
+ if val[0] is '"'
+ val = val.slice(1, -1)
+ if cookies[key] is undefined
+ cookies[key] = require('querystring').unescape(val, true)
+ return cookies
+
+ _eatCookie: (req) =>
+ cookies = req.cookies or @_getCookies req
+ cookie = cookies["fbs_#{@app_id}"]
+ return false unless cookie
+ params = require('querystring').parse cookie
+ if @_verifyFBSession(params)
+ @state = 'logged_in'
+ @params = params
+ return true
+ else return false
+
+# Hook up this middleware and you're set
+module.exports = (fb_app_id, fb_app_secret, domain) ->
+ return ((req, res, next) ->
+ fb_session = new FBSession(fb_app_id, fb_app_secret, domain)
+ fb_session.initialize req
+ next()
+ )
+
+module.exports.FBSession = FBSession
@@ -0,0 +1,2 @@
+require('coffee-script');
+module.exports = require('./facebook-session-cookie.coffee');
View
@@ -11,7 +11,7 @@
{
"type": "git",
"url": "http://github.com/jonashuckestein/node-facebook-session-cookie.git",
- "main": "./index.js"
+ "main": "./facebook-session-cookie"
}
],
"dependencies": {

0 comments on commit 27ca4fd

Please sign in to comment.