Skip to content

Commit 012fe45

Browse files
authored
Merge pull request #1119 from FlowiseAI/bugfix/API-Authentication
Bugfix/Chatflow API Authentication
2 parents 43ca334 + 2f0b4e1 commit 012fe45

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

packages/server/src/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -809,18 +809,21 @@ export class App {
809809
* @param {Response} res
810810
* @param {ChatFlow} chatflow
811811
*/
812-
async validateKey(req: Request, res: Response, chatflow: ChatFlow) {
812+
async validateKey(req: Request, chatflow: ChatFlow) {
813813
const chatFlowApiKeyId = chatflow.apikeyid
814-
const authorizationHeader = (req.headers['Authorization'] as string) ?? (req.headers['authorization'] as string) ?? ''
814+
if (!chatFlowApiKeyId) return true
815815

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
817818

818819
const suppliedKey = authorizationHeader.split(`Bearer `).pop()
819-
if (chatFlowApiKeyId && suppliedKey) {
820+
if (suppliedKey) {
820821
const keys = await getAPIKeys()
821822
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
823825
}
826+
return false
824827
}
825828

826829
/**
@@ -846,7 +849,8 @@ export class App {
846849
if (!chatId) chatId = chatflowid
847850

848851
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')
850854
}
851855

852856
let isStreamValid = false

0 commit comments

Comments
 (0)