Skip to content

Commit

Permalink
Merge pull request #1177 from elisherer/master
Browse files Browse the repository at this point in the history
Add bearer token support
  • Loading branch information
kornelski committed Feb 16, 2017
2 parents e46f890 + 9a250ad commit 4723176
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 9 additions & 2 deletions lib/client.js
Expand Up @@ -501,13 +501,16 @@ Request.prototype.accept = function(type){
* Set Authorization field value with `user` and `pass`.
*
* @param {String} user
* @param {String} pass
* @param {Object} options with 'type' property 'auto' or 'basic' (default 'basic')
* @param {String} [pass] optional in case of using 'bearer' as type
* @param {Object} options with 'type' property 'auto', 'basic' or 'bearer' (default 'basic')
* @return {Request} for chaining
* @api public
*/

Request.prototype.auth = function(user, pass, options){
if (typeof pass === 'object' && pass !== null) { // pass is optional and can substitute for options
options = pass;
}
if (!options) {
options = {
type: 'function' === typeof btoa ? 'basic' : 'auto',
Expand All @@ -523,6 +526,10 @@ Request.prototype.auth = function(user, pass, options){
this.username = user;
this.password = pass;
break;

case 'bearer': // usage would be .auth(accessToken, { type: 'bearer' })
this.set('Authorization', 'Bearer ' + user);
break;
}
return this;
};
Expand Down
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 4723176

Please sign in to comment.