account system and syslogs
This commit is contained in:
@@ -1,52 +1,9 @@
|
||||
import { timingSafeEqual } from "node:crypto";
|
||||
import type { AdminRole } from "@/lib/auth/roles";
|
||||
import { verifyAccountCredentials } from "@/lib/auth/accounts-store";
|
||||
import type { SessionAccount } from "@/lib/auth/permissions";
|
||||
|
||||
function safeEqual(a: string, b: string): boolean {
|
||||
const aBuf = Buffer.from(a);
|
||||
const bBuf = Buffer.from(b);
|
||||
if (aBuf.length !== bBuf.length) return false;
|
||||
return timingSafeEqual(aBuf, bBuf);
|
||||
}
|
||||
|
||||
export function getAdminUsername(): string {
|
||||
return process.env.ADMIN_USERNAME?.trim() || "admin";
|
||||
}
|
||||
|
||||
export function getAdminPassword(): string | null {
|
||||
const password = process.env.ADMIN_PASSWORD?.trim();
|
||||
return password || null;
|
||||
}
|
||||
|
||||
export function getSupervisorUsername(): string {
|
||||
return process.env.SUPERVISOR_USERNAME?.trim() || "supervisor";
|
||||
}
|
||||
|
||||
export function getSupervisorPassword(): string | null {
|
||||
const password = process.env.SUPERVISOR_PASSWORD?.trim();
|
||||
return password || null;
|
||||
}
|
||||
|
||||
export function verifyCredentials(
|
||||
export async function verifyCredentials(
|
||||
username: string,
|
||||
password: string,
|
||||
): AdminRole | null {
|
||||
const adminPassword = getAdminPassword();
|
||||
if (
|
||||
adminPassword &&
|
||||
safeEqual(username, getAdminUsername()) &&
|
||||
safeEqual(password, adminPassword)
|
||||
) {
|
||||
return "admin";
|
||||
}
|
||||
|
||||
const supervisorPassword = getSupervisorPassword();
|
||||
if (
|
||||
supervisorPassword &&
|
||||
safeEqual(username, getSupervisorUsername()) &&
|
||||
safeEqual(password, supervisorPassword)
|
||||
) {
|
||||
return "supervisor";
|
||||
}
|
||||
|
||||
return null;
|
||||
): Promise<SessionAccount | null> {
|
||||
return verifyAccountCredentials(username, password);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user