Skip to content

Commit

Permalink
added automatic refresh on the GET function, haven't tested it proper…
Browse files Browse the repository at this point in the history
…ly yet
  • Loading branch information
Jordan Walsh committed Mar 9, 2017
1 parent 9bcd056 commit 7dd4b15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
46 changes: 40 additions & 6 deletions lib/application.js
Expand Up @@ -207,10 +207,13 @@ Object.assign(Application.prototype, {
if (options.format)
self.oa._headers['Accept'] = 'application/' + options.format;

if (options.pager)
getResource(options.pager.start || 1)
else
getResource();
self.checkExpiry()
.then(function() {
if (options.pager)
getResource(options.pager.start || 1)
else
getResource();
});

function getResource(offset) {
var endPointUrl = options.api === 'payroll' ? self.options.payrollAPIEndPointUrl : self.options.coreAPIEndPointUrl;
Expand Down Expand Up @@ -413,6 +416,17 @@ Object.assign(Application.prototype, {
var builder = new xml2js.Builder({ rootName: rootName, headless: true });
var obj = builder.buildObject(obj);
return obj;
},
checkExpiry: function() {
var d1 = new Date(this.options.tokenExpiry),
d2 = new Date();

if (d2 > d1) {
return this.refreshAccessToken();
} else {
logger.debug("Dates are fine, no need for refresh");
return Promise.resolve();
}
}
})

Expand Down Expand Up @@ -475,7 +489,15 @@ var RequireAuthorizationApplication = Application.extend({
if (err)
reject(err);
else {
self.setOptions({ accessToken: results.oauth_token, accessSecret: results.oauth_token_secret, sessionHandle: results.oauth_session_handle });
var exp = new Date();
exp.setSeconds(exp.getSeconds() + results.oauth_expires_in);

self.setOptions({
accessToken: results.oauth_token,
accessSecret: results.oauth_token_secret,
sessionHandle: results.oauth_session_handle,
tokenExpiry: exp.toISOString()
});
resolve({ results: results });
}
callback && callback.apply(callback, arguments);
Expand All @@ -490,7 +512,15 @@ var RequireAuthorizationApplication = Application.extend({
if (err)
reject(err);
else {
self.setOptions({ accessToken: results.oauth_token, accessSecret: results.oauth_token_secret, sessionHandle: results.oauth_session_handle });
var exp = new Date();
exp.setSeconds(exp.getSeconds() + results.oauth_expires_in);

self.setOptions({
accessToken: results.oauth_token,
accessSecret: results.oauth_token_secret,
sessionHandle: results.oauth_session_handle,
tokenExpiry: exp.toISOString()
});
resolve({ results: results });
}

Expand All @@ -506,6 +536,10 @@ var RequireAuthorizationApplication = Application.extend({
this.options.accessToken = options.accessToken;
this.options.accessSecret = options.accessSecret;
this.options.sessionHandle = options.sessionHandle;
this.options.tokenExpiry = options.tokenExpiry;

console.log("options set!");
console.log(this.options);
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/accountingtests.js
Expand Up @@ -85,7 +85,7 @@ describe('get access for public or partner application', function() {
runScripts: false
});

browser.debug();
//browser.debug();

before(function(done) {
if (APPTYPE === "PRIVATE") {
Expand Down

0 comments on commit 7dd4b15

Please sign in to comment.