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: 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 { CONTROLLED_ENVIRONMENT } from "@carbon/auth";
import { getUserScopedClient } from "@carbon/auth/client.server";
import { userHasVerifiedTotpFactor } from "@carbon/auth/mfa.server";
import { requireAuthSession } from "@carbon/auth/session.server";
import { getUserClaims } from "@carbon/auth/users.server";
import { PANEL_SESSION_MESSAGE } from "@carbon/ee";
import { createPanelSession } from "@carbon/ee/onshape/panel-session.server";
import { getLogger } from "@carbon/logger";
import { requiresItarEntityCertification } from "@carbon/utils";
import type { LoaderFunctionArgs } from "react-router";
import { getCompanySettings } from "~/modules/settings/settings.service";
import { getItarCertificationStatus } from "~/modules/users/users.service";
import { path } from "~/utils/path";
export const config = {
runtime: "nodejs"
};
const logger = getLogger("erp", "onshape", "panel-auth");
/** Panel sign-in popup; applies the app shell's MFA and ITAR gates itself. */
export async function loader({ request }: LoaderFunctionArgs) {
const authSession = await requireAuthSession(request);
const { companyId, userId } = authSession;
// Also refuses customer and supplier portal accounts.
const claims = await getUserClaims(userId, companyId);
if (claims.role !== "employee") {
return page("The Onshape panel is available to employees only.", 403);
}
// A console session is a shared device; writes would be stamped with it.
if (authSession.console) {
return page(
"The Onshape panel can't be used from a console session. Sign in to Carbon with your own account.",
403
);
}
const client = await getUserScopedClient(userId);
const [companySettings, itar] = await Promise.all([
getCompanySettings(client, companyId),
CONTROLLED_ENVIRONMENT
? getItarCertificationStatus(client, companyId, userId)
: Promise.resolve({ entityCertified: true, userCertified: true })
]);
const itarBlocked =
CONTROLLED_ENVIRONMENT &&
((requiresItarEntityCertification(authSession.email) &&
!itar.entityCertified) ||
!itar.userCertified);
const mfaRequired =
(CONTROLLED_ENVIRONMENT || companySettings.data?.requireMfa === true) &&
!authSession.ssoProviderId;
if (
itarBlocked ||
(mfaRequired && !(await userHasVerifiedTotpFactor(userId)))
) {
return page(
"Finish setting up your Carbon account, then sign in to the panel again.",
403,
{ href: path.to.authenticatedRoot, label: "Open Carbon" }
);
}
const token = await createPanelSession(authSession);
if (!token) {
logger.error("Could not store an Onshape panel session", {
companyId,
userId
});
return page("Carbon couldn't sign you in. Try again in a moment.", 503);
}
// Safe inside a <script>: no "</" and no JS line terminators.
const message = JSON.stringify({ type: PANEL_SESSION_MESSAGE, token })
.replace(/</g, "\\u003c")
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<title>Carbon</title>
</head>
<body style="font-family: system-ui, sans-serif; padding: 2rem; color: #333;">
<p id="status">Signing you in to the Onshape panel…</p>
<script>
(function () {
var message = ${message};
var opener = window.opener;
var delivered = false;
try {
if (opener && !opener.closed) {
opener.postMessage(message, window.location.origin);
delivered = true;
}
} catch (_) {}
var status = document.getElementById("status");
if (delivered) {
status.textContent = "Signed in. You can close this window.";
window.close();
} else {
status.textContent = "Open this page from the Carbon panel in Onshape.";
}
})();
</script>
</body>
</html>
`;
return new Response(html, {
status: 200,
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-store"
}
});
}
function escapeHtml(value: string) {
return value
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
/** A refusal the user reads in the popup. Nothing is posted to the panel. */
function page(
message: string,
status: number,
link?: { href: string; label: string }
) {
const action = link
? `<p><a href="${escapeHtml(link.href)}">${escapeHtml(link.label)}</a></p>`
: "";
return new Response(
`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<title>Carbon</title>
</head>
<body style="font-family: system-ui, sans-serif; padding: 2rem; color: #333;">
<p>${escapeHtml(message)}</p>
${action}
</body>
</html>
`,
{
status,
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-store"
}
}
);
}