mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-30 16:08:56 +00:00
fix(login): enable explicit IDP linking using existing session id (#11225)
Closes #11191 # Which Problems Are Solved This PR fixes the IDP linking for accounts with no authentication method. We strictly validate session ownership and generate a hash of the sessionId + the user's fingerprintId cookie. This hash is passed to the IDP as linkFingerprint. This PR addtionally addresses TypeScript errors regarding unknown types in catch blocks within the IDP intent processing logic. It ensures that errors occurring during IDP linking are safely handled, and that meaningful error messages are propagated to the client-side redirect URLs. # How the Problems Are Solved - Safely handle unknown errors: Updated catch blocks in `processIDPCallback` to check if the caught object is an instance of Error before accessing error.message. - Fix "IDP Taken" check: Implemented a safe check for the gRPC error code 6 (Already Exists) by casting the error, resolving the Property 'code' does not exist on type 'unknown' build error. - Error Propagation: The specific error message is now passed to the linking-failed page params, allowing for better user feedback instead of generic errors.
This commit is contained in:
@@ -12,6 +12,10 @@ vi.mock("next/navigation", () => ({
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/cookies", () => ({
|
||||
getSessionCookieById: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../service-url", () => ({
|
||||
getServiceConfig: vi.fn(),
|
||||
}));
|
||||
@@ -25,6 +29,11 @@ vi.mock("../zitadel", () => ({
|
||||
startIdentityProviderFlow: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../fingerprint", () => ({
|
||||
getFingerprintIdCookie: vi.fn(),
|
||||
getOrSetFingerprintId: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("redirectToIdp", () => {
|
||||
let mockHeaders: any;
|
||||
let mockGetServiceUrlFromHeaders: any;
|
||||
@@ -40,6 +49,8 @@ describe("redirectToIdp", () => {
|
||||
const { getServiceConfig } = await import("../service-url");
|
||||
const { getInstanceHost, getPublicHost } = await import("./host");
|
||||
const { startIdentityProviderFlow } = await import("../zitadel");
|
||||
const { getFingerprintIdCookie, getOrSetFingerprintId } = await import("../fingerprint");
|
||||
const { getSessionCookieById } = await import("@/lib/cookies");
|
||||
|
||||
// Setup mocks
|
||||
mockHeaders = vi.mocked(headers);
|
||||
@@ -47,8 +58,14 @@ describe("redirectToIdp", () => {
|
||||
mockGetInstanceHost = vi.mocked(getInstanceHost);
|
||||
mockGetPublicHost = vi.mocked(getPublicHost);
|
||||
mockStartIdentityProviderFlow = vi.mocked(startIdentityProviderFlow);
|
||||
const mockGetFingerprintIdCookie = vi.mocked(getFingerprintIdCookie);
|
||||
const mockGetOrSetFingerprintId = vi.mocked(getOrSetFingerprintId);
|
||||
const mockGetSessionCookieById = vi.mocked(getSessionCookieById);
|
||||
|
||||
// Default mock implementations
|
||||
mockGetFingerprintIdCookie.mockResolvedValue({ name: "fingerprintId", value: "fp123" });
|
||||
mockGetOrSetFingerprintId.mockResolvedValue("fp123");
|
||||
mockGetSessionCookieById.mockResolvedValue({} as any);
|
||||
mockHeaders.mockResolvedValue({} as any);
|
||||
mockGetServiceUrlFromHeaders.mockReturnValue({
|
||||
serviceConfig: { baseUrl: "https://api.example.com" },
|
||||
@@ -203,7 +220,7 @@ describe("redirectToIdp", () => {
|
||||
const formData = new FormData();
|
||||
formData.append("id", "idp123");
|
||||
formData.append("provider", "google");
|
||||
formData.append("linkOnly", "true");
|
||||
formData.append("sessionId", "session123");
|
||||
formData.append("postErrorRedirectUrl", "/custom-error");
|
||||
|
||||
mockStartIdentityProviderFlow.mockResolvedValue("https://idp.example.com/auth");
|
||||
@@ -220,9 +237,14 @@ describe("redirectToIdp", () => {
|
||||
const failureUrl = callArgs.urls.failureUrl;
|
||||
|
||||
// Both parameters should be present
|
||||
expect(successUrl).toContain("link=true");
|
||||
// Both parameters should be present
|
||||
expect(successUrl).toContain("linkToSessionId=session123");
|
||||
// We can't easily predict the exact fingerprint hash without mocking crypto in this test,
|
||||
// but we can check if the parameter key exists
|
||||
expect(successUrl).toContain("linkFingerprint=");
|
||||
expect(successUrl).toContain("postErrorRedirectUrl=%2Fcustom-error");
|
||||
expect(failureUrl).toContain("link=true");
|
||||
expect(failureUrl).toContain("linkToSessionId=session123");
|
||||
expect(failureUrl).toContain("linkFingerprint=");
|
||||
expect(failureUrl).toContain("postErrorRedirectUrl=%2Fcustom-error");
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user