Skip to content

Commit

Permalink
Add node client support for bearer token authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
elisherer committed Feb 13, 2017
1 parent 9dc0976 commit 9a250ad
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/node/index.js
Expand Up @@ -468,18 +468,30 @@ Request.prototype._redirect = function(res){
* .auth('tobi', 'learnboost')
* .auth('tobi:learnboost')
* .auth('tobi')
* .auth(accessToken, { type: 'bearer' })
*
* @param {String} user
* @param {String} pass
* @param {String} [pass]
* @param {Object} [options] options with authorization type 'basic' or 'bearer' ('basic' is default)
* @return {Request} for chaining
* @api public
*/

Request.prototype.auth = function(user, pass){
Request.prototype.auth = function(user, pass, options){
if (1 === arguments.length) pass = '';
if (!~user.indexOf(':')) user = user + ':';
var str = new Buffer(user + pass).toString('base64');
return this.set('Authorization', 'Basic ' + str);
if (2 === arguments.length && typeof pass === 'object') options = pass;
if (!options) {
options = { type: 'basic' };
}
switch (options.type) {
case 'bearer':
return this.set('Authorization', 'Bearer ' + user);

default: // 'basic'
if (!~user.indexOf(':')) user = user + ':';
var str = new Buffer(user + pass).toString('base64');
return this.set('Authorization', 'Basic ' + str);
}
};

/**
Expand Down

0 comments on commit 9a250ad

Please sign in to comment.