Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(auth): show sub-org selector and fix SAML-enforced org login

Igor Corread25fcbb0e98e
diff --git a/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx b/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx
index aa68ac2a49..8315833210 100644
--- a/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx
+++ b/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx
@@ -62,77 +62,34 @@ export const PasswordStep = ({
providerAuthToken
});
- // attemptCliLogin
- const cliUrl = `http://127.0.0.1:${callbackPort}/`;
-
// unset provider auth token in case it was used
SecurityClient.setProviderAuthToken("");
// set JWT token
SecurityClient.setToken(oauthLogin.token);
- // case: organization ID is present from the provider auth token -- select the org and use the new jwt token in the CLI, then navigate to the org
- if (organizationId) {
- const finishWithOrgWorkflow = async () => {
- const { token, isMfaEnabled, mfaMethod } = await selectOrganization({ organizationId });
-
- if (isMfaEnabled) {
- SecurityClient.setMfaToken(token);
- setMfaSuccessCallback(() => finishWithOrgWorkflow);
- if (mfaMethod) {
- setRequiredMfaMethod(mfaMethod);
- }
- toggleShowMfa.on();
- return;
- }
-
- if (callbackPort) {
- console.log("organization id was present. new JWT token to be used in CLI:", token);
- const instance = axios.create();
- const payload = {
- privateKey: "", // note(daniel): no longer needed by the CLI, because the CLI only uses the private key to create service tokens, and the private key isn't used anymore when creating service tokens.
- email,
- JTWToken: token
- };
- await instance.post(cliUrl, payload).catch(() => {
- // if error happens to communicate we set the token with an expiry in sessino storage
- // the cli-redirect page has logic to show this to user and ask them to paste it in terminal
- sessionStorage.setItem(
- SessionStorageKeys.CLI_TERMINAL_TOKEN,
- JSON.stringify({
- expiry: formatISO(addSeconds(new Date(), 30)),
- data: window.btoa(JSON.stringify(payload))
- })
- );
- });
- navigate({ to: "/cli-redirect" });
- return;
- }
+ if (callbackPort) {
+ navigateToSelectOrganization(callbackPort, isAdminLogin);
+ return;
+ }
- const userDuplicateAccount = await fetchUserDuplicateAccounts();
- const hasDuplicate = userDuplicateAccount?.length > 1;
- if (hasDuplicate) {
- setRemoveDuplicateLater(false);
- return;
- }
+ if (organizationId) {
+ const userDuplicateAccount = await fetchUserDuplicateAccounts();
+ const hasDuplicate = userDuplicateAccount?.length > 1;
+ if (hasDuplicate) {
+ setRemoveDuplicateLater(false);
+ return;
+ }
+ }
- await navigateUserToOrg({ navigate, organizationId });
- };
+ // case: user has orgs, so we navigate the user to select an org
+ const userOrgs = await fetchOrganizations();
- await finishWithOrgWorkflow();
+ if (userOrgs.length > 0) {
+ navigateToSelectOrganization(undefined, isAdminLogin);
}
- // case: no organization ID is present -- navigate to the select org page IF the user has any orgs
- // if the user has no orgs, navigate to the create org page
+ // case: no orgs found, so we navigate the user to create an org
else {
- const userOrgs = await fetchOrganizations();
-
- // case: user has orgs, so we navigate the user to select an org
- if (userOrgs.length > 0) {
- navigateToSelectOrganization(callbackPort, isAdminLogin);
- }
- // case: no orgs found, so we navigate the user to create an org
- else {
- await navigateUserToOrg({ navigate });
- }
+ await navigateUserToOrg({ navigate });
}
} catch (err: any) {
setIsLoading(false);
diff --git a/frontend/src/pages/auth/SelectOrgPage/SelectOrgSection.tsx b/frontend/src/pages/auth/SelectOrgPage/SelectOrgSection.tsx
index 8bfb8dd885..46e0ae0144 100644
--- a/frontend/src/pages/auth/SelectOrgPage/SelectOrgSection.tsx
+++ b/frontend/src/pages/auth/SelectOrgPage/SelectOrgSection.tsx
@@ -54,6 +54,15 @@ export const SelectOrganizationSection = () => {
const mfaPending = queryParams.get("mfa_pending") === "true";
const defaultSelectedOrg = organizations.data?.find((org) => org.id === orgId);
+ const willAutoSelectDefaultOrg = useMemo(() => {
+ if (!defaultSelectedOrg || callbackPort || orgsWithSubOrgs.isPending) return false;
+ const orgEntry = orgsWithSubOrgs.data?.find((o) => o.id === defaultSelectedOrg.id);
+ return (orgEntry?.subOrganizations.length ?? 0) === 0;
+ }, [defaultSelectedOrg, callbackPort, orgsWithSubOrgs.isPending, orgsWithSubOrgs.data]);
+
+ const isLoadingSubOrgCheck =
+ Boolean(defaultSelectedOrg) && !callbackPort && orgsWithSubOrgs.isPending;
+
const logout = useLogoutUser(true);
const handleLogout = useCallback(async () => {
try {
@@ -133,9 +142,11 @@ export const SelectOrganizationSection = () => {
callbackPort ? `&callbackPort=${callbackPort}` : ""
}`;
} else if (organization.orgAuthMethod === AuthMethod.SAML) {
- url = `/api/v1/sso/redirect/saml2/organizations/${organization.slug}`;
- if (callbackPort) {
- url += `?callback_port=${callbackPort}`;
+ if (authToken.authMethod !== AuthMethod.SAML) {
+ url = `/api/v1/sso/redirect/saml2/organizations/${organization.slug}`;
+ if (callbackPort) {
+ url += `?callback_port=${callbackPort}`;
+ }
}
}
@@ -282,10 +293,9 @@ export const SelectOrganizationSection = () => {
orgsWithSubOrgs.data.length === 1 && orgsWithSubOrgs.data[0].subOrganizations.length === 0;
if (onlyOneRootOrgWithNoSubOrgs) {
- if (callbackPort) {
- handleCliRedirect();
- setIsInitialOrgCheckLoading(false);
- } else {
+ // CLI flow (callbackPort) or no org_id in URL: auto-select the only org.
+ // When defaultSelectedOrg is set without callbackPort, willAutoSelectDefaultOrg handles selection via the second useEffect.
+ if (callbackPort || !defaultSelectedOrg) {
handleSelectOrganization(organizations.data[0]);
}
} else {
@@ -295,7 +305,8 @@ export const SelectOrganizationSection = () => {
organizations.isPending,
organizations.data,
orgsWithSubOrgs.isPending,
- orgsWithSubOrgs.data
+ orgsWithSubOrgs.data,
+ defaultSelectedOrg
]);
useEffect(() => {
@@ -311,10 +322,10 @@ export const SelectOrganizationSection = () => {
}
}
- if (defaultSelectedOrg) {
+ if (willAutoSelectDefaultOrg && defaultSelectedOrg) {
handleSelectOrganization(defaultSelectedOrg);
}
- }, [defaultSelectedOrg, mfaPending]);
+ }, [defaultSelectedOrg, mfaPending, willAutoSelectDefaultOrg]);
const renderListContent = () => {
if (orgsWithSubOrgs.isPending) {
@@ -492,7 +503,8 @@ export const SelectOrganizationSection = () => {
if (
userLoading ||
!user ||
- ((isInitialOrgCheckLoading || defaultSelectedOrg) && !shouldShowMfa)
+ ((isInitialOrgCheckLoading || willAutoSelectDefaultOrg || isLoadingSubOrgCheck) &&
+ !shouldShowMfa)
) {
return (
<div className="h-screen w-screen bg-bunker-800">