@@ -145,6 +145,48 @@ router.post('/ping/**', async (req, res) => {
145
145
}
146
146
} ) ;
147
147
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
+
148
190
/**
149
191
* Forward requests to actual API using the API Configuration
150
192
*/
@@ -154,6 +196,7 @@ router.all('/:name/**', async (req, res) => {
154
196
$or : [ { name : req . params . name } , { id : req . params . name } ] ,
155
197
status : 'active' ,
156
198
} ) . select ( 'name authType endpoint settings id' ) ;
199
+
157
200
if ( ! api ) {
158
201
return res . status ( 404 ) . send ( i18next . t ( 'common.errors.dataNotFound' ) ) ;
159
202
}
0 commit comments