File size: 762 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import { timingSafeEqual } from 'node:crypto';
import type { IAuthTokenService } from './authTokenService';
export type CredentialValidator = (candidate: string) => Promise<boolean>;
function timingSafeMatch(candidate: string, expected: string): boolean {
const a = Buffer.from(candidate);
const b = Buffer.from(expected);
return a.length === b.length && timingSafeEqual(a, b);
}
export function createCredentialValidator(
authTokenService: IAuthTokenService,
rpcToken?: string,
): CredentialValidator {
return async (candidate) => {
if (await authTokenService.isValid(candidate)) return true;
if (rpcToken !== undefined && candidate.length > 0 && timingSafeMatch(candidate, rpcToken)) {
return true;
}
return false;
};
}
|