mirror of
https://github.com/wassname/talk.git
synced 2026-07-23 13:10:20 +08:00
[CORL-281] Metrics (#2298)
* feat: iunitial metrics implementation * fix: graphql endpoint was throwing errors. * feat: add metrics env variables to readme
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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");
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user