Files
onebox/ts/utils/auth.ts
T

29 lines
813 B
TypeScript
Raw Normal View History

import * as plugins from '../plugins.ts';
const bcryptHashPattern = /^\$2[abxy]\$\d\d\$/;
export function isBcryptHash(passwordHash: string): boolean {
return bcryptHashPattern.test(passwordHash);
}
export function needsPasswordUpgrade(passwordHash: string): boolean {
return !isBcryptHash(passwordHash);
}
export async function hashPassword(password: string): Promise<string> {
return await plugins.bcrypt.hash(password);
}
export async function verifyPassword(password: string, passwordHash: string): Promise<boolean> {
if (!passwordHash) {
return false;
}
if (isBcryptHash(passwordHash)) {
return await plugins.bcrypt.compare(password, passwordHash);
}
// Legacy compatibility for older databases that stored base64-encoded passwords.
return passwordHash === btoa(password);
}