From 9dc09768e5e475c43f85793db25148871012db67 Mon Sep 17 00:00:00 2001 From: Eli Sherer Date: Mon, 13 Feb 2017 22:17:11 +0200 Subject: [PATCH] Add client support for bearer token authentication --- lib/client.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 6cf1b47ce..d7b7cfe2e 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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', @@ -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; };