Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { createHmac, randomUUID } from "node:crypto";
|
||||
|
||||
import { getDbClient } from "./client.server.js";
|
||||
import type { ApiKey, ApiKeyEntity, ApiKeyLog, Prisma, Tenant } from "./prisma-browser.js";
|
||||
import type { ApiKey, ApiKeyLog, Prisma, Tenant } from "./prisma-browser.js";
|
||||
|
||||
export type LegacyApiKeyUserSummary = {
|
||||
id: string;
|
||||
@@ -15,7 +15,6 @@ export type LegacyApiKeyUserSummary = {
|
||||
|
||||
export type LegacyApiKeyWithDetails = ApiKey & {
|
||||
tenant: { id: string; name: string; slug: string; deactivatedReason: string | null };
|
||||
entities: (ApiKeyEntity & { entity: { id: string; title: string; titlePlural: string; slug: string } })[];
|
||||
createdByUser: LegacyApiKeyUserSummary;
|
||||
_count: { apiKeyLogs: number };
|
||||
};
|
||||
@@ -42,14 +41,9 @@ export type LegacyApiKeySimple = {
|
||||
alias: string;
|
||||
expires: Date | null;
|
||||
tenantId: string;
|
||||
createdByUserId: string;
|
||||
active: boolean;
|
||||
entities: {
|
||||
create: boolean;
|
||||
read: boolean;
|
||||
update: boolean;
|
||||
delete: boolean;
|
||||
entityId: string;
|
||||
}[];
|
||||
scopes: unknown;
|
||||
tenant: { id: string; name: string; slug: string; deactivatedReason: string | null };
|
||||
};
|
||||
|
||||
@@ -83,6 +77,7 @@ export type LegacyApiKeyCreateData = {
|
||||
expires: Date | null;
|
||||
active: boolean;
|
||||
key?: string;
|
||||
scopes?: Prisma.InputJsonValue;
|
||||
};
|
||||
|
||||
export type LegacyApiKeyCreateStoredData = LegacyApiKeyCreateData & LegacyApiKeyHashMetadata & { key: string };
|
||||
@@ -150,14 +145,7 @@ export type LegacyApiKeyUpdateData = {
|
||||
alias: string;
|
||||
expires: Date | null;
|
||||
active: boolean;
|
||||
};
|
||||
|
||||
export type LegacyApiKeyEntityPermissionData = {
|
||||
entityId: string;
|
||||
create: boolean;
|
||||
read: boolean;
|
||||
update: boolean;
|
||||
delete: boolean;
|
||||
scopes?: Prisma.InputJsonValue;
|
||||
};
|
||||
|
||||
export type LegacyApiKeyReaderClient = {
|
||||
@@ -228,21 +216,6 @@ export type LegacyApiKeyLogWriterClient = {
|
||||
findFirst(args: { where: { id: string; tenantId: string } }): Promise<ApiKey | null>;
|
||||
deleteMany(args: { where: { id: string; tenantId: string } }): Promise<Prisma.BatchPayload>;
|
||||
};
|
||||
apiKeyEntity: {
|
||||
create(args: { data: LegacyApiKeyEntityPermissionData & { apiKeyId: string } }): Promise<unknown>;
|
||||
deleteMany(args: { where: Prisma.ApiKeyEntityWhereInput }): Promise<unknown>;
|
||||
};
|
||||
entity: {
|
||||
findMany(args: {
|
||||
where: {
|
||||
id: { in: string[] };
|
||||
active: true;
|
||||
hasApi: true;
|
||||
type: { in: ["app", "all"] };
|
||||
};
|
||||
select: { id: true };
|
||||
}): Promise<{ id: string }[]>;
|
||||
};
|
||||
apiKeyLog: {
|
||||
create(args: {
|
||||
data: LegacyApiKeyLogCreateData;
|
||||
@@ -268,36 +241,6 @@ export type LegacyApiKeyLogWriterClient = {
|
||||
};
|
||||
};
|
||||
|
||||
async function validateLegacyApiKeyEntityScopes({
|
||||
tenantId,
|
||||
entities,
|
||||
client,
|
||||
}: {
|
||||
tenantId: string;
|
||||
entities: LegacyApiKeyEntityPermissionData[];
|
||||
client: LegacyApiKeyLogWriterClient;
|
||||
}): Promise<void> {
|
||||
const entityIds = [...new Set(entities.map((entity) => entity.entityId).filter(Boolean))];
|
||||
if (entityIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const allowedEntities = await client.entity.findMany({
|
||||
where: {
|
||||
id: { in: entityIds },
|
||||
active: true,
|
||||
hasApi: true,
|
||||
type: { in: ["app", "all"] },
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
const allowedIds = new Set(allowedEntities.map((entity) => entity.id));
|
||||
const invalidEntityIds = entityIds.filter((entityId) => !allowedIds.has(entityId));
|
||||
if (invalidEntityIds.length > 0) {
|
||||
throw new Error(`API key entity scope is not allowed for tenant ${tenantId}.`);
|
||||
}
|
||||
}
|
||||
|
||||
const selectLegacyApiKeyUserSummary = {
|
||||
id: true,
|
||||
email: true,
|
||||
@@ -309,7 +252,7 @@ const selectLegacyApiKeyUserSummary = {
|
||||
} as const;
|
||||
|
||||
export const LEGACY_API_KEY_HASH_VERSION = 1 as const;
|
||||
export const DEFAULT_LEGACY_API_KEY_HASH_SECRET = "boilerplate-api-key-hash-v1";
|
||||
export const DEFAULT_LEGACY_API_KEY_HASH_SECRET = "hivegrid-api-key-hash-v1";
|
||||
export const DEFAULT_LEGACY_API_KEY_HASH_BACKFILL_BATCH_SIZE = 100;
|
||||
export const LEGACY_API_KEY_PREFIX_LENGTH = 8;
|
||||
export const LEGACY_API_KEY_LAST_FOUR_LENGTH = 4;
|
||||
@@ -398,11 +341,6 @@ export async function backfillLegacyApiKeyHashes({
|
||||
|
||||
export const legacyApiKeyDetailsInclude = {
|
||||
tenant: { select: { id: true, name: true, slug: true, deactivatedReason: true } },
|
||||
entities: {
|
||||
include: {
|
||||
entity: { select: { id: true, title: true, titlePlural: true, slug: true } },
|
||||
},
|
||||
},
|
||||
createdByUser: {
|
||||
select: selectLegacyApiKeyUserSummary,
|
||||
},
|
||||
@@ -418,8 +356,9 @@ const legacyApiKeySimpleSelect = {
|
||||
alias: true,
|
||||
expires: true,
|
||||
tenantId: true,
|
||||
createdByUserId: true,
|
||||
active: true,
|
||||
entities: { select: { create: true, read: true, update: true, delete: true, entityId: true } },
|
||||
scopes: true,
|
||||
tenant: { select: { id: true, name: true, slug: true, deactivatedReason: true } },
|
||||
} as const;
|
||||
|
||||
@@ -593,12 +532,9 @@ export async function findFirstLegacyActiveApiKey(
|
||||
|
||||
export async function createLegacyApiKey(
|
||||
data: LegacyApiKeyCreateData,
|
||||
entities: LegacyApiKeyEntityPermissionData[],
|
||||
client: LegacyApiKeyLogWriterClient = getDbClient() as unknown as LegacyApiKeyLogWriterClient,
|
||||
hashSecret?: string | null,
|
||||
): Promise<LegacyApiKeyCreateResult> {
|
||||
await validateLegacyApiKeyEntityScopes({ tenantId: data.tenantId, entities, client });
|
||||
|
||||
const key = data.key ?? randomUUID();
|
||||
const hashMetadata = deriveLegacyApiKeyHashMetadata(key, hashSecret);
|
||||
const apiKey = await client.apiKey.create({
|
||||
@@ -610,17 +546,6 @@ export async function createLegacyApiKey(
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
entities.map(async (entity) => {
|
||||
return client.apiKeyEntity.create({
|
||||
data: {
|
||||
apiKeyId: apiKey.id,
|
||||
...entity,
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
...apiKey,
|
||||
key,
|
||||
@@ -630,11 +555,8 @@ export async function createLegacyApiKey(
|
||||
export async function updateLegacyApiKey(
|
||||
id: string,
|
||||
data: LegacyApiKeyUpdateData,
|
||||
entities: LegacyApiKeyEntityPermissionData[],
|
||||
client: LegacyApiKeyLogWriterClient = getDbClient() as unknown as LegacyApiKeyLogWriterClient,
|
||||
): Promise<ApiKey> {
|
||||
await validateLegacyApiKeyEntityScopes({ tenantId: data.tenantId, entities, client });
|
||||
|
||||
const updated = await client.apiKey.updateMany({
|
||||
where: { id, tenantId: data.tenantId },
|
||||
data,
|
||||
@@ -643,24 +565,6 @@ export async function updateLegacyApiKey(
|
||||
throw new Error(`API key ${id} not found`);
|
||||
}
|
||||
|
||||
await client.apiKeyEntity.deleteMany({
|
||||
where: {
|
||||
apiKeyId: id,
|
||||
apiKey: { tenantId: data.tenantId },
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
entities.map(async (entity) => {
|
||||
return client.apiKeyEntity.create({
|
||||
data: {
|
||||
apiKeyId: id,
|
||||
...entity,
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const apiKey = await client.apiKey.findFirst({ where: { id, tenantId: data.tenantId } });
|
||||
if (!apiKey) {
|
||||
throw new Error(`API key ${id} could not be reloaded`);
|
||||
|
||||
Reference in new issue
Block a user