From 74faf248019cd726ad84807e235f0adff0801d6a Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Fri, 21 Aug 2015 21:46:26 +0000 Subject: [PATCH] don't enforce one auth grant_type. allow user to pass access token --- index.js | 24 +----------------------- lib/one.js | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index 564615e..e6cb075 100644 --- a/index.js +++ b/index.js @@ -16,29 +16,11 @@ function NPR(config) { util._extend(this, config || {}); - if (! this.client_id) - throw('OAuth Client ID not supplied'); - - if (! this.client_secret) - throw('OAuth Client Secret not supplied'); - - if (! this.username) - throw('NPR username not supplied'); - - if (! this.password) - throw('NPR password not supplied'); - if (! this.log) this.log = bunyan.createLogger({name: 'npr'}); this.one = One({ - log: this.log.child({ module: 'one' }), - credentials: { - client_id: this.client_id, - client_secret: this.client_secret, - username: this.username, - password: this.password - } + log: this.log.child({ module: 'one' }) }); } @@ -48,9 +30,5 @@ NPR.Helpers = Helpers; NPR.One = One; /*************************** DEFAULTS *****************************/ -proto.client_id = false; -proto.client_secret = false; -proto.username = false; -proto.password = false; proto.one = false; proto.log = false; diff --git a/lib/one.js b/lib/one.js index 1b88720..d305882 100644 --- a/lib/one.js +++ b/lib/one.js @@ -15,20 +15,13 @@ function One(config) { util._extend(this, config || {}); - if(! this.credentials) - throw('Missing API credentials'); - if(! this.log) this.log = bunyan.createLogger({name: 'npr-one'}); - this.credentials.grant_type = 'password'; - this.getAccessToken = this.getAccessToken.bind(this); - } /*************************** DEFAULTS *****************************/ -proto.credentials = false; -proto.client = false; +proto.token = false; proto.swagger_url = 'https://api.npr.org/documentation/beryllium/api-docs'; proto.log = false; @@ -54,22 +47,37 @@ proto.init = function() { }; +proto.setAccessToken = function(token) { + + var self = this; + + this.token = token; + + return new Promise(function(resolve, reject) { + + if(! self.token) + return reject('no token supplied'); + + var auth = new swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + token, 'header'); + + self.client.clientAuthorizations.add('oauth2', auth); + + resolve(); + + }); + +}; + proto.getAccessToken = function() { var self = this; return new Promise(function(resolve, reject) { - if(self.access_token) - return resolve(self.access_token); + if(! self.token) + return reject('no token supplied'); - self.authorization - .createToken(self.credentials) - .then(function(res) { - self.access_token = res.access_token; - resolve(self.access_token); - }) - .catch(reject); + resolve(self.token); });