@@ -7,12 +7,13 @@ import {
77 ParseIntPipe ,
88 Post ,
99 Query ,
10+ Req ,
1011 Res ,
1112 Sse ,
1213 UnauthorizedException ,
1314} from '@nestjs/common' ;
1415import { ApiOperation , ApiTags } from '@nestjs/swagger' ;
15- import { Response } from 'express' ;
16+ import { Response , Request } from 'express' ;
1617import Redis from 'ioredis' ;
1718import { Observable , Subscriber } from 'rxjs' ;
1819import { Public } from '~/_common/decorators/public.decorator' ;
@@ -22,6 +23,9 @@ import { SyncIdentitiesDto } from './_dto/sync-identities.dto';
2223import { Types } from 'mongoose' ;
2324import { ActionType } from './_enum/action-type.enum' ;
2425import { DeleteIdentitiesDto } from './_dto/delete-identities.dto' ;
26+ import { hash } from 'crypto' ;
27+ import { AgentsService } from '../agents/agents.service' ;
28+ import { Agents } from '../agents/_schemas/agents.schema' ;
2529
2630function fireMessage ( observer : Subscriber < MessageEvent > , channel : string , message : any , loggername : string ) {
2731 try {
@@ -40,6 +44,7 @@ export class BackendsController {
4044 private readonly logger = new Logger ( BackendsController . name ) ;
4145
4246 constructor (
47+ private agentsService : AgentsService ,
4348 private backendsService : BackendsService ,
4449 @InjectRedis ( ) protected readonly redis : Redis ,
4550 ) { }
@@ -124,8 +129,11 @@ export class BackendsController {
124129 @Public ( )
125130 @Sse ( 'sse' )
126131 @ApiOperation ( { summary : 'Server Sent Event - Récupère en temps réel les Jobs et affiche leurs état' } )
127- public async sse ( @Res ( ) res : Response , @Query ( 'key' ) key : string ) : Promise < Observable < MessageEvent > > {
128- if ( key !== 'hZcdVqHScVDsDFdHOdcjmufEKFJVKaS8' ) throw new UnauthorizedException ( ) ;
132+ public async sse ( @Res ( ) res : Response , @Query ( 'id' ) id : string , @Query ( 'key' ) key : string ) : Promise < Observable < MessageEvent > > {
133+ if ( ! id || ! key ) throw new UnauthorizedException ( ) ;
134+ const user = await this . agentsService . findById < Agents > ( id ) ;
135+ if ( ! user ) throw new UnauthorizedException ( ) ;
136+ if ( key !== hash ( 'sha256' , user . security . secretKey ) ) throw new UnauthorizedException ( ) ;
129137
130138 res . socket . on ( 'close' , ( ) => {
131139 Logger . debug ( `Observer close connection` , this . constructor . name ) ;
0 commit comments