Password protection for config settings API
This commit is contained in:
parent
7798854b2b
commit
fe8b68d2be
@ -6,6 +6,7 @@
|
|||||||
#include "ArduinoJson.h"
|
#include "ArduinoJson.h"
|
||||||
#include "AsyncJson.h"
|
#include "AsyncJson.h"
|
||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
|
#include "WebApi.h"
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
|
|
||||||
void WebApiConfigClass::init(AsyncWebServer* server)
|
void WebApiConfigClass::init(AsyncWebServer* server)
|
||||||
@ -32,11 +33,19 @@ void WebApiConfigClass::loop()
|
|||||||
|
|
||||||
void WebApiConfigClass::onConfigGet(AsyncWebServerRequest* request)
|
void WebApiConfigClass::onConfigGet(AsyncWebServerRequest* request)
|
||||||
{
|
{
|
||||||
|
if (!WebApi.checkCredentials(request)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
request->send(LittleFS, CONFIG_FILENAME_JSON, String(), true);
|
request->send(LittleFS, CONFIG_FILENAME_JSON, String(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebApiConfigClass::onConfigDelete(AsyncWebServerRequest* request)
|
void WebApiConfigClass::onConfigDelete(AsyncWebServerRequest* request)
|
||||||
{
|
{
|
||||||
|
if (!WebApi.checkCredentials(request)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||||
JsonObject retMsg = response->getRoot();
|
JsonObject retMsg = response->getRoot();
|
||||||
retMsg[F("type")] = F("warning");
|
retMsg[F("type")] = F("warning");
|
||||||
@ -93,6 +102,10 @@ void WebApiConfigClass::onConfigDelete(AsyncWebServerRequest* request)
|
|||||||
|
|
||||||
void WebApiConfigClass::onConfigUploadFinish(AsyncWebServerRequest* request)
|
void WebApiConfigClass::onConfigUploadFinish(AsyncWebServerRequest* request)
|
||||||
{
|
{
|
||||||
|
if (!WebApi.checkCredentials(request)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// the request handler is triggered after the upload has finished...
|
// the request handler is triggered after the upload has finished...
|
||||||
// create the response, add header, and send response
|
// create the response, add header, and send response
|
||||||
|
|
||||||
@ -108,6 +121,10 @@ void WebApiConfigClass::onConfigUploadFinish(AsyncWebServerRequest* request)
|
|||||||
|
|
||||||
void WebApiConfigClass::onConfigUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final)
|
void WebApiConfigClass::onConfigUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t* data, size_t len, bool final)
|
||||||
{
|
{
|
||||||
|
if (!WebApi.checkCredentials(request)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!index) {
|
if (!index) {
|
||||||
// open the file on first call and store the file handle in the request object
|
// open the file on first call and store the file handle in the request object
|
||||||
request->_tempFile = LittleFS.open(CONFIG_FILENAME_JSON, "w");
|
request->_tempFile = LittleFS.open(CONFIG_FILENAME_JSON, "w");
|
||||||
|
|||||||
@ -100,7 +100,7 @@ const router = createRouter({
|
|||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
// redirect to login page if not logged in and trying to access a restricted page
|
// redirect to login page if not logged in and trying to access a restricted page
|
||||||
const publicPages = ['/', '/login', '/about', '/info/network', '/info/system', '/info/ntp', '/info/mqtt',
|
const publicPages = ['/', '/login', '/about', '/info/network', '/info/system', '/info/ntp', '/info/mqtt',
|
||||||
'/firmware/upgrade', '/settings/config', ];
|
'/firmware/upgrade', ];
|
||||||
const authRequired = !publicPages.includes(to.path);
|
const authRequired = !publicPages.includes(to.path);
|
||||||
const loggedIn = localStorage.getItem('user');
|
const loggedIn = localStorage.getItem('user');
|
||||||
|
|
||||||
|
|||||||
@ -112,6 +112,7 @@ import {
|
|||||||
} from 'bootstrap-icons-vue';
|
} from 'bootstrap-icons-vue';
|
||||||
import * as bootstrap from 'bootstrap';
|
import * as bootstrap from 'bootstrap';
|
||||||
import BootstrapAlert from "@/components/BootstrapAlert.vue";
|
import BootstrapAlert from "@/components/BootstrapAlert.vue";
|
||||||
|
import { handleResponse, authHeader } from '@/utils/authentication';
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
components: {
|
components: {
|
||||||
@ -152,15 +153,10 @@ export default defineComponent({
|
|||||||
|
|
||||||
fetch("/api/config/delete", {
|
fetch("/api/config/delete", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
headers: authHeader(),
|
||||||
body: formData,
|
body: formData,
|
||||||
})
|
})
|
||||||
.then(function (response) {
|
.then(handleResponse)
|
||||||
if (response.status != 200) {
|
|
||||||
throw response.status;
|
|
||||||
} else {
|
|
||||||
return response.json();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(
|
.then(
|
||||||
(response) => {
|
(response) => {
|
||||||
this.alertMessage = response.message;
|
this.alertMessage = response.message;
|
||||||
@ -171,10 +167,17 @@ export default defineComponent({
|
|||||||
this.modalFactoryReset.hide();
|
this.modalFactoryReset.hide();
|
||||||
},
|
},
|
||||||
downloadConfig() {
|
downloadConfig() {
|
||||||
const link = document.createElement('a')
|
fetch("/api/config/get", { headers: authHeader() })
|
||||||
link.href = "/api/config/get"
|
.then(res => res.blob())
|
||||||
link.download = 'config.json'
|
.then(blob => {
|
||||||
link.click()
|
var file = window.URL.createObjectURL(blob);
|
||||||
|
var a = document.createElement('a');
|
||||||
|
a.href = file;
|
||||||
|
a.download = "config.json";
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
a.remove();
|
||||||
|
});
|
||||||
},
|
},
|
||||||
uploadConfig(event: Event | null) {
|
uploadConfig(event: Event | null) {
|
||||||
this.uploading = true;
|
this.uploading = true;
|
||||||
@ -206,6 +209,9 @@ export default defineComponent({
|
|||||||
|
|
||||||
formData.append("config", this.file, "config");
|
formData.append("config", this.file, "config");
|
||||||
request.open("post", "/api/config/upload");
|
request.open("post", "/api/config/upload");
|
||||||
|
authHeader().forEach((value, key) => {
|
||||||
|
request.setRequestHeader(key, value);
|
||||||
|
});
|
||||||
request.send(formData);
|
request.send(formData);
|
||||||
},
|
},
|
||||||
clear() {
|
clear() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user