Check if subscription is valid
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { AppSettingsService } from "~/server/service/app-settings-service";
|
||||
import { parseSesHook } from "~/server/service/ses-hook-parser";
|
||||
import { SnsNotificationMessage } from "~/types/aws-types";
|
||||
import { APP_SETTINGS } from "~/utils/constants";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
console.log("GET", req);
|
||||
@@ -10,6 +13,14 @@ export async function POST(req: Request) {
|
||||
|
||||
console.log(data, data.Message);
|
||||
|
||||
const isEventValid = await checkEventValidity(data);
|
||||
|
||||
console.log("isEventValid: ", isEventValid);
|
||||
|
||||
if (!isEventValid) {
|
||||
return Response.json({ data: "Event is not valid" });
|
||||
}
|
||||
|
||||
if (data.Type === "SubscriptionConfirmation") {
|
||||
return handleSubscription(data);
|
||||
}
|
||||
@@ -19,7 +30,7 @@ export async function POST(req: Request) {
|
||||
try {
|
||||
message = JSON.parse(data.Message || "{}");
|
||||
const status = await parseSesHook(message);
|
||||
console.log("Error is parsing hook", status);
|
||||
console.log("Error is parsing hook", !status);
|
||||
if (!status) {
|
||||
return Response.json({ data: "Error is parsing hook" });
|
||||
}
|
||||
@@ -38,3 +49,17 @@ async function handleSubscription(message: any) {
|
||||
|
||||
return Response.json({ data: "Success" });
|
||||
}
|
||||
|
||||
// A simple check to ensure that the event is from the correct topic
|
||||
async function checkEventValidity(message: SnsNotificationMessage) {
|
||||
const { TopicArn } = message;
|
||||
const configuredTopicArn = await AppSettingsService.getSetting(
|
||||
APP_SETTINGS.SNS_TOPIC_ARN
|
||||
);
|
||||
|
||||
if (TopicArn !== configuredTopicArn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -1,8 +1,11 @@
|
||||
import pgBoss from "pg-boss";
|
||||
import { env } from "~/env";
|
||||
import { EmailAttachment, EmailContent } from "~/types";
|
||||
import { db } from "../db";
|
||||
import { sendEmailThroughSes, sendEmailWithAttachments } from "../aws/ses";
|
||||
import { EmailAttachment } from "~/types";
|
||||
import { db } from "~/server/db";
|
||||
import {
|
||||
sendEmailThroughSes,
|
||||
sendEmailWithAttachments,
|
||||
} from "~/server/aws/ses";
|
||||
import { getConfigurationSetName } from "~/utils/ses-utils";
|
||||
|
||||
const boss = new pgBoss({
|
||||
|
Reference in New Issue
Block a user