Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(scim): write audit log on user creation via SCIM POST (#13496)

Steffen Schmitzd6b549be14b0
diff --git a/web/src/__tests__/server/scim-api.servertest.ts b/web/src/__tests__/server/scim-api.servertest.ts
index d2d18d9ecd..3895138c94 100644
--- a/web/src/__tests__/server/scim-api.servertest.ts
+++ b/web/src/__tests__/server/scim-api.servertest.ts
@@ -574,6 +574,43 @@ describe("SCIM API", () => {
expect(orgMemberships[0].role).toBe("ADMIN");
});
+ it("should write an audit log entry when creating a user", async () => {
+ const uniqueEmail = `test.user.${randomUUID().substring(0, 8)}@example.com`;
+ const response = await makeAPICall(
+ "POST",
+ "/api/public/scim/Users",
+ {
+ userName: uniqueEmail,
+ name: {
+ formatted: "Audited User",
+ },
+ roles: ["MEMBER"],
+ },
+ createBasicAuthHeader(orgApiKey, orgSecretKey),
+ );
+
+ expect(response.status).toBe(201);
+ testUserId = response.body.id;
+
+ const orgMembership = await prisma.organizationMembership.findFirst({
+ where: { userId: testUserId, orgId: orgId },
+ });
+ expect(orgMembership).not.toBeNull();
+
+ const auditLogs = await prisma.auditLog.findMany({
+ where: {
+ resourceType: "orgMembership",
+ resourceId: orgMembership!.id,
+ action: "create",
+ orgId: orgId,
+ },
+ });
+ expect(auditLogs.length).toBe(1);
+ expect(auditLogs[0].apiKeyId).not.toBeNull();
+ expect(auditLogs[0].userId).toBeNull();
+ expect(auditLogs[0].after).toContain(orgMembership!.id);
+ });
+
it("should return 409 when user with the same userName already exists", async () => {
const uniqueEmail = `test.user.${randomUUID().substring(0, 8)}@example.com`;
diff --git a/web/src/pages/api/public/scim/Users/index.ts b/web/src/pages/api/public/scim/Users/index.ts
index c68a34de90..23631026ef 100644
--- a/web/src/pages/api/public/scim/Users/index.ts
+++ b/web/src/pages/api/public/scim/Users/index.ts
@@ -7,6 +7,7 @@ import { type NextApiRequest, type NextApiResponse } from "next";
import { hashPassword } from "@/src/features/auth-credentials/lib/credentialsServerUtils";
import { z } from "zod";
import { type Role } from "@langfuse/shared";
+import { auditLog } from "@/src/features/audit-logs/auditLog";
export default async function handler(
req: NextApiRequest,
@@ -215,13 +216,21 @@ export default async function handler(
},
update: {},
});
- await prisma.organizationMembership.create({
+ const orgMembership = await prisma.organizationMembership.create({
data: {
userId: user.id,
orgId: authCheck.scope.orgId,
role,
},
});
+ await auditLog({
+ resourceType: "orgMembership",
+ resourceId: orgMembership.id,
+ action: "create",
+ after: orgMembership,
+ apiKeyId: authCheck.scope.apiKeyId,
+ orgId: authCheck.scope.orgId,
+ });
logger.info(
`[SCIM] Assigned user ${user.id} to org ${authCheck.scope.orgId} with role ${role}`,
);