-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdev-app-orchstub.js
100 lines (87 loc) · 3.04 KB
/
dev-app-orchstub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// THIS IS FOR DEV TESTING ONLY
const express = require("express");
const pino = require("pino");
const axios = require("axios").default;
const url = require("url");
const stubUrls = {
authdev2: "https://orchstub.authdev2.sandpit.account.gov.uk",
authdev1: "https://orchstub.authdev1.sandpit.account.gov.uk",
dev: "https://orchstub.signin.dev.account.gov.uk",
};
require("dotenv").config();
const logger = pino({ level: process.env.LOG_LEVEL || "info" });
const app = express();
const port = process.env.PORT || 3002;
const frontendPort = process.env.FRONTEND_PORT || 3000;
const stubUrl = stubUrls[process.env.DEPLOYMENT_NAME];
if (stubUrl === undefined) {
logger.warn(
`Warning: No orch stub configured for environment ${process.env.DEPLOYMENT_NAME}`
);
}
const getCookieValue = (cookie, cookieName) => {
let value;
cookie.forEach((item) => {
const name = item.split("=")[0].toLowerCase().trim();
if (name.indexOf(cookieName) !== -1) {
value = item.split("=")[1];
}
});
return value;
};
const setGsCookie = (orchStubResponse, res) => {
let sessionCookieValue;
const sessionCookie = orchStubResponse.headers["set-cookie"][0];
if (sessionCookie) {
sessionCookieValue = getCookieValue(sessionCookie.split(";"), "gs");
res.cookie("gs", sessionCookieValue, {
maxAge: new Date(new Date().getTime() + 60 * 60000),
});
}
};
const setLngCookie = (orchStubResponse, res) => {
let lngCookieValue;
const lngCookie = orchStubResponse.headers["set-cookie"][2];
if (lngCookie) {
lngCookieValue = getCookieValue(lngCookie.split(";"), "lng");
res.cookie("lng", lngCookieValue, {
maxAge: new Date(new Date().getTime() + 60 * 60000),
});
}
};
const buildRedirectURL = (response) => {
const location = url.parse(response.headers.location, true);
logger.info(`orch response location is: ${JSON.stringify(location)}`);
const params = new URLSearchParams(location.query);
const redirectUri = new URL(`http://localhost:${frontendPort}/authorize`);
redirectUri.search = params.toString();
return redirectUri;
};
app.get("/", async (req, res) => {
if (stubUrl === undefined) {
res.send(
"No orchestration stub url defined for this environment. You may want to use the RP stub at localhost:2000 instead. " +
`Currently, this stub is for environments configured for the orchestration stub (${Object.keys(stubUrls).join(", ")})`
);
}
// call orch stub
const orchStubResponse = await axios.post(
stubUrl,
"reauthenticate=&level=Cl.Cm&authenticated=no&authenticatedLevel=Cl.Cm&channel=none",
{
validateStatus: (status) => {
return status === 302;
},
maxRedirects: 0,
}
);
setGsCookie(orchStubResponse, res);
setLngCookie(orchStubResponse, res);
const redirectUrl = buildRedirectURL(orchStubResponse);
res.redirect(redirectUrl.toString());
});
app.listen(port, () => {
logger.info("TEST APP TO REDIRECT FOR NEW SESSION : DEV ONLY");
logger.info(`RUNNING ON http://localhost:${port}`);
logger.info(`FRONTEND PORT: ${frontendPort}`);
});