Spaces:
Runtime error
Runtime error
import type { Request, Response, NextFunction } from "express"; | |
import { config } from "../config"; | |
import { proxyKeys } from './proxy-keys'; | |
const PROXY_KEY = config.proxyKey; | |
const KEY_PASSWORD = config.keyPassword; | |
export const auth = (req: Request, res: Response, next: NextFunction) => { | |
if (req.headers.authorization?.startsWith('Bearer ')) { | |
req.authKey = req.headers.authorization.split(' ').pop(); | |
delete req.headers.authorization; | |
} | |
if (config.proxyCaptcha) { | |
if (proxyKeys.check(req.authKey)) { | |
next(); | |
} else { | |
res.status(401).json({ error: "Unauthorized" }); | |
} | |
return; | |
} | |
if (!PROXY_KEY || req.authKey === `${PROXY_KEY}`) { | |
next(); | |
} else { | |
res.status(401).json({ error: "Unauthorized" }); | |
} | |
}; | |
export const authPassword = (req: Request, res: Response, next: NextFunction) => { | |
if (!KEY_PASSWORD) { | |
next(); | |
return; | |
} | |
if (req.query.password === KEY_PASSWORD) { | |
next(); | |
} else { | |
res.status(401).json({ error: "Unauthorized" }); | |
} | |
}; | |