@@ -25,39 +25,39 @@ export class WebsocketController extends EventController implements EventControl
2525
2626 this . socket = new SocketIO ( httpServer , {
2727 cors : { origin : this . cors } ,
28- allowRequest : async ( req , callback ) => {
29- try {
30- const url = new URL ( req . url || '' , 'http://localhost' ) ;
31- const params = new URLSearchParams ( url . search ) ;
32-
33- // Permite conexões internas do Socket.IO (EIO=4 é o Engine.IO v4)
34- if ( params . has ( 'EIO' ) ) {
35- return callback ( null , true ) ;
36- }
37-
38- const apiKey = params . get ( 'apikey' ) || ( req . headers . apikey as string ) ;
39-
40- if ( ! apiKey ) {
41- this . logger . error ( 'Connection rejected: apiKey not provided' ) ;
42- return callback ( 'apiKey is required' , false ) ;
43- }
44-
45- const instance = await this . prismaRepository . instance . findFirst ( { where : { token : apiKey } } ) ;
46-
47- if ( ! instance ) {
48- const globalToken = configService . get < Auth > ( 'AUTHENTICATION' ) . API_KEY . KEY ;
49- if ( apiKey !== globalToken ) {
50- this . logger . error ( 'Connection rejected: invalid global token' ) ;
51- return callback ( 'Invalid global token' , false ) ;
52- }
53- }
54-
55- callback ( null , true ) ;
56- } catch ( error ) {
57- this . logger . error ( 'Authentication error:' ) ;
58- this . logger . error ( error ) ;
59- callback ( 'Authentication error' , false ) ;
60- }
28+ allowRequest : async ( req , callback ) => {
29+ try {
30+ const url = new URL ( req . url || '' , 'http://localhost' ) ;
31+ const params = new URLSearchParams ( url . search ) ;
32+
33+ // Permite conexões internas do Socket.IO (EIO=4 é o Engine.IO v4)
34+ if ( params . has ( 'EIO' ) ) {
35+ return callback ( null , true ) ;
36+ }
37+
38+ const apiKey = params . get ( 'apikey' ) || ( req . headers . apikey as string ) ;
39+
40+ if ( ! apiKey ) {
41+ this . logger . error ( 'Connection rejected: apiKey not provided' ) ;
42+ return callback ( 'apiKey is required' , false ) ;
43+ }
44+
45+ const instance = await this . prismaRepository . instance . findFirst ( { where : { token : apiKey } } ) ;
46+
47+ if ( ! instance ) {
48+ const globalToken = configService . get < Auth > ( 'AUTHENTICATION' ) . API_KEY . KEY ;
49+ if ( apiKey !== globalToken ) {
50+ this . logger . error ( 'Connection rejected: invalid global token' ) ;
51+ return callback ( 'Invalid global token' , false ) ;
52+ }
53+ }
54+
55+ callback ( null , true ) ;
56+ } catch ( error ) {
57+ this . logger . error ( 'Authentication error:' ) ;
58+ this . logger . error ( error ) ;
59+ callback ( 'Authentication error' , false ) ;
60+ }
6161 } ,
6262 } ) ;
6363
0 commit comments