facebook-node-sdk
Advanced tools
Comparing version 0.1.11 to 0.1.12
@@ -0,3 +1,3 @@ | ||
var assert = require('assert'); | ||
var https = require('https'); | ||
var assert = require('assert'); | ||
var crypto = require('crypto'); | ||
@@ -7,2 +7,4 @@ var util = require('util'); | ||
var querystring = require('querystring'); | ||
var cb = require('./cbutil'); | ||
var requestUtil = require('./requestutil'); | ||
@@ -201,7 +203,6 @@ /** | ||
*/ | ||
BaseFacebook.prototype.getAccessToken = function(callback) { | ||
BaseFacebook.prototype.getAccessToken = function getAccessToken(callback) { | ||
if (this.accessToken !== null) { | ||
// we've done this already and cached it. Just return. | ||
callback(null, this.accessToken); | ||
callback = null; | ||
} | ||
@@ -214,18 +215,13 @@ else { | ||
var self = this; | ||
this.getUserAccessToken(function(err, userAccessToken) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
this.getUserAccessToken(cb.returnToCallback(callback, false, function(userAccessToken) { | ||
if (userAccessToken) { | ||
self.setAccessToken(userAccessToken); | ||
} | ||
else { | ||
if (userAccessToken) { | ||
self.setAccessToken(userAccessToken); | ||
} | ||
callback(null, self.accessToken); | ||
callback = null; | ||
} | ||
}); | ||
return self.accessToken; | ||
})); | ||
} | ||
}; | ||
BaseFacebook.prototype.getAccessToken = cb.wrap(BaseFacebook.prototype.getAccessToken); | ||
/** | ||
@@ -241,3 +237,3 @@ * Determines and returns the user access token, first using | ||
*/ | ||
BaseFacebook.prototype.getUserAccessToken = function(callback) { | ||
BaseFacebook.prototype.getUserAccessToken = function getUserAccessToken(callback) { | ||
// first, consider a signed request if it's supplied. | ||
@@ -253,3 +249,2 @@ // if there is a signed request, then it alone determines | ||
callback(null, accessToken); | ||
callback = null; | ||
} | ||
@@ -261,3 +256,3 @@ else { | ||
var self = this; | ||
this.getAccessTokenFromCode(code, null, handleAccessTokenFromCode); | ||
this.getAccessTokenFromCode(code, null, cb.returnToCallback(callback, false, handleAccessTokenFromCode)); | ||
} | ||
@@ -271,3 +266,2 @@ else { | ||
callback(null, false); | ||
callback = null; | ||
} | ||
@@ -280,3 +274,3 @@ } | ||
var self = this; | ||
this.getAccessTokenFromCode(code, null, handleAccessTokenFromCode); | ||
this.getAccessTokenFromCode(code, null, cb.returnToCallback(callback, false, handleAccessTokenFromCode)); | ||
} | ||
@@ -289,26 +283,17 @@ else { | ||
callback(null, this.getPersistentData('access_token')); | ||
callback = null; | ||
} | ||
} | ||
function handleAccessTokenFromCode(err, accessToken) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
function handleAccessTokenFromCode(accessToken) { | ||
if (accessToken) { | ||
self.setPersistentData('code', code); | ||
self.setPersistentData('access_token', accessToken); | ||
return accessToken; | ||
} | ||
else { | ||
if (accessToken) { | ||
self.setPersistentData('code', code); | ||
self.setPersistentData('access_token', accessToken); | ||
callback(null, accessToken); | ||
callback = null; | ||
} | ||
else { | ||
// signed request states there's no access token, so anything | ||
// stored should be cleared. | ||
self.clearAllPersistentData(); | ||
// respect the signed request's data, even | ||
// if there's an authorization code or something else | ||
callback(null, false); | ||
callback = null; | ||
} | ||
// signed request states there's no access token, so anything | ||
// stored should be cleared. | ||
self.clearAllPersistentData(); | ||
// respect the signed request's data, even | ||
// if there's an authorization code or something else | ||
return false; | ||
} | ||
@@ -318,2 +303,4 @@ } | ||
BaseFacebook.prototype.getUserAccessToken = cb.wrap(BaseFacebook.prototype.getUserAccessToken); | ||
BaseFacebook.prototype.mergeObject = function() { | ||
@@ -375,3 +362,3 @@ var obj = {}; | ||
*/ | ||
BaseFacebook.prototype.getLogoutUrl = function(/* params, callback */) { | ||
BaseFacebook.prototype.getLogoutUrl = function getLogoutUrl(/* params, callback */) { | ||
var args = [].slice.call(arguments); | ||
@@ -385,23 +372,11 @@ var callback = args.pop(); | ||
var self = this; | ||
this.getAccessToken(function(err, accessToken) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
} | ||
else { | ||
try { | ||
var currentUrl = self.getCurrentUrl(); | ||
} | ||
catch (err) { | ||
callback(err, null); | ||
callback = null; | ||
return; | ||
} | ||
this.getAccessToken(cb.returnToCallback(callback, false, function(accessToken) { | ||
var currentUrl = self.getCurrentUrl(); | ||
var queryMap = self.mergeObject({ next: currentUrl, access_token: accessToken }, params); | ||
var query = querystring.stringify(queryMap) | ||
return 'https://www.facebook.com/logout.php?' + query; | ||
})); | ||
}; | ||
callback(null, 'https://www.facebook.com/logout.php?' + querystring.stringify( | ||
self.mergeObject({ next: currentUrl, access_token: accessToken }, params))); | ||
callback = null; | ||
} | ||
}); | ||
} | ||
BaseFacebook.prototype.getLogoutUrl = cb.wrap(BaseFacebook.prototype.getLogoutUrl); | ||
@@ -506,24 +481,18 @@ /** | ||
*/ | ||
BaseFacebook.prototype.getUser = function(callback) { | ||
BaseFacebook.prototype.getUser = function getUser(callback) { | ||
if (this.user !== null) { | ||
// we've already determined this and cached the value. | ||
callback(null, this.user); | ||
callback = null; | ||
} | ||
else { | ||
var self = this; | ||
this.getUserFromAvailableData(function(err, user) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
} | ||
else { | ||
self.user = user; | ||
callback(null, self.user); | ||
callback = null; | ||
} | ||
}); | ||
this.getUserFromAvailableData(cb.returnToCallback(callback, false, function(user) { | ||
self.user = user; | ||
return self.user; | ||
})); | ||
} | ||
}; | ||
BaseFacebook.prototype.getUser = cb.wrap(BaseFacebook.prototype.getUser); | ||
/** | ||
@@ -537,3 +506,3 @@ * Determines the connected user by first examining any signed | ||
*/ | ||
BaseFacebook.prototype.getUserFromAvailableData = function(callback) { | ||
BaseFacebook.prototype.getUserFromAvailableData = function getUserFromAvailableData(callback) { | ||
// if a signed request is supplied, then it solely determines | ||
@@ -548,3 +517,2 @@ // who the user is. | ||
callback(null, user); | ||
callback = null; | ||
} | ||
@@ -556,3 +524,2 @@ else { | ||
callback(null, 0); | ||
callback = null; | ||
} | ||
@@ -567,44 +534,30 @@ } | ||
this.getAccessToken(function(err, accessToken) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
} | ||
else { | ||
if (accessToken) { | ||
try { | ||
if (err) { | ||
throw err; | ||
} | ||
if ((accessToken) && | ||
// access_token is not application access_token | ||
if (accessToken !== self.getApplicationAccessToken()) { | ||
(accessToken !== self.getApplicationAccessToken()) && | ||
// undefined user or access_token is old | ||
if (!user || persistedAccessToken !== accessToken) { | ||
self.getUserFromAccessToken(function(err, user) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
} | ||
else { | ||
if (user) { | ||
assert.ok(typeof user === 'string' && user.match(/^\d+$/)); | ||
self.setPersistentData('user_id', user); | ||
} else { | ||
self.clearAllPersistentData(); | ||
} | ||
callback(null, user); | ||
callback = null; | ||
} | ||
}); | ||
(!user || persistedAccessToken !== accessToken)) { | ||
self.getUserFromAccessToken(cb.returnToCallback(callback, false, function(user) { | ||
if (user) { | ||
assert.ok(typeof user === 'string' && user.match(/^\d+$/)); | ||
self.setPersistentData('user_id', user); | ||
} | ||
else { | ||
callback(null, user); | ||
callback = null; | ||
self.clearAllPersistentData(); | ||
} | ||
} | ||
else { | ||
callback(null, user); | ||
callback = null; | ||
} | ||
return user; | ||
})); | ||
} | ||
else { | ||
callback(null, user); | ||
callback = null; | ||
} | ||
} | ||
catch (err) { | ||
callback(err, null); | ||
} | ||
}); | ||
@@ -614,2 +567,4 @@ } | ||
BaseFacebook.prototype.getUserFromAvailableData = cb.wrap(BaseFacebook.prototype.getUserFromAvailableData); | ||
/** | ||
@@ -620,3 +575,3 @@ * Make an API call. | ||
*/ | ||
BaseFacebook.prototype.api = function(/* polymorphic */) { | ||
BaseFacebook.prototype.api = function api(/* polymorphic */) { | ||
var args = [].slice.call(arguments); | ||
@@ -631,2 +586,4 @@ if (args[0] && typeof args[0] === 'object') { | ||
BaseFacebook.prototype.api = cb.wrap(BaseFacebook.prototype.api); | ||
/** | ||
@@ -639,3 +596,3 @@ * Invoke the old restserver.php endpoint. | ||
*/ | ||
BaseFacebook.prototype.restserver = function(params, callback) { | ||
BaseFacebook.prototype.restserver = function restserver(params, callback) { | ||
// generic application level parameters | ||
@@ -647,32 +604,24 @@ params.api_key = this.getAppId(); | ||
var host = this.getApiHost(params['method']) | ||
this.oauthRequest(host, '/restserver.php', params, function(err, response) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
this.oauthRequest(host, '/restserver.php', params, cb.returnToCallback(callback, false, function(response) { | ||
try { | ||
var result = JSON.parse(response); | ||
} | ||
catch (err) { | ||
throw new Error('Parse REST server response error: ' + err.message); | ||
} | ||
// results are returned, errors are thrown | ||
if (result && typeof result === 'object' && result.hasOwnProperty('error_code')) { | ||
throw self.createApiError(result); | ||
} | ||
else { | ||
try { | ||
var result = JSON.parse(response); | ||
if (params.method === 'auth.expireSession' || params.method === 'auth.revokeAuthorization') { | ||
self.destroySession(); | ||
} | ||
catch (err) { | ||
callback(new Error('Parse REST server response error: ' + err), null); | ||
callback = null; | ||
return; | ||
} | ||
// results are returned, errors are thrown | ||
if (result && typeof result === 'object' && result.hasOwnProperty('error_code')) { | ||
callback(self.createApiError(result), null); | ||
callback = null; | ||
} | ||
else { | ||
if (params.method === 'auth.expireSession' || params.method === 'auth.revokeAuthorization') { | ||
self.destroySession(); | ||
} | ||
callback(null, result); | ||
callback = null; | ||
} | ||
return result; | ||
} | ||
}) | ||
})); | ||
}; | ||
BaseFacebook.prototype.restserver = cb.wrap(BaseFacebook.prototype.restserver); | ||
/** | ||
@@ -687,3 +636,3 @@ * Invoke the Graph API. | ||
*/ | ||
BaseFacebook.prototype.graph = function(/* path, method, params, callback */) { | ||
BaseFacebook.prototype.graph = function graph(/* path, method, params, callback */) { | ||
var args = [].slice.call(arguments); | ||
@@ -710,28 +659,20 @@ var callback = args.pop(); | ||
var self = this; | ||
this.oauthRequest('graph.facebook.com', path, params, function(err, response) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
this.oauthRequest('graph.facebook.com', path, params, cb.returnToCallback(callback, false, function(response) { | ||
try { | ||
result = JSON.parse(response); | ||
} | ||
catch (err) { | ||
throw new Error('Parse Graph API server response error: ' + err.message); | ||
} | ||
if (result && typeof result === 'object' && result.hasOwnProperty('error')) { | ||
throw self.createApiError(result); | ||
} | ||
else { | ||
try { | ||
result = JSON.parse(response); | ||
} | ||
catch (err) { | ||
callback(new Error('Parse Graph API server response error: ' + err), null); | ||
callback = null; | ||
return; | ||
} | ||
if (result && typeof result === 'object' && result.hasOwnProperty('error')) { | ||
callback(self.createApiError(result), null); | ||
callback = null; | ||
} | ||
else { | ||
callback(null, result); | ||
callback = null; | ||
} | ||
return result; | ||
} | ||
}); | ||
})); | ||
}; | ||
BaseFacebook.prototype.graph = cb.wrap(BaseFacebook.prototype.graph); | ||
/** | ||
@@ -944,15 +885,15 @@ * Analyzes the supplied result to see if it was thrown | ||
*/ | ||
BaseFacebook.prototype.getUserFromAccessToken = function(callback) { | ||
this.api('/me', function(err, userInfo) { | ||
BaseFacebook.prototype.getUserFromAccessToken = function getUserFromAccessToken(callback) { | ||
this.api('/me', cb.returnToCallback(callback, true, function(err, userInfo) { | ||
if (err) { | ||
callback(null, 0); | ||
callback = null; | ||
return 0; | ||
} | ||
else { | ||
callback(null, userInfo.id); | ||
callback = null; | ||
return userInfo.id; | ||
} | ||
}); | ||
})); | ||
}; | ||
BaseFacebook.prototype.getUserFromAccessToken = cb.wrap(BaseFacebook.prototype.getUserFromAccessToken); | ||
/** | ||
@@ -970,17 +911,9 @@ * Retrieves an access token for the given authorization code | ||
*/ | ||
BaseFacebook.prototype.getAccessTokenFromCode = function(code, redirectUri, callback) { | ||
BaseFacebook.prototype.getAccessTokenFromCode = function getAccessTokenFromCode(code, redirectUri, callback) { | ||
if (!code) { | ||
callback(null, false); | ||
callback = null; | ||
} | ||
else { | ||
if (!redirectUri) { | ||
try { | ||
redirectUri = this.getCurrentUrl(); | ||
} | ||
catch (err) { | ||
callback(err, null); | ||
callback = null; | ||
return; | ||
} | ||
redirectUri = this.getCurrentUrl(); | ||
} | ||
@@ -996,3 +929,3 @@ | ||
}, | ||
function(err, accessTokenResponse) { | ||
cb.returnToCallback(callback, true, function(err, accessTokenResponse) { | ||
if (err) { | ||
@@ -1002,8 +935,6 @@ if (err instanceof FacebookApiError) { | ||
// In any event, we don't have an access token, so say so. | ||
callback(null, false); | ||
callback = null; | ||
return false; | ||
} | ||
else { | ||
callback(err, null); | ||
callback = null; | ||
throw err; | ||
} | ||
@@ -1013,4 +944,3 @@ } | ||
if (!accessTokenResponse) { | ||
callback(null, false); | ||
callback = null; | ||
return false; | ||
} | ||
@@ -1020,15 +950,15 @@ else { | ||
if (!responseParams.hasOwnProperty('access_token')) { | ||
callback(null, false); | ||
callback = null; | ||
return false; | ||
} | ||
else { | ||
callback(null, responseParams.access_token); | ||
callback = null; | ||
return responseParams.access_token; | ||
} | ||
} | ||
} | ||
}); | ||
})); | ||
} | ||
}; | ||
BaseFacebook.prototype.getAccessTokenFromCode = cb.wrap(BaseFacebook.prototype.getAccessTokenFromCode); | ||
/** | ||
@@ -1116,14 +1046,16 @@ * Returns the Current URL, stripping it of known FB parameters that should | ||
*/ | ||
BaseFacebook.prototype.oauthRequest = function(host, path, params, callback) { | ||
BaseFacebook.prototype.oauthRequest = function oauthRequest(host, path, params, callback) { | ||
var self = this; | ||
if (!params.hasOwnProperty('access_token')) { | ||
this.getAccessToken(function(err, accessToken) { | ||
if (err) { | ||
callback(err, null); | ||
callback = null; | ||
} | ||
else { | ||
try { | ||
if (err) { | ||
throw err; | ||
} | ||
params['access_token'] = accessToken; | ||
next(); | ||
} | ||
catch (err) { | ||
callback(err, null); | ||
} | ||
}); | ||
@@ -1144,6 +1076,7 @@ } | ||
self.makeRequest(host, path, params, callback); | ||
callback = null; | ||
} | ||
}; | ||
BaseFacebook.prototype.oauthRequest = cb.wrap(BaseFacebook.prototype.oauthRequest); | ||
/** | ||
@@ -1159,25 +1092,8 @@ * Makes an HTTP request. This method can be overridden by subclasses if | ||
*/ | ||
BaseFacebook.prototype.makeRequest = function(host, path, params, callback) { | ||
// TODO request timeout setting | ||
// TODO user agent setting | ||
// TODO support multipart/form-data | ||
var req = https.request({ host: host, path: path, port: 443, method: 'POST' }, function (res) { | ||
res.setEncoding('utf8'); | ||
var body = []; | ||
res.on('data', function(data) { | ||
body.push(data); | ||
}); | ||
res.on('end', function(e) { | ||
callback(null, body.join('')); | ||
callback = null; | ||
}); | ||
}); | ||
req.on('error', function(err) { | ||
callback(err, null); | ||
callback = null; | ||
}); | ||
req.write(querystring.stringify(params)); | ||
req.end(); | ||
BaseFacebook.prototype.makeRequest = function makeRequest(host, path, params, callback) { | ||
requestUtil.requestFacebookApi(https, host, 443, path, params, callback); | ||
}; | ||
BaseFacebook.prototype.makeRequest = cb.wrap(BaseFacebook.prototype.makeRequest); | ||
/** | ||
@@ -1184,0 +1100,0 @@ * Prints to the error log if you aren't in command line mode. |
{ | ||
"name": "facebook-node-sdk", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"description": "Node.js SDK for the Facebook API", | ||
@@ -5,0 +5,0 @@ "tags": ["facebook"], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107411
17
3068
12
3