Comparing version
16
index.js
var api = require('./lib/api'); | ||
var _ = require('underscore'); | ||
var createClient = exports = module.exports = function(apiUrl, clientId, clientSecret) { | ||
var createClient = function(apiUrl, clientId, clientSecret) { | ||
var client = _.extend({}, api); | ||
client.init(); | ||
client.set('url', apiUrl); | ||
client.set('clientId', clientId); | ||
client.set('clientSecret', clientSecret); | ||
if (_.isFunction(clientId)) { | ||
client.set('tokenFetcher', clientId); | ||
} else { | ||
client.set('tokenFetcher', _.partial(api.getClientCredentialsToken, apiUrl + '/token', clientId, clientSecret)); | ||
} | ||
return client; | ||
}; | ||
module.exports = exports = { | ||
createClient: createClient, | ||
getClientCredentialsToken: api.getClientCredentialsToken, | ||
getResourceOwnerPasswordCredentialsToken: api.getResourceOwnerPasswordCredentialsToken | ||
}; |
@@ -7,5 +7,5 @@ var request = require('request'); | ||
var api = exports = module.exports = {}; | ||
var tokenPromise = null; | ||
api.init = function() { | ||
this.tokenPromise = null; | ||
this.settings = {}; | ||
@@ -34,18 +34,5 @@ this.defaultConfiguration(); | ||
api.getToken = function() { | ||
if (!tokenPromise) { | ||
var deferred = q.defer(); | ||
tokenPromise = deferred.promise; | ||
this.tokenPromise = this.tokenPromise || this.settings.tokenFetcher(); | ||
request( | ||
{ | ||
url: this.urlFor(this.settings.tokenEndpoint), | ||
method: 'POST', | ||
form: {grant_type: 'client_credentials', client_id: this.settings.clientId, client_secret: this.settings.clientSecret}, | ||
json: true | ||
}, | ||
resolveResponse(deferred) | ||
); | ||
} | ||
return tokenPromise.get('body').get('access_token'); | ||
return this.tokenPromise; | ||
}; | ||
@@ -129,3 +116,3 @@ | ||
} else if (api && req && isExpiredToken(res, body)) { | ||
tokenPromise = null; | ||
api.tokenPromise = null; | ||
api.getToken().then(_.bind(function(token) { | ||
@@ -146,1 +133,38 @@ req.headers.Authorization = 'Bearer ' + token; | ||
} | ||
api.getClientCredentialsToken = function(tokenUrl, clientId, clientSecret) { | ||
var deferred = q.defer(); | ||
request( | ||
{ | ||
url: tokenUrl, | ||
method: 'POST', | ||
form: {grant_type: 'client_credentials', client_id: clientId, client_secret: clientSecret}, | ||
json: true | ||
}, | ||
resolveResponse(deferred) | ||
); | ||
return deferred.promise.get('body').get('access_token'); | ||
}; | ||
api.getResourceOwnerPasswordCredentialsToken = function(tokenUrl, clientId, clientSecret, username, password, scope) { | ||
var deferred = q.defer(); | ||
var form = {grant_type: 'password', client_id: clientId, client_secret: clientSecret, username: username, password: password}; | ||
if (scope !== undefined) { | ||
form.scope = scope; | ||
} | ||
request( | ||
{ | ||
url: tokenUrl, | ||
method: 'POST', | ||
form: form, | ||
json: true | ||
}, | ||
resolveResponse(deferred) | ||
); | ||
return deferred.promise.get('body').get('access_token'); | ||
}; |
{ | ||
"name": "tol-api", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "TraderOnline api client", | ||
"dependencies": { | ||
"dependencies": { | ||
"q": "~1.0", | ||
@@ -7,0 +7,0 @@ "request": "~2.34", |
6459
16.13%151
20.8%