Files
cloudflare-domain-manager/apps/api/src/services/auth.ts
T
Denozordec d11414666f
Build, Test, and Push CFDM Docker Image / test (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / build-and-push (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / create-release (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / update-wiki (push) Has been cancelled
Refactor project to transition from Rust backend to Node.js with Fastify; update Dockerfile and Docker configurations for new build process; enhance local development instructions in CONTRIBUTING.md; implement health checks in Docker Compose; update pnpm-lock.yaml with new dependencies for API and shared packages; revise README.md to reflect new stack and development setup.
2026-06-19 12:06:32 +07:00

41 lines
1.1 KiB
TypeScript

import { verify } from "@node-rs/argon2";
import type { JwtClaims, LoginRequest, LoginResponse } from "@cfdm/shared";
import type { AppConfig } from "../config.js";
import { AppError } from "../errors.js";
export async function verifyPassword(
config: AppConfig,
password: string,
): Promise<void> {
if (config.adminPasswordHash === "devplaceholder") {
if (password === "admin") return;
throw AppError.unauthorized();
}
const ok = await verify(config.adminPasswordHash, password);
if (!ok) throw AppError.unauthorized();
}
export async function login(
config: AppConfig,
sign: (payload: JwtClaims) => string,
req: LoginRequest,
): Promise<LoginResponse> {
if (req.username !== config.adminUsername) {
throw AppError.unauthorized();
}
await verifyPassword(config, req.password);
const expiresAt = new Date(
Date.now() + config.jwtTtlHours * 60 * 60 * 1000,
);
const token = sign({
sub: req.username,
exp: Math.floor(expiresAt.getTime() / 1000),
});
return {
token,
expires_at: expiresAt.toISOString(),
};
}