mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-12-06 21:23:05 +05:00
feat: add an API token for pushing sensor values (#312)
A device can set sensor values with `Authorization: Bearer <token>` on POST /api/sensor/value while portal authentication stays enabled, so the portal password is never given to the device. - the token is 32 random hex chars, created on first boot - it is accepted only by /api/sensor/value; Basic auth keeps working - the portal settings show it read-only, with a button to regenerate it (POST /api/portal/token/regenerate), which revokes the old one - GET /api/settings/portal and the backup include `api_token`; posting a valid token restores it The token is kept in NVS rather than in PortalSettings: adding a field to Settings changes the /settings.conf layout and would reset all settings on upgrade.
This commit is contained in:
1 parent
2ad969bcab
commit
75e22e92e9
10 files changed
+219
-6
No files matched your search
@@ -6,6 +6,8 @@
|
||||
#include <ETH.h>
|
||||
#endif
|
||||
#include <FileData.h>
|
||||
#include <esp_random.h>
|
||||
#include <nvs.h>
|
||||
#include <LittleFS.h>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
@@ -166,6 +168,76 @@ void saveSettingsNow() {
|
||||
}
|
||||
}
|
||||
|
||||
// API token
|
||||
// Kept in NVS rather than in Settings, so the settings file layout does not change.
|
||||
|
||||
static constexpr char API_TOKEN_NAMESPACE[] = "otgw";
|
||||
static constexpr char API_TOKEN_KEY[] = "api_token";
|
||||
static constexpr size_t API_TOKEN_LENGTH = 32;
|
||||
static char apiToken[API_TOKEN_LENGTH + 1] = "";
|
||||
|
||||
static bool isApiTokenValid(const char* token) {
|
||||
if (strlen(token) != API_TOKEN_LENGTH) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < API_TOKEN_LENGTH; i++) {
|
||||
if (!isxdigit(static_cast<unsigned char>(token[i]))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool storeApiToken(const char* token) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(API_TOKEN_NAMESPACE, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool stored = nvs_set_str(handle, API_TOKEN_KEY, token) == ESP_OK && nvs_commit(handle) == ESP_OK;
|
||||
nvs_close(handle);
|
||||
|
||||
if (stored) {
|
||||
strcpy(apiToken, token);
|
||||
}
|
||||
|
||||
return stored;
|
||||
}
|
||||
|
||||
const char* getApiToken() {
|
||||
return apiToken;
|
||||
}
|
||||
|
||||
bool regenerateApiToken() {
|
||||
uint8_t bytes[API_TOKEN_LENGTH / 2];
|
||||
esp_fill_random(bytes, sizeof(bytes));
|
||||
|
||||
char token[API_TOKEN_LENGTH + 1];
|
||||
for (size_t i = 0; i < sizeof(bytes); i++) {
|
||||
snprintf(token + i * 2, 3, "%02x", bytes[i]);
|
||||
}
|
||||
|
||||
return storeApiToken(token);
|
||||
}
|
||||
|
||||
static void loadApiToken() {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(API_TOKEN_NAMESPACE, NVS_READONLY, &handle) == ESP_OK) {
|
||||
size_t size = sizeof(apiToken);
|
||||
if (nvs_get_str(handle, API_TOKEN_KEY, apiToken, &size) != ESP_OK) {
|
||||
apiToken[0] = '\0';
|
||||
}
|
||||
nvs_close(handle);
|
||||
}
|
||||
|
||||
if (!isApiTokenValid(apiToken) && !regenerateApiToken()) {
|
||||
apiToken[0] = '\0';
|
||||
ESP_LOGW(L_SETTINGS, "Failed to create the API token");
|
||||
}
|
||||
}
|
||||
|
||||
// Initialization
|
||||
static bool loadNetworkSettings() {
|
||||
bool loaded = false;
|
||||
@@ -282,6 +354,7 @@ void initConfig() {
|
||||
loadNetworkSettings();
|
||||
loadSettings();
|
||||
loadSensorsSettings();
|
||||
loadApiToken();
|
||||
configureLoggingTask.forceRun();
|
||||
|
||||
appTaskManager.addTask(settingsSaveTask.setInterval(100));
|
||||
@@ -832,6 +905,7 @@ JsonVariant toJson(const PortalSettings& src, JsonVariant dst) {
|
||||
portal[S_AUTH] = src.auth;
|
||||
portal[S_LOGIN] = src.login;
|
||||
portal[S_PASSWORD] = src.password;
|
||||
portal[S_API_TOKEN] = apiToken;
|
||||
portal[S_MDNS] = src.mdns;
|
||||
|
||||
return dst;
|
||||
@@ -878,6 +952,22 @@ SettingResult fromJson(JsonVariantConst src, PortalSettings& dst) {
|
||||
}
|
||||
}
|
||||
|
||||
// Accepted for restoring a backup; the portal shows the token read-only.
|
||||
if (src[S_API_TOKEN].is<JsonString>()) {
|
||||
const JsonString value = src[S_API_TOKEN].as<JsonString>();
|
||||
|
||||
if (strlen(value.c_str()) == value.size() && isApiTokenValid(value.c_str())) {
|
||||
char token[API_TOKEN_LENGTH + 1];
|
||||
for (size_t i = 0; i < sizeof(token); i++) {
|
||||
token[i] = static_cast<char>(tolower(static_cast<unsigned char>(value.c_str()[i])));
|
||||
}
|
||||
|
||||
if (strcmp(token, apiToken) == 0 || storeApiToken(token)) {
|
||||
result.accepted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dst.auth && (!strlen(dst.login) || !strlen(dst.password))) {
|
||||
dst.auth = false;
|
||||
result.changed = true;
|
||||
|
||||
Reference in new issue
Block a user