Merge commit from fork

* validate IDP linking permissions

* fix: check policy before linking

* apply suggested review comments

---------

Co-authored-by: Max Peintner <[email protected]>
This commit is contained in:
Livio Spring
2025-11-12 13:48:40 +01:00
committed by GitHub
co-authored by Max Peintner
parent 75791361f3
commit 33c51deb20
3 changed files with 385 additions and 17 deletions
+272 -3
View File
@@ -24,6 +24,8 @@ vi.mock("../zitadel", () => ({
addHuman: vi.fn(),
getLoginSettings: vi.fn(),
getOrgsByDomain: vi.fn(),
getActiveIdentityProviders: vi.fn(),
getUserByID: vi.fn(),
getDefaultOrg: vi.fn(),
}));
@@ -47,6 +49,8 @@ describe("processIDPCallback", () => {
let mockAddHuman: any;
let mockGetLoginSettings: any;
let mockGetOrgsByDomain: any;
let mockGetActiveIdentityProviders: any;
let mockGetUserByID: any;
let mockGetDefaultOrg: any;
let mockCreateNewSessionFromIdpIntent: any;
@@ -117,6 +121,8 @@ describe("processIDPCallback", () => {
addHuman,
getLoginSettings,
getOrgsByDomain,
getActiveIdentityProviders,
getUserByID,
getDefaultOrg,
} = await import("../zitadel");
const { createNewSessionFromIdpIntent } = await import("./idp");
@@ -132,6 +138,8 @@ describe("processIDPCallback", () => {
mockAddHuman = vi.mocked(addHuman);
mockGetLoginSettings = vi.mocked(getLoginSettings);
mockGetOrgsByDomain = vi.mocked(getOrgsByDomain);
mockGetActiveIdentityProviders = vi.mocked(getActiveIdentityProviders);
mockGetUserByID = vi.mocked(getUserByID);
mockGetDefaultOrg = vi.mocked(getDefaultOrg);
mockCreateNewSessionFromIdpIntent = vi.mocked(createNewSessionFromIdpIntent);
@@ -145,6 +153,20 @@ describe("processIDPCallback", () => {
mockCreateNewSessionFromIdpIntent.mockResolvedValue({
redirect: "https://app.example.com/success",
});
// Default mocks for validation functions
mockGetUserByID.mockResolvedValue({
userId: "user123",
details: {
resourceOwner: "org123",
},
});
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockResolvedValue({
identityProviders: [{ id: "idp123", name: "Test IDP" }],
});
});
afterEach(() => {
@@ -400,13 +422,19 @@ describe("processIDPCallback", () => {
options: {
...defaultIdp.config.options,
autoLinking: AutoLinkingOption.EMAIL,
isLinkingAllowed: true,
},
},
});
});
test("should auto-link user by email and create session", async () => {
const foundUser = { userId: "found123" };
const foundUser = {
userId: "found123",
details: {
resourceOwner: "org123",
},
};
mockListUsers.mockResolvedValue({
result: [foundUser],
});
@@ -474,6 +502,7 @@ describe("processIDPCallback", () => {
options: {
...defaultIdp.config.options,
autoLinking: AutoLinkingOption.USERNAME,
isLinkingAllowed: true,
},
},
});
@@ -481,7 +510,14 @@ describe("processIDPCallback", () => {
test("should auto-link user by username", async () => {
mockListUsers.mockResolvedValue({
result: [{ userId: "found123" }],
result: [
{
userId: "found123",
details: {
resourceOwner: "org123",
},
},
],
});
const result = await processIDPCallback(defaultParams);
@@ -772,12 +808,20 @@ describe("processIDPCallback", () => {
options: {
...defaultIdp.config.options,
autoLinking: AutoLinkingOption.EMAIL,
isLinkingAllowed: true,
isAutoCreation: true,
},
},
});
mockListUsers.mockResolvedValue({
result: [{ userId: "found123" }],
result: [
{
userId: "found123",
details: {
resourceOwner: "org123",
},
},
],
});
await processIDPCallback(defaultParams);
@@ -828,3 +872,228 @@ describe("processIDPCallback", () => {
});
});
});
describe("validateIDPLinkingPermissions", () => {
let mockGetLoginSettings: any;
let mockGetActiveIdentityProviders: any;
let validateIDPLinkingPermissions: any;
beforeEach(async () => {
vi.clearAllMocks();
// Import mocked modules
const { getLoginSettings, getActiveIdentityProviders } = await import("../zitadel");
const { validateIDPLinkingPermissions: validate } = await import("./idp-intent");
mockGetLoginSettings = vi.mocked(getLoginSettings);
mockGetActiveIdentityProviders = vi.mocked(getActiveIdentityProviders);
validateIDPLinkingPermissions = validate;
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("Organization login settings validation", () => {
test("should return false when allowExternalIdp is false", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: false,
});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(false);
expect(mockGetLoginSettings).toHaveBeenCalledWith({
serviceUrl: "https://api.example.com",
organization: "org123",
});
expect(mockGetActiveIdentityProviders).not.toHaveBeenCalled();
});
test("should return false when login settings are undefined", async () => {
mockGetLoginSettings.mockResolvedValue(undefined);
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(false);
});
test("should return false when allowExternalIdp is missing", async () => {
mockGetLoginSettings.mockResolvedValue({});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(false);
});
});
describe("Active IDP validation", () => {
test("should return false when IDP is not in active list", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockResolvedValue({
identityProviders: [
{ id: "idp456", name: "Other IDP" },
{ id: "idp789", name: "Another IDP" },
],
});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(false);
expect(mockGetActiveIdentityProviders).toHaveBeenCalledWith({
serviceUrl: "https://api.example.com",
orgId: "org123",
linking_allowed: true,
});
});
test("should return false when identityProviders list is empty", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockResolvedValue({
identityProviders: [],
});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(false);
});
test("should return false when identityProviders is undefined", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockResolvedValue({});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(false);
});
});
describe("Successful validation", () => {
test("should return true when all validations pass", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockResolvedValue({
identityProviders: [
{ id: "idp123", name: "Target IDP" },
{ id: "idp456", name: "Other IDP" },
],
});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(true);
expect(mockGetLoginSettings).toHaveBeenCalledWith({
serviceUrl: "https://api.example.com",
organization: "org123",
});
expect(mockGetActiveIdentityProviders).toHaveBeenCalledWith({
serviceUrl: "https://api.example.com",
orgId: "org123",
linking_allowed: true,
});
});
test("should find IDP in middle of list", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockResolvedValue({
identityProviders: [
{ id: "idp111", name: "IDP 1" },
{ id: "idp222", name: "IDP 2" },
{ id: "idp123", name: "Target IDP" },
{ id: "idp333", name: "IDP 3" },
],
});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(true);
});
});
describe("Edge cases", () => {
test("should handle getLoginSettings throwing an error", async () => {
mockGetLoginSettings.mockRejectedValue(new Error("Network error"));
await expect(
validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
}),
).rejects.toThrow("Network error");
});
test("should handle getActiveIdentityProviders throwing an error", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockRejectedValue(new Error("Network error"));
await expect(
validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
}),
).rejects.toThrow("Network error");
});
test("should perform case-sensitive IDP ID comparison", async () => {
mockGetLoginSettings.mockResolvedValue({
allowExternalIdp: true,
});
mockGetActiveIdentityProviders.mockResolvedValue({
identityProviders: [{ id: "IDP123", name: "Target IDP" }],
});
const result = await validateIDPLinkingPermissions({
serviceUrl: "https://api.example.com",
userOrganizationId: "org123",
idpId: "idp123",
});
expect(result).toBe(false);
});
});
});