don't enforce one auth grant_type. allow user to pass access token
This commit is contained in:
parent
f97b43b1e1
commit
74faf24801
2 changed files with 26 additions and 40 deletions
24
index.js
24
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;
|
||||
|
|
|
|||
42
lib/one.js
42
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);
|
||||
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue