bitfocus / companion-module-generic-http

Generic HTTP module
MIT License
9 stars 19 forks source link

v3 Not working with { "Authorization": "basic 123456789123456789==" } header #49

Closed jgwhitebeard closed 10 months ago

jgwhitebeard commented 10 months ago

Hello. I have 2 versions of companion running the generic-http module with identical connection and button settings. V2.2.0 authenticates fine using the "Authorization" header, but V3.1.2 gets "HTTP GET Request failed (Response code 401 (Unauthorized))". Am I missing something? Thanks.

For Info: The device I'm controlling is the Lindy Power Switch Classic 8 using the web cgi API.

Julusian commented 10 months ago

I'm not able to reproduce this. If I create a simple http server expecting basic authentication with:

import express from "express";

const app = express();

function authentication(req, res, next) {
  const authheader = req.headers.authorization;
  console.log(req.headers);

  if (!authheader) {
    let err = new Error("You are not authenticated!");
    res.setHeader("WWW-Authenticate", "Basic");
    err.status = 401;
    return next(err);
  }

  const auth = new Buffer.from(authheader.split(" ")[1], "base64")
    .toString()
    .split(":");
  const user = auth[0];
  const pass = auth[1];

  if (user == "admin" && pass == "password") {
    // If Authorized user
    next();
  } else {
    let err = new Error("You are not authenticated!");
    res.setHeader("WWW-Authenticate", "Basic");
    err.status = 401;
    return next(err);
  }
}

app.use(authentication);

app.use("/abc", (req, res) => {
  console.log("ok");
  // console.log(req);
  res.sendStatus(200);
});

// Server setup
app.listen(3123, () => {
  console.log("Server is Running ");
});

And make the request: image

It successfully authenticates

jgwhitebeard commented 10 months ago

Thanks for testing it Julusian. Still couldn't get it to work for me today so reinstalled and it's working now. Very strange. Sorry to have taken up your time.