Skip to content
Browse files

Move off of sessions. Node v0.8.

Encode state in a cookie. Roll a new version, v0.0.2.
  • Loading branch information...
1 parent 54142f9 commit 698659effefefc7d34bf342d592b3fc1d3bd5ea1 @smurthas committed Sep 27, 2012
Showing with 40 additions and 32 deletions.
  1. +10 −5 example/test.js
  2. +24 −24 lib/fitbit_client.js
  3. +6 −3 package.json
View
15 example/test.js
@@ -1,10 +1,10 @@
var express = require('express'),
connect = require('connect'),
app = express.createServer(connect.bodyParser(),
- connect.cookieParser('session'),
- connect.session());
+ connect.cookieParser('sess'));
+
var fs = require('fs');
-var fitbitClient = require('../')('yourConsumerKey', 'yourConsumerSecret');
+var fitbitClient = require('../')(process.argv[2], process.argv[3]);
var token;
app.get('/', function (req, res) {
@@ -24,8 +24,13 @@ app.get('/getStuff', function (req, res) {
res.writeHead(200, 'application/json');
res.end(JSON.stringify(resp));
});
-
+
});
+app.get('/cookie', function(req, res) {
+ res.send('wahoo!');
+});
+
+
app.listen(8553);
-console.log('listening at http://localhost:8553/');
+console.log('listening at http://localhost:8553/');
View
48 lib/fitbit_client.js
@@ -7,19 +7,21 @@
var url = require("url"),
http = require('http'),
OAuth = require('oauth').OAuth,
- querystring = require("querystring");
+ querystring = require("querystring"),
+ Serializer = require('serializer');
var baseURI = 'http://api.fitbit.com/1';
module.exports = function (api_key, api_secret, callbackURI) {
- var client = {version: '0.0.0'};
+ var client = {version: '0.0.2'};
+ var serializer = Serializer.createSecureSerializer(api_key, api_secret);
var oAuth = new OAuth('http://api.fitbit.com/oauth/request_token',
'http://api.fitbit.com/oauth/access_token',
api_key, api_secret, '1.0', callbackURI,
'HMAC-SHA1', null,
{'Accept': '*/*', 'Connection': 'close', 'User-Agent': 'fitbit-js ' + client.version});
-
+
function requestCallback(callback) {
return function (err, data, response) {
@@ -55,33 +57,31 @@ module.exports = function (api_key, api_secret, callbackURI) {
}
client.getAccessToken = function (req, res, callback) {
- var parsedUrl = url.parse(req.url, true),
- callbackUrl = (req.socket.encrypted ? 'https' : 'http') + '://' + req.headers.host + parsedUrl.pathname,
- has_token = parsedUrl.query && parsedUrl.query.oauth_token,
- has_secret = req.session.auth && req.session.auth.fitbit_oauth_token_secret;
+ var sess;
+ if(req.cookies && req.cookies["fitbit_client"]) {
+ try { sess = serializer.parse(req.cookies["fitbit_client"]) }catch(E){ }
+ }
+ var qs = url.parse(req.url, true).query;
+
+ var has_token = qs && qs.oauth_token,
+ has_secret = sess && sess.token_secret;
if(has_token && has_secret) { // Access token
- oAuth.getOAuthAccessToken(parsedUrl.query.oauth_token,
- req.session.auth.fitbit_oauth_token_secret,
- parsedUrl.query.oauth_verifier,
+ oAuth.getOAuthAccessToken(qs.oauth_token,
+ sess.tokenSecret,
+ qs.oauth_verifier,
function (error, oauth_token, oauth_token_secret, additionalParameters) {
- if (error)
- callback(error, null);
- else
- callback(null, {oauth_token: oauth_token, oauth_token_secret: oauth_token_secret});
+ if (error) return callback(error, null);
+ callback(null, {oauth_token: oauth_token, oauth_token_secret: oauth_token_secret});
});
} else { // Request token
- oAuth.getOAuthRequestToken({oauth_callback: callbackUrl},
+ oAuth.getOAuthRequestToken({oauth_callback: callbackURI},
function (error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters) {
- if(!error) {
- req.session.fitbit_redirect_url = req.url;
- req.session.auth = req.session.auth || {};
- req.session.auth.fitbit_oauth_token_secret = oauth_token_secret;
- req.session.auth.fitbit_oauth_token = oauth_token;
- res.redirect("http://www.fitbit.com/oauth/authorize?oauth_token=" + oauth_token);
- } else {
- callback(error, null);
- }
+ if (error) return callback(error, null);
+ res.cookie('fitbit_client',
+ serializer.stringify({token_secret:oauth_token_secret}),
+ { path: '/', httpOnly: false }); // stash the secret
+ res.redirect("http://www.fitbit.com/oauth/authorize?oauth_token=" + oauth_token);
});
}
};
View
9 package.json
@@ -1,12 +1,15 @@
{
"name": "fitbit-js",
"description": "Simple FitBit API client",
- "version": "0.0.1",
+ "version": "0.0.2",
"author": "Simon Murtha-Smith <simon@murtha-smith.com>",
"keywords": ["fitbit"],
"main" : "lib/fitbit_client.js",
"directories" : { "lib" : "./lib" },
- "dependencies": { "oauth": ">= 0.8.2" },
+ "dependencies": {
+ "oauth": ">= 0.8.2",
+ "serializer": ">=0.0.2 <0.1.0"
+ },
"repository" : {"type": "git" , "url": "http://github.com/smurthas/fitbit-js.git" },
- "engines": { "node": ">=0.2.0 <0.7.0" }
+ "engines": { "node": ">=0.8.0 <0.9.0" }
}

0 comments on commit 698659e

Please sign in to comment.
Something went wrong with that request. Please try again.