Skip to content

Commit

Permalink
Initiate WSO2 token refresh with "start()" method. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirgene authored and bushjames committed Dec 19, 2019
1 parent d8fd912 commit 2dc52ad
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
16 changes: 10 additions & 6 deletions src/lib/WSO2Auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class WSO2Auth {
}

async _refreshToken() {
if (this.stopped) {
return;
}
this.logger.log('WSO2 token refresh initiated');
const reqOpts = {
agent: this.agent,
Expand All @@ -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() {
Expand Down
22 changes: 11 additions & 11 deletions src/lib/mojaloop-requests/mojaloopRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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}`;
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
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.4",
"version": "8.6.5",
"description": "An set of standard components for connecting to Mojaloop API enabled switches",
"main": "index.js",
"scripts": {
Expand Down
13 changes: 9 additions & 4 deletions src/test/WSO2Auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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);
Expand Down

0 comments on commit 2dc52ad

Please sign in to comment.