node-http-proxy
node-http-proxy copied to clipboard
auth config is not working
//
// Basic Http Proxy Server
//
httpProxy.createServer({
target:'http://localhost:9003',
auth: 'amos:gogo'
}).listen(8003);
I'm seeing the same thing. Did you ever figure out what was going on?
Same.
Same.
@shallwefootball try using header directly:
httpProxy.createServer({
target:'http://localhost:9003',
auth: 'amos:gogo',
headers: {
Authorization: 'the calculation result here'
}
}).listen(8003);
It works for me.
This still does not work with the headers...does anyone have another example? Do we have to strip out any headers from request?
this STILL does not work
Very broken, very sad :(
I am going to try to add this npm package and handle the auth separately https://www.npmjs.com/package/basic-auth
Here is how you can use https://www.npmjs.com/package/basic-auth to provide auth with node-http-proxy
:
const http = require('http'),
httpProxy = require('http-proxy'),
auth = require('basic-auth');
//
// Create a proxy server with custom application logic
//
const proxy = httpProxy.createProxyServer({changeOrigin: true, autoRewrite: true, hostRewrite: true, followRedirects: true});
const server = http.createServer(function(req, res) {
const authRequired = true;//subdomain.endsWith('-p');
if (authRequired) {
const credentials = auth(req)
if (!credentials || !check(credentials.name, credentials.pass)) {
res.statusCode = 401
res.setHeader('WWW-Authenticate', 'Basic realm="example"')
res.end('Access denied, please contact the BBI team for access.')
} else {
// do nothing, carry on
// res.end('Access granted')
}
}
proxy.on('proxyRes', function(proxyRes, req, res) {
// console.log('Raw [target] response', JSON.stringify(proxyRes.headers, true, 2));
proxyRes.headers['x-reverse-proxy'] = "password-proxy";
});
proxy.web(req, res, { target: `https://macao20.com` });
});
console.log("reverse proxy started on port 3000...");
server.listen(3000);
const check = function (name, pass) {
var valid = true
// Simple method to prevent short-circut and use timing-safe compare
valid = name === 'john' && valid
valid = pass === 'secret' && valid
return valid
}
What is working is setting the header (again):
{
target: 'some.target',
onProxyReq: (proxyReq) => {
// Removing existing cookie and overwriting header with authorized credentials
const authHeader = Buffer.from(
`${process.env.DATABASE_USER}:${process.env.DATABASE_PASSWORD}`,
).toString('base64');
proxyReq.setHeader('authorization', `Basic ${authHeader}`);
},
}
What is not working is:
{
target: 'some.target',
auth: 'user:pass'
}
If the request to the proxy already has a auth header. In this case the existing auth header will not be overwritten.