Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 Asyn/Await , Const and Let #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions lib/strategy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Load modules.
var OAuth2Strategy = require('passport-oauth2')
const OAuth2Strategy = require('passport-oauth2')
, util = require('util')
, uri = require('url')
, crypto = require('crypto')
Expand Down Expand Up @@ -45,15 +45,15 @@ var OAuth2Strategy = require('passport-oauth2')
* @param {function} verify
* @access public
*/
function Strategy(options, verify) {
async function Strategy(options, verify) {
options = options || {};
var version = options.graphAPIVersion || 'v3.2';
const version = options.graphAPIVersion || 'v3.2';

options.authorizationURL = options.authorizationURL || 'https://www.facebook.com/' + version + '/dialog/oauth';
options.tokenURL = options.tokenURL || 'https://graph.facebook.com/' + version + '/oauth/access_token';
options.scopeSeparator = options.scopeSeparator || ',';

OAuth2Strategy.call(this, options, verify);
await OAuth2Strategy.call(this, options, verify);
this.name = 'facebook';
this._profileURL = options.profileURL || 'https://graph.facebook.com/' + version + '/me';
this._profileFields = options.profileFields || null;
Expand All @@ -72,7 +72,7 @@ util.inherits(Strategy, OAuth2Strategy);
* @param {object} options
* @access protected
*/
Strategy.prototype.authenticate = function(req, options) {
Strategy.prototype.authenticate = async function(req, options) {
// Facebook doesn't conform to the OAuth 2.0 specification, with respect to
// redirecting with error codes.
//
Expand All @@ -81,7 +81,7 @@ Strategy.prototype.authenticate = function(req, options) {
return this.error(new FacebookAuthorizationError(req.query.error_message, parseInt(req.query.error_code, 10)));
}

OAuth2Strategy.prototype.authenticate.call(this, req, options);
await OAuth2Strategy.prototype.authenticate.call(this, req, options);
};

/**
Expand Down Expand Up @@ -134,8 +134,8 @@ Strategy.prototype.authorizationParams = function (options) {
* @param {function} done
* @access protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
var url = uri.parse(this._profileURL);
Strategy.prototype.userProfile = async function(accessToken, done) {
const url = await uri.parse(this._profileURL);
if (this._enableProof) {
// Secure API call by adding proof of the app secret. This is required when
// the "Require AppSecret Proof for Server API calls" setting has been
Expand All @@ -144,17 +144,17 @@ Strategy.prototype.userProfile = function(accessToken, done) {
//
// For further details, refer to:
// https://developers.facebook.com/docs/reference/api/securing-graph-api/
var proof = crypto.createHmac('sha256', this._clientSecret).update(accessToken).digest('hex');
const proof = await crypto.createHmac('sha256', this._clientSecret).update(accessToken).digest('hex');
url.search = (url.search ? url.search + '&' : '') + 'appsecret_proof=' + proof;
}
if (this._profileFields) {
var fields = this._convertProfileFields(this._profileFields);
const fields = await this._convertProfileFields(this._profileFields);
if (fields !== '') { url.search = (url.search ? url.search + '&' : '') + 'fields=' + fields; }
}
url = uri.format(url);
url = await uri.format(url);

this._oauth2.get(url, accessToken, function (err, body, res) {
var json;
await this._oauth2.get(url, accessToken, function (err, body, res) {
const json;

if (err) {
if (err.data) {
Expand All @@ -164,23 +164,23 @@ Strategy.prototype.userProfile = function(accessToken, done) {
}

if (json && json.error && typeof json.error == 'object') {
return done(new FacebookGraphAPIError(json.error.message, json.error.type, json.error.code, json.error.error_subcode, json.error.fbtrace_id));
return await done(new FacebookGraphAPIError(json.error.message, json.error.type, json.error.code, json.error.error_subcode, json.error.fbtrace_id));
}
return done(new InternalOAuthError('Failed to fetch user profile', err));
return await done(new InternalOAuthError('Failed to fetch user profile', err));
}

try {
json = JSON.parse(body);
} catch (ex) {
return done(new Error('Failed to parse user profile'));
return await done(new Error('Failed to parse user profile'));
}

var profile = Profile.parse(json);
const profile = Profile.parse(json);
profile.provider = 'facebook';
profile._raw = body;
profile._json = json;

done(null, profile);
await done(null, profile);
});
};

Expand All @@ -192,12 +192,12 @@ Strategy.prototype.userProfile = function(accessToken, done) {
* @return {Error}
* @access protected
*/
Strategy.prototype.parseErrorResponse = function(body, status) {
var json = JSON.parse(body);
Strategy.prototype.parseErrorResponse = async function(body, status) {
const json = JSON.parse(body);
if (json.error && typeof json.error == 'object') {
return new FacebookTokenError(json.error.message, json.error.type, json.error.code, json.error.error_subcode, json.error.fbtrace_id);
}
return OAuth2Strategy.prototype.parseErrorResponse.call(this, body, status);
return await OAuth2Strategy.prototype.parseErrorResponse.call(this, body, status);
};

/**
Expand All @@ -207,8 +207,8 @@ Strategy.prototype.parseErrorResponse = function(body, status) {
* @return {string}
* @access protected
*/
Strategy.prototype._convertProfileFields = function(profileFields) {
var map = {
Strategy.prototype._convertProfileFields = async function(profileFields) {
const map = {
'id': 'id',
'username': 'username',
'displayName': 'name',
Expand All @@ -220,7 +220,7 @@ Strategy.prototype._convertProfileFields = function(profileFields) {
'photos': 'picture'
};

var fields = [];
let fields = [];

profileFields.forEach(function(f) {
// return raw Facebook profile field to support the many fields that don't
Expand Down