Files
OpenBK7231T_App/src/httpserver/http_basic_auth.c
Lubomir Kaplan 95c30dd800 Add HTTP basic authentication (#996)
* add http basic auth

* add feature check for basic auth process

* fix http basic auth build for unsupported platforms (W800, W600)

* fix includes in http_basic_auth

* fix basic auth invocations when unavailable

* add http basic auth allow checks to http_fns

* remove unnecessary checks

* add my_strnicmp export

* silence warning

* update web password config ui

* update submodules

* add base64 lib to boufallo.mk for BL602

* add safe mode http basic auth bypass flag

* fix bl602 build with http auth bypass flag

* disable shared led driver

---------

Co-authored-by: Tester23 <openshwprojects@gmail.com>
2023-12-13 15:19:11 +01:00

56 lines
1.6 KiB
C

#include "http_basic_auth.h"
#include <stdio.h>
#include "../logging/logging.h"
#include "../base64/base64.h"
#include "../new_pins.h"
#include "../new_cfg.h"
#define LOG_FEATURE LOG_FEATURE_HTTP
int http_basic_auth_eval(http_request_t *request) {
#if ALLOW_WEB_PASSWORD
if (strlen(g_cfg.webPassword) == 0 || (bSafeMode && CFG_HasFlag(OBK_FLAG_HTTP_DISABLE_AUTH_IN_SAFE_MODE))) {
return HTTP_BASIC_AUTH_OK;
}
char tmp_auth[256];
for (int i = 0; i < request->numheaders; i++) {
char *header = request->headers[i];
if (!my_strnicmp(header, "Authorization: Basic ", 21)) {
char *basic_token = header + 21;
size_t decoded_len = b64_decoded_size(basic_token);
if (decoded_len > 255) {
break;
}
if (!b64_decode(basic_token, (unsigned char *)tmp_auth, decoded_len + 1)) {
ADDLOGF_ERROR("AUTH: Failed to decode B64 token.");
break;
}
tmp_auth[decoded_len] = 0;
if (!my_strnicmp(tmp_auth, "admin:", 6)) {
char *basic_auth_password = tmp_auth + 6;
if (strncmp(basic_auth_password, g_cfg.webPassword, 32) == 0) {
return HTTP_BASIC_AUTH_OK;
}
}
break;
}
}
return HTTP_BASIC_AUTH_FAIL;
#else
return HTTP_BASIC_AUTH_OK;
#endif
}
int http_basic_auth_run(http_request_t *request) {
int result = http_basic_auth_eval(request);
if (result == HTTP_BASIC_AUTH_FAIL) {
poststr(request, "HTTP/1.1 401 Unauthorized\r\n");
poststr(request, "Connection: close");
poststr(request, "\r\n");
poststr(request, "WWW-Authenticate: Basic realm=\"OpenBeken HTTP Server\"");
poststr(request, "\r\n");
poststr(request, "\r\n");
poststr(request, NULL);
}
return result;
}