Skip to content

Commit

Permalink
upd fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ikethecoder committed Nov 15, 2021
1 parent 3629494 commit 03d5e3b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 19 deletions.
43 changes: 43 additions & 0 deletions src/api-proxy-ds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const express = require('express');
const pathModule = require('path');

const { createProxyMiddleware } = require('http-proxy-middleware');

class ApiDSProxyApp {
constructor({ url }) {
this._url = url;
}

prepareMiddleware({ keystone }) {
const app = express();

const apiProxy = createProxyMiddleware({
target: this._url,
changeOrigin: true,
pathRewrite: { '^/int/api/': '/ds/api/' },
onProxyReq: (proxyReq, req) => {
proxyReq.removeHeader('cookie');
proxyReq.setHeader('Accept', 'application/json');
proxyReq.setHeader(
'Authorization',
`Bearer ${req.header('x-forwarded-access-token')}`
);
},
onError: (err, req, res, target) => {
console.log('CAUGHT ERROR!');
console.log(err);
res.writeHead(400, {
'Content-Type': 'text/plain',
});
res.end('error reaching api');
},
});
app.all(/^\/int\/api\//, apiProxy);

return app;
}
}

module.exports = {
ApiDSProxyApp,
};
2 changes: 1 addition & 1 deletion src/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
verbose: true,
testEnvironment: 'node',
testMatch: ['**/?(*.)+(test.{ts,js,jsx})'],
collectCoverageFrom: ['services/**/*.js'],
collectCoverageFrom: ['services/**/*.js', 'services/**/*.ts'],
coveragePathIgnorePatterns: ['.*/__mocks__/.*', '.*/@types/.*'],
coverageDirectory: '__coverage__',
coverageReporters: ['lcov', 'text-summary'],
Expand Down
46 changes: 28 additions & 18 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { ApiProxyApp } = require('./api-proxy');
const { ApiGraphqlWhitelistApp } = require('./api-graphql-whitelist');
const { ApiHealthApp } = require('./api-health');
const { ApiOpenapiApp } = require('./api-openapi');
const { ApiDSProxyApp } = require('./api-proxy-ds');

var Keycloak = require('keycloak-connect');

Expand Down Expand Up @@ -216,9 +217,13 @@ const authStrategy =
req: any,
res: any
) => {
console.log('Token = ' + token);
console.log('Redirecting to /');
res.redirect(302, '/');
const redirect = req.query?.f ? req.query.f : '/';
// Doing a 302 redirect does not set the cookie properly because it is SameSite 'Strict'
// and the Origin of the request was from an IdP
res.header('Content-Type', 'text/html');
res.send(
`<html><head><meta http-equiv="refresh" content="0;URL='${redirect}'"></head></html>`
);
},
},
hooks: {
Expand Down Expand Up @@ -268,6 +273,7 @@ const apps = [
return true;
},
}),
new ApiDSProxyApp({ url: process.env.SSR_API_ROOT }),
new ApiProxyApp({ gwaApiUrl: process.env.GWA_API_URL }),
new NextApp({ dir: 'nextapp' }),
];
Expand All @@ -282,26 +288,30 @@ const configureExpress = (app: any) => {
// console.log(req.path)
// req.path == "/" ? res.redirect('/home') : next()
// })
app.get('/feed/:entity/:refKey/:refKeyValue', (req: any, res: any) =>
getFeedWorker(keystone, req, res).catch((err: any) => {
app.get('/feed/:entity/:refKey/:refKeyValue', (req: any, res: any) => {
const context = keystone.createContext({ skipAccessControl: true });
getFeedWorker(context, req, res).catch((err: any) => {
console.log(err);
res.status(400).json({ result: 'error', error: '' + err });
})
);
app.put('/feed/:entity', (req: any, res: any) =>
putFeedWorker(keystone, req, res).catch((err: any) => {
});
});
app.put('/feed/:entity', (req: any, res: any) => {
const context = keystone.createContext({ skipAccessControl: true });
putFeedWorker(context, req, res).catch((err: any) => {
console.log(err);
res.status(400).json({ result: 'error', error: '' + err });
})
);
app.put('/feed/:entity/:id', (req: any, res: any) =>
putFeedWorker(keystone, req, res).catch((err: any) =>
});
});
app.put('/feed/:entity/:id', (req: any, res: any) => {
const context = keystone.createContext({ skipAccessControl: true });
putFeedWorker(context, req, res).catch((err: any) =>
res.status(400).json({ result: 'error', error: '' + err })
)
);
app.delete('/feed/:entity/:id', (req: any, res: any) =>
deleteFeedWorker(keystone, req, res)
);
);
});
app.delete('/feed/:entity/:id', (req: any, res: any) => {
const context = keystone.createContext({ skipAccessControl: true });
deleteFeedWorker(context, req, res);
});

app.put('/migration/import', async (req: any, res: any) => {
const { MigrationFromV1 } = require('./batch/migrationV1');
Expand Down

0 comments on commit 03d5e3b

Please sign in to comment.