mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 01:28:36 +08:00
d4b8e5ef70
* feat: iunitial metrics implementation * fix: graphql endpoint was throwing errors. * feat: add metrics env variables to readme
33 lines
835 B
TypeScript
33 lines
835 B
TypeScript
import auth from "basic-auth";
|
|
import compare from "tsscmp";
|
|
|
|
import { RequestHandler } from "talk-server/types/express";
|
|
|
|
export const basicAuth = (
|
|
username: string,
|
|
password: string
|
|
): RequestHandler => {
|
|
function check(name: string, pass: string) {
|
|
let valid = true;
|
|
|
|
// Simple method to prevent short-circuit and use timing-safe compare.
|
|
valid = compare(name, username) && valid;
|
|
valid = compare(pass, password) && valid;
|
|
|
|
return valid;
|
|
}
|
|
|
|
return (req, res, next) => {
|
|
// Pull the credentials out of the request.
|
|
const credentials = auth(req);
|
|
|
|
// Check credentials
|
|
if (credentials && check(credentials.name, credentials.pass)) {
|
|
return next();
|
|
}
|
|
|
|
res.setHeader("WWW-Authenticate", `Basic realm="${req.originalUrl}"`);
|
|
res.status(401).send("Access denied");
|
|
};
|
|
};
|