Secured mutations with an api-key. When in production mode it should return null if an API key is not provided or is incorrect.
All checks were successful
Main / build-and-push-docker-image (20.x) (push) Successful in 5m6s

This commit is contained in:
2025-12-10 16:42:21 -05:00
parent 678d1e7b5e
commit 30b4f65ec6
3 changed files with 156 additions and 78 deletions

View File

@@ -8,8 +8,6 @@ interface NextContext {
params: Promise<Record<string, string>>; params: Promise<Record<string, string>>;
} }
console.log(process.env);
const environment = process.env.NODE_ENV || "development"; const environment = process.env.NODE_ENV || "development";
const isValidApiKey = (apiKey: string): boolean => { const isValidApiKey = (apiKey: string): boolean => {

View File

@@ -1,6 +1,9 @@
import prisma from "@/lib/prismaClient"; import prisma from "@/lib/prismaClient";
import { BigIntResolver } from "graphql-scalars"; import { BigIntResolver } from "graphql-scalars";
const envMutationKey = process.env.MUTATION_KEY || "";
const env = process.env.NODE_ENV || "development";
// Prisma // Prisma
export const resolvers = { export const resolvers = {
// scalars // scalars
@@ -39,11 +42,23 @@ export const resolvers = {
}) })
}, },
Mutation: { Mutation: {
init: async () => init: async (
// _parent: unknown, _parent: unknown,
// data: { newWeek: boolean; newMonth: boolean } data: { mutationKey?: string }
// _ctx: unknown // _ctx: unknown
{ ) => {
const { mutationKey } = data;
if (env !== "development") {
if (!mutationKey) {
return null;
}
if (mutationKey !== envMutationKey) {
return null;
}
}
const date = new Date().toISOString(); const date = new Date().toISOString();
let count = 0; let count = 0;
@@ -58,11 +73,22 @@ export const resolvers = {
return `${count} tables have been initialized with data.`; return `${count} tables have been initialized with data.`;
}, },
cronJob: async () => cronJob: async (
// _parent: unknown, _parent: unknown,
// data: { newWeek: boolean; newMonth: boolean } data: { mutationKey?: string }
// _ctx: unknown // _ctx: unknown
{ ) => {
const { mutationKey } = data;
if (env !== "development") {
if (!mutationKey) {
return null;
}
if (mutationKey !== envMutationKey) {
return null;
}
}
const date = new Date().toISOString(); const date = new Date().toISOString();
await prisma.dailyStats.create({ data: { createdAt: date } }); await prisma.dailyStats.create({ data: { createdAt: date } });
@@ -105,10 +131,25 @@ export const resolvers = {
}, },
addGroup: async ( addGroup: async (
_parent: unknown, _parent: unknown,
data: { groupID: number; groupName: string; groupUsername: string } data: {
groupID: number;
groupName: string;
groupUsername: string;
mutationKey?: string;
}
// _ctx: unknown // _ctx: unknown
) => { ) => {
const { groupID, groupName, groupUsername } = data; const { groupID, groupName, groupUsername, mutationKey } = data;
if (env !== "development") {
if (!mutationKey) {
return null;
}
if (mutationKey !== envMutationKey) {
return null;
}
}
const existingGroup = await prisma.groups.findFirst({ const existingGroup = await prisma.groups.findFirst({
where: { telegramID: groupID } where: { telegramID: groupID }
@@ -139,10 +180,20 @@ export const resolvers = {
}, },
incrementGroup: async ( incrementGroup: async (
_parent: unknown, _parent: unknown,
data: { groupID: number; linksDeleted: number } data: { groupID: number; linksDeleted: number; mutationKey?: string }
// _ctx: unknown // _ctx: unknown
) => { ) => {
const { groupID, linksDeleted } = data; const { groupID, linksDeleted, mutationKey } = data;
if (env !== "development") {
if (!mutationKey) {
return null;
}
if (mutationKey !== envMutationKey) {
return null;
}
}
return await prisma.groups.update({ return await prisma.groups.update({
where: { telegramID: groupID }, where: { telegramID: groupID },
@@ -151,10 +202,25 @@ export const resolvers = {
}, },
increment: async ( increment: async (
_parent: unknown, _parent: unknown,
data: { link: boolean; command: boolean; trigger: boolean } data: {
link: boolean;
command: boolean;
trigger: boolean;
mutationKey?: string;
}
// _ctx: unknown // _ctx: unknown
) => { ) => {
const { link, command, trigger } = data; const { link, command, trigger, mutationKey } = data;
if (env !== "development") {
if (!mutationKey) {
return null;
}
if (mutationKey !== envMutationKey) {
return null;
}
}
return await prisma.dailyStats return await prisma.dailyStats
.findFirst({ .findFirst({

View File

@@ -11,11 +11,25 @@ const typeDefs = /* GraphQL */ `
getTotalStats: TotalStats! getTotalStats: TotalStats!
} }
type Mutation { type Mutation {
init: String! init(mutationKey: String): String
cronJob: TotalStats! cronJob(mutationKey: String): TotalStats
addGroup(groupID: BigInt, groupName: String, groupUsername: String): Groups! addGroup(
incrementGroup(groupID: BigInt, linksDeleted: Int): Groups! groupID: BigInt!
increment(link: Boolean, command: Boolean, trigger: Boolean): DailyStats! groupName: String!
groupUsername: String
mutationKey: String
): Groups
incrementGroup(
groupID: BigInt!
linksDeleted: Int!
mutationKey: String
): Groups
increment(
link: Boolean
command: Boolean
trigger: Boolean
mutationKey: String
): DailyStats
} }
type Groups { type Groups {