Skip to content

Commit

Permalink
refactor(package): Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaiklor committed Jan 1, 2016
1 parent 7acdc56 commit 114fd3b
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class FacebookTokenStrategy extends OAuth2Strategy {
let accessToken = this.lookup(req, this._accessTokenField);
let refreshToken = this.lookup(req, this._refreshTokenField);

if (!accessToken) return this.fail({ message: `You should provide ${this._accessTokenField}` });
if (!accessToken) return this.fail({message: `You should provide ${this._accessTokenField}`});

this._loadUserProfile(accessToken, (error, profile) => {
if (error) return this.error(error);
Expand Down Expand Up @@ -141,62 +141,60 @@ export default class FacebookTokenStrategy extends OAuth2Strategy {
}

/**
* Converts array of profile fields to string
* @param {Array} _profileFields Profile fields i.e. ['id', 'email']
* @returns {String}
* Parses an OAuth2 RFC6750 bearer authorization token, this method additionally is RFC 2616 compliant and respects
* case insensitive headers.
*
* @param {Object} req http request object
* @returns {String} value for field within body, query, or headers
*/
static convertProfileFields(_profileFields) {
let profileFields = _profileFields || [];
let map = {
'id': 'id',
'displayName': 'name',
'name': ['last_name', 'first_name', 'middle_name'],
'gender': 'gender',
'profileUrl': 'link',
'emails': 'email',
'photos': 'picture'
};

return profileFields.reduce((acc, field) => acc.concat(map[field] || field), []).join(',');
}

/**
* Parses an OAuth2 RFC6750 bearer authorization token, this method additionally is RFC 2616 compliant and respects
* case insensitive headers.
*
* @param {Object} req http request object
* @returns {String} value for field within body, query, or headers
*/
parseOAuth2Token(req) {
const OAuth2AuthorizationField = 'Authorization';
let headerValue = (req.headers && (req.headers[OAuth2AuthorizationField] || req.headers[OAuth2AuthorizationField.toLowerCase()]));
const headerValue = (req.headers && (req.headers[OAuth2AuthorizationField] || req.headers[OAuth2AuthorizationField.toLowerCase()]));

return (
headerValue && (() => {
const bearerRE = /Bearer\ (.*)/;
let match = bearerRE.exec(headerValue);
return (match && match[1]);
})()
);
);
}

/**
* Performs a lookup of the param field within the request, this method handles searhing the body, query, and header.
* Additionally this method is RFC 2616 compliant and allows for case insensitive headers. This method additionally will
* delegate outwards to the OAuth2Token parser to validate whether a OAuth2 bearer token has been provided.
*
* @param {Object} req http request object
* @param {String} field
* @returns {String} value for field within body, query, or headers
*/
* Performs a lookup of the param field within the request, this method handles searhing the body, query, and header.
* Additionally this method is RFC 2616 compliant and allows for case insensitive headers. This method additionally will
* delegate outwards to the OAuth2Token parser to validate whether a OAuth2 bearer token has been provided.
*
* @param {Object} req http request object
* @param {String} field
* @returns {String} value for field within body, query, or headers
*/
lookup(req, field) {
return (
req.body && req.body[field] ||
req.query && req.query[field] ||
req.headers && (req.headers[field] || req.headers[field.toLowerCase()]) ||
this.parseOAuth2Token(req)
);
);
}

/**
* Converts array of profile fields to string
* @param {Array} _profileFields Profile fields i.e. ['id', 'email']
* @returns {String}
*/
static convertProfileFields(_profileFields) {
let profileFields = _profileFields || [];
let map = {
'id': 'id',
'displayName': 'name',
'name': ['last_name', 'first_name', 'middle_name'],
'gender': 'gender',
'profileUrl': 'link',
'emails': 'email',
'photos': 'picture'
};


return profileFields.reduce((acc, field) => acc.concat(map[field] || field), []).join(',');
}
}

0 comments on commit 114fd3b

Please sign in to comment.