Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

8.7 KB
Raw Blame
import * as z from "zod";
import { instantToWallTime } from "@/lib/datetime/wall-time";
import type { Conferencing, ConferencingProvider } from "./schema";

// Which OAuth integration backs each provider. Google Meet shares the Google
// OAuth app (and credential row) with Google Calendar; Microsoft Teams shares
// the Microsoft app with sign in.
export const conferencingProviderIntegrations: Record<
  ConferencingProvider,
  { integrationId: string; oauthProvider: string }
> = {
  meet: { integrationId: "google-meet", oauthProvider: "google" },
  zoom: { integrationId: "zoom", oauthProvider: "zoom" },
  teams: { integrationId: "microsoft-teams", oauthProvider: "microsoft" },
  webex: { integrationId: "webex", oauthProvider: "webex" },
};

export function integrationIdToConferencingProvider(
  integrationId: string,
): ConferencingProvider | null {
  for (const [provider, integration] of Object.entries(
    conferencingProviderIntegrations,
  )) {
    if (integration.integrationId === integrationId) {
      return provider as ConferencingProvider;
    }
  }
  return null;
}

export const conferencingProviderLabels: Record<ConferencingProvider, string> =
  {
    meet: "Google Meet",
    zoom: "Zoom",
    teams: "Microsoft Teams",
    webex: "Webex",
  };

export const zoomMeetingResponseSchema = z.object({
  id: z.number(),
  join_url: z.url(),
  password: z.string().optional(),
});

export function zoomMeetingToConferencing(
  meeting: z.infer<typeof zoomMeetingResponseSchema>,
): Conferencing {
  return {
    provider: "zoom",
    uri: meeting.join_url,
    meetingId: String(meeting.id),
    password: meeting.password || undefined,
  };
}

export const meetSpaceResponseSchema = z.object({
  meetingUri: z.url(),
  meetingCode: z.string().optional(),
});

export function meetSpaceToConferencing(
  space: z.infer<typeof meetSpaceResponseSchema>,
): Conferencing {
  return {
    provider: "meet",
    uri: space.meetingUri,
    meetingId: space.meetingCode,
  };
}

export const teamsMeetingResponseSchema = z.object({
  joinWebUrl: z.url(),
  joinMeetingIdSettings: z
    .object({
      joinMeetingId: z.string().nullish(),
      passcode: z.string().nullish(),
    })
    .nullish(),
});

export function teamsMeetingToConferencing(
  meeting: z.infer<typeof teamsMeetingResponseSchema>,
): Conferencing {
  return {
    provider: "teams",
    uri: meeting.joinWebUrl,
    meetingId: meeting.joinMeetingIdSettings?.joinMeetingId || undefined,
    password: meeting.joinMeetingIdSettings?.passcode || undefined,
  };
}

// The meeting Rallly creates and deletes to check an account at connect time.
export const teamsMeetingProbeResponseSchema = z.object({
  id: z.string().min(1),
});

// Graph answers a request the account is not entitled to make with a client
// error: no Teams license, Teams never opened, or a policy that blocks
// meetings. 401 (token), 408 (timeout) and 429 (throttling) say nothing about
// the account.
export function isTeamsMeetingRefusal(status: number) {
  return (
    status >= 400 &&
    status < 500 &&
    status !== 401 &&
    status !== 408 &&
    status !== 429
  );
}

export const webexMeetingResponseSchema = z.object({
  webLink: z.url(),
  meetingNumber: z.string().nullish(),
  password: z.string().nullish(),
});

export function webexMeetingToConferencing(
  meeting: z.infer<typeof webexMeetingResponseSchema>,
): Conferencing {
  return {
    provider: "webex",
    uri: meeting.webLink,
    meetingId: meeting.meetingNumber || undefined,
    password: meeting.password || undefined,
  };
}

const WEBEX_MIN_DURATION_MS = 10 * 60_000;
const WEBEX_MAX_DURATION_MS = (23 * 60 + 59) * 60_000;

// Webex refuses a meeting that starts in the past or lasts under 10 minutes
// or over 23 hours 59 minutes. An event already under way starts its meeting
// now, and an all day event gets the longest meeting Webex allows; the link
// is what matters, and the invite carries the event's real times.
export function getWebexMeetingWindow({
  start,
  end,
  now,
}: {
  start: Date;
  end: Date;
  now: Date;
}) {
  // Webex schedules to the minute, so "now" is the next whole minute.
  const earliest = new Date(Math.ceil((now.getTime() + 1) / 60_000) * 60_000);
  const windowStart = start < earliest ? earliest : start;
  const duration = Math.min(
    Math.max(end.getTime() - windowStart.getTime(), WEBEX_MIN_DURATION_MS),
    WEBEX_MAX_DURATION_MS,
  );
  return {
    start: windowStart,
    end: new Date(windowStart.getTime() + duration),
  };
}

// Webex rejects a time whose offset differs from the meeting's `timezone`, so
// a zoned meeting is written as that zone's wall time with its offset
// (2026-10-16T12:00:00+01:00) and a floating one in UTC.
export function formatWebexDateTime({
  date,
  timeZone,
}: {
  date: Date;
  timeZone?: string | null;
}) {
  if (!timeZone) {
    return date.toISOString();
  }
  const wallTime = instantToWallTime(date, timeZone);
  const offsetMinutes = Math.round(
    (Date.parse(`${wallTime}Z`) - date.getTime()) / 60_000,
  );
  const sign = offsetMinutes < 0 ? "-" : "+";
  const hours = String(Math.floor(Math.abs(offsetMinutes) / 60)).padStart(
    2,
    "0",
  );
  const minutes = String(Math.abs(offsetMinutes) % 60).padStart(2, "0");
  return `${wallTime}${sign}${hours}:${minutes}`;
}

// Graph's online meetings API serves work and school accounts only, so a
// multitenant registration connects Teams through the `organizations`
// authority, which keeps personal accounts from starting the flow. A single
// tenant registration keeps its own tenant.
export function getMicrosoftTeamsAuthority(tenantId: string) {
  return tenantId === "common" || tenantId === "consumers"
    ? "organizations"
    : tenantId;
}

// Returns the URI suitable for an href / ICS CONFERENCE value.
// Phone numbers are formatted as `tel:` with DTMF pause + extension if present.
export function getConferencingUri(conferencing: Conferencing): string {
  if (conferencing.provider === "phone") {
    return conferencing.extension
      ? `tel:${conferencing.number},,${conferencing.extension}`
      : `tel:${conferencing.number}`;
  }
  return conferencing.uri;
}

// What moderation sees of a pasted link: the origin and path only. A Zoom
// join link carries the meeting password in its query string.
export function moderatedLinkText(uri: string | undefined) {
  if (!uri) {
    return "";
  }
  try {
    const { origin, pathname } = new URL(uri);
    return `${origin}${pathname}`;
  } catch {
    return uri;
  }
}

// How long a captured, correctly signed request stays replayable.
const ZOOM_WEBHOOK_TOLERANCE_MS = 5 * 60_000;

const encoder = new TextEncoder();

function importHmacKey(secretToken: string) {
  return crypto.subtle.importKey(
    "raw",
    encoder.encode(secretToken),
    { name: "HMAC", hash: "SHA-256" },
    false,
    ["sign", "verify"],
  );
}

function fromHex(hex: string) {
  if (!/^(?:[0-9a-f]{2})+$/i.test(hex)) {
    return null;
  }
  const bytes = new Uint8Array(hex.length / 2);
  for (let i = 0; i < bytes.length; i++) {
    bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
  }
  return bytes;
}

function toHex(buffer: ArrayBuffer) {
  return Array.from(new Uint8Array(buffer))
    .map((byte) => byte.toString(16).padStart(2, "0"))
    .join("");
}

// Zoom signs every event notification with the app's Secret Token:
// `v0=` + hex HMAC-SHA256 of `v0:{x-zm-request-timestamp}:{raw body}`, the
// timestamp in seconds.
export async function verifyZoomWebhookSignature({
  secretToken,
  signature,
  timestamp,
  body,
  now,
}: {
  secretToken: string;
  signature: string | null;
  timestamp: string | null;
  body: string;
  now: Date;
}): Promise<
  | { ok: true }
  | { ok: false; reason: "missing_headers" | "stale" | "invalid_signature" }
> {
  if (!signature || !timestamp || !/^\d+$/.test(timestamp)) {
    return { ok: false, reason: "missing_headers" };
  }
  if (
    Math.abs(now.getTime() - Number(timestamp) * 1000) >
    ZOOM_WEBHOOK_TOLERANCE_MS
  ) {
    return { ok: false, reason: "stale" };
  }
  const expected = signature.startsWith("v0=")
    ? fromHex(signature.slice(3))
    : null;
  // `verify` compares in constant time, unlike comparing hex strings.
  const valid =
    expected !== null &&
    (await crypto.subtle.verify(
      "HMAC",
      await importHmacKey(secretToken),
      expected,
      encoder.encode(`v0:${timestamp}:${body}`),
    ));
  return valid ? { ok: true } : { ok: false, reason: "invalid_signature" };
}

// The answer to the challenge Zoom sends when the endpoint URL is saved.
export async function createZoomUrlValidationResponse({
  secretToken,
  plainToken,
}: {
  secretToken: string;
  plainToken: string;
}) {
  const encryptedToken = toHex(
    await crypto.subtle.sign(
      "HMAC",
      await importHmacKey(secretToken),
      encoder.encode(plainToken),
    ),
  );
  return { plainToken, encryptedToken };
}