Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

4.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { getAppUrl } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { consumeOAuthState } from "@carbon/auth/oauth-state.server";
import { getIntegrationConfigById } from "@carbon/ee";
import {
  exchangeCodeForTokens,
  getAccessibleResources
} from "@carbon/ee/jira.server";
import { getLogger } from "@carbon/logger";
import { redirectExternal } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { data } from "react-router";
import { upsertCompanyIntegration } from "~/modules/settings/settings.service";
import { oAuthCallbackSchema } from "~/modules/shared/shared.models";
import { path } from "~/utils/path";

const logger = getLogger("erp", "integrations-jira-oauth");

export const config = {
  runtime: "nodejs"
};

export async function loader({ request }: LoaderFunctionArgs) {
  const { client, userId, companyId } = await requirePermissions(request, {
    update: "settings"
  });

  const url = new URL(request.url);
  const searchParams = Object.fromEntries(url.searchParams.entries());

  const jiraAuthResponse = oAuthCallbackSchema.safeParse(searchParams);

  if (!jiraAuthResponse.success) {
    return data({ error: "Invalid Jira auth response" }, { status: 400 });
  }

  const { data: params } = jiraAuthResponse;

  // The state must be the one the integrations page issued to THIS browser
  // for this user and company (IntegrationCard puts it on the authorize URL).
  // Without the check anyone could send a victim a callback URL carrying the
  // attacker's own authorization code, linking the victim's company to the
  // attacker's Jira account. Single-use: consumed whether it matches or not.
  const consumedState = await consumeOAuthState(request, params.state, {
    integrationId: "jira",
    userId,
    companyId
  });

  if (!consumedState.valid) {
    logger.error("Invalid Jira OAuth state", { companyId, userId });
    return data(
      { error: "Invalid state parameter" },
      { status: 400, headers: { "Set-Cookie": consumedState.cookie } }
    );
  }

  try {
    // Must match the authorize-time redirect_uri, which IntegrationCard builds
    // from `window.location.origin`. `new URL(request.url).origin` is the
    // internal proxy address behind a TLS-terminating proxy (e.g. the portless
    // dev proxy → http://127.0.0.1:<port>), which fails as a redirect_uri
    // mismatch. `getAppUrl()` is the canonical public origin in dev/preview/prod.
    const redirectUri = `${getAppUrl()}/api/integrations/jira/oauth`;

    // Exchange the authorization code for tokens
    const tokens = await exchangeCodeForTokens(params.code, redirectUri);

    if (!tokens) {
      return data(
        { error: "Failed to exchange code for token" },
        { status: 500 }
      );
    }

    // Get accessible resources to find cloudId
    const resources = await getAccessibleResources(tokens.accessToken);

    if (resources.length === 0) {
      return data(
        {
          error:
            "No Jira Cloud sites found. Make sure you have access to at least one Jira site."
        },
        { status: 400 }
      );
    }

    // Use the first available resource
    const resource = resources[0];

    const createdJiraIntegration = await upsertCompanyIntegration(client, {
      id: "jira",
      active: true,
      metadata: {
        credentials: {
          accessToken: tokens.accessToken,
          refreshToken: tokens.refreshToken,
          expiresAt: Date.now() + tokens.expiresIn * 1000,
          cloudId: resource.id,
          siteUrl: resource.url
        }
      },
      updatedBy: userId,
      companyId: companyId
    });

    const config = getIntegrationConfigById("jira");

    typeof config?.onInstall === "function" &&
      (await config.onInstall(companyId));

    if (createdJiraIntegration?.data?.metadata) {
      // Redirect on the canonical public origin — `request.url`'s origin is the
      // internal proxy address in dev, which would drop the session cookies.
      return redirectExternal(`${getAppUrl()}${path.to.integrations}`, {
        headers: { "Set-Cookie": consumedState.cookie }
      });
    } else {
      return data(
        { error: "Failed to save Jira integration" },
        { status: 500 }
      );
    }
  } catch (err) {
    logger.error("Jira OAuth Error", { error: err });
    return data(
      { error: "Failed to exchange code for token" },
      { status: 500 }
    );
  }
}