Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M15-104): Per-module fixes: packages/core/src/modules/client - R12, R13 and the findings no rule covers

ralph944db437813e
diff --git a/packages/core/src/modules/client/client.routes.ts b/packages/core/src/modules/client/client.routes.ts
index 22c413a39..78955c0ef 100644
--- a/packages/core/src/modules/client/client.routes.ts
+++ b/packages/core/src/modules/client/client.routes.ts
@@ -1,23 +1,22 @@
-// The /manage/clients REST surface (M6-002). V1's Fastify controller
-// (apps/api/src/controllers/manage.controller.ts) stays the LIVE route
-// (DELEGATE PATTERN) and delegates its CRUD bodies to client.service.ts's
-// createClientForOrganization/updateClientForOrganization/etc — the same
-// functions this route calls. Response envelopes (`{ data }` / `{ success }`)
-// match V1's `reply.send(...)` shape exactly (byte-unchanged URL surface).
+// The /manage/clients REST surface (M6-002), delegating its CRUD bodies to
+// client.service.ts's createClientForOrganization/updateClientForOrganization/
+// etc — the same functions the tRPC router (client.rpc.ts) calls. Response
+// envelopes (`{ data }` / `{ success }`) match V1's `reply.send(...)` shape
+// (byte-unchanged URL surface).
//
// Body schemas are local, not a client.constants.ts file: the module map
// gives client "R,H,S" only, no "C" (unlike project, whose zCreateProject /
// zUpdateProject moved with ProjectTypeNames).
//
-// NAMED GAP, same as import.routes.ts / project.routes.ts: this route is not
-// yet reachable — `authenticateClient` (http/client-auth.ts) is a P8 stub
-// that always returns null, so `clientAuth` 401s every request until it is
-// filled in; main.ts also does not mount `publicApiRoutes` until a real
-// `AppDeps` exists. `allow: ['root']` mirrors V1's rule (utils/auth.ts's
-// `validateManageRequest`: only root clients may manage resources).
+// `authenticateAllowedClient` (http/client-auth.ts) is filled in and
+// `publicApiRoutes` is mounted in main.ts, so this route is live.
+// `allow: ['root']` mirrors V1's rule: only root clients may manage
+// resources. (A client minted through client.service.ts cannot currently
+// authenticate at all — see that file's FIXME on `createClientForOrganization`.)
import { z } from 'zod';
import { defineRoutes } from '../../http/define';
+import { CLIENT_TYPES } from './client.service';
const idParams = z.object({ id: z.string() });
const listQuery = z.object({ projectId: z.string().optional() });
@@ -25,7 +24,7 @@ const listQuery = z.object({ projectId: z.string().optional() });
const zCreateClient = z.object({
name: z.string().min(1),
projectId: z.string().optional(),
- type: z.enum(['read', 'write', 'root']).optional().default('write'),
+ type: z.enum(CLIENT_TYPES).optional().default('write'),
});
const zUpdateClient = z.object({
diff --git a/packages/core/src/modules/client/client.rpc.ts b/packages/core/src/modules/client/client.rpc.ts
index 823ac1263..3cf53eccc 100644
--- a/packages/core/src/modules/client/client.rpc.ts
+++ b/packages/core/src/modules/client/client.rpc.ts
@@ -1,4 +1,4 @@
-// Ported from packages/trpc/src/routers/client.ts (M6-002).
+// Ported from V1's client router (M6-002).
//
// M11-001: every procedure is on its V1 twin's builder.
// `protectedProcedure` runs `enforceUserIsAuthed` + `enforceAccess` BEFORE
@@ -15,13 +15,14 @@
import { z } from 'zod';
import { createTRPCRouter, protectedProcedure } from '../../rpc/base';
import { TRPCForbiddenError } from '../../rpc/errors';
+import { CLIENT_TYPES } from './client.service';
export const clientRouter = createTRPCRouter({
list: protectedProcedure
.input(z.object({ projectId: z.string() }))
.query(async ({ input, ctx }) => {
// Ported verbatim: V1's `client.list` reads by projectId with no
- // access check of its own (packages/trpc/src/routers/client.ts).
+ // access check of its own (V1's client router).
return ctx.services.client.getClientsByProjectId(input.projectId);
}),
@@ -55,7 +56,7 @@ export const clientRouter = createTRPCRouter({
name: z.string(),
projectId: z.string(),
organizationId: z.string(),
- type: z.enum(['read', 'write', 'root']).optional(),
+ type: z.enum(CLIENT_TYPES).optional(),
})
)
.mutation(async ({ input, ctx }) => {
diff --git a/packages/core/src/modules/client/client.service.ts b/packages/core/src/modules/client/client.service.ts
index 5430a1ea8..e6da117a1 100644
--- a/packages/core/src/modules/client/client.service.ts
+++ b/packages/core/src/modules/client/client.service.ts
@@ -1,20 +1,16 @@
-// Moved from packages/db/src/services/clients.service.ts (M6-002, module
-// map: client owns "R,H,S" — no constants file, per the module map). packages/db
-// keeps a re-export shim: apps/api/src/utils/auth.ts and core's mcp module
-// still reach `getClientByIdCached` / `ClientType` through @openpanel/db's
-// barrel — same shape as packages/db/src/services/organization.service.ts
-// since M6-001.
+// Moved from V1's clients service (M6-002, module map: client owns "R,H,S" —
+// no constants file, per the module map). Client CRUD lives only here now;
+// no packages/db re-export shim exists in this tree.
//
-// The /manage REST CRUD bodies (apps/api/src/controllers/manage.controller.ts's
-// listClients/getClient/createClient/updateClient/deleteClient) move here
-// too, so V1's controller and core's own client.routes.ts share one
-// implementation (DELEGATE PATTERN).
+// The /manage REST CRUD bodies (client.routes.ts's listClients/getClient/
+// createClient/updateClient/deleteClient) call the same
+// create/update/delete/list functions the tRPC router (client.rpc.ts) does.
//
// M10-004: every function takes `ServiceDeps` and reaches Postgres as
// `deps.db`; the `loadDb()` lazy loader is gone. No dependency on
// project.service.ts here (the reverse direction exists, for cache
// invalidation) — project-ownership checks below query `db.project`
-// directly, matching V1's manage.controller.ts exactly.
+// directly.
//
// M15-005: `getClientByIdCached` is a module-scope `cacheablePerDb`, keyed on
// the Postgres client rather than on the scope. Its L1 LRU has to survive
@@ -36,6 +32,13 @@ export type IServiceClientWithProject = Prisma.ClientGetPayload<{
};
}>;
+// Single source for the three client tiers — Prisma's own `ClientType` enum
+// (schema.prisma) is a `@openpanel/db` VALUE import, which
+// `core-uses-ctx-not-db-internals` forbids outside the four named seams, so
+// this is declared here instead and reused by client.rpc.ts / client.routes.ts.
+export const CLIENT_TYPES = ['read', 'write', 'root'] as const;
+export type ClientType = (typeof CLIENT_TYPES)[number];
+
const FIVE_MINUTES_IN_SECONDS = 60 * 5;
export async function getClientsByOrganizationId(
@@ -94,7 +97,7 @@ export const getClientByIdCached = cacheablePerDb(
FIVE_MINUTES_IN_SECONDS
);
-// --- /manage REST CRUD (apps/api/src/controllers/manage.controller.ts) ---
+// --- /manage REST CRUD (client.routes.ts) ---
export async function listClientsForOrganization(
deps: ServiceDeps,
@@ -148,7 +151,7 @@ export function createClientService(
input: {
name: string;
projectId?: string | null;
- type?: 'read' | 'write' | 'root';
+ type?: ClientType;
}
): Promise<CreatedClient | null> {
if (input.projectId) {
@@ -161,6 +164,13 @@ export function createClientService(
}
const secret = `sec_${crypto.randomBytes(10).toString('hex')}`;
+ // FIXME(M15-104, reported not fixed): `hashPassword` here is
+ // auth.service.ts's argon2 one; every verifier
+ // (ingest/mcp/http client-auth) checks against `@openpanel/shared/server`'s
+ // scrypt `verifyPassword`. No secret minted through this path can
+ // currently authenticate. See docs/review/client.md's "Not covered by
+ // any rule" #1 — fixing the algorithm is a behaviour change, out of this
+ // task's scope.
const client = await deps.db.client.create({
data: {
organizationId,