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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.
import { getCarbonServiceRole } from "@carbon/auth/client.server";
import { issueOAuthState } from "@carbon/auth/oauth-state.server";
import { resolveIntegrationSecrets } from "../integrations/secrets";
import {
getConflictingOnshapeIntegration,
isOnshapeIntegrationId
} from "./lib/connection";
import type { OnshapeOAuthIntegrationId } from "./lib/integration-id";
import {
getOnshapeAuthorizeUrl,
getOnshapeOAuthConfig,
type OnshapeOAuthConfig
} from "./lib/oauth";
/**
* The OAuth client for one of a company's Onshape connections. The public app
* reads this instance's environment; a Government private app reads the
* client id, secret and hosts its admin saved in the integration settings (the
* secret from Vault, so this needs the service role).
*/
export async function loadOnshapeOAuthConfig(
companyId: string,
integrationId: OnshapeOAuthIntegrationId
): Promise<OnshapeOAuthConfig | null> {
const serviceRole = getCarbonServiceRole();
const integration = await serviceRole
.from("companyIntegration")
.select("metadata, secretRef")
.eq("id", integrationId)
.eq("companyId", companyId)
.maybeSingle();
let metadata: Record<string, unknown> = {};
if (integration.data) {
try {
metadata = (await resolveIntegrationSecrets(
serviceRole,
companyId,
integrationId,
integration.data.metadata,
integration.data.secretRef
)) as Record<string, unknown>;
} catch {
// No vaulted secret yet: a Government connection is simply not
// configured, and the public app never needed one to start.
metadata = (integration.data.metadata ?? {}) as Record<string, unknown>;
}
}
return getOnshapeOAuthConfig(integrationId, metadata);
}
export type OnshapeAuthorizationStart =
| { ok: true; url: string; cookie: string }
| { ok: false; reason: "not-configured" | "connection-conflict" };
/**
* Refuses a sync connection while the other one is active; the panel's grant
* sits beside either. The state is bound to this browser, user, company and
* integration, so a callback URL carrying another account's code is refused.
*/
export async function beginOnshapeAuthorization(
request: Request,
params: {
integrationId: OnshapeOAuthIntegrationId;
userId: string;
companyId: string;
}
): Promise<OnshapeAuthorizationStart> {
if (isOnshapeIntegrationId(params.integrationId)) {
const conflict = await getConflictingOnshapeIntegration(
getCarbonServiceRole(),
params.companyId,
params.integrationId
);
if (conflict) return { ok: false, reason: "connection-conflict" };
}
const config = await loadOnshapeOAuthConfig(
params.companyId,
params.integrationId
);
if (!config) return { ok: false, reason: "not-configured" };
const { state, cookie } = await issueOAuthState(params, request);
return { ok: true, url: getOnshapeAuthorizeUrl(config, state), cookie };
}