feat: login IT contra backend it-login + allowlist

This commit is contained in:
Nurfog committed 2026-09-09 15:26:01 -03:00
1 parent 57b64af1ba
commit 1ec2edc99b
1 file changed
+23 -5
+23 -5
View File
@@ -1,6 +1,11 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { COOKIE, isAllowed, parseUsers, signSession, verifyPassword, SESSION_MAX_AGE } from "@/lib/auth"; import { COOKIE, isAllowed, signSession, SESSION_MAX_AGE } from "@/lib/auth";
const API_URL =
process.env.NEXT_PUBLIC_API_URL || "https://api.devops.norteamericano.cl";
// Login IT: valida en el backend (sige_sam_v3.usuario, plano o SHA256)
// y exige que el correo esté en IT_USERS (allowlist).
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
try { try {
const { email, password } = await req.json(); const { email, password } = await req.json();
@@ -8,10 +13,23 @@ export async function POST(req: NextRequest) {
if (!mail || !password) { if (!mail || !password) {
return NextResponse.json({ error: "Faltan credenciales" }, { status: 400 }); return NextResponse.json({ error: "Faltan credenciales" }, { status: 400 });
} }
const users = parseUsers(); if (!isAllowed(mail)) {
const hash = users.get(mail); await new Promise((r) => setTimeout(r, 600));
if (!hash || !isAllowed(mail) || !(await verifyPassword(String(password), hash))) { return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 });
// Mensaje genérico: no revela si el correo existe }
let ok = false;
try {
const res = await fetch(`${API_URL}/api/Auth/it-login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: mail, password: String(password) }),
cache: "no-store",
});
ok = res.ok;
} catch {
ok = false;
}
if (!ok) {
await new Promise((r) => setTimeout(r, 600)); await new Promise((r) => setTimeout(r, 600));
return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 }); return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 });
} }