Skip to content

Commit 3c3e00c

Browse files
committed
it works
1 parent 2115ffb commit 3c3e00c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

config/custom-environment-variables.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ module.exports = {
6060
clientId: 'COMMON_SERVICES_CLIENT_ID',
6161
clientSecret: 'COMMON_SERVICES_CLIENT_SECRET',
6262
scope: 'COMMON_SERVICES_SCOPE',
63+
authUrl: 'COMMON_SERVICES_AUTH_URL',
64+
url: 'COMMON_SERVICES_URL',
6365
},
6466
};

src/routes/proxy/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,48 @@ router.post('/ping/**', async (req, res) => {
145145
}
146146
});
147147

148+
router.all('/common-services/**', async (req, res) => {
149+
const commonServiceConfig = {
150+
name: 'common-services',
151+
status: 'active',
152+
authType: 'service-to-service',
153+
endpoint: config.get<string>('commonServices.url'),
154+
settings: {
155+
authTargetUrl: config.get<string>('commonServices.authUrl'),
156+
apiClientId: config.get<string>('commonServices.clientId'),
157+
scope: config.get<string>('commonServices.scope'),
158+
secret: config.get<string>('commonServices.clientSecret'),
159+
},
160+
};
161+
162+
//encrypt settings
163+
const settings = {
164+
authTargetUrl: commonServiceConfig.settings.authTargetUrl,
165+
apiClientID: commonServiceConfig.settings.apiClientId,
166+
scope: commonServiceConfig.settings.scope,
167+
safeSecret: commonServiceConfig.settings.secret,
168+
};
169+
const encryptedSettings = CryptoJS.AES.encrypt(
170+
JSON.stringify(settings),
171+
config.get('encryption.key')
172+
).toString();
173+
174+
//save common service config
175+
const commonServiceConfigToSave = {
176+
...commonServiceConfig,
177+
settings: encryptedSettings,
178+
};
179+
180+
try {
181+
const api = new ApiConfiguration(commonServiceConfigToSave);
182+
const path = req.originalUrl.split('common-services').pop().substring(1);
183+
await proxyAPIRequest(req, res, api, path);
184+
} catch (err) {
185+
logger.error(err.message, { stack: err.stack });
186+
return res.status(500).send(req.t('common.errors.internalServerError'));
187+
}
188+
});
189+
148190
/**
149191
* Forward requests to actual API using the API Configuration
150192
*/
@@ -154,6 +196,7 @@ router.all('/:name/**', async (req, res) => {
154196
$or: [{ name: req.params.name }, { id: req.params.name }],
155197
status: 'active',
156198
}).select('name authType endpoint settings id');
199+
157200
if (!api) {
158201
return res.status(404).send(i18next.t('common.errors.dataNotFound'));
159202
}

0 commit comments

Comments
 (0)