Skip to content

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

fix(admin-api): handle Okta provisioning for POST and PATCH (#8064)

Steffen Schmitz48b05247c84a
diff --git a/web/src/pages/api/public/scim/Users/[id].ts b/web/src/pages/api/public/scim/Users/[id].ts
index c3e2f5930b..5fe3553892 100644
--- a/web/src/pages/api/public/scim/Users/[id].ts
+++ b/web/src/pages/api/public/scim/Users/[id].ts
@@ -12,7 +12,7 @@ export default async function handler(
) {
await runMiddleware(req, res, cors);
- if (!["GET", "DELETE"].includes(req.method || "")) {
+ if (!["GET", "DELETE", "PATCH"].includes(req.method || "")) {
logger.error(
`Method not allowed for ${req.method} on /api/public/scim/Users/[id]`,
);
@@ -74,6 +74,8 @@ export default async function handler(
// Route to the appropriate handler based on HTTP method
try {
switch (req.method) {
+ case "PATCH":
+ return handlePatch(req, res, orgMembership.user, authCheck.scope.orgId);
case "GET":
return handleGet(req, res, orgMembership.user);
case "DELETE":
@@ -133,6 +135,111 @@ async function handleGet(
});
}
+// PATCH - Update user details (Use only for deprovisioning for now)
+// Payload is a string like: "{\"schemas\":[\"urn:ietf:params:scim:api:messages:2.0:PatchOp\"],\"Operations\":[{\"op\":\"replace\",\"value\":{\"active\":false}}]}"
+async function handlePatch(
+ req: NextApiRequest,
+ res: NextApiResponse,
+ user: User,
+ orgId: string,
+) {
+ let body = req.body;
+
+ // Check if body is a string and parse it
+ if (typeof body === "string") {
+ try {
+ body = JSON.parse(body);
+ } catch (error) {
+ logger.warn("Failed to parse JSON body", error);
+ return res.status(400).json({
+ schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
+ detail: "Invalid JSON body",
+ status: 400,
+ });
+ }
+ }
+
+ // Validate the request body
+ if (
+ !body.schemas ||
+ !Array.isArray(body.schemas) ||
+ !body.schemas.includes("urn:ietf:params:scim:api:messages:2.0:PatchOp")
+ ) {
+ logger.warn(
+ "Invalid request body. Must include 'schemas' with 'urn:ietf:params:scim:api:messages:2.0:PatchOp'.",
+ body,
+ );
+ return res.status(400).json({
+ schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
+ detail:
+ "Invalid request body. Must include 'schemas' with 'urn:ietf:params:scim:api:messages:2.0:PatchOp'.",
+ status: 400,
+ });
+ }
+
+ // Check for operations
+ if (!body.Operations || !Array.isArray(body.Operations)) {
+ logger.warn(
+ "Invalid request body. Must include 'Operations' array with at least one operation.",
+ body,
+ );
+ return res.status(400).json({
+ schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
+ detail:
+ "Invalid request body. Must include 'Operations' array with at least one operation.",
+ status: 400,
+ });
+ }
+
+ // Process each operation
+ for (const op of body.Operations) {
+ if (
+ op.op === "replace" &&
+ op.value &&
+ typeof op.value.active === "boolean"
+ ) {
+ if (op.value.active) {
+ // Provision the user by adding them to the organization
+ await prisma.organizationMembership.upsert({
+ where: {
+ orgId_userId: {
+ orgId: orgId,
+ userId: user.id,
+ },
+ },
+ create: {
+ userId: user.id,
+ orgId: orgId,
+ role: "NONE",
+ },
+ update: {},
+ });
+ } else {
+ // Deprovision the user by removing them from the organization
+ await prisma.organizationMembership.deleteMany({
+ where: {
+ userId: user.id,
+ orgId: orgId,
+ },
+ });
+ }
+ } else {
+ logger.error(
+ "Unsupported operation or invalid value in request body. Only 'replace' with 'active' field is supported.",
+ op,
+ );
+ return res.status(400).json({
+ schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
+ detail:
+ "Unsupported operation or invalid value in request body. Only 'replace' with 'active' field is supported.",
+ status: 400,
+ });
+ }
+ }
+
+ return handleGet(req, res, user);
+}
+
// DELETE - Remove user from organization
async function handleDelete(
req: NextApiRequest,
diff --git a/web/src/pages/api/public/scim/Users/index.ts b/web/src/pages/api/public/scim/Users/index.ts
index 8e00029d42..fbd14d6b25 100644
--- a/web/src/pages/api/public/scim/Users/index.ts
+++ b/web/src/pages/api/public/scim/Users/index.ts
@@ -133,9 +133,24 @@ export default async function handler(
if (req.method === "POST") {
try {
- const { userName, name, password } = req.body;
+ const body = req.body;
+ if (typeof body === "string") {
+ try {
+ req.body = JSON.parse(body);
+ } catch (error) {
+ logger.error("Failed to parse JSON body", error);
+ return res.status(400).json({
+ schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
+ detail: "Invalid JSON body",
+ status: 400,
+ });
+ }
+ }
+
+ const { userName, name, password, displayName } = req.body;
if (!userName) {
+ logger.warn("userName is required for SCIM user creation");
return res.status(400).json({
schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
detail: "userName is required",
@@ -154,6 +169,9 @@ export default async function handler(
});
if (existingUser.length > 0) {
+ logger.warn(
+ `User with userName ${userName} already exists in organization ${authCheck.scope.orgId}`,
+ );
return res.status(409).json({
schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
detail: "User with this userName already exists",
@@ -168,7 +186,7 @@ export default async function handler(
},
create: {
email: userName.toLowerCase(),
- name: name.formatted,
+ name: name?.formatted || displayName,
password: password ? await hashPassword(password) : undefined,
},
update: {},