mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-26 14:28:46 +00:00
# Which Problems Are Solved When users authenticate via IDP (Identity Provider) without explicit organization context, the flow could fail or create users without proper organization assignment. This occurred when: - No organization parameter was provided in the IDP callback - Domain discovery didn't find a matching organization - OIDC requests didn't include organization scopes # How the Problems Are Solved Implemented a fallback mechanism that ensures organization context is always available: - Centralized organization resolution in `resolveOrganizationForUser()` - First: Use explicitly provided organization - Second: Attempt domain discovery from username - Third: Fallback to default organization (NEW) - Explicit error handling: Users are never created without organization context. If no organization can be determined (including no default org), the flow fails gracefully with a clear error message. - Applied to both creation flows: - CASE 4: Auto-creation of users - CASE 5: Manual user registration Co-authored-by: Ramon <[email protected]>
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
addHuman,
|
||||
getLoginSettings,
|
||||
getOrgsByDomain,
|
||||
getDefaultOrg,
|
||||
} from "@/lib/zitadel";
|
||||
import { headers } from "next/headers";
|
||||
import { create } from "@zitadel/client";
|
||||
@@ -56,7 +57,10 @@ async function resolveOrganizationForUser({
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
|
||||
// Fallback to default organization if no org was resolved through discovery
|
||||
const defaultOrg = await getDefaultOrg({ serviceUrl });
|
||||
return defaultOrg?.id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -333,20 +337,21 @@ export async function processIDPCallback({
|
||||
serviceUrl,
|
||||
});
|
||||
|
||||
let addHumanUserWithOrganization: AddHumanUserRequest;
|
||||
if (orgToRegisterOn) {
|
||||
const organizationSchema = create(OrganizationSchema, {
|
||||
org: { case: "orgId", value: orgToRegisterOn },
|
||||
});
|
||||
|
||||
addHumanUserWithOrganization = create(AddHumanUserRequestSchema, {
|
||||
...addHumanUser,
|
||||
organization: organizationSchema,
|
||||
});
|
||||
} else {
|
||||
addHumanUserWithOrganization = create(AddHumanUserRequestSchema, addHumanUser);
|
||||
if (!orgToRegisterOn) {
|
||||
console.error("[IDP Process] Could not determine organization for auto-creation (no default org available)");
|
||||
const params = buildRedirectParams();
|
||||
return { redirect: `/idp/${provider}/failure?${params}&error=no_organization_context` };
|
||||
}
|
||||
|
||||
const organizationSchema = create(OrganizationSchema, {
|
||||
org: { case: "orgId", value: orgToRegisterOn },
|
||||
});
|
||||
|
||||
const addHumanUserWithOrganization = create(AddHumanUserRequestSchema, {
|
||||
...addHumanUser,
|
||||
organization: organizationSchema,
|
||||
});
|
||||
|
||||
try {
|
||||
const newUser = await addHuman({
|
||||
serviceUrl,
|
||||
@@ -394,7 +399,7 @@ export async function processIDPCallback({
|
||||
});
|
||||
|
||||
if (!orgToRegisterOn) {
|
||||
console.error("[IDP Process] Could not determine organization for registration");
|
||||
console.error("[IDP Process] Could not determine organization for registration (no default org available)");
|
||||
const params = buildRedirectParams();
|
||||
return { redirect: `/idp/${provider}/registration-failed?${params}` };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user