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:
rsvln authored and GitHub committed 2026-12-06 11:57:58 +03:00
1 parent 2ad969bcab
commit 75e22e92e9
10 files changed
+219 -6

No files matched your search

+2
View File
@@ -30,6 +30,8 @@ extern Settings settings;
extern Sensors::Settings sensorsSettings[SENSORS_AMOUNT];
void initConfig();
void saveSettingsNow();
const char* getApiToken();
bool regenerateApiToken();
void requestSaveNetworkSettings();
void requestSaveSettings();
// Call after releasing lifecycle, MQTT queue and Sensors data locks.
+1
View File
@@ -37,6 +37,7 @@ inline const char S_ADDRESS[] = "address";
inline const char S_ANTI_STUCK_INTERVAL[] = "antistuck_interval";
inline const char S_ANTI_STUCK_TIME[] = "antistuck_time";
inline const char S_AP[] = "ap";
inline const char S_API_TOKEN[] = "api_token";
inline const char S_APP_VERSION[] = "app_version";
inline const char S_AUTH[] = "auth";
inline const char S_AUTO_DIAG_RESET[] = "auto_diag_reset";