From 2dc52adf16f2c0e188f7c3d8cbae6eb4cb21e42d Mon Sep 17 00:00:00 2001 From: Yevhen Date: Thu, 19 Dec 2019 17:51:04 +0200 Subject: [PATCH] Initiate WSO2 token refresh with "start()" method. (#36) --- src/lib/WSO2Auth/index.js | 16 +++++++++----- src/lib/mojaloop-requests/mojaloopRequests.js | 22 +++++++++---------- src/package-lock.json | 2 +- src/package.json | 2 +- src/test/WSO2Auth.test.js | 13 +++++++---- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/lib/WSO2Auth/index.js b/src/lib/WSO2Auth/index.js index 5eac77bb..20cf0000 100644 --- a/src/lib/WSO2Auth/index.js +++ b/src/lib/WSO2Auth/index.js @@ -67,6 +67,9 @@ class WSO2Auth { } async _refreshToken() { + if (this.stopped) { + return; + } this.logger.log('WSO2 token refresh initiated'); const reqOpts = { agent: this.agent, @@ -91,16 +94,17 @@ class WSO2Auth { } catch (error) { this.logger.log(`Error performing WSO2 token refresh: ${error.message}`); } - if (!this.stopped) { - this.tokenRefreshInterval = setTimeout(this._refreshToken.bind(this), this.refreshSeconds * 1000); - } + setTimeout(this._refreshToken.bind(this), this.refreshSeconds * 1000); + } + + getToken() { + return this.token; } - async getToken() { - if (this.token === undefined && !this.tokenRefreshInterval) { + async start() { + if (this.token === undefined) { await this._refreshToken(); } - return this.token; } stop() { diff --git a/src/lib/mojaloop-requests/mojaloopRequests.js b/src/lib/mojaloop-requests/mojaloopRequests.js index 559ce305..02f1e530 100644 --- a/src/lib/mojaloop-requests/mojaloopRequests.js +++ b/src/lib/mojaloop-requests/mojaloopRequests.js @@ -183,7 +183,7 @@ class MojaloopRequests { * * @returns {object} - headers object for use in requests to mojaloop api endpoints */ - async _buildHeaders (method, resourceType, dest) { + _buildHeaders(method, resourceType, dest) { let headers = { 'content-type': `application/vnd.interoperability.${resourceType}+json;version=1.0`, 'date': new Date().toUTCString(), @@ -195,7 +195,7 @@ class MojaloopRequests { } //Need to populate Bearer Token if we are in OAuth2.0 environment - const token = await this.wso2Auth.getToken(); + const token = this.wso2Auth.getToken(); if(token) { headers['Authorization'] = `Bearer ${token}`; } @@ -233,11 +233,11 @@ class MojaloopRequests { } - async _get(url, resourceType, dest) { + _get(url, resourceType, dest) { const reqOpts = { method: 'GET', uri: buildUrl(this._pickPeerEndpoint(resourceType), url), - headers: await this._buildHeaders('GET', resourceType, dest), + headers: this._buildHeaders('GET', resourceType, dest), agent: this.agent, resolveWithFullResponse: true, simple: false @@ -247,7 +247,7 @@ class MojaloopRequests { try { this.logger.log(`Executing HTTP GET: ${util.inspect(reqOpts)}`); - return await request(reqOpts).then(throwOrJson); + return request(reqOpts).then(throwOrJson); } catch (e) { this.logger.log('Error attempting GET. URL:', url, 'Opts:', reqOpts, 'Error:', e); @@ -256,11 +256,11 @@ class MojaloopRequests { } - async _put(url, resourceType, body, dest) { + _put(url, resourceType, body, dest) { const reqOpts = { method: 'PUT', uri: buildUrl(this._pickPeerEndpoint(resourceType), url), - headers: await this._buildHeaders('PUT', resourceType, dest), + headers: this._buildHeaders('PUT', resourceType, dest), body: body, agent: this.agent, resolveWithFullResponse: true, @@ -275,7 +275,7 @@ class MojaloopRequests { try { this.logger.log(`Executing HTTP PUT: ${util.inspect(reqOpts)}`); - return await request(reqOpts).then(throwOrJson); + return request(reqOpts).then(throwOrJson); } catch (e) { this.logger.log('Error attempting PUT. URL:', url, 'Opts:', reqOpts, 'Body:', body, 'Error:', e); @@ -284,11 +284,11 @@ class MojaloopRequests { } - async _post(url, resourceType, body, dest) { + _post(url, resourceType, body, dest) { const reqOpts = { method: 'POST', uri: buildUrl(this._pickPeerEndpoint(resourceType), url), - headers: await this._buildHeaders('POST', resourceType, dest), + headers: this._buildHeaders('POST', resourceType, dest), body: body, agent: this.agent, resolveWithFullResponse: true, @@ -303,7 +303,7 @@ class MojaloopRequests { try { this.logger.log(`Executing HTTP POST: ${util.inspect(reqOpts)}`); - return await request(reqOpts).then(throwOrJson); + return request(reqOpts).then(throwOrJson); } catch (e) { this.logger.log('Error attempting POST. URL:', url, 'Opts:', reqOpts, 'Body:', body, 'Error:', e); diff --git a/src/package-lock.json b/src/package-lock.json index b1a3c5bd..767d1001 100644 --- a/src/package-lock.json +++ b/src/package-lock.json @@ -1,6 +1,6 @@ { "name": "@mojaloop/sdk-standard-components", - "version": "8.6.1", + "version": "8.6.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/package.json b/src/package.json index 258b8931..a9c38c6b 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "@mojaloop/sdk-standard-components", - "version": "8.6.4", + "version": "8.6.5", "description": "An set of standard components for connecting to Mojaloop API enabled switches", "main": "index.js", "scripts": { diff --git a/src/test/WSO2Auth.test.js b/src/test/WSO2Auth.test.js index 383f5b51..106698f8 100644 --- a/src/test/WSO2Auth.test.js +++ b/src/test/WSO2Auth.test.js @@ -48,8 +48,8 @@ async function testTokenRefresh(t, userRefreshSeconds, tokenExpiresMs) { return {access_token: TOKEN, expires_in: tokenExpiresMs}; }); const auth = new WSO2Auth(opts); - const token = await auth.getToken(); - auth.stop(); + await auth.start(); + const token = auth.getToken(); t.assert(request.Request.calledOnce); t.is(request.Request.getCall(0).args[0].headers['Authorization'], `Basic ${basicToken}`); t.is(token, TOKEN); @@ -60,6 +60,7 @@ async function testTokenRefresh(t, userRefreshSeconds, tokenExpiresMs) { t.assert(request.Request.calledTwice); const tokenRefreshInterval = tokenRefreshTime - now; t.assert((tokenRefreshInterval - actualRefreshMs) < 1000); + auth.stop(); } test('should return static token when static token was provided', async t => { @@ -68,7 +69,10 @@ test('should return static token when static token was provided', async t => { logger: loggerStub, staticToken: TOKEN }); - t.is(await auth.getToken(), TOKEN); + await auth.start(); + const token = auth.getToken(); + t.is(token, TOKEN); + auth.stop(); }); test.serial('should return new token when token API info was provided', async t => { @@ -83,7 +87,8 @@ test.serial('should return new token when token API info was provided', async t .toString('base64'); sandbox.stub(request, 'Request').resolves({access_token: TOKEN}); const auth = new WSO2Auth(opts); - const token = await auth.getToken(); + await auth.start(); + const token = auth.getToken(); t.assert(request.Request.calledOnce); t.is(request.Request.getCall(0).args[0].headers['Authorization'], `Basic ${basicToken}`); t.is(token, TOKEN);