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

159 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-11-23 20:08:56 -05:00
import { verify } from "@node-rs/argon2";
2025-01-01 21:41:31 -05:00
import { generateSessionToken } from "@server/auth/sessions/app";
2024-11-23 20:08:56 -05:00
import db from "@server/db";
2024-12-15 17:47:07 -05:00
import {
orgs,
resourceOtp,
resourcePincode,
2024-12-16 22:40:42 -05:00
resources,
resourceWhitelist
2024-12-15 17:47:07 -05:00
} from "@server/db/schema";
2024-11-23 20:08:56 -05:00
import HttpCode from "@server/types/HttpCode";
2025-01-01 21:41:31 -05:00
import response from "@server/lib/response";
2024-12-15 17:47:07 -05:00
import { and, eq } from "drizzle-orm";
2024-11-23 20:08:56 -05:00
import { NextFunction, Request, Response } from "express";
import createHttpError from "http-errors";
import { z } from "zod";
import { fromError } from "zod-validation-error";
import {
createResourceSession,
2024-12-15 17:47:07 -05:00
serializeResourceSessionCookie
2025-01-01 21:41:31 -05:00
} from "@server/auth/sessions/resource";
2024-11-23 20:08:56 -05:00
import logger from "@server/logger";
2025-01-01 21:41:31 -05:00
import config from "@server/lib/config";
2024-12-15 17:47:07 -05:00
import { AuthWithPasswordResponse } from "./authWithPassword";
import { isValidOtp, sendResourceOtpEmail } from "@server/auth/resourceOtp";
2024-12-22 16:59:30 -05:00
import { verifyPassword } from "@server/auth/password";
2024-11-23 20:08:56 -05:00
2024-12-21 21:01:12 -05:00
export const authWithPincodeBodySchema = z
.object({
pincode: z.string()
})
.strict();
export const authWithPincodeParamsSchema = z
.object({
resourceId: z
.string()
.transform(Number)
.pipe(z.number().int().positive())
})
.strict();
2024-11-23 20:08:56 -05:00
export type AuthWithPincodeResponse = {
session?: string;
2024-11-23 20:08:56 -05:00
};
export async function authWithPincode(
req: Request,
res: Response,
2024-12-15 17:47:07 -05:00
next: NextFunction
2024-11-23 20:08:56 -05:00
): Promise<any> {
const parsedBody = authWithPincodeBodySchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-15 17:47:07 -05:00
fromError(parsedBody.error).toString()
)
2024-11-23 20:08:56 -05:00
);
}
const parsedParams = authWithPincodeParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-15 17:47:07 -05:00
fromError(parsedParams.error).toString()
)
2024-11-23 20:08:56 -05:00
);
}
const { resourceId } = parsedParams.data;
2024-12-16 22:40:42 -05:00
const { pincode } = parsedBody.data;
2024-11-23 20:08:56 -05:00
try {
const [result] = await db
.select()
.from(resources)
.leftJoin(
resourcePincode,
2024-12-15 17:47:07 -05:00
eq(resourcePincode.resourceId, resources.resourceId)
2024-11-23 20:08:56 -05:00
)
2024-12-15 17:47:07 -05:00
.leftJoin(orgs, eq(orgs.orgId, resources.orgId))
2024-11-23 20:08:56 -05:00
.where(eq(resources.resourceId, resourceId))
.limit(1);
const resource = result?.resources;
2024-12-15 17:47:07 -05:00
const org = result?.orgs;
2024-11-23 20:08:56 -05:00
const definedPincode = result?.resourcePincode;
2024-12-15 17:47:07 -05:00
if (!org) {
2024-11-23 20:08:56 -05:00
return next(
createHttpError(
2024-12-15 17:47:07 -05:00
HttpCode.INTERNAL_SERVER_ERROR,
"Org does not exist"
)
);
}
if (!resource) {
return next(
createHttpError(HttpCode.BAD_REQUEST, "Resource does not exist")
2024-11-23 20:08:56 -05:00
);
}
if (!definedPincode) {
return next(
createHttpError(
HttpCode.UNAUTHORIZED,
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-15 17:47:07 -05:00
"Resource has no pincode protection"
)
)
2024-11-23 20:08:56 -05:00
);
}
2024-12-22 16:59:30 -05:00
const validPincode = verifyPassword(
pincode,
definedPincode.pincodeHash
);
2024-11-23 20:08:56 -05:00
if (!validPincode) {
return next(
2024-12-15 17:47:07 -05:00
createHttpError(HttpCode.UNAUTHORIZED, "Incorrect PIN")
2024-11-23 20:08:56 -05:00
);
}
const token = generateSessionToken();
await createResourceSession({
resourceId,
token,
2024-12-16 22:40:42 -05:00
pincodeId: definedPincode.pincodeId
2024-11-23 20:08:56 -05:00
});
const cookieName = `${config.getRawConfig().server.resource_session_cookie_name}_${resource.resourceId}`;
2024-12-21 21:01:12 -05:00
const cookie = serializeResourceSessionCookie(cookieName, token);
res.appendHeader("Set-Cookie", cookie);
return response<AuthWithPincodeResponse>(res, {
data: {
2024-12-15 17:47:07 -05:00
session: token
},
2024-11-23 20:08:56 -05:00
success: true,
error: false,
message: "Authenticated with resource successfully",
2024-12-15 17:47:07 -05:00
status: HttpCode.OK
2024-11-23 20:08:56 -05:00
});
} catch (e) {
2024-12-15 17:47:07 -05:00
logger.error(e);
2024-11-23 20:08:56 -05:00
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
2024-12-15 17:47:07 -05:00
"Failed to authenticate with resource"
)
2024-11-23 20:08:56 -05:00
);
}
}