Skip to content

Instantly share code, notes, and snippets.

@dunnza
Last active November 7, 2022 07:54
Show Gist options
  • Save dunnza/67c7f1705daba5a1bb5f750d5f5766b1 to your computer and use it in GitHub Desktop.
Save dunnza/67c7f1705daba5a1bb5f750d5f5766b1 to your computer and use it in GitHub Desktop.
How to proxy api requests with NTLM authentication in a create-react-app project
const http = require("http");
const proxy = require("http-proxy-middleware");
module.exports = function(app) {
app.use(
"/api",
proxy({
agent: new http.Agent({ keepAlive: true }),
target: "http://example.com/",
onProxyRes(proxyRes) {
const header = "www-authenticate";
proxyRes.headers[header] =
proxyRes.headers[header] && proxyRes.headers[header].split(",");
}
})
);
};
@dunnza
Copy link
Author

dunnza commented Feb 14, 2019

I still do not 100% understand how this works, but the primary issues seem to be

  1. That the connection is not kept alive throughout the challenge and client response
  2. The "www-authenticate" header needs to be an array, but instead it is a single comma separated string

We use an http agent to keep the connection alive, and we use the onProxyRes hook to massage the "www-authenticate" header into the correct format.

I first found this issue which described the problem, which was then referenced by this pull request. The solution was laid out in the comments. The solution eventually got added to the examples in the repo, but it's not obvious that it's out there.

@nullberri
Copy link

Still fails most of the time. I usually have to make an API call in a new tab (ie http://testEnv/api/bootstrap) to the real site to get any connections to work via the local proxy on any consistent basis. otherwise it just get hung on sending basic auth info over and over again.

@dunnza
Copy link
Author

dunnza commented Feb 14, 2019

@nullberri what is your setup like? I'm running an ASP.NET Core WebAPI application through Visual Studio (IIS Express). I haven't encountered any problems yet ...

@TeonWasTaken
Copy link

@dunnza - thanks very much for sharing this. Worked perfectly for me and got me out of a bind.

@dunnza
Copy link
Author

dunnza commented Jun 6, 2019

@TeonWasTaken I'm glad it helped you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment