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
@@ -366,7 +366,13 @@
|
||||
"login": "登录",
|
||||
"password": "密码",
|
||||
"auth": "需身份验证",
|
||||
"mdns": "使用 mDNS"
|
||||
"mdns": "使用 mDNS",
|
||||
"apiToken": {
|
||||
"title": "API token",
|
||||
"note": "Lets a device push sensor values without the portal password: <code>Authorization: Bearer <token></code> for <code>POST /api/sensor/value?id=N</code>. Works only for this endpoint.",
|
||||
"regenerate": "Regenerate token",
|
||||
"regenerateConfirm": "The current token will stop working. Continue?"
|
||||
}
|
||||
},
|
||||
"system": {
|
||||
"unit": "单位",
|
||||
|
||||
@@ -366,7 +366,13 @@
|
||||
"login": "Login",
|
||||
"password": "Password",
|
||||
"auth": "Require authentication",
|
||||
"mdns": "Use mDNS"
|
||||
"mdns": "Use mDNS",
|
||||
"apiToken": {
|
||||
"title": "API token",
|
||||
"note": "Lets a device push sensor values without the portal password: <code>Authorization: Bearer <token></code> for <code>POST /api/sensor/value?id=N</code>. Works only for this endpoint.",
|
||||
"regenerate": "Regenerate token",
|
||||
"regenerateConfirm": "The current token will stop working. Continue?"
|
||||
}
|
||||
},
|
||||
"system": {
|
||||
"unit": "Unit system",
|
||||
|
||||
@@ -366,7 +366,13 @@
|
||||
"login": "Login",
|
||||
"password": "Password",
|
||||
"auth": "Richiede autenticazione",
|
||||
"mdns": "Usa mDNS"
|
||||
"mdns": "Usa mDNS",
|
||||
"apiToken": {
|
||||
"title": "API token",
|
||||
"note": "Lets a device push sensor values without the portal password: <code>Authorization: Bearer <token></code> for <code>POST /api/sensor/value?id=N</code>. Works only for this endpoint.",
|
||||
"regenerate": "Regenerate token",
|
||||
"regenerateConfirm": "The current token will stop working. Continue?"
|
||||
}
|
||||
},
|
||||
"system": {
|
||||
"unit": "Unità di misura",
|
||||
|
||||
@@ -366,7 +366,13 @@
|
||||
"login": "Gebruikersnaam",
|
||||
"password": "Wachtwoord",
|
||||
"auth": "Authenticatie vereisen",
|
||||
"mdns": "Gebruik mDNS"
|
||||
"mdns": "Gebruik mDNS",
|
||||
"apiToken": {
|
||||
"title": "API token",
|
||||
"note": "Lets a device push sensor values without the portal password: <code>Authorization: Bearer <token></code> for <code>POST /api/sensor/value?id=N</code>. Works only for this endpoint.",
|
||||
"regenerate": "Regenerate token",
|
||||
"regenerateConfirm": "The current token will stop working. Continue?"
|
||||
}
|
||||
},
|
||||
"system": {
|
||||
"unit": "Eenheidssysteem",
|
||||
|
||||
@@ -366,7 +366,13 @@
|
||||
"login": "Логин",
|
||||
"password": "Пароль",
|
||||
"auth": "Требовать аутентификацию",
|
||||
"mdns": "Использовать mDNS"
|
||||
"mdns": "Использовать mDNS",
|
||||
"apiToken": {
|
||||
"title": "API-токен",
|
||||
"note": "Позволяет устройству передавать значения датчиков без пароля портала: <code>Authorization: Bearer <токен></code> для <code>POST /api/sensor/value?id=N</code>. Действует только для этого адреса.",
|
||||
"regenerate": "Сгенерировать новый токен",
|
||||
"regenerateConfirm": "Текущий токен перестанет работать. Продолжить?"
|
||||
}
|
||||
},
|
||||
"system": {
|
||||
"unit": "Система единиц",
|
||||
|
||||
@@ -64,6 +64,13 @@
|
||||
<input type="checkbox" name="mdns" value="true">
|
||||
<span data-i18n>settings.portal.mdns</span>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span data-i18n>settings.portal.apiToken.title</span>
|
||||
<input type="text" id="portal-api-token" readonly>
|
||||
<small data-i18n>settings.portal.apiToken.note</small>
|
||||
</label>
|
||||
<button type="button" class="secondary" id="portal-api-token-regenerate" data-i18n>settings.portal.apiToken.regenerate</button>
|
||||
<br />
|
||||
|
||||
<button type="submit" data-i18n>button.save</button>
|
||||
@@ -1185,6 +1192,7 @@
|
||||
setInputValue("[name='login']", data.portal.login, {}, form);
|
||||
setInputValue("[name='password']", data.portal.password, {}, form);
|
||||
setCheckboxValue("[name='mdns']", data.portal.mdns, form);
|
||||
form.querySelector("#portal-api-token").value = data.portal.api_token ?? '';
|
||||
|
||||
} else if (section === 'opentherm') {
|
||||
setInputValue("[name='in_gpio']", data.opentherm.in_gpio < 255 ? data.opentherm.in_gpio : '', {}, form);
|
||||
@@ -1507,6 +1515,32 @@
|
||||
};
|
||||
|
||||
setupSettingsForm('#portal-settings', ['login', 'password']);
|
||||
|
||||
document.querySelector('#portal-api-token-regenerate').addEventListener('click', async (event) => {
|
||||
if (!confirm(i18n("settings.portal.apiToken.regenerateConfirm"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
const button = event.currentTarget;
|
||||
button.setAttribute('aria-busy', true);
|
||||
try {
|
||||
const response = await fetch("/api/portal/token/regenerate", {
|
||||
method: "POST",
|
||||
cache: "no-cache",
|
||||
credentials: "include"
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
document.querySelector('#portal-api-token').value = (await response.json()).api_token;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
} finally {
|
||||
button.removeAttribute('aria-busy');
|
||||
}
|
||||
});
|
||||
setupSettingsForm('#system-settings', ['ntp.server', 'ntp.timezone']);
|
||||
setupSettingsForm('#heating-settings');
|
||||
setupSettingsForm('#dhw-settings');
|
||||
|
||||
Reference in new issue
Block a user