Last active
August 29, 2015 13:57
-
-
Save rfaisal/9899185 to your computer and use it in GitHub Desktop.
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
var async = require('async'), | |
https = require('https'), | |
querystring = require('querystring'), | |
jwt = require('../shared/jwt'), | |
crypto = require('crypto'), | |
app_secret = 'CH..'; // get it from foursquare | |
exports.register = function (api) { | |
api.post('foursquare', foursquare); | |
}; | |
function foursquare(request, response) { | |
if (request.body.access_token) { | |
var decipher = crypto.createDecipher('aes256', app_secret); | |
var access_token = null; | |
try { | |
access_token = decipher.update(request.body.access_token, 'hex', 'utf8') + decipher.final('utf8'); | |
} catch (err) { | |
response.send(statusCodes.UNAUTHORIZED, { | |
code: 401, | |
error: "Cannot verify the access token." | |
}); | |
} | |
if (access_token) { | |
var params = { | |
oauth_token: access_token, | |
v: "20140331" | |
}; | |
https.get('https://api.foursquare.com/v2/users/self?' + querystring.stringify(params), function (res) { | |
var msg = ''; | |
res.on('data', function (chunk) { | |
msg += chunk; | |
}); | |
res.on('end', function () { | |
var _user = JSON.parse(msg); | |
if (_user && _user.response && _user.response.user && _user.response.user.id) { | |
response.send(statusCodes.OK, { | |
"user": { | |
"userId": 'Foursquare:' + _user.response.user.id | |
}, | |
"authenticationToken": jwt.createZumoJwt(request.service.config.masterKey, 4, 'Foursquare', 'Foursquare:' + _user.response.user.id, { | |
userId: "Foursquare:" + _user.response.user.id, | |
accessToken: access_token | |
}) | |
}); | |
} else { | |
response.send(statusCodes.UNAUTHORIZED, { | |
code: 401, | |
error: "Cannot verify the access token." | |
}); | |
} | |
}); | |
}).on('error', function (e) { | |
response.send(statusCodes.UNAUTHORIZED, { | |
code: 401, | |
error: "Cannot verify the access token." | |
}); | |
}); | |
} | |
} else { | |
response.send(statusCodes.UNAUTHORIZED, { | |
code: 401, | |
error: "An access_token must be provided" | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment