-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.mjs
215 lines (185 loc) · 6.22 KB
/
server.mjs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import express from 'express'
import path from 'path'
import { createProxyMiddleware } from 'http-proxy-middleware'
import winston from 'winston'
import fetch from 'cross-fetch'
import { URLSearchParams } from 'url'
import { Issuer } from 'openid-client'
import * as jose from 'jose';
import timeout from 'connect-timeout';
import { fileURLToPath } from 'url';
const app = express();
app.use(timeout('60s'));
app.disable("x-powered-by");
const azureAdConfig = {
clientId: process.env.AZURE_APP_CLIENT_ID,
clientSecret: process.env.AZURE_APP_CLIENT_SECRET,
tokenEndpoint: process.env.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT
};
let _issuer
let _remoteJWKSet
const onBehalfOf = function(scope, assertion) {
const params = new URLSearchParams();
params.append("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
params.append("client_id", azureAdConfig.clientId);
params.append("client_secret", azureAdConfig.clientSecret);
params.append("scope", scope);
params.append("assertion", assertion);
params.append("requested_token_use", "on_behalf_of");
return fetch(azureAdConfig.tokenEndpoint, {
body: params,
method: "POST"
});
}
const logger = winston.createLogger({
transports: new winston.transports.Console({
level: "info",
handleExceptions: true,
colorize: false,
}),
exitOnError: false,
});
async function validerToken(token) {
return jose.jwtVerify(token, await jwks(), {
issuer: (await issuer()).metadata.issuer,
});
}
async function jwks() {
if (typeof _remoteJWKSet === "undefined") {
_remoteJWKSet = jose.createRemoteJWKSet(new URL(process.env.AZURE_OPENID_CONFIG_JWKS_URI));
}
return _remoteJWKSet;
}
async function issuer() {
if (typeof _issuer === "undefined") {
if (!process.env.AZURE_APP_WELL_KNOWN_URL)
throw new Error(`Miljøvariabelen "AZURE_APP_WELL_KNOWN_URL" må være satt`);
_issuer = await Issuer.discover(process.env.AZURE_APP_WELL_KNOWN_URL);
}
return _issuer;
}
const validateAuthorization = async (authorization) => {
try {
const token = authorization.split(" ")[1];
const JWTVerifyResult = await validerToken(token);
return !!JWTVerifyResult?.payload;
} catch (e) {
logger.error('azure ad error', e);
return false;
}
}
const handleCallback = (req, res) => {
//let paths = req.originalUrl.split('/')
// /callback/123/456/789/012/Uføretrygd/ => ['', 'callback', '123', '456', '789', '012', 'Uføretrygd']
//logger.debug('handleCallback: redirecting to ' + redirectPath)
logger.debug('handleCallback: redirecting to redirect path')
res.redirect("/")
}
// require token
const apiAuth = function (scope) {
return async function (req, res, next) {
if (!req.headers.authorization) {
logger.debug('redirecting to /oauth2/login')
res.redirect("/oauth2/login");
} else {
try {
//logger.debug('apiAuth: trying onBehalfOf with ' + req.headers.authorization)
const response = await onBehalfOf(
scope,
req.headers.authorization.substring("Bearer ".length)
);
const body = await response.json();
//logger.debug('apiAuth: got ' + body.access_token)
if (response.ok) {
res.locals.on_behalf_of_authorization = "Bearer " + body.access_token;
next();
} else {
logger.error(
"Error fetching on behalf of token. Status code " + response.status
);
logger.error(body.error_description);
logger.error(body);
res.redirect("/oauth2/login");
}
} catch (error) {
logger.error(error.message);
res.redirect("/oauth2/login");
}
}
}
}
const apiProxy = function (target, pathRewrite) {
//logger.debug('On apiProxy, with target ' + target)
return createProxyMiddleware( {
target: target,
logLevel: 'silent',
changeOrigin: true,
xfwd: true,
// pathRewrite: pathRewrite,
onProxyReq: function onProxyReq(proxyReq, req, res) {
//logger.debug('proxy frontend: adding header auth ' + res.locals.on_behalf_of_authorization)
proxyReq.setHeader(
"Authorization",
res.locals.on_behalf_of_authorization
)
proxyReq.removeHeader('io.nais.wonderwall.session')
proxyReq.removeHeader('JSESSIONID')
proxyReq.removeHeader('cookie')
req.cookies
}
})
}
// const socketProxy = createProxyMiddleware({
// target: process.env.VITE_EESSI_PENSJON_WEBSOCKETURL + ':8080-*****/bucUpdate',
// ws: true
// })
//
const timedOut = function (req, res, next) {
if (!req.timedout) {
next()
} else {
//logger.warning('request for ' + req.originalUrl + ' timed out!')
logger.warning('request for original url timed out!')
}
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.get('/test', (req, res) => res.send('hello world'));
app.get('/callback/*', handleCallback);
app.get('/internal/isAlive|isReady|metrics', (req, res) => res.sendStatus(200));
// app.use('/assets', express.static(path.join(__dirname, "build", "assets")));
app.use('/static', express.static(path.join(__dirname, "build", "static")));
app.use('/locales', express.static(path.join(__dirname, "build", "locales")));
app.use('/favicon', express.static(path.join(__dirname, "build", "favicon")));
app.get(["/oauth2/login"], async (req, res) => {
logger.error("Wonderwall must handle /oauth2/login")
res.status(502).send({
message: "Wonderwall must handle /oauth2/login",
});
});
app.use('/api',
timedOut,
apiAuth(process.env.NEESSI_BACKEND_TOKEN_SCOPE),
apiProxy(process.env.NEESSI_BACKEND_URL,{ '^/frontend/' : '/' })
)
app.use('/v2',
timedOut,
apiAuth(process.env.NEESSI_BACKEND_TOKEN_SCOPE),
apiProxy(process.env.NEESSI_BACKEND_URL,{ '^/frontend/' : '/' })
)
app.use('/v3',
timedOut,
apiAuth(process.env.NEESSI_BACKEND_TOKEN_SCOPE),
apiProxy(process.env.NEESSI_BACKEND_URL,{ '^/frontend/' : '/' })
)
app.use('/v4',
timedOut,
apiAuth(process.env.NEESSI_BACKEND_TOKEN_SCOPE),
apiProxy(process.env.NEESSI_BACKEND_URL,{ '^/frontend/' : '/' })
)
// app.use('/websocket', socketProxy)
app.use('*', express.static(path.join(__dirname, "build")));
// start express server on port 8080
app.listen(8080, () => {
logger.info("neessi-frontend started on port 8080");
});