0

Whats the situation?

I've got staging site which is built with Django + React.

Parts of the API you have to login to access. I'm using Django's token authentication for that.

I then wanted to put the entire site behind basic auth, to prevent anyone of accidentally stumbling across it.

What's the problem?

This means I need to pass two authentication methods with my requests. This is possible as described here.

Authorization: Token lksdjf893kj2nlk2n3rl2dOPOnm, Basic YXNkZnNhZGZzYWRmOlZLdDVOMVhk

The token is set in my JS code after being provided to the user when they login in.

Basic authentication is triggered on the first page load, after this the browser stores it and I believe automatically appends it onto any requests where the server has the following header:

WWW-Authenticate: basic

I have configured Django to return the following header:

WWW-Authenticate: basic, token

This successfully causes a XHR request sent via axios to have the basic header appended, when the Authorization header is empty.

The problem is the Authorization header isn't empty, because I need to set a token value in there.

 const axiosConfig = {
    method: requestType,
    url: `${url}`,
    withCredentials: true,
    headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
    },
    data: payload
};

// If we're logged in then send our auth token
if (localStorage.auth_token) {
    // Axios can't see the basic authentication header here so we can't append.
    console.log(axiosConfig.headers.Authorization);

    // Basic auth will only get sent if I don't set anything here
    axiosConfig.headers.Authorization = `Token ${localStorage.auth_token}`;
}

At this point the browser doesn't seem to append the basic header anymore and so my authentication fails.

Is there a way around this?

I wanted a blanket basic auth because there's no way I can accidentally expose anything on staging, otherwise I have to rely entirely on the token authentication and robots.txt which is less than ideal.

Dominic Woodman
  • 563
  • 1
  • 5
  • 17

1 Answers1

0

The answer in the end was port forwarding.

I removed basic auth, turned off ports 80 and 443 and then used port forwarding to map my SSH to local host.

i.e. ssh -N -L 8755:127.0.0.1:443 user@ip_address

Dominic Woodman
  • 563
  • 1
  • 5
  • 17