Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -16,16 +16,24 @@
|
||||
* Refs: docs/improvement-plan/05-business-features/realtime-collab.md
|
||||
*/
|
||||
import type { ActionFunctionArgs } from "react-router";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import { evaluateFlagForContext } from "~/utils/services/.server/featureFlagEvaluator.server";
|
||||
import { getTenantUserType } from "~/utils/db/tenants.db.server";
|
||||
import { knowledgeBaseArticleBelongsToTenant } from "~/modules/knowledgeBase/db/kbArticles.db.server";
|
||||
import { getUserPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
|
||||
const TOKEN_TTL_SECONDS = 60 * 15; // 15 minutes — matches spec.
|
||||
const TOKEN_REFRESH_SKEW_SECONDS = 60;
|
||||
const TOKEN_REFRESH_AFTER_SECONDS = TOKEN_TTL_SECONDS - TOKEN_REFRESH_SKEW_SECONDS;
|
||||
const COLLAB_TOKEN_ISSUER = "hivegrid-web";
|
||||
const COLLAB_TOKEN_AUDIENCE = "hivegrid-hocuspocus";
|
||||
const MIN_AUTH_SECRET_BYTES = 32;
|
||||
const DOCUMENT_NAME_PART_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,255}$/u;
|
||||
const RESOURCE_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,255}$/u;
|
||||
|
||||
type CollabTokenRequest = {
|
||||
tenantId?: unknown;
|
||||
@@ -38,7 +46,7 @@ function isNonEmptyString(value: unknown): value is string {
|
||||
}
|
||||
|
||||
function isDocumentNamePart(value: unknown, options: { allowColon?: boolean } = {}): value is string {
|
||||
return isNonEmptyString(value) && (options.allowColon === true || !value.includes(":"));
|
||||
return isNonEmptyString(value) && (options.allowColon === true ? RESOURCE_ID_PATTERN.test(value) : DOCUMENT_NAME_PART_PATTERN.test(value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +71,7 @@ export async function action({ request }: ActionFunctionArgs) {
|
||||
}
|
||||
|
||||
const secret = process.env.HOCUSPOCUS_AUTH_SECRET?.trim();
|
||||
if (!secret) {
|
||||
if (!secret || Buffer.byteLength(secret, "utf8") < MIN_AUTH_SECRET_BYTES) {
|
||||
return Response.json({ error: "collab_disabled", reason: "missing-server-secret" }, { status: 503 });
|
||||
}
|
||||
|
||||
@@ -81,49 +89,69 @@ export async function action({ request }: ActionFunctionArgs) {
|
||||
if (!isDocumentNamePart(body.tenantId) || !isDocumentNamePart(body.resourceType) || !isDocumentNamePart(body.resourceId, { allowColon: true })) {
|
||||
return Response.json({ error: "bad_request", reason: "missing-fields" }, { status: 400 });
|
||||
}
|
||||
const tenantId = body.tenantId;
|
||||
const resourceType = body.resourceType;
|
||||
const resourceId = body.resourceId;
|
||||
|
||||
const membership = await getTenantUserType(session.userId, body.tenantId);
|
||||
const membership = await getTenantUserType(session.userId, tenantId);
|
||||
if (!membership) {
|
||||
return Response.json({ error: "forbidden", reason: "tenant-membership-required" }, { status: 403 });
|
||||
}
|
||||
|
||||
// Per-tenant feature-flag gate. `realtime-collab-enabled` defaults to
|
||||
// disabled when the flag row does not exist, so self-hosted installs that
|
||||
// do not seed the flag stay on single-user mode automatically.
|
||||
const flag = await evaluateFlagForContext("realtime-collab-enabled", {
|
||||
flagName: "realtime-collab-enabled",
|
||||
tenantId: body.tenantId,
|
||||
const { userPermission } = await getUserPermission({
|
||||
userId: session.userId,
|
||||
permissionName: "admin.kb.update",
|
||||
});
|
||||
if (!flag.enabled) {
|
||||
return Response.json({ error: "feature_disabled", reason: flag.reason }, { status: 403 });
|
||||
if (!userPermission) {
|
||||
return Response.json({ error: "forbidden", reason: "resource-write-permission-required" }, { status: 403 });
|
||||
}
|
||||
|
||||
const authorized = await isCollabResourceAuthorized({
|
||||
userId: session.userId,
|
||||
tenantId: body.tenantId,
|
||||
resourceType: body.resourceType,
|
||||
resourceId: body.resourceId,
|
||||
});
|
||||
if (!authorized) {
|
||||
return Response.json({ error: "forbidden", reason: "resource-access-denied" }, { status: 403 });
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
return runWithRlsContext({ tenantId, userId: session.userId }, async () => {
|
||||
// Per-tenant feature-flag gate. `realtime-collab-enabled` defaults to
|
||||
// disabled when the flag row does not exist, so self-hosted installs that
|
||||
// do not seed the flag stay on single-user mode automatically.
|
||||
const flag = await evaluateFlagForContext("realtime-collab-enabled", {
|
||||
flagName: "realtime-collab-enabled",
|
||||
tenantId,
|
||||
userId: session.userId,
|
||||
tenantId: body.tenantId,
|
||||
resourceType: body.resourceType,
|
||||
resourceId: body.resourceId,
|
||||
},
|
||||
secret,
|
||||
{ algorithm: "HS256", expiresIn: TOKEN_TTL_SECONDS },
|
||||
);
|
||||
});
|
||||
if (!flag.enabled) {
|
||||
return Response.json({ error: "feature_disabled", reason: flag.reason }, { status: 403 });
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
token,
|
||||
expiresIn: TOKEN_TTL_SECONDS,
|
||||
refreshAfter: TOKEN_REFRESH_AFTER_SECONDS,
|
||||
documentName: `${body.tenantId}:${body.resourceType}:${body.resourceId}`,
|
||||
const authorized = await isCollabResourceAuthorized({
|
||||
userId: session.userId,
|
||||
tenantId,
|
||||
resourceType,
|
||||
resourceId,
|
||||
});
|
||||
if (!authorized) {
|
||||
return Response.json({ error: "forbidden", reason: "resource-access-denied" }, { status: 403 });
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
userId: session.userId,
|
||||
tenantId,
|
||||
resourceType,
|
||||
resourceId,
|
||||
},
|
||||
secret,
|
||||
{
|
||||
algorithm: "HS256",
|
||||
audience: COLLAB_TOKEN_AUDIENCE,
|
||||
expiresIn: TOKEN_TTL_SECONDS,
|
||||
issuer: COLLAB_TOKEN_ISSUER,
|
||||
jwtid: randomUUID(),
|
||||
subject: session.userId,
|
||||
}
|
||||
);
|
||||
|
||||
return Response.json({
|
||||
token,
|
||||
expiresIn: TOKEN_TTL_SECONDS,
|
||||
refreshAfter: TOKEN_REFRESH_AFTER_SECONDS,
|
||||
documentName: `${tenantId}:${resourceType}:${resourceId}`,
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user