Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporated new sdk-standard-components functionality into SDK #224

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/InboundServer/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getAuthorizationsById = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -62,7 +62,7 @@ const getParticipantsByTypeAndId = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -100,7 +100,7 @@ const getPartiesByTypeAndId = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -148,7 +148,7 @@ const postQuotes = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -185,7 +185,7 @@ const postTransfers = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -221,7 +221,7 @@ const getTransfersById = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -259,7 +259,7 @@ const postTransactionRequests = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -413,7 +413,7 @@ const patchTransfersById = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -486,7 +486,7 @@ const getBulkQuotesById = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -523,7 +523,7 @@ const postBulkQuotes = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -585,7 +585,7 @@ const getBulkTransfersById = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down Expand Up @@ -622,7 +622,7 @@ const postBulkTransfers = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
resourceVersions: ctx.resourceVersions,
});

Expand Down
35 changes: 24 additions & 11 deletions src/InboundServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const http = require('http');
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const EventEmitter = require('events');

const { WSO2Auth } = require('@mojaloop/sdk-standard-components');

Expand All @@ -24,15 +25,23 @@ const router = require('@internal/router');
const handlers = require('./handlers');
const middlewares = require('./middlewares');

class InboundApi {
class InboundApi extends EventEmitter {
constructor(conf, logger, cache, validator) {
super({ captureExceptions: true });
this._conf = conf;
this._cache = cache;
this._wso2Auth = new WSO2Auth({
...conf.wso2Auth,
logger,
tlsCreds: conf.outbound.tls.mutualTLS.enabled && conf.outbound.tls.creds,
this._wso2 = {
auth: new WSO2Auth({
...conf.wso2.auth,
logger,
tlsCreds: conf.outbound.tls.mutualTLS.enabled && conf.outbound.tls.creds,
}),
retryWso2AuthFailureTimes: conf.wso2.requestAuthFailureRetryTimes,
};
this._wso2.auth.on('error', (msg) => {
this.emit('error', 'WSO2 auth error in InboundApi', msg);
});

if (conf.validateInboundJws) {
this._jwsVerificationKeys = InboundApi._GetJwsKeys(conf.jwsVerificationKeysDirectory);
}
Expand All @@ -42,19 +51,19 @@ class InboundApi {
validator,
cache,
jwsVerificationKeys: this._jwsVerificationKeys,
wso2Auth: this._wso2Auth,
wso2: this._wso2,
});
}

async start() {
this._startJwsWatcher();
if (!this._conf.testingDisableWSO2AuthStart) {
await this._wso2Auth.start();
await this._wso2.auth.start();
}
}

stop() {
this._wso2Auth.stop();
this._wso2.auth.stop();
if (this._keyWatcher) {
this._keyWatcher.close();
this._keyWatcher = null;
Expand Down Expand Up @@ -93,7 +102,7 @@ class InboundApi {
}
}

static _SetupApi({ conf, logger, validator, cache, jwsVerificationKeys, wso2Auth }) {
static _SetupApi({ conf, logger, validator, cache, jwsVerificationKeys, wso2 }) {
const api = new Koa();

api.use(middlewares.createErrorHandler(logger));
Expand All @@ -104,7 +113,7 @@ class InboundApi {
api.use(middlewares.createJwsValidator(logger, jwsVerificationKeys, jwsExclusions));
}

api.use(middlewares.applyState({ cache, wso2Auth, conf }));
api.use(middlewares.applyState({ cache, wso2, conf }));
api.use(middlewares.createLogger(logger));
api.use(middlewares.createRequestValidator(validator));
api.use(middlewares.assignFspiopIdentifier());
Expand Down Expand Up @@ -134,8 +143,9 @@ class InboundApi {
}
}

class InboundServer {
class InboundServer extends EventEmitter {
constructor(conf, logger, cache) {
super({ captureExceptions: true });
this._conf = conf;
this._validator = new Validate();
this._logger = logger;
Expand All @@ -145,6 +155,9 @@ class InboundServer {
cache,
this._validator
);
this._api.on('error', (...args) => {
this.emit('error', ...args);
});
this._server = this._createServer(
conf.inbound.tls.mutualTLS.enabled,
conf.inbound.tls.creds,
Expand Down
22 changes: 11 additions & 11 deletions src/OutboundServer/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const postTransfers = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

// initialize the transfer model and start it running
Expand Down Expand Up @@ -132,7 +132,7 @@ const getTransfers = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

// initialize the transfer model and start it running
Expand Down Expand Up @@ -160,7 +160,7 @@ const putTransfers = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

// TODO: check the incoming body to reject party or quote when requested to do so
Expand Down Expand Up @@ -194,7 +194,7 @@ const postBulkTransfers = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

await model.initialize(bulkTransferRequest);
Expand Down Expand Up @@ -225,7 +225,7 @@ const getBulkTransfers = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

await model.initialize(bulkTransferRequest);
Expand Down Expand Up @@ -254,7 +254,7 @@ const postBulkQuotes = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

await model.initialize(bulkQuoteRequest);
Expand Down Expand Up @@ -285,7 +285,7 @@ const getBulkQuoteById = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

await model.initialize(bulkQuoteRequest);
Expand Down Expand Up @@ -315,7 +315,7 @@ const postRequestToPayTransfer = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

// initialize the transfer model and start it running
Expand All @@ -342,7 +342,7 @@ const putRequestToPayTransfer = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

// TODO: check the incoming body to reject party or quote when requested to do so
Expand Down Expand Up @@ -376,7 +376,7 @@ const postAccounts = async (ctx) => {
tls: ctx.state.conf.outbound.tls,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

const state = {
Expand Down Expand Up @@ -408,7 +408,7 @@ const postRequestToPay = async (ctx) => {
...ctx.state.conf,
cache: ctx.state.cache,
logger: ctx.state.logger,
wso2Auth: ctx.state.wso2Auth,
wso2: ctx.state.wso2,
});

// initialize the transfer model and start it running
Expand Down
32 changes: 22 additions & 10 deletions src/OutboundServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const koaBody = require('koa-body');
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const EventEmitter = require('events');

const { WSO2Auth } = require('@mojaloop/sdk-standard-components');

Expand All @@ -25,23 +26,30 @@ const middlewares = require('./middlewares');

const endpointRegex = /\/.*/g;

class OutboundApi {
class OutboundApi extends EventEmitter {
constructor(conf, logger, cache, validator) {
super({ captureExceptions: true });
this._logger = logger;
this._api = new Koa();
this._conf = conf;
this._cache = cache;

this._wso2Auth = new WSO2Auth({
...this._conf.wso2Auth,
logger: this._logger,
tlsCreds: this._conf.outbound.tls.mutualTLS.enabled && this._conf.outbound.tls.creds,
this._wso2 = {
auth: new WSO2Auth({
...this._conf.wso2.auth,
logger: this._logger,
tlsCreds: this._conf.outbound.tls.mutualTLS.enabled && this._conf.outbound.tls.creds,
}),
retryWso2AuthFailureTimes: conf.wso2.requestAuthFailureRetryTimes,
};
this._wso2.auth.on('error', (msg) => {
this.emit('error', 'WSO2 auth error in OutboundApi', msg);
});

this._api.use(middlewares.createErrorHandler(this._logger));
this._api.use(middlewares.createRequestIdGenerator());
this._api.use(koaBody()); // outbound always expects application/json
this._api.use(middlewares.applyState({ cache, wso2Auth: this._wso2Auth, conf }));
this._api.use(middlewares.applyState({ cache, wso2: this._wso2, conf }));
this._api.use(middlewares.createLogger(this._logger));

//Note that we strip off any path on peerEndpoint config after the origin.
Expand All @@ -53,7 +61,7 @@ class OutboundApi {
peerEndpoint: conf.peerEndpoint.replace(endpointRegex, ''),
proxyConfig: conf.proxyConfig,
logger: this._logger,
wso2Auth: this._wso2Auth,
wso2Auth: this._wso2.auth,
tls: conf.outbound.tls,
}));
}
Expand All @@ -64,21 +72,22 @@ class OutboundApi {

async start() {
if (!this._conf.testingDisableWSO2AuthStart) {
await this._wso2Auth.start();
await this._wso2.auth.start();
}
}

async stop() {
this._wso2Auth.stop();
this._wso2.auth.stop();
}

callback() {
return this._api.callback();
}
}

class OutboundServer {
class OutboundServer extends EventEmitter {
constructor(conf, logger, cache) {
super({ captureExceptions: true });
this._validator = new Validate();
this._conf = conf;
this._logger = logger;
Expand All @@ -89,6 +98,9 @@ class OutboundServer {
cache,
this._validator
);
this._api.on('error', (...args) => {
this.emit('error', ...args);
});
this._server = http.createServer(this._api.callback());
}

Expand Down
Loading