From 7dd4b15f59dd08aa57119ecb35a0a68f0aa7eeb0 Mon Sep 17 00:00:00 2001 From: Jordan Walsh Date: Thu, 9 Mar 2017 17:35:48 +1100 Subject: [PATCH] added automatic refresh on the GET function, haven't tested it properly yet --- lib/application.js | 46 +++++++++++++++++++++++++++++++++++------ test/accountingtests.js | 2 +- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/application.js b/lib/application.js index 4ee02c05..43e205f4 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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; @@ -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(); + } } }) @@ -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); @@ -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 }); } @@ -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); } }); diff --git a/test/accountingtests.js b/test/accountingtests.js index 3880438d..1336022c 100644 --- a/test/accountingtests.js +++ b/test/accountingtests.js @@ -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") {