el.xwx.moe/lib/api/controllers/tokens/postToken.ts

97 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-01-13 00:20:06 -06:00
import { prisma } from "@/lib/api/db";
2024-09-14 15:00:19 -05:00
import {
PostTokenSchemaType,
PostTokenSchema,
} from "@/lib/shared/schemaValidation";
import { TokenExpiry } from "@/types/global";
2024-01-13 00:20:06 -06:00
import crypto from "crypto";
import { decode, encode } from "next-auth/jwt";
2024-01-13 00:20:06 -06:00
export default async function postToken(
2024-09-14 15:00:19 -05:00
body: PostTokenSchemaType,
2024-01-13 00:20:06 -06:00
userId: number
) {
2024-09-14 15:00:19 -05:00
const dataValidation = PostTokenSchema.safeParse(body);
2024-01-13 00:20:06 -06:00
2024-09-14 15:00:19 -05:00
if (!dataValidation.success) {
2024-01-13 00:20:06 -06:00
return {
2024-09-14 15:00:19 -05:00
response: `Error: ${
dataValidation.error.issues[0].message
} [${dataValidation.error.issues[0].path.join(", ")}]`,
2024-01-13 00:20:06 -06:00
status: 400,
};
2024-09-14 15:00:19 -05:00
}
const { name, expires } = dataValidation.data;
2024-01-13 00:20:06 -06:00
const checkIfTokenExists = await prisma.accessToken.findFirst({
2024-01-13 00:20:06 -06:00
where: {
2024-09-14 15:00:19 -05:00
name: name,
revoked: false,
2024-01-13 00:20:06 -06:00
userId,
},
});
if (checkIfTokenExists) {
return {
response: "Token with that name already exists.",
status: 400,
};
}
2024-01-24 11:51:16 -06:00
const now = Date.now();
2024-01-13 00:20:06 -06:00
let expiryDate = new Date();
2024-01-24 11:51:16 -06:00
const oneDayInSeconds = 86400;
let expiryDateSecond = 7 * oneDayInSeconds;
2024-01-13 00:20:06 -06:00
2024-09-14 15:00:19 -05:00
if (expires === TokenExpiry.oneMonth) {
2024-01-24 11:51:16 -06:00
expiryDate.setDate(expiryDate.getDate() + 30);
expiryDateSecond = 30 * oneDayInSeconds;
2024-09-14 15:00:19 -05:00
} else if (expires === TokenExpiry.twoMonths) {
2024-01-24 11:51:16 -06:00
expiryDate.setDate(expiryDate.getDate() + 60);
expiryDateSecond = 60 * oneDayInSeconds;
2024-09-14 15:00:19 -05:00
} else if (expires === TokenExpiry.threeMonths) {
2024-01-24 11:51:16 -06:00
expiryDate.setDate(expiryDate.getDate() + 90);
expiryDateSecond = 90 * oneDayInSeconds;
2024-09-14 15:00:19 -05:00
} else if (expires === TokenExpiry.never) {
2024-01-24 11:51:16 -06:00
expiryDate.setDate(expiryDate.getDate() + 73000); // 200 years (not really never)
expiryDateSecond = 73050 * oneDayInSeconds;
} else {
expiryDate.setDate(expiryDate.getDate() + 7);
expiryDateSecond = 7 * oneDayInSeconds;
2024-01-13 00:20:06 -06:00
}
2024-01-24 11:51:16 -06:00
const token = await encode({
token: {
id: userId,
iat: now / 1000,
exp: (expiryDate as any) / 1000,
jti: crypto.randomUUID(),
},
maxAge: expiryDateSecond || 604800,
2024-06-27 17:19:07 -05:00
secret: process.env.NEXTAUTH_SECRET as string,
2024-01-24 11:51:16 -06:00
});
2024-01-13 00:20:06 -06:00
2024-01-24 11:51:16 -06:00
const tokenBody = await decode({
token,
2024-06-27 17:19:07 -05:00
secret: process.env.NEXTAUTH_SECRET as string,
2024-01-24 11:51:16 -06:00
});
2024-01-13 00:20:06 -06:00
const createToken = await prisma.accessToken.create({
2024-01-13 00:20:06 -06:00
data: {
2024-09-14 15:00:19 -05:00
name: name,
2024-01-13 00:20:06 -06:00
userId,
2024-01-24 11:51:16 -06:00
token: tokenBody?.jti as string,
2024-01-13 00:20:06 -06:00
expires: expiryDate,
},
});
return {
response: {
secretKey: token,
token: createToken,
},
2024-01-13 00:20:06 -06:00
status: 200,
};
}