Express NTLM Error when parsing Authorization header

1.1k views Asked by At

I posted a question here but after some investigation I have realised it wasn't my axios request that was causing the issue. I'm not sure how to close a question sorry.

I am receiving the following error when trying to send an Axios Get request to my express backend server. Front end is hosted on localhost:8080 and backend is localhost:5000.

[express-ntlm] Error when parsing Authorization header for URI http://localhost:5000/api/user/generateToken

Here is my Client Side Axios Request

axios.defaults.baseURL = 'http://localhost:5000/';

const generateToken = () => {
    axios
      .get('/api/user/generateToken', {
        withCredentials: true,
      })
      .then(payload => {
        // Set Auth Header with returned JWT Token
      })
      .catch(err => {
        console.log(err.response);
      });

This is the first request that is sent after the site is first launched and it should return a JWT Token which I will use to log the user in.

I then have express-ntlm setup to get the users logged in username to check if their in our database and if so return a valid token.

server code that sets up express-ntlm is

const express = require('express');
const http = require('http');
const path = require('path');
const cors = require('cors');
const ntlm = require('express-ntlm');
const passport = require('passport');

const server = express();

server.use(
  ntlm({
    debug: function() {
      var args = Array.prototype.slice.apply(arguments);
      console.log.apply(null, args);
    },
    domain: 'domain',
    domaincontroller: 'ldap://domainAddress',
  })
);

If I then navigate in a browser directly to http://localhost:5000/api/user/generateToken I get my expected result. however if I use axios.get and proxy to the url I get the above error in my server console and a 400 Bad Request in the client Console.

Here is the Router.get code

// @route   GET api/user/generateToken
// @desc    Create JWT Token
// @access  Public
router.get('/generateToken', (req, res) => {
  const errors = {};

  const username = req.ntlm.UserName;

  return User.findOne({ username }).then(user => {
    // Create JWT and return it to the user
  });
});

I'm not sure where to go from here its either some aditional settings needed for axios to set the header or something i need to change for express-ntlm. its strange as I can go to the page directly and it works but use axios and it fails

1

There are 1 answers

0
kost On

Try to print headers to console and check what's wrong with the Authorization header. Probably Axios modifies it.

Compare the headers you get from the manual athorization vs. through axios.