Skip to content

Commit

Permalink
Fix WSO2 token refresh issue. (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirgene authored and bushjames committed Dec 19, 2019
1 parent 2dc52ad commit 235821a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/lib/WSO2Auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class WSO2Auth {
this.token = response.access_token;
const tokenExpiry = ((typeof response.expires_in === 'number') && (response.expires_in > 0))
? response.expires_in : Infinity;
this.refreshSeconds = Math.min(this.refreshSeconds * 1000, tokenExpiry) / 1000;
this.refreshSeconds = Math.min(this.refreshSeconds, tokenExpiry);
this.logger.log('WSO2 token refreshed successfully');
} catch (error) {
this.logger.log(`Error performing WSO2 token refresh: ${error.message}`);
Expand Down
2 changes: 1 addition & 1 deletion src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mojaloop/sdk-standard-components",
"version": "8.6.5",
"version": "8.6.6",
"description": "An set of standard components for connecting to Mojaloop API enabled switches",
"main": "index.js",
"scripts": {
Expand Down
16 changes: 8 additions & 8 deletions src/test/WSO2Auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ test.afterEach.always(() => {
sandbox.restore();
});

async function testTokenRefresh(t, userRefreshSeconds, tokenExpiresMs) {
async function testTokenRefresh(t, userRefreshSeconds, tokenExpiresSeconds) {
const TOKEN = 'new-token';
const tokenExpiry = ((typeof tokenExpiresMs === 'number') && (tokenExpiresMs > 0))
? tokenExpiresMs : Infinity;
const actualRefreshMs = Math.min(userRefreshSeconds * 1000, tokenExpiry);
const tokenExpirySeconds = ((typeof tokenExpiresSeconds === 'number') && (tokenExpiresSeconds > 0))
? tokenExpiresSeconds : Infinity;
const actualRefreshMs = Math.min(userRefreshSeconds, tokenExpirySeconds) * 1000;
const opts = {
logger: loggerStub,
clientKey: 'client-key',
Expand All @@ -45,7 +45,7 @@ async function testTokenRefresh(t, userRefreshSeconds, tokenExpiresMs) {
let tokenRefreshTime = now;
sandbox.stub(request, 'Request').callsFake(async () => {
tokenRefreshTime = Date.now();
return {access_token: TOKEN, expires_in: tokenExpiresMs};
return {access_token: TOKEN, expires_in: tokenExpiresSeconds};
});
const auth = new WSO2Auth(opts);
await auth.start();
Expand All @@ -59,7 +59,7 @@ async function testTokenRefresh(t, userRefreshSeconds, tokenExpiresMs) {
});
t.assert(request.Request.calledTwice);
const tokenRefreshInterval = tokenRefreshTime - now;
t.assert((tokenRefreshInterval - actualRefreshMs) < 1000);
t.assert((tokenRefreshInterval - actualRefreshMs) < 500);
auth.stop();
}

Expand Down Expand Up @@ -96,7 +96,7 @@ test.serial('should return new token when token API info was provided', async t
});

test.serial('should refresh token using user provided interval value', t =>
testTokenRefresh(t, 3, 1000e3));
testTokenRefresh(t, 3, 1000));

test.serial('should refresh token using user provided interval value when token expiry is negative', t =>
testTokenRefresh(t, 3, -1));
Expand All @@ -105,4 +105,4 @@ test.serial('should refresh token using user provided interval value when token
testTokenRefresh(t, 3, '1'));

test.serial('should refresh token using OAuth2 token expiry value', t =>
testTokenRefresh(t, 3600, 3e3));
testTokenRefresh(t, 4, 3));

0 comments on commit 235821a

Please sign in to comment.