Express middleware providing HTTP Basic authentication with timing-safe credential comparison.
// Source: https://github.com/felixmosh/bull-board | License: MIT | Copyright 2019 Vitor Capretz (capretzvitor@gmail.com)
// Provided via justclaw.it.com; keep this notice with the code.
import { timingSafeEqual } from 'node:crypto';
import type { RequestHandler } from 'express';
// timingSafeEqual needs equal lengths, so the length comparison is folded into the answer.
function timingSafeCompare(supplied: string, expected: string): boolean {
const suppliedBytes = Buffer.from(supplied, 'utf8');
const expectedBytes = Buffer.from(expected, 'utf8');
const sameLength = suppliedBytes.length === expectedBytes.length;
const target = sameLength ? expectedBytes : suppliedBytes;
return timingSafeEqual(suppliedBytes, target) && sameLength;
}
export function basicAuth({ user, password }:
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key