Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Auth with Router #155

Open
rbnrtg opened this issue Oct 2, 2024 · 5 comments
Open

Basic Auth with Router #155

rbnrtg opened this issue Oct 2, 2024 · 5 comments

Comments

@rbnrtg
Copy link

rbnrtg commented Oct 2, 2024

Hi,

I have these methods defined from Router and assigning that Router to Application.

router.get("/ip/:ip", &change_ip_arduino);
router.get("/id", &get_id_arduino);
router.get("/semaphore/:rgbId/:time", &on_semaphore);
router.get("/offsemaphore", &off_semaphore);
router.get("/beat/:rgbId/:ncodes", &beat);
router.get("/spiral/:type/:rgbId/:ncodes", &spiral);
router.get("/alarm/:time", &on_alarm);
router.get("/offalarm", &off_alarm);
router.get("/shortlongalarm/:alarmtype/:ncodes", &short_long_alarm);
router.get("/combined/:semaphoretype/:rgbId/:time/:ncodes", &combined);
router.get("/relay/:type/:relayId", &on_off_relay);
router.get("/watchdog/:time", &watchdog);
router.get("/dhcp/:type", &add_dhcp);
router.get("/dhcp", &dhcpState);
router.get("/reset", &resetIP);

app.use("/box", &router);

And I am trying to be able to verify authentication with Router and Application instead of Application only as in this example: https://github.com/lasselukkari/aWOT/blob/master/examples/AuthMiddleware/AuthMiddleware.ino

How could I perform this authentication?

@lasselukkari
Copy link
Owner

Did you already try adding router.use(&auth); before the router.get("/ip/:ip", &change_ip_arduino);?

@lasselukkari
Copy link
Owner

I'm not sure did I understand you correctly. If you want to have the whole app including the router protected by the auth middleware just mount the app.use(&auth); as as the first things before app.use("/box", &router);.

@rbnrtg
Copy link
Author

rbnrtg commented Oct 3, 2024

Yes, I am trying to make authentication available for all calls in the application but when the auth() function is executed it always returns a 401 code because the Authorization header is not obtained correctly. What could be the problem?:

void auth(Request &req, Response &res) {
char * authHeader = req.get("Authorization");
Serial.println(authHeader);
if (!str_iseq(authHeader, "Basic c3VwZXI6YWRtaW4=")) { // super:admin in base64
res.set("WWW-Authenticate", "Basic realm="Secret Area"");
res.sendStatus(401);
res.end();
}
}

@rbnrtg
Copy link
Author

rbnrtg commented Oct 3, 2024

Sorry, I solved this problem with this:

app.header("Authorization", authBuffer, 200);
app.use(&auth);

but I have another problem as I have all the functions I mentioned above defined in my API (15 functions):

router.get("/ip/:ip", &change_ip_arduino);
router.get("/id", &get_id_arduino);
router.get("/semaphore/:rgbId/:time", &on_semaphore);
router.get("/offsemaphore", &off_semaphore);
router.get("/beat/:rgbId/:ncodes", &beat);
router.get("/spiral/:type/:rgbId/:ncodes", &spiral);
router.get("/alarm/:time", &on_alarm);
router.get("/offalarm", &off_alarm);
router.get("/shortlongalarm/:alarmtype/:ncodes", &short_long_alarm);
router.get("/combined/:semaphoretype/:rgbId/:time/:ncodes", &combined);
router.get("/relay/:type/:relayId", &on_off_relay);
router.get("/watchdog/:time", &watchdog);
router.get("/dhcp/:type", &add_dhcp);
router.get("/dhcp", &dhcpState);
router.get("/reset", &resetIP);

app.header("Authorization", authBuffer, 200);
app.use(&auth);

app.use("/box", &router);

and when I launch this code on my Arduino, the API does not receive the calls.

But when I comment a few calls defined in Router (7 functions) and leave the other functions active (8), the API calls are received correctly. Could it be that there is a limitation in the library with the number of calls that can be defined?

As additional information, the program storage space is as follows:
Sketch uses 27890 bytes (86%) of program storage space. Maximum is 32256 bytes.
Global variables use 1387 bytes (67%) of dynamic memory, leaving 661 bytes for local variables. Maximum is 2048 bytes.

@lasselukkari
Copy link
Owner

There is no such limitation on the library. Sounds like you are running out of memory. The SRAM usage can't be predicted at compile time. See https://learn.adafruit.com/memories-of-an-arduino/measuring-free-memory#sram-370031.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants