2024-11-17 22:44:11 -05:00
|
|
|
import { verify } from "@node-rs/argon2";
|
|
|
|
import { generateSessionToken } from "@server/auth";
|
|
|
|
import db from "@server/db";
|
2024-12-15 17:47:07 -05:00
|
|
|
import { orgs, resourceOtp, resourcePassword, resources } from "@server/db/schema";
|
2024-11-17 22:44:11 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import response from "@server/utils/response";
|
2024-12-15 17:47:07 -05:00
|
|
|
import { and, eq } from "drizzle-orm";
|
2024-11-17 22:44:11 -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
|
2024-11-17 22:44:11 -05:00
|
|
|
} from "@server/auth/resource";
|
2024-11-23 17:56:21 -05:00
|
|
|
import logger from "@server/logger";
|
2024-11-27 00:07:40 -05:00
|
|
|
import config from "@server/config";
|
2024-12-15 17:47:07 -05:00
|
|
|
import { isValidOtp, sendResourceOtpEmail } from "@server/auth/resourceOtp";
|
2024-11-17 22:44:11 -05:00
|
|
|
|
|
|
|
export const authWithPasswordBodySchema = z.object({
|
|
|
|
password: z.string(),
|
|
|
|
email: z.string().email().optional(),
|
2024-12-15 17:47:07 -05:00
|
|
|
otp: z.string().optional()
|
2024-11-17 22:44:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
export const authWithPasswordParamsSchema = z.object({
|
2024-12-15 17:47:07 -05:00
|
|
|
resourceId: z.string().transform(Number).pipe(z.number().int().positive())
|
2024-11-17 22:44:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
export type AuthWithPasswordResponse = {
|
2024-12-15 17:47:07 -05:00
|
|
|
otpRequested?: boolean;
|
|
|
|
otpSent?: boolean;
|
2024-11-23 23:31:22 -05:00
|
|
|
session?: string;
|
2024-11-17 22:44:11 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function authWithPassword(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
2024-12-15 17:47:07 -05:00
|
|
|
next: NextFunction
|
2024-11-17 22:44:11 -05:00
|
|
|
): 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()
|
|
|
|
)
|
2024-11-17 22:44:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
)
|
2024-11-17 22:44:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { resourceId } = parsedParams.data;
|
2024-12-15 17:47:07 -05:00
|
|
|
const { email, password, otp } = parsedBody.data;
|
2024-11-17 22:44:11 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
const [result] = await db
|
|
|
|
.select()
|
|
|
|
.from(resources)
|
|
|
|
.leftJoin(
|
|
|
|
resourcePassword,
|
2024-12-15 17:47:07 -05:00
|
|
|
eq(resourcePassword.resourceId, resources.resourceId)
|
2024-11-17 22:44:11 -05:00
|
|
|
)
|
2024-12-15 17:47:07 -05:00
|
|
|
.leftJoin(orgs, eq(orgs.orgId, resources.orgId))
|
2024-11-17 22:44:11 -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-17 22:44:11 -05:00
|
|
|
const definedPassword = result?.resourcePassword;
|
|
|
|
|
2024-12-15 17:47:07 -05:00
|
|
|
if (!org) {
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.BAD_REQUEST, "Resource does not exist")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-17 22:44:11 -05:00
|
|
|
if (!resource) {
|
|
|
|
return next(
|
2024-12-15 17:47:07 -05:00
|
|
|
createHttpError(HttpCode.BAD_REQUEST, "Resource does not exist")
|
2024-11-17 22:44:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-17 22:44:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const validPassword = await verify(
|
|
|
|
definedPassword.passwordHash,
|
|
|
|
password,
|
|
|
|
{
|
|
|
|
memoryCost: 19456,
|
|
|
|
timeCost: 2,
|
|
|
|
outputLen: 32,
|
2024-12-15 17:47:07 -05:00
|
|
|
parallelism: 1
|
|
|
|
}
|
2024-11-17 22:44:11 -05:00
|
|
|
);
|
|
|
|
if (!validPassword) {
|
|
|
|
return next(
|
2024-12-15 17:47:07 -05:00
|
|
|
createHttpError(HttpCode.UNAUTHORIZED, "Incorrect password")
|
2024-11-17 22:44:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-12-15 17:47:07 -05:00
|
|
|
if (resource.otpEnabled) {
|
|
|
|
if (otp && email) {
|
|
|
|
const isValidCode = await isValidOtp(
|
|
|
|
email,
|
|
|
|
resource.resourceId,
|
|
|
|
otp
|
|
|
|
);
|
|
|
|
if (!isValidCode) {
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.UNAUTHORIZED, "Incorrect OTP")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await db
|
|
|
|
.delete(resourceOtp)
|
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(resourceOtp.email, email),
|
|
|
|
eq(resourceOtp.resourceId, resource.resourceId)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else if (email) {
|
|
|
|
try {
|
|
|
|
await sendResourceOtpEmail(
|
|
|
|
email,
|
|
|
|
resource.resourceId,
|
|
|
|
resource.name,
|
|
|
|
org.name
|
|
|
|
);
|
|
|
|
return response<AuthWithPasswordResponse>(res, {
|
|
|
|
data: { otpSent: true },
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Sent one-time otp to email address",
|
|
|
|
status: HttpCode.ACCEPTED
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
|
|
"Failed to send one-time otp. Make sure the email address is correct and try again."
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2024-11-17 22:44:11 -05:00
|
|
|
return response<AuthWithPasswordResponse>(res, {
|
2024-12-15 17:47:07 -05:00
|
|
|
data: { otpRequested: true },
|
2024-11-17 22:44:11 -05:00
|
|
|
success: true,
|
|
|
|
error: false,
|
2024-12-15 17:47:07 -05:00
|
|
|
message: "One-time otp required to complete authentication",
|
|
|
|
status: HttpCode.ACCEPTED
|
2024-11-17 22:44:11 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = generateSessionToken();
|
|
|
|
await createResourceSession({
|
|
|
|
resourceId,
|
|
|
|
token,
|
|
|
|
passwordId: definedPassword.passwordId,
|
2024-12-15 17:47:07 -05:00
|
|
|
usedOtp: otp !== undefined,
|
|
|
|
email
|
2024-11-17 22:44:11 -05:00
|
|
|
});
|
2024-11-27 14:35:38 -05:00
|
|
|
const cookieName = `${config.server.resource_session_cookie_name}_${resource.resourceId}`;
|
2024-11-27 00:07:40 -05:00
|
|
|
const cookie = serializeResourceSessionCookie(
|
|
|
|
cookieName,
|
|
|
|
token,
|
2024-12-15 17:47:07 -05:00
|
|
|
resource.fullDomain
|
2024-11-27 00:07:40 -05:00
|
|
|
);
|
|
|
|
res.appendHeader("Set-Cookie", cookie);
|
|
|
|
|
2024-11-23 23:31:22 -05:00
|
|
|
return response<AuthWithPasswordResponse>(res, {
|
|
|
|
data: {
|
2024-12-15 17:47:07 -05:00
|
|
|
session: token
|
2024-11-23 23:31:22 -05:00
|
|
|
},
|
2024-11-17 22:44:11 -05:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Authenticated with resource successfully",
|
2024-12-15 17:47:07 -05:00
|
|
|
status: HttpCode.OK
|
2024-11-17 22:44:11 -05:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
2024-12-15 17:47:07 -05:00
|
|
|
"Failed to authenticate with resource"
|
|
|
|
)
|
2024-11-17 22:44:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|