passport-bullhorn
Version:
Bullhorn (OAuth) authentication strategy for Passport
111 lines (95 loc) • 5.06 kB
JavaScript
/**
* Module dependencies.
*/
;
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _passportOauth = require('passport-oauth');
/**
* `Strategy` constructor.
*
* The Bullhorn authentication strategy authenticates requests by delegating to
* Bullhorn using the OAuth 2.0 protocol.
*
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and service-specific `profile`, and then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `clientID` your Bullhorn application's client id
* - `clientSecret` your Bullhorn application's client secret
* - `callbackURL` URL to which Bullhorn will redirect the user after granting authorization
*
* Examples:
*
* passport.use(new BullhornStrategy({
* clientID: '123-456-789',
* clientSecret: 'shhh-its-a-secret'
* callbackURL: 'https://www.example.net/auth/yourapp/callback'
* },
* function(accessToken, refreshToken, profile, done) {
* User.findOrCreate(..., function (err, user) {
* done(err, user);
* });
* }
* ));
*
* @param {Object} options
* @param {Function} verify
* @api public
*/
var BullhornOAuthStrategy = (function (_OAuth2Strategy) {
_inherits(BullhornOAuthStrategy, _OAuth2Strategy);
function BullhornOAuthStrategy(options, verify) {
_classCallCheck(this, BullhornOAuthStrategy);
options = options || {};
options.authorizationURL = options.authorizationURL || 'http://rest.bullhornstaffing.com/oauth/authorize';
options.tokenURL = options.tokenURL || 'http://rest.bullhornstaffing.com/oauth/token';
_get(Object.getPrototypeOf(BullhornOAuthStrategy.prototype), 'constructor', this).call(this, options, verify);
this.name = 'bullhorn';
this.profileURL = options.profileURL || 'http://rest.bullhorn.com/rest-services/login?version=*';
}
/**
* Retrieve BhRestToken and restUrl from Bullhorn.
*
* This function constructs a normalized profile, with the following properties:
*
* - `id`
* - `token`
* - `url`
*
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
_createClass(BullhornOAuthStrategy, [{
key: 'userProfile',
value: function userProfile(accessToken, done) {
this._oauth2.get(this.profileURL, accessToken, function (err, body, res) {
if (err) {
return done(new _passportOauth.InternalOAuthError('failed to fetch user profile', err));
}
try {
var json = JSON.parse(body);
var profile = { provider: 'bullhorn' };
profile.id = json.BhRestToken;
profile.token = json.BhRestToken;
profile.url = json.restUrl;
profile._raw = body;
profile._json = json;
done(null, profile);
} catch (e) {
done(e);
}
});
}
}]);
return BullhornOAuthStrategy;
})(_passportOauth.OAuth2Strategy);
exports.BullhornOAuthStrategy = BullhornOAuthStrategy;