mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-22 12:58:46 +00:00
fix(login): correctly redirect to /password when ignoreUnknownUsername is set (#11130)
Closes #11006 # Which Problems Are Solved This PR addresses an issue where the ignoreUnknownUsernames setting was not being respected in certain scenarios during the login flow. Specifically: - When a user was found but rejected due to login settings (e.g., disableLoginWithEmail), the system would return a "User not found" error instead of redirecting to the password page as dictated by ignoreUnknownUsernames. - When a user was not found, and the flow fell through the registration checks (e.g., IDP redirect failed), it would also return "User not found" without checking ignoreUnknownUsernames. # How the Problems Are Solved - Introduced a helper function `handleUserNotFound` in `apps/login/src/lib/server/loginname.ts` to centralize the logic for checking ignoreUnknownUsernames and redirecting to the password page. - Updated the user validation logic to call handleUserNotFound when a user is found but rejected by policy. - Updated the registration fallback logic to call handleUserNotFound instead of directly returning an error. - Added new test cases in `apps/login/src/lib/server/loginname.test.ts` to verify the fix and ensure no regressions. # Additional Changes - Fixed inconsistency in translation files --------- Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
co-authored by
Livio Spring
parent
e72203ca6d
commit
26f8238499
@@ -3,7 +3,7 @@
|
||||
import { addSessionToCookie, updateSessionCookie } from "@/lib/cookies";
|
||||
import {
|
||||
createSessionForUserIdAndIdpIntent,
|
||||
createSessionFromChecks,
|
||||
createSessionFromChecksAndChallenges,
|
||||
getSecuritySettings,
|
||||
getSession,
|
||||
setSession,
|
||||
@@ -44,7 +44,8 @@ export async function createSessionAndUpdateCookie(command: {
|
||||
checks: Checks;
|
||||
requestId: string | undefined;
|
||||
lifetime?: Duration;
|
||||
}): Promise<Session> {
|
||||
challenges?: RequestChallenges;
|
||||
}): Promise<{ session: Session; sessionCookie: CustomCookieData; challenges?: Challenges }> {
|
||||
const _headers = await headers();
|
||||
const { serviceConfig } = getServiceConfig(_headers);
|
||||
|
||||
@@ -59,12 +60,17 @@ export async function createSessionAndUpdateCookie(command: {
|
||||
} as Duration; // for usecases where the lifetime is not specified (user discovery)
|
||||
}
|
||||
|
||||
const createdSession = await createSessionFromChecks({ serviceConfig, checks: command.checks,
|
||||
const createdSession = await createSessionFromChecksAndChallenges({
|
||||
serviceConfig,
|
||||
checks: command.checks,
|
||||
lifetime: sessionLifetime,
|
||||
challenges: command.challenges,
|
||||
});
|
||||
|
||||
if (createdSession) {
|
||||
return getSession({ serviceConfig, sessionId: createdSession.sessionId,
|
||||
return getSession({
|
||||
serviceConfig,
|
||||
sessionId: createdSession.sessionId,
|
||||
sessionToken: createdSession.sessionToken,
|
||||
}).then(async (response) => {
|
||||
if (response?.session && response.session?.factors?.user?.loginName) {
|
||||
@@ -90,7 +96,7 @@ export async function createSessionAndUpdateCookie(command: {
|
||||
|
||||
await addSessionToCookie({ session: sessionCookie, iFrameEnabled });
|
||||
|
||||
return response.session as Session;
|
||||
return { session: response.session as Session, sessionCookie, challenges: createdSession.challenges };
|
||||
} else {
|
||||
throw "could not get session or session does not have loginName";
|
||||
}
|
||||
@@ -128,7 +134,9 @@ export async function createSessionForIdpAndUpdateCookie({
|
||||
} as Duration;
|
||||
}
|
||||
|
||||
const createdSession = await createSessionForUserIdAndIdpIntent({ serviceConfig, userId,
|
||||
const createdSession = await createSessionForUserIdAndIdpIntent({
|
||||
serviceConfig,
|
||||
userId,
|
||||
idpIntent,
|
||||
lifetime: sessionLifetime,
|
||||
}).catch((error: ErrorDetail | CredentialsCheckError) => {
|
||||
@@ -146,7 +154,9 @@ export async function createSessionForIdpAndUpdateCookie({
|
||||
throw "Could not create session";
|
||||
}
|
||||
|
||||
const { session } = await getSession({ serviceConfig, sessionId: createdSession.sessionId,
|
||||
const { session } = await getSession({
|
||||
serviceConfig,
|
||||
sessionId: createdSession.sessionId,
|
||||
sessionToken: createdSession.sessionToken,
|
||||
});
|
||||
|
||||
@@ -194,7 +204,9 @@ export async function setSessionAndUpdateCookie(command: {
|
||||
const _headers = await headers();
|
||||
const { serviceConfig } = getServiceConfig(_headers);
|
||||
|
||||
return setSession({ serviceConfig, sessionId: command.recentCookie.id,
|
||||
return setSession({
|
||||
serviceConfig,
|
||||
sessionId: command.recentCookie.id,
|
||||
sessionToken: command.recentCookie.token,
|
||||
challenges: command.challenges,
|
||||
checks: command.checks,
|
||||
@@ -217,40 +229,40 @@ export async function setSessionAndUpdateCookie(command: {
|
||||
sessionCookie.requestId = command.requestId;
|
||||
}
|
||||
|
||||
return getSession({ serviceConfig, sessionId: sessionCookie.id,
|
||||
sessionToken: sessionCookie.token,
|
||||
}).then(async (response) => {
|
||||
if (!response?.session || !response.session.factors?.user?.loginName) {
|
||||
throw "could not get session or session does not have loginName";
|
||||
}
|
||||
return getSession({ serviceConfig, sessionId: sessionCookie.id, sessionToken: sessionCookie.token }).then(
|
||||
async (response) => {
|
||||
if (!response?.session || !response.session.factors?.user?.loginName) {
|
||||
throw "could not get session or session does not have loginName";
|
||||
}
|
||||
|
||||
const { session } = response;
|
||||
const newCookie: CustomCookieData = {
|
||||
id: sessionCookie.id,
|
||||
token: updatedSession.sessionToken,
|
||||
creationTs: sessionCookie.creationTs,
|
||||
expirationTs: sessionCookie.expirationTs,
|
||||
// just overwrite the changeDate with the new one
|
||||
changeTs: updatedSession.details?.changeDate ? `${timestampMs(updatedSession.details.changeDate)}` : "",
|
||||
loginName: session.factors?.user?.loginName ?? "",
|
||||
organization: session.factors?.user?.organizationId ?? "",
|
||||
};
|
||||
const { session } = response;
|
||||
const newCookie: CustomCookieData = {
|
||||
id: sessionCookie.id,
|
||||
token: updatedSession.sessionToken,
|
||||
creationTs: sessionCookie.creationTs,
|
||||
expirationTs: sessionCookie.expirationTs,
|
||||
// just overwrite the changeDate with the new one
|
||||
changeTs: updatedSession.details?.changeDate ? `${timestampMs(updatedSession.details.changeDate)}` : "",
|
||||
loginName: session.factors?.user?.loginName ?? "",
|
||||
organization: session.factors?.user?.organizationId ?? "",
|
||||
};
|
||||
|
||||
if (sessionCookie.requestId) {
|
||||
newCookie.requestId = sessionCookie.requestId;
|
||||
}
|
||||
if (sessionCookie.requestId) {
|
||||
newCookie.requestId = sessionCookie.requestId;
|
||||
}
|
||||
|
||||
const securitySettings = await getSecuritySettings({ serviceConfig });
|
||||
const iFrameEnabled = !!securitySettings?.embeddedIframe?.enabled;
|
||||
const securitySettings = await getSecuritySettings({ serviceConfig });
|
||||
const iFrameEnabled = !!securitySettings?.embeddedIframe?.enabled;
|
||||
|
||||
return updateSessionCookie({
|
||||
id: sessionCookie.id,
|
||||
session: newCookie,
|
||||
iFrameEnabled,
|
||||
}).then(() => {
|
||||
return { challenges: updatedSession.challenges, ...session };
|
||||
});
|
||||
});
|
||||
return updateSessionCookie({
|
||||
id: sessionCookie.id,
|
||||
session: newCookie,
|
||||
iFrameEnabled,
|
||||
}).then(() => {
|
||||
return { challenges: updatedSession.challenges, ...session };
|
||||
});
|
||||
},
|
||||
);
|
||||
} else {
|
||||
throw "Session not be set";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user