@@ -809,18 +809,21 @@ export class App {
809
809
* @param {Response } res
810
810
* @param {ChatFlow } chatflow
811
811
*/
812
- async validateKey ( req : Request , res : Response , chatflow : ChatFlow ) {
812
+ async validateKey ( req : Request , chatflow : ChatFlow ) {
813
813
const chatFlowApiKeyId = chatflow . apikeyid
814
- const authorizationHeader = ( req . headers [ 'Authorization' ] as string ) ?? ( req . headers [ 'authorization' ] as string ) ?? ''
814
+ if ( ! chatFlowApiKeyId ) return true
815
815
816
- if ( chatFlowApiKeyId && ! authorizationHeader ) return res . status ( 401 ) . send ( `Unauthorized` )
816
+ const authorizationHeader = ( req . headers [ 'Authorization' ] as string ) ?? ( req . headers [ 'authorization' ] as string ) ?? ''
817
+ if ( chatFlowApiKeyId && ! authorizationHeader ) return false
817
818
818
819
const suppliedKey = authorizationHeader . split ( `Bearer ` ) . pop ( )
819
- if ( chatFlowApiKeyId && suppliedKey ) {
820
+ if ( suppliedKey ) {
820
821
const keys = await getAPIKeys ( )
821
822
const apiSecret = keys . find ( ( key ) => key . id === chatFlowApiKeyId ) ?. apiSecret
822
- if ( ! compareKeys ( apiSecret , suppliedKey ) ) return res . status ( 401 ) . send ( `Unauthorized` )
823
+ if ( ! compareKeys ( apiSecret , suppliedKey ) ) return false
824
+ return true
823
825
}
826
+ return false
824
827
}
825
828
826
829
/**
@@ -846,7 +849,8 @@ export class App {
846
849
if ( ! chatId ) chatId = chatflowid
847
850
848
851
if ( ! isInternal ) {
849
- await this . validateKey ( req , res , chatflow )
852
+ const isKeyValidated = await this . validateKey ( req , chatflow )
853
+ if ( ! isKeyValidated ) return res . status ( 401 ) . send ( 'Unauthorized' )
850
854
}
851
855
852
856
let isStreamValid = false
0 commit comments