Skip to content

Commit

Permalink
Merge pull request #1119 from FlowiseAI/bugfix/API-Authentication
Browse files Browse the repository at this point in the history
Bugfix/Chatflow API Authentication
  • Loading branch information
HenryHengZJ authored Oct 23, 2023
2 parents 43ca334 + 2f0b4e1 commit 012fe45
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,18 +809,21 @@ export class App {
* @param {Response} res
* @param {ChatFlow} chatflow
*/
async validateKey(req: Request, res: Response, chatflow: ChatFlow) {
async validateKey(req: Request, chatflow: ChatFlow) {
const chatFlowApiKeyId = chatflow.apikeyid
const authorizationHeader = (req.headers['Authorization'] as string) ?? (req.headers['authorization'] as string) ?? ''
if (!chatFlowApiKeyId) return true

if (chatFlowApiKeyId && !authorizationHeader) return res.status(401).send(`Unauthorized`)
const authorizationHeader = (req.headers['Authorization'] as string) ?? (req.headers['authorization'] as string) ?? ''
if (chatFlowApiKeyId && !authorizationHeader) return false

const suppliedKey = authorizationHeader.split(`Bearer `).pop()
if (chatFlowApiKeyId && suppliedKey) {
if (suppliedKey) {
const keys = await getAPIKeys()
const apiSecret = keys.find((key) => key.id === chatFlowApiKeyId)?.apiSecret
if (!compareKeys(apiSecret, suppliedKey)) return res.status(401).send(`Unauthorized`)
if (!compareKeys(apiSecret, suppliedKey)) return false
return true
}
return false
}

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

if (!isInternal) {
await this.validateKey(req, res, chatflow)
const isKeyValidated = await this.validateKey(req, chatflow)
if (!isKeyValidated) return res.status(401).send('Unauthorized')
}

let isStreamValid = false
Expand Down

0 comments on commit 012fe45

Please sign in to comment.