Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use tokens for public mutating endpoints #14946

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/web/modules/bookings/views/bookings-single-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
TITLE_FIELD,
} from "@calcom/features/bookings/lib/SystemField";
import { APP_NAME } from "@calcom/lib/constants";
import { symmetricEncrypt } from "@calcom/lib/crypto";
import {
formatToLocalizedDate,
formatToLocalizedTime,
Expand Down Expand Up @@ -193,7 +194,9 @@ export default function Success(props: PageProps) {
}, []);

const sendFeedback = async (rating: string, comment: string) => {
mutation.mutate({ bookingUid: bookingInfo.uid, rating: rateValue, comment: comment });
const data = { bookingUid: bookingInfo.uid, rating: rateValue, comment: comment };
const token = symmetricEncrypt(JSON.stringify(data), process.env.CALENDSO_ENCRYPTION_KEY || "");
mutation.mutate({ ...data, token });
};

function setIsCancellationMode(value: boolean) {
Expand Down
12 changes: 10 additions & 2 deletions packages/trpc/server/routers/publicViewer/_router.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { symmetricDecrypt } from "@calcom/lib/crypto";

import publicProcedure from "../../procedures/publicProcedure";
import { importHandler, router } from "../../trpc";
import { slotsRouter } from "../viewer/slots/_router";
Expand Down Expand Up @@ -25,8 +27,14 @@ export const publicViewerRouter = router({
return handler(opts);
}),
submitRating: publicProcedure.input(ZSubmitRatingInputSchema).mutation(async (opts) => {
const handler = await importHandler(namespaced("submitRating"), () => import("./submitRating.handler"));
return handler(opts);
const { token, ...rest } = opts.input;
const data = symmetricDecrypt(token, process.env.CALENDSO_ENCRYPTION_KEY || "");
if (data === JSON.stringify(rest)) {
const handler = await importHandler(namespaced("submitRating"), () => import("./submitRating.handler"));
return handler(opts);
} else {
throw new Error("Invalid token");
}
}),
noShow: publicProcedure.input(ZNoShowInputSchema).mutation(async (opts) => {
const handler = await importHandler(namespaced("noShow"), () => import("./noShow.handler"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const ZSubmitRatingInputSchema = z.object({
bookingUid: z.string(),
rating: z.number(),
comment: z.string().optional(),
token: z.string(),
});

export type TSubmitRatingInputSchema = z.infer<typeof ZSubmitRatingInputSchema>;