* extract NextRequest * update api and tests * booking limits tests * fix more tests to use new approach * update more tests with new format * extract getOrgDomainConfig to not use req * extract req from loadNewUsers and pass in hostname/forcedSlug * fix instant meeting types and hostname fixes * fix handleNewBookingReq * fix type errors in tests * make hostName and forcedSlug optional * fix type err * Revert "fix type err" This reverts commit 9d5de9019d9dafe348c97b876baaa1d0675967e5. * wip fix e2e * fix: add missing headers * migrate handle recurring event and also create tests specific to fn * platform recurringbooking * fix type * hard code types on request object * bump libraries * fixup! bump libraries * fix: accessing host if headers not passed * fix: v2 recurring booking * fix: accessing host if headers not passed * chore: bump platform libraries * fix tests * push * chore: bump libraries * push lock changes * bump libraries --------- Co-authored-by: amrit <[email protected]> Co-authored-by: Morgan <[email protected]> Co-authored-by: Morgan Vernay <[email protected]> Co-authored-by: supalarry <[email protected]> Co-authored-by: Lauris Skraucis <[email protected]>
190 lines
5.7 KiB
TypeScript
190 lines
5.7 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
import type { IncomingMessage } from "http";
|
|
|
|
import { IS_PRODUCTION, WEBSITE_URL } from "@calcom/lib/constants";
|
|
import { ALLOWED_HOSTNAMES, RESERVED_SUBDOMAINS, WEBAPP_URL } from "@calcom/lib/constants";
|
|
import logger from "@calcom/lib/logger";
|
|
import slugify from "@calcom/lib/slugify";
|
|
|
|
const log = logger.getSubLogger({
|
|
prefix: ["orgDomains.ts"],
|
|
});
|
|
/**
|
|
* return the org slug
|
|
* @param hostname
|
|
*/
|
|
export function getOrgSlug(hostname: string, forcedSlug?: string) {
|
|
if (forcedSlug) {
|
|
if (process.env.NEXT_PUBLIC_IS_E2E) {
|
|
log.debug("Using provided forcedSlug in E2E", {
|
|
forcedSlug,
|
|
});
|
|
return forcedSlug;
|
|
}
|
|
log.debug("Ignoring forcedSlug in non-test mode", {
|
|
forcedSlug,
|
|
});
|
|
}
|
|
|
|
if (!hostname.includes(".")) {
|
|
log.warn('Org support not enabled for hostname without "."', { hostname });
|
|
// A no-dot domain can never be org domain. It automatically considers localhost to be non-org domain
|
|
return null;
|
|
}
|
|
// Find which hostname is being currently used
|
|
const currentHostname = ALLOWED_HOSTNAMES.find((ahn) => {
|
|
const url = new URL(WEBAPP_URL);
|
|
const testHostname = `${url.hostname}${url.port ? `:${url.port}` : ""}`;
|
|
return testHostname.endsWith(`.${ahn}`);
|
|
});
|
|
|
|
if (!currentHostname) {
|
|
log.warn("Match of WEBAPP_URL with ALLOWED_HOSTNAME failed", { WEBAPP_URL, ALLOWED_HOSTNAMES });
|
|
return null;
|
|
}
|
|
// Define which is the current domain/subdomain
|
|
const slug = hostname.replace(currentHostname ? `.${currentHostname}` : "", "");
|
|
const hasNoDotInSlug = slug.indexOf(".") === -1;
|
|
if (hasNoDotInSlug) {
|
|
return slug;
|
|
}
|
|
log.warn("Derived slug ended up having dots, so not considering it an org domain", { slug });
|
|
return null;
|
|
}
|
|
|
|
export function getOrgDomainConfig({
|
|
hostname,
|
|
fallback,
|
|
forcedSlug,
|
|
isPlatform,
|
|
}: {
|
|
hostname: string;
|
|
fallback?: string | string[];
|
|
forcedSlug?: string;
|
|
isPlatform?: boolean;
|
|
}) {
|
|
if (isPlatform && forcedSlug) {
|
|
return {
|
|
isValidOrgDomain: true,
|
|
currentOrgDomain: forcedSlug,
|
|
};
|
|
}
|
|
|
|
return getOrgDomainConfigFromHostname({
|
|
hostname,
|
|
fallback,
|
|
forcedSlug,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use `getOrgDomainConfig` instead. To be removed in a future release. getOrgDomainConfig is more flexible and can be used without next request.
|
|
*/
|
|
export function orgDomainConfig(req: IncomingMessage | undefined, fallback?: string | string[]) {
|
|
const forPlatform = isPlatformRequest(req);
|
|
const forcedSlugHeader = req?.headers?.["x-cal-force-slug"];
|
|
const forcedSlug = forcedSlugHeader instanceof Array ? forcedSlugHeader[0] : forcedSlugHeader;
|
|
|
|
if (forPlatform && forcedSlug) {
|
|
return {
|
|
isValidOrgDomain: true,
|
|
currentOrgDomain: forcedSlug,
|
|
};
|
|
}
|
|
|
|
const hostname = req?.headers?.host || "";
|
|
return getOrgDomainConfigFromHostname({
|
|
hostname,
|
|
fallback,
|
|
forcedSlug,
|
|
});
|
|
}
|
|
|
|
function isPlatformRequest(req: IncomingMessage | undefined) {
|
|
return !!req?.headers?.["x-cal-client-id"];
|
|
}
|
|
|
|
export function getOrgDomainConfigFromHostname({
|
|
hostname,
|
|
fallback,
|
|
forcedSlug,
|
|
}: {
|
|
hostname: string;
|
|
fallback?: string | string[];
|
|
forcedSlug?: string;
|
|
}) {
|
|
const currentOrgDomain = getOrgSlug(hostname, forcedSlug);
|
|
const isValidOrgDomain = currentOrgDomain !== null && !RESERVED_SUBDOMAINS.includes(currentOrgDomain);
|
|
if (isValidOrgDomain || !fallback) {
|
|
return {
|
|
currentOrgDomain: isValidOrgDomain ? currentOrgDomain : null,
|
|
isValidOrgDomain,
|
|
};
|
|
}
|
|
const fallbackOrgSlug = fallback as string;
|
|
const isValidFallbackDomain = !RESERVED_SUBDOMAINS.includes(fallbackOrgSlug);
|
|
return {
|
|
currentOrgDomain: isValidFallbackDomain ? fallbackOrgSlug : null,
|
|
isValidOrgDomain: isValidFallbackDomain,
|
|
};
|
|
}
|
|
|
|
export function subdomainSuffix() {
|
|
if (!IS_PRODUCTION && process.env.LOCAL_TESTING_DOMAIN_VERCEL) {
|
|
// Allow testing with a valid domain so that we can test with deployment services like Vercel and Cloudflare locally.
|
|
return process.env.LOCAL_TESTING_DOMAIN_VERCEL;
|
|
}
|
|
const urlSplit = WEBAPP_URL.replace("https://", "")?.replace("http://", "").split(".");
|
|
return urlSplit.length === 3 ? urlSplit.slice(1).join(".") : urlSplit.join(".");
|
|
}
|
|
|
|
export function getOrgFullOrigin(slug: string | null, options: { protocol: boolean } = { protocol: true }) {
|
|
if (!slug)
|
|
return options.protocol ? WEBSITE_URL : WEBSITE_URL.replace("https://", "").replace("http://", "");
|
|
|
|
const orgFullOrigin = `${
|
|
options.protocol ? `${new URL(WEBSITE_URL).protocol}//` : ""
|
|
}${slug}.${subdomainSuffix()}`;
|
|
return orgFullOrigin;
|
|
}
|
|
|
|
/**
|
|
* @deprecated You most probably intend to query for an organization only, use `whereClauseForOrgWithSlugOrRequestedSlug` instead which will only return the organization and not a team accidentally.
|
|
*/
|
|
export function getSlugOrRequestedSlug(slug: string) {
|
|
const slugifiedValue = slugify(slug);
|
|
return {
|
|
OR: [
|
|
{ slug: slugifiedValue },
|
|
{
|
|
metadata: {
|
|
path: ["requestedSlug"],
|
|
equals: slugifiedValue,
|
|
},
|
|
},
|
|
],
|
|
} satisfies Prisma.TeamWhereInput;
|
|
}
|
|
|
|
export function whereClauseForOrgWithSlugOrRequestedSlug(slug: string) {
|
|
const slugifiedValue = slugify(slug);
|
|
|
|
return {
|
|
OR: [
|
|
{ slug: slugifiedValue },
|
|
{
|
|
metadata: {
|
|
path: ["requestedSlug"],
|
|
equals: slug,
|
|
},
|
|
},
|
|
],
|
|
isOrganization: true,
|
|
} satisfies Prisma.TeamWhereInput;
|
|
}
|
|
|
|
export function userOrgQuery(req: IncomingMessage | undefined, fallback?: string | string[]) {
|
|
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(req, fallback);
|
|
return isValidOrgDomain && currentOrgDomain ? getSlugOrRequestedSlug(currentOrgDomain) : null;
|
|
}
|