121 lines
3.8 KiB
JavaScript
121 lines
3.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.securityWsHub = exports.SecurityWsHub = void 0;
|
|
const config_1 = require("../../config");
|
|
const client_1 = require("../redis/client");
|
|
const securityKeys_1 = require("../redis/securityKeys");
|
|
const roleAccess_1 = require("../../utils/roleAccess");
|
|
const auth_1 = require("./auth");
|
|
function canAccessSecurityFeed(auth) {
|
|
if (!(0, roleAccess_1.hasAdminAccess)(auth))
|
|
return false;
|
|
if (auth.roles.includes('super_admin'))
|
|
return true;
|
|
return auth.permissions.includes('security.admin');
|
|
}
|
|
function parseSecurityEvent(raw) {
|
|
try {
|
|
const parsed = JSON.parse(raw);
|
|
if (!parsed?.type?.startsWith('security.'))
|
|
return null;
|
|
return parsed;
|
|
}
|
|
catch {
|
|
return null;
|
|
}
|
|
}
|
|
function serialize(msg) {
|
|
return JSON.stringify(msg);
|
|
}
|
|
class SecurityWsHub {
|
|
constructor() {
|
|
this.clients = new Set();
|
|
}
|
|
async start() {
|
|
if (!config_1.config.redis.enabled) {
|
|
console.warn('[security-ws] Redis disabled — security push unavailable');
|
|
return;
|
|
}
|
|
await (0, client_1.connectRedis)();
|
|
const sub = (0, client_1.getRedisSubscriber)();
|
|
if (!sub)
|
|
return;
|
|
const channel = securityKeys_1.securityRedisKeys.pubsubAdmin();
|
|
sub.on('message', (ch, message) => {
|
|
if (ch !== channel)
|
|
return;
|
|
const event = parseSecurityEvent(message);
|
|
if (!event)
|
|
return;
|
|
this.dispatch(event);
|
|
});
|
|
await sub.subscribe(channel);
|
|
console.log(`[security-ws] subscribed to ${channel}`);
|
|
}
|
|
register(socket, reqUrl, authHeader) {
|
|
const url = new URL(reqUrl, 'http://localhost');
|
|
const queryToken = url.searchParams.get('token') ?? undefined;
|
|
const auth = (0, auth_1.authenticateWsToken)((0, auth_1.extractBearerToken)(authHeader) ?? queryToken);
|
|
if (!auth || !canAccessSecurityFeed(auth)) {
|
|
socket.send(serialize({
|
|
type: 'error',
|
|
code: 'forbidden',
|
|
message: 'Security feed requires administrator access',
|
|
}));
|
|
socket.close(4403, 'Forbidden');
|
|
return;
|
|
}
|
|
const state = { socket, auth, subscribed: false };
|
|
this.clients.add(state);
|
|
socket.on('message', (data) => {
|
|
this.handleClientMessage(state, String(data));
|
|
});
|
|
socket.on('close', () => {
|
|
this.clients.delete(state);
|
|
});
|
|
socket.send(serialize({
|
|
type: 'connected',
|
|
authenticated: true,
|
|
role: auth.role,
|
|
path: config_1.config.ws.securityPath,
|
|
}));
|
|
}
|
|
handleClientMessage(state, raw) {
|
|
let msg;
|
|
try {
|
|
msg = JSON.parse(raw);
|
|
}
|
|
catch {
|
|
return;
|
|
}
|
|
if (msg.type === 'ping') {
|
|
this.send(state, { type: 'pong', ts: Date.now() });
|
|
return;
|
|
}
|
|
if (msg.type === 'subscribe_admin') {
|
|
state.subscribed = true;
|
|
this.send(state, { type: 'subscribed_admin' });
|
|
}
|
|
}
|
|
dispatch(event) {
|
|
const payload = { ...event, ts: event.ts ?? Date.now() };
|
|
for (const client of this.clients) {
|
|
if (!client.subscribed)
|
|
continue;
|
|
this.send(client, payload);
|
|
}
|
|
}
|
|
send(client, msg) {
|
|
if (client.socket.readyState !== 1)
|
|
return;
|
|
try {
|
|
client.socket.send(serialize(msg));
|
|
}
|
|
catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|
|
exports.SecurityWsHub = SecurityWsHub;
|
|
exports.securityWsHub = new SecurityWsHub();
|
|
//# sourceMappingURL=securityHub.js.map
|