From e27c225cf2132e80d80485ac711c5207e393c123 Mon Sep 17 00:00:00 2001 From: mertmit Date: Thu, 20 Apr 2023 19:41:22 +0300 Subject: [PATCH] feat: auth for jobs socket Signed-off-by: mertmit --- packages/nc-gui/plugins/events.ts | 2 -- .../src/modules/jobs/jobs.gateway.ts | 27 ++++++++++++++----- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/nc-gui/plugins/events.ts b/packages/nc-gui/plugins/events.ts index 884642da52..b39f2086b4 100644 --- a/packages/nc-gui/plugins/events.ts +++ b/packages/nc-gui/plugins/events.ts @@ -13,8 +13,6 @@ export default defineNuxtPlugin(async (nuxtApp) => { const url = new URL(appInfo.ncSiteUrl, window.location.href.split(/[?#]/)[0]) - url.port = '8081' - socket = io(`${url.href}jobs`, { extraHeaders: { 'xc-auth': token }, }) diff --git a/packages/nocodb-nest/src/modules/jobs/jobs.gateway.ts b/packages/nocodb-nest/src/modules/jobs/jobs.gateway.ts index 90014d67d8..a645eee474 100644 --- a/packages/nocodb-nest/src/modules/jobs/jobs.gateway.ts +++ b/packages/nocodb-nest/src/modules/jobs/jobs.gateway.ts @@ -6,26 +6,39 @@ import { WebSocketServer, } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; -import { Injectable, UseGuards } from '@nestjs/common'; -import { GlobalGuard } from 'src/guards/global/global.guard'; -import { ExtractProjectIdMiddleware } from 'src/middlewares/extract-project-id/extract-project-id.middleware'; +import { Injectable } from '@nestjs/common'; +import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host'; +import { AuthGuard } from '@nestjs/passport'; import { JobsService } from './jobs.service'; +import type { OnModuleInit } from '@nestjs/common'; -@WebSocketGateway(8081, { +@WebSocketGateway({ cors: { origin: '*', + allowedHeaders: ['xc-auth'], + credentials: true, }, - allowedHeaders: ['xc-auth'], - credentials: true, namespace: 'jobs', }) @Injectable() -export class JobsGateway { +export class JobsGateway implements OnModuleInit { constructor(private readonly jobsService: JobsService) {} @WebSocketServer() server: Server; + async onModuleInit() { + this.server.use(async (socket, next) => { + try { + const context = new ExecutionContextHost([socket.handshake as any]); + const guard = new (AuthGuard('jwt'))(context); + await guard.canActivate(context); + } catch {} + + next(); + }); + } + @SubscribeMessage('subscribe') async subscribe( @MessageBody() data: { type: string; id: string },