forked from jlguenego/node-expose-sspi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-proxy-server.js
58 lines (49 loc) · 1.44 KB
/
reverse-proxy-server.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
const express = require('express');
const { sso } = require('node-expose-sspi');
const { createProxyMiddleware } = require('http-proxy-middleware');
// Thanks https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
function str2ab(str) {
const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
const bufView = new Uint16Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
{
// Business server
const app = express();
app.use((req, res, next) => {
if (req.header('x-sso')) {
req.sso = JSON.parse(ab2str(sso.decode(req.header('x-sso'))));
}
next();
});
app.use((req, res) => {
res.json({
sso: req.sso,
});
});
app.listen(3000, () => console.log('Server started on port 3000'));
}
{
// Reverse proxy
const proxy = createProxyMiddleware('http://localhost:3000');
const app = express();
app.use(sso.auth({ useOwner: false }));
app.use((req, res, next) => {
if (req.sso) {
// avoid header too large (error HTTP 431), so reduce it.
const xSso = {
user: req.sso.user,
};
req.headers['x-sso'] = sso.encode(str2ab(JSON.stringify(xSso)));
}
next();
});
app.use(proxy);
app.listen(9000, () => console.log('Reverse proxy started on port 9000'));
}