fosrl.pangolin/server/routers/resource/authWithPassword.ts

151 lines
4.3 KiB
TypeScript
Raw Normal View History

import { verify } from "@node-rs/argon2";
2025-01-01 21:41:31 -05:00
import { generateSessionToken } from "@server/auth/sessions/app";
import db from "@server/db";
2024-12-16 22:40:42 -05:00
import { orgs, resourcePassword, resources } from "@server/db/schema";
import HttpCode from "@server/types/HttpCode";
2025-01-01 21:41:31 -05:00
import response from "@server/lib/response";
2024-12-16 22:40:42 -05:00
import { eq } from "drizzle-orm";
import { NextFunction, Request, Response } from "express";
import createHttpError from "http-errors";
import { z } from "zod";
import { fromError } from "zod-validation-error";
2025-01-27 22:43:32 -05:00
import { createResourceSession } from "@server/auth/sessions/resource";
2024-12-21 21:01:12 -05:00
import logger from "@server/logger";
2024-12-22 16:59:30 -05:00
import { verifyPassword } from "@server/auth/password";
2025-01-27 22:43:32 -05:00
import config from "@server/lib/config";
2024-12-21 21:01:12 -05:00
export const authWithPasswordBodySchema = z
.object({
password: z.string()
})
.strict();
export const authWithPasswordParamsSchema = z
.object({
resourceId: z
.string()
.transform(Number)
.pipe(z.number().int().positive())
})
.strict();
export type AuthWithPasswordResponse = {
session?: string;
};
export async function authWithPassword(
req: Request,
res: Response,
2024-12-15 17:47:07 -05:00
next: NextFunction
): Promise<any> {
const parsedBody = authWithPasswordBodySchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-15 17:47:07 -05:00
fromError(parsedBody.error).toString()
)
);
}
const parsedParams = authWithPasswordParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-15 17:47:07 -05:00
fromError(parsedParams.error).toString()
)
);
}
const { resourceId } = parsedParams.data;
2024-12-16 22:40:42 -05:00
const { password } = parsedBody.data;
try {
const [result] = await db
.select()
.from(resources)
.leftJoin(
resourcePassword,
2024-12-15 17:47:07 -05:00
eq(resourcePassword.resourceId, resources.resourceId)
)
2024-12-15 17:47:07 -05:00
.leftJoin(orgs, eq(orgs.orgId, resources.orgId))
.where(eq(resources.resourceId, resourceId))
.limit(1);
const resource = result?.resources;
2024-12-15 17:47:07 -05:00
const org = result?.orgs;
const definedPassword = result?.resourcePassword;
2024-12-15 17:47:07 -05:00
if (!org) {
return next(
2025-01-27 22:43:32 -05:00
createHttpError(HttpCode.BAD_REQUEST, "Org does not exist")
2024-12-15 17:47:07 -05:00
);
}
if (!resource) {
return next(
2024-12-15 17:47:07 -05:00
createHttpError(HttpCode.BAD_REQUEST, "Resource does not exist")
);
}
if (!definedPassword) {
return next(
createHttpError(
HttpCode.UNAUTHORIZED,
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-15 17:47:07 -05:00
"Resource has no password protection"
)
)
);
}
2024-12-22 16:59:30 -05:00
const validPassword = await verifyPassword(
password,
2024-12-22 16:59:30 -05:00
definedPassword.passwordHash
);
if (!validPassword) {
2025-01-27 22:43:32 -05:00
if (config.getRawConfig().app.log_failed_attempts) {
logger.info(
`Resource password incorrect. Resource ID: ${resource.resourceId}. IP: ${req.ip}.`
);
}
return next(
2024-12-15 17:47:07 -05:00
createHttpError(HttpCode.UNAUTHORIZED, "Incorrect password")
);
}
const token = generateSessionToken();
await createResourceSession({
resourceId,
token,
passwordId: definedPassword.passwordId,
isRequestToken: true,
expiresAt: Date.now() + 1000 * 30, // 30 seconds
sessionLength: 1000 * 30,
doNotExtend: true
});
return response<AuthWithPasswordResponse>(res, {
data: {
2024-12-15 17:47:07 -05:00
session: token
},
success: true,
error: false,
message: "Authenticated with resource successfully",
2024-12-15 17:47:07 -05:00
status: HttpCode.OK
});
} catch (e) {
2024-12-21 21:01:12 -05:00
logger.error(e);
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
2024-12-15 17:47:07 -05:00
"Failed to authenticate with resource"
)
);
}
}