node-freeagent2
Version:
Node.js OAuth2 REST Library for use with the FreeAgent v2 API, with Passport Authentication Strategy
56 lines (42 loc) • 1.53 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var InternalOAuthError, OAuth2Strategy, Strategy, baseUri, util;
InternalOAuthError = void 0;
OAuth2Strategy = void 0;
Strategy = void 0;
util = void 0;
baseUri = 'https://api.freeagent.com/v2/';
Strategy = function(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || baseUri + 'approve_app';
options.tokenURL = options.tokenURL || baseUri + 'token_endpoint';
options.scopeSeparator = options.scopeSeparator || ',';
OAuth2Strategy.call(this, options, verify);
return this.name = 'freeagent';
};
util = require('util');
OAuth2Strategy = require('passport-oauth').OAuth2Strategy;
InternalOAuthError = require('passport-oauth').InternalOAuthError;
util.inherits(Strategy, OAuth2Strategy);
Strategy.prototype.userProfile = function(accessToken, done) {
return this._oauth2._request('GET', baseUri + 'users/me', {
'User-Agent': 'passport-freeagent2',
'Authorization': "Bearer " + accessToken
}, null, accessToken, function(err, body, res) {
var e, json;
if (!err) {
try {
json = JSON.parse(body);
json.provider = 'FreeAgent';
return done(null, json.user);
} catch (_error) {
e = _error;
return done(e);
}
} else {
return done(new InternalOAuthError('failed to fetch user profile', err));
}
});
};
module.exports = Strategy;
}).call(this);