add authorization

This commit is contained in:
Henry
2023-05-13 23:10:59 +01:00
parent 8270707668
commit 05bd7bc793
9 changed files with 317 additions and 9 deletions
+13
View File
@@ -4,6 +4,7 @@ import path from 'path'
import cors from 'cors'
import http from 'http'
import * as fs from 'fs'
import basicAuth from 'express-basic-auth'
import { IChatFlow, IncomingInput, IReactFlowNode, IReactFlowObject, INodeData } from './Interface'
import {
@@ -69,6 +70,18 @@ export class App {
// Allow access from *
this.app.use(cors())
if (process.env.USERNAME && process.env.PASSWORD) {
const username = process.env.USERNAME.toLocaleLowerCase()
const password = process.env.PASSWORD.toLocaleLowerCase()
const basicAuthMiddleware = basicAuth({
users: { [username]: password }
})
const whitelistURLs = ['node-icon', 'static', 'favicon']
this.app.use((req, res, next) =>
whitelistURLs.some((url) => req.url.includes(url)) || req.url === '/' ? next() : basicAuthMiddleware(req, res, next)
)
}
const upload = multer({ dest: `${path.join(__dirname, '..', 'uploads')}/` })
// ----------------------------------------