Add authentication to websocket
This commit is contained in:
parent
cc342673df
commit
2a19b46d52
@ -5,8 +5,8 @@
|
||||
#include "WebApi_ws_live.h"
|
||||
#include "AsyncJson.h"
|
||||
#include "Configuration.h"
|
||||
#include "defaults.h"
|
||||
#include "WebApi.h"
|
||||
#include "defaults.h"
|
||||
|
||||
WebApiWsLiveClass::WebApiWsLiveClass()
|
||||
: _ws("/livedata")
|
||||
@ -66,6 +66,13 @@ void WebApiWsLiveClass::loop()
|
||||
String buffer;
|
||||
if (buffer) {
|
||||
serializeJson(root, buffer);
|
||||
|
||||
if (Configuration.get().Security_AllowReadonly) {
|
||||
_ws.setAuthentication("", "");
|
||||
} else {
|
||||
_ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password);
|
||||
}
|
||||
|
||||
_ws.textAll(buffer);
|
||||
}
|
||||
|
||||
|
||||
@ -16,6 +16,18 @@ export function authHeader(): Headers {
|
||||
return new Headers(headers);
|
||||
}
|
||||
|
||||
export function authUrl(): string {
|
||||
let user = null;
|
||||
try {
|
||||
user = JSON.parse(localStorage.getItem('user') || "");
|
||||
} catch { }
|
||||
|
||||
if (user && user.authdata) {
|
||||
return encodeURIComponent(atob(user.authdata)).replace("%3A", ":") + '@';
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
// remove user from local storage to log user out
|
||||
localStorage.removeItem('user');
|
||||
|
||||
@ -342,7 +342,7 @@ import type { EventlogItems } from '@/types/EventlogStatus';
|
||||
import type { LiveData, Inverter } from '@/types/LiveDataStatus';
|
||||
import type { LimitStatus } from '@/types/LimitStatus';
|
||||
import type { LimitConfig } from '@/types/LimitConfig';
|
||||
import { isLoggedIn, handleResponse, authHeader } from '@/utils/authentication';
|
||||
import { isLoggedIn, handleResponse, authHeader, authUrl } from '@/utils/authentication';
|
||||
import { formatNumber } from '@/utils';
|
||||
|
||||
export default defineComponent({
|
||||
@ -474,8 +474,9 @@ export default defineComponent({
|
||||
console.log("Starting connection to WebSocket Server");
|
||||
|
||||
const { protocol, host } = location;
|
||||
const authString = authUrl();
|
||||
const webSocketUrl = `${protocol === "https:" ? "wss" : "ws"
|
||||
}://${host}/livedata`;
|
||||
}://${authString}${host}/livedata`;
|
||||
|
||||
this.socket = new WebSocket(webSocketUrl);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user