Search code examples
node.jspassport.jspassport-azure-ad

Passport.js treating azure-ad authentication link as relative, returns 404 on attempted login


We are using passport-azure-ad to allow users in our domain to log into an internal node app with their Microsoft AD credentials. I had this system working previously with just 1 node app, but now we are trying a new system where we have 2 separate apps running locally with PM2 and serving them up through IIS under the same URL but you access each app like this: mywebsite.com/app1 OR mywebsite.com/app2

Only the second app needs authentication. The issue I am running into is when you hit login, the passport.authenticate method seems to be trying to redirect to a relative authorization link: mywebsite.com/{tenant}/oauth2/v2.0/authorize , rather than the actual authorization endpoint which is: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize

Does anyone know why this may be and what I could do to stop passport from treating the auth endpoint like a relative link? This causes a 404 every time a user hits /login because /{tenant}/oauth2/v2.0/authorize route doesn't exist.


Solution

  • RIP to anyone finding this in the future.

    For me, the solution was as follows:

    • Open IIS and select your SERVER (not site), then open Application Request Routing Cache under IIS enter image description here
    • On the far right Actions pane, select Server Proxy Settings... under Proxy enter image description here
    • UNCHECK Reverse rewrite host in response headers checkbox under Proxy Setting, and make sure you hit Apply on right pane! enter image description here
    • It is important to note I also had to update my entry point app.js to use extended url encoding: app.use(express.urlencoded({ extended: true }));, as well as updating the redirect URLs in your authentication routes. Azure will also need to know your redirect URIs.