mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-23 13:28:45 +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
@@ -20,10 +20,12 @@ vi.mock("../service-url", () => ({
|
||||
vi.mock("../zitadel", () => ({
|
||||
getLoginSettings: vi.fn(),
|
||||
getUserByID: vi.fn(),
|
||||
listUsers: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./cookie", () => ({
|
||||
setSessionAndUpdateCookie: vi.fn(),
|
||||
createSessionAndUpdateCookie: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../cookies", () => ({
|
||||
@@ -50,7 +52,9 @@ describe("sendPasskey", () => {
|
||||
let mockGetServiceUrlFromHeaders: any;
|
||||
let mockGetLoginSettings: any;
|
||||
let mockGetUserByID: any;
|
||||
let mockListUsers: any;
|
||||
let mockSetSessionAndUpdateCookie: any;
|
||||
let mockCreateSessionAndUpdateCookie: any;
|
||||
let mockGetSessionCookieById: any;
|
||||
let mockGetSessionCookieByLoginName: any;
|
||||
let mockGetMostRecentSessionCookie: any;
|
||||
@@ -63,8 +67,8 @@ describe("sendPasskey", () => {
|
||||
// Import mocked modules
|
||||
const { headers } = await import("next/headers");
|
||||
const { getServiceConfig } = await import("../service-url");
|
||||
const { getLoginSettings, getUserByID } = await import("../zitadel");
|
||||
const { setSessionAndUpdateCookie } = await import("./cookie");
|
||||
const { getLoginSettings, getUserByID, listUsers } = await import("../zitadel");
|
||||
const { setSessionAndUpdateCookie, createSessionAndUpdateCookie } = await import("./cookie");
|
||||
const { getSessionCookieById, getSessionCookieByLoginName, getMostRecentSessionCookie } = await import("../cookies");
|
||||
const { checkEmailVerification } = await import("../verify-helper");
|
||||
const { completeFlowOrGetUrl } = await import("../client");
|
||||
@@ -74,7 +78,13 @@ describe("sendPasskey", () => {
|
||||
mockGetServiceUrlFromHeaders = vi.mocked(getServiceConfig);
|
||||
mockGetLoginSettings = vi.mocked(getLoginSettings);
|
||||
mockGetUserByID = vi.mocked(getUserByID);
|
||||
mockListUsers = vi.mocked(listUsers);
|
||||
mockSetSessionAndUpdateCookie = vi.mocked(setSessionAndUpdateCookie);
|
||||
mockCreateSessionAndUpdateCookie = vi.mocked(createSessionAndUpdateCookie);
|
||||
mockCreateSessionAndUpdateCookie.mockResolvedValue({
|
||||
session: { id: "new-session", factors: { user: { id: "user-1", loginName: "user" } } } as any,
|
||||
sessionCookie: { id: "new-session", token: "token", loginName: "user" },
|
||||
});
|
||||
mockGetSessionCookieById = vi.mocked(getSessionCookieById);
|
||||
mockGetSessionCookieByLoginName = vi.mocked(getSessionCookieByLoginName);
|
||||
mockGetMostRecentSessionCookie = vi.mocked(getMostRecentSessionCookie);
|
||||
@@ -82,7 +92,9 @@ describe("sendPasskey", () => {
|
||||
mockCompleteFlowOrGetUrl = vi.mocked(completeFlowOrGetUrl);
|
||||
|
||||
// Default mock implementations
|
||||
mockHeaders.mockResolvedValue(new Headers());
|
||||
const headersList = new Headers();
|
||||
headersList.set("host", "test.com");
|
||||
mockHeaders.mockResolvedValue(headersList);
|
||||
mockGetServiceUrlFromHeaders.mockReturnValue({
|
||||
serviceUrl: "https://example.com",
|
||||
});
|
||||
@@ -104,7 +116,7 @@ describe("sendPasskey", () => {
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
error: "verify.errors.couldNotFindSession",
|
||||
error: "couldNotFindSession",
|
||||
});
|
||||
expect(mockGetSessionCookieById).toHaveBeenCalledWith({
|
||||
sessionId: "test-session-id",
|
||||
@@ -112,7 +124,8 @@ describe("sendPasskey", () => {
|
||||
});
|
||||
|
||||
test("should return error when session cookie is not found by loginName", async () => {
|
||||
mockGetSessionCookieByLoginName.mockResolvedValue(null);
|
||||
mockGetSessionCookieByLoginName.mockResolvedValue(null); // Not found
|
||||
mockCreateSessionAndUpdateCookie.mockRejectedValue(new Error("Creation failed")); // Force creation failure
|
||||
|
||||
const result = await sendPasskey({
|
||||
loginName: "[email protected]",
|
||||
@@ -121,7 +134,7 @@ describe("sendPasskey", () => {
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
error: "verify.errors.couldNotFindSession",
|
||||
error: "couldNotFindSession",
|
||||
});
|
||||
expect(mockGetSessionCookieByLoginName).toHaveBeenCalledWith({
|
||||
loginName: "[email protected]",
|
||||
@@ -137,7 +150,7 @@ describe("sendPasskey", () => {
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
error: "verify.errors.couldNotFindSession",
|
||||
error: "couldNotFindSession",
|
||||
});
|
||||
expect(mockGetMostRecentSessionCookie).toHaveBeenCalled();
|
||||
});
|
||||
@@ -163,7 +176,7 @@ describe("sendPasskey", () => {
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
error: "verify.errors.couldNotUpdateSession",
|
||||
error: "couldNotUpdateSession",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -176,9 +189,59 @@ describe("sendPasskey", () => {
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
error: "verify.errors.couldNotUpdateSession",
|
||||
error: "couldNotUpdateSession",
|
||||
});
|
||||
});
|
||||
|
||||
test("should fallback to createSessionAndUpdateCookie when setSessionAndUpdateCookie fails and checks are present", async () => {
|
||||
mockSetSessionAndUpdateCookie.mockRejectedValue(new Error("session already terminated"));
|
||||
|
||||
mockCreateSessionAndUpdateCookie.mockResolvedValue({
|
||||
session: {
|
||||
id: "new-session-123",
|
||||
factors: {
|
||||
user: {
|
||||
id: "user-123",
|
||||
loginName: "[email protected]",
|
||||
},
|
||||
},
|
||||
},
|
||||
sessionCookie: {
|
||||
id: "new-session-123",
|
||||
token: "new-token",
|
||||
},
|
||||
});
|
||||
|
||||
mockListUsers.mockResolvedValue({
|
||||
details: { totalResult: BigInt(1) },
|
||||
result: [{ userId: "user-123" }],
|
||||
});
|
||||
|
||||
mockGetUserByID.mockResolvedValue({
|
||||
user: {
|
||||
id: "user-123",
|
||||
type: {
|
||||
case: "human",
|
||||
value: { email: { isVerified: true } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
mockCheckEmailVerification.mockResolvedValue(true);
|
||||
mockCompleteFlowOrGetUrl.mockResolvedValue({ redirect: "/dashboard" });
|
||||
|
||||
const result = await sendPasskey({
|
||||
sessionId: "session-123",
|
||||
checks: { webAuthN: { credentialAssertionData: {} } } as any,
|
||||
});
|
||||
|
||||
// It should succeed with the new session
|
||||
expect(result).toEqual({
|
||||
redirect: "/dashboard",
|
||||
});
|
||||
|
||||
expect(mockCreateSessionAndUpdateCookie).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("User Validation", () => {
|
||||
|
||||
Reference in New Issue
Block a user