don't enforce one auth grant_type. allow user to pass access token

This commit is contained in:
Todd Treece 2015-08-21 21:46:26 +00:00
parent f97b43b1e1
commit 74faf24801
2 changed files with 26 additions and 40 deletions

View file

@ -16,29 +16,11 @@ function NPR(config) {
util._extend(this, 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) if (! this.log)
this.log = bunyan.createLogger({name: 'npr'}); this.log = bunyan.createLogger({name: 'npr'});
this.one = One({ this.one = One({
log: this.log.child({ module: 'one' }), log: this.log.child({ module: 'one' })
credentials: {
client_id: this.client_id,
client_secret: this.client_secret,
username: this.username,
password: this.password
}
}); });
} }
@ -48,9 +30,5 @@ NPR.Helpers = Helpers;
NPR.One = One; NPR.One = One;
/*************************** DEFAULTS *****************************/ /*************************** DEFAULTS *****************************/
proto.client_id = false;
proto.client_secret = false;
proto.username = false;
proto.password = false;
proto.one = false; proto.one = false;
proto.log = false; proto.log = false;

View file

@ -15,20 +15,13 @@ function One(config) {
util._extend(this, config || {}); util._extend(this, config || {});
if(! this.credentials)
throw('Missing API credentials');
if(! this.log) if(! this.log)
this.log = bunyan.createLogger({name: 'npr-one'}); this.log = bunyan.createLogger({name: 'npr-one'});
this.credentials.grant_type = 'password';
this.getAccessToken = this.getAccessToken.bind(this);
} }
/*************************** DEFAULTS *****************************/ /*************************** DEFAULTS *****************************/
proto.credentials = false; proto.token = false;
proto.client = false;
proto.swagger_url = 'https://api.npr.org/documentation/beryllium/api-docs'; proto.swagger_url = 'https://api.npr.org/documentation/beryllium/api-docs';
proto.log = false; 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() { proto.getAccessToken = function() {
var self = this; var self = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if(self.access_token) if(! self.token)
return resolve(self.access_token); return reject('no token supplied');
self.authorization resolve(self.token);
.createToken(self.credentials)
.then(function(res) {
self.access_token = res.access_token;
resolve(self.access_token);
})
.catch(reject);
}); });