-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from mojaloop/feature/MBXSDK-9
Add OAuth2.0 periodic token refresh functionality
- Loading branch information
Showing
5 changed files
with
280 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/************************************************************************** | ||
* (C) Copyright ModusBox Inc. 2019 - All rights reserved. * | ||
* * | ||
* This file is made available under the terms of the license agreement * | ||
* specified in the corresponding source code repository. * | ||
* * | ||
* ORIGINAL AUTHOR: * | ||
* Yevhen Kyriukha - [email protected] * | ||
**************************************************************************/ | ||
|
||
'use strict'; | ||
|
||
const request = require('request-promise-native'); | ||
|
||
const DEFAULT_REFRESH_INTERVAL_SECONDS = 3600; | ||
|
||
/** | ||
* Obtain WSO2 bearer token and periodically refresh it | ||
*/ | ||
class WSO2Auth { | ||
/** | ||
* | ||
* @param {Object} opts | ||
* @param {String} opts.logger | ||
* @param {String} [opts.clientKey] Customer Key | ||
* @param {String} [opts.clientSecret] Customer Secret | ||
* @param {String} [opts.tokenEndpoint] WSO2 Endpoint URL | ||
* @param {String} [opts.refreshSeconds] WSO2 token refresh interval in seconds | ||
* @param {String} [opts.staticToken] WSO2 static bearer token | ||
*/ | ||
constructor(opts) { | ||
this.logger = opts.logger; | ||
this.refreshSeconds = opts.refreshSeconds || DEFAULT_REFRESH_INTERVAL_SECONDS; | ||
|
||
if (this.refreshSeconds <= 0) { | ||
throw new Error('WSO2 auth config: token must be a positive integer value'); | ||
} | ||
if (!this.logger) { | ||
throw new Error('WSO2 auth config requires logger property'); | ||
} | ||
|
||
if (opts.tokenEndpoint && opts.clientKey && opts.clientSecret) { | ||
this.basicToken = Buffer.from(`${opts.clientKey}:${opts.clientSecret}`) | ||
.toString('base64'); | ||
this.endpoint = opts.tokenEndpoint; | ||
} else if (opts.staticToken) { | ||
this.logger.info('WSO2 auth config token API data not set, fallback to static token'); | ||
this.token = opts.staticToken; | ||
} else { | ||
// throw new Error('WSO2 auth error: neither token API data nor static token is set'); | ||
this.token = null; | ||
} | ||
} | ||
|
||
async refreshToken() { | ||
this.logger.debug('WSO2 token refresh initiated'); | ||
const reqOpts = { | ||
method: 'POST', | ||
uri: this.endpoint, | ||
headers: { | ||
'Authorization': `Basic ${this.basicToken}`, | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: { | ||
grant_type: 'client_credentials' | ||
}, | ||
json: true | ||
}; | ||
try { | ||
const response = await request(reqOpts); | ||
this.token = response.access_token; | ||
this.logger.debug('WSO2 token refreshed successfully'); | ||
} catch (error) { | ||
this.logger.error(`Error performing WSO2 token refresh: ${error.cause}`); | ||
} | ||
} | ||
|
||
async getToken() { | ||
if (this.token === undefined && !this.tokenRefreshInterval) { | ||
await this.refreshToken(); | ||
this.tokenRefreshInterval = setInterval(this.refreshToken.bind(this), this.refreshSeconds * 1000); | ||
} | ||
return this.token; | ||
} | ||
} | ||
|
||
module.exports = WSO2Auth; |
Oops, something went wrong.