Harden bot, storage and web API
This commit is contained in:
parent
e974bc6660
commit
20624a5c9c
43 changed files with 1265 additions and 1012 deletions
|
|
@ -1,4 +1,9 @@
|
|||
import { config } from "../../config.js";
|
||||
import { createHash, timingSafeEqual } from "node:crypto";
|
||||
|
||||
function tokenDigest(value) {
|
||||
return createHash("sha256").update(value).digest();
|
||||
}
|
||||
|
||||
function authMiddleware(req, res, next) {
|
||||
if (!config.webApiKey) {
|
||||
|
|
@ -6,9 +11,10 @@ function authMiddleware(req, res, next) {
|
|||
}
|
||||
|
||||
const header = req.headers.authorization || "";
|
||||
const token = header.replace("Bearer ", "");
|
||||
const match = /^Bearer ([^\s]+)$/.exec(header);
|
||||
const token = match?.[1] || "";
|
||||
|
||||
if (token === config.webApiKey) {
|
||||
if (timingSafeEqual(tokenDigest(token), tokenDigest(config.webApiKey))) {
|
||||
return next();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue