2023-03-23 18:46:33 +00:00
|
|
|
import type { Session } from "next-auth";
|
|
|
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
2022-09-29 16:58:29 +00:00
|
|
|
import superjson from "superjson";
|
|
|
|
|
2023-03-23 18:46:33 +00:00
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
import { defaultAvatarSrc } from "@calcom/lib/defaultAvatarImage";
|
2023-03-23 22:10:01 +00:00
|
|
|
import rateLimit from "@calcom/lib/rateLimit";
|
2023-03-23 18:46:33 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-12-13 21:34:21 +00:00
|
|
|
|
2023-03-23 18:46:33 +00:00
|
|
|
import type { Maybe } from "@trpc/server";
|
2022-09-29 16:58:29 +00:00
|
|
|
import { initTRPC, TRPCError } from "@trpc/server";
|
|
|
|
|
2023-03-06 13:17:16 +00:00
|
|
|
import type { createContextInner } from "./createContext";
|
2022-09-29 16:58:29 +00:00
|
|
|
|
2023-03-23 18:46:33 +00:00
|
|
|
async function getUserFromSession({ session }: { session: Maybe<Session> }) {
|
|
|
|
if (!session?.user?.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
bio: true,
|
|
|
|
timeZone: true,
|
|
|
|
weekStart: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
defaultScheduleId: true,
|
|
|
|
bufferTime: true,
|
|
|
|
theme: true,
|
|
|
|
createdDate: true,
|
|
|
|
hideBranding: true,
|
|
|
|
avatar: true,
|
|
|
|
twoFactorEnabled: true,
|
|
|
|
disableImpersonation: true,
|
|
|
|
identityProvider: true,
|
|
|
|
brandColor: true,
|
|
|
|
darkBrandColor: true,
|
|
|
|
away: true,
|
|
|
|
credentials: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
type: true,
|
|
|
|
key: true,
|
|
|
|
userId: true,
|
|
|
|
appId: true,
|
|
|
|
invalid: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
id: "asc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
selectedCalendars: {
|
|
|
|
select: {
|
|
|
|
externalId: true,
|
|
|
|
integration: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
completedOnboarding: true,
|
|
|
|
destinationCalendar: true,
|
|
|
|
locale: true,
|
|
|
|
timeFormat: true,
|
|
|
|
trialEndsAt: true,
|
|
|
|
metadata: true,
|
|
|
|
role: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// some hacks to make sure `username` and `email` are never inferred as `null`
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const { email, username } = user;
|
|
|
|
if (!email) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const rawAvatar = user.avatar;
|
|
|
|
// This helps to prevent reaching the 4MB payload limit by avoiding base64 and instead passing the avatar url
|
|
|
|
user.avatar = rawAvatar ? `${WEBAPP_URL}/${user.username}/avatar.png` : defaultAvatarSrc({ email });
|
|
|
|
|
|
|
|
return {
|
|
|
|
...user,
|
|
|
|
rawAvatar,
|
|
|
|
email,
|
|
|
|
username,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:17:16 +00:00
|
|
|
const t = initTRPC.context<typeof createContextInner>().create({
|
2022-09-29 16:58:29 +00:00
|
|
|
transformer: superjson,
|
|
|
|
});
|
|
|
|
|
|
|
|
const perfMiddleware = t.middleware(async ({ path, type, next }) => {
|
|
|
|
performance.mark("Start");
|
|
|
|
const result = await next();
|
|
|
|
performance.mark("End");
|
|
|
|
performance.measure(`[${result.ok ? "OK" : "ERROR"}][$1] ${type} '${path}'`, "Start", "End");
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
export const isAuthed = t.middleware(async ({ ctx: { session, locale, ...ctx }, next }) => {
|
2023-03-23 18:46:33 +00:00
|
|
|
const user = await getUserFromSession({ session });
|
|
|
|
if (!user || !session) {
|
2022-09-29 16:58:29 +00:00
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
2023-03-23 18:46:33 +00:00
|
|
|
const i18n =
|
|
|
|
user.locale && user.locale !== locale
|
|
|
|
? await serverSideTranslations(user.locale, ["common", "vital"])
|
|
|
|
: ctx.i18n;
|
|
|
|
locale = user.locale || locale;
|
2022-09-29 16:58:29 +00:00
|
|
|
return next({
|
|
|
|
ctx: {
|
2023-03-23 18:46:33 +00:00
|
|
|
i18n,
|
2022-09-29 16:58:29 +00:00
|
|
|
// infers that `user` and `session` are non-nullable to downstream procedures
|
2023-03-23 18:46:33 +00:00
|
|
|
session,
|
|
|
|
user: {
|
|
|
|
...user,
|
|
|
|
locale,
|
|
|
|
},
|
2022-09-29 16:58:29 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-23 18:46:33 +00:00
|
|
|
const isAdminMiddleware = isAuthed.unstable_pipe(({ ctx, next }) => {
|
2023-02-28 21:40:19 +00:00
|
|
|
if (ctx.user.role !== "ADMIN") {
|
2022-12-07 21:47:02 +00:00
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
return next({
|
2023-02-28 21:40:19 +00:00
|
|
|
ctx: { user: ctx.user },
|
2022-12-07 21:47:02 +00:00
|
|
|
});
|
|
|
|
});
|
2023-02-28 21:40:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
interface IRateLimitOptions {
|
|
|
|
intervalInMs: number;
|
|
|
|
limit: number;
|
|
|
|
}
|
|
|
|
const isRateLimitedByUserIdMiddleware = ({ intervalInMs, limit }: IRateLimitOptions) =>
|
|
|
|
t.middleware(({ ctx, next }) => {
|
|
|
|
// validate user exists
|
|
|
|
if (!ctx.user) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const { isRateLimited } = rateLimit({ intervalInMs }).check(limit, ctx.user.id.toString());
|
|
|
|
|
|
|
|
if (isRateLimited) {
|
|
|
|
throw new TRPCError({ code: "TOO_MANY_REQUESTS" });
|
|
|
|
}
|
|
|
|
|
|
|
|
return next({
|
|
|
|
ctx: {
|
|
|
|
// infers that `user` and `session` are non-nullable to downstream procedures
|
|
|
|
session: ctx.session,
|
|
|
|
user: ctx.user,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
export const router = t.router;
|
|
|
|
export const mergeRouters = t.mergeRouters;
|
|
|
|
export const middleware = t.middleware;
|
2022-09-29 16:58:29 +00:00
|
|
|
export const publicProcedure = t.procedure.use(perfMiddleware);
|
2023-03-23 18:46:33 +00:00
|
|
|
export const authedProcedure = t.procedure.use(perfMiddleware).use(isAuthed);
|
2023-03-23 22:10:01 +00:00
|
|
|
export const authedRateLimitedProcedure = ({ intervalInMs, limit }: IRateLimitOptions) =>
|
|
|
|
authedProcedure.use(isRateLimitedByUserIdMiddleware({ intervalInMs, limit }));
|
2022-12-07 21:47:02 +00:00
|
|
|
export const authedAdminProcedure = t.procedure.use(perfMiddleware).use(isAdminMiddleware);
|