mirror of
https://github.com/outline/outline.git
synced 2026-11-27 16:08:58 +03:00
feat: Gate group sync behind the GroupSync entitlement
Add a syncGroups team policy requiring an admin and the GroupSync plan feature, enforced when enabling or setting up group sync and before each sync task runs. Group sync can still be disabled after the entitlement is lost. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b3e0780752
commit
cdc8d17b9b
6 files changed
+277
-118
No files matched your search
@@ -17,6 +17,7 @@ import Text from "~/components/Text";
|
||||
import env from "~/env";
|
||||
import useConsumeQueryParam from "~/hooks/useConsumeQueryParam";
|
||||
import useCurrentTeam from "~/hooks/useCurrentTeam";
|
||||
import usePolicy from "~/hooks/usePolicy";
|
||||
import useRequest from "~/hooks/useRequest";
|
||||
import useStores from "~/hooks/useStores";
|
||||
import SettingRow from "./components/SettingRow";
|
||||
@@ -33,6 +34,7 @@ import { VStack } from "~/components/primitives/VStack";
|
||||
function Authentication() {
|
||||
const { authenticationProviders, dialogs } = useStores();
|
||||
const team = useCurrentTeam();
|
||||
const can = usePolicy(team);
|
||||
const { t } = useTranslation();
|
||||
const theme = useTheme();
|
||||
|
||||
@@ -193,129 +195,139 @@ function Authentication() {
|
||||
|
||||
<Heading as="h2">{t("Sign In")}</Heading>
|
||||
|
||||
{authenticationProviders.orderedData.map((provider) => (
|
||||
<React.Fragment key={provider.name}>
|
||||
<SettingRow
|
||||
label={
|
||||
<Flex gap={8} align="center">
|
||||
<PluginIcon id={provider.name} /> {provider.displayName}
|
||||
</Flex>
|
||||
}
|
||||
name={provider.name}
|
||||
description={
|
||||
provider.isConnected
|
||||
? t("Allow members to sign-in with {{ authProvider }}", {
|
||||
authProvider: provider.displayName,
|
||||
})
|
||||
: t("Connect {{ authProvider }} to allow members to sign-in", {
|
||||
authProvider: provider.displayName,
|
||||
})
|
||||
}
|
||||
border={!(provider.isActive && provider.groupSyncSupported)}
|
||||
>
|
||||
<Flex align="center" gap={12}>
|
||||
{provider.isConnected ? (
|
||||
<VStack align="start">
|
||||
{authenticationProviders.orderedData.map((provider) => {
|
||||
// Group sync can still be disabled after losing the entitlement.
|
||||
const showGroupSync =
|
||||
provider.isActive &&
|
||||
provider.groupSyncSupported &&
|
||||
(!!can.syncGroups || !!provider.settings?.groupSyncEnabled);
|
||||
|
||||
return (
|
||||
<React.Fragment key={provider.name}>
|
||||
<SettingRow
|
||||
label={
|
||||
<Flex gap={8} align="center">
|
||||
<PluginIcon id={provider.name} /> {provider.displayName}
|
||||
</Flex>
|
||||
}
|
||||
name={provider.name}
|
||||
description={
|
||||
provider.isConnected
|
||||
? t("Allow members to sign-in with {{ authProvider }}", {
|
||||
authProvider: provider.displayName,
|
||||
})
|
||||
: t(
|
||||
"Connect {{ authProvider }} to allow members to sign-in",
|
||||
{
|
||||
authProvider: provider.displayName,
|
||||
}
|
||||
)
|
||||
}
|
||||
border={!showGroupSync}
|
||||
>
|
||||
<Flex align="center" gap={12}>
|
||||
{provider.isConnected ? (
|
||||
<VStack align="start">
|
||||
<Button
|
||||
icon={
|
||||
provider.isEnabled ? (
|
||||
<ConnectedIcon />
|
||||
) : (
|
||||
<ConnectedIcon color={theme.textSecondary} />
|
||||
)
|
||||
}
|
||||
onClick={() =>
|
||||
!provider.isEnabled
|
||||
? handleToggleProvider(provider, true)
|
||||
: handleRemoveProvider(provider)
|
||||
}
|
||||
neutral
|
||||
>
|
||||
{provider.isEnabled ? t("Connected") : t("Disabled")}
|
||||
</Button>
|
||||
<Text type="tertiary" size="small">
|
||||
{provider.providerId}
|
||||
</Text>
|
||||
</VStack>
|
||||
) : (
|
||||
<Button
|
||||
icon={
|
||||
provider.isEnabled ? (
|
||||
<ConnectedIcon />
|
||||
) : (
|
||||
<ConnectedIcon color={theme.textSecondary} />
|
||||
)
|
||||
}
|
||||
onClick={() =>
|
||||
!provider.isEnabled
|
||||
? handleToggleProvider(provider, true)
|
||||
: handleRemoveProvider(provider)
|
||||
}
|
||||
onClick={() => handleConnectProvider(provider.name)}
|
||||
neutral
|
||||
>
|
||||
{provider.isEnabled ? t("Connected") : t("Disabled")}
|
||||
{t("Connect")}
|
||||
</Button>
|
||||
<Text type="tertiary" size="small">
|
||||
{provider.providerId}
|
||||
</Text>
|
||||
</VStack>
|
||||
) : (
|
||||
<Button
|
||||
onClick={() => handleConnectProvider(provider.name)}
|
||||
neutral
|
||||
>
|
||||
{t("Connect")}
|
||||
</Button>
|
||||
)}
|
||||
</Flex>
|
||||
</SettingRow>
|
||||
{provider.isActive && provider.groupSyncSupported && (
|
||||
<SettingRow
|
||||
label={t("Group sync")}
|
||||
name={`groupSync-${provider.name}`}
|
||||
description={
|
||||
provider.groupSyncRequiresSetup &&
|
||||
!provider.settings?.groupSyncEnabled
|
||||
? t(
|
||||
"An administrator of {{ authProvider }} must approve group access for this workspace",
|
||||
{ authProvider: provider.displayName }
|
||||
)
|
||||
: t(
|
||||
"Sync group memberships from {{ authProvider }} on each sign-in",
|
||||
{ authProvider: provider.displayName }
|
||||
)
|
||||
}
|
||||
border={
|
||||
!(
|
||||
provider.settings?.groupSyncEnabled &&
|
||||
provider.groupSyncUsesClaim
|
||||
)
|
||||
}
|
||||
>
|
||||
{provider.groupSyncRequiresSetup &&
|
||||
!provider.settings?.groupSyncEnabled ? (
|
||||
<Button
|
||||
onClick={() => handleToggleGroupSync(provider, true)}
|
||||
neutral
|
||||
>
|
||||
{t("Set up group sync")}
|
||||
</Button>
|
||||
) : (
|
||||
<Switch
|
||||
id={`groupSync-${provider.name}`}
|
||||
checked={provider.settings?.groupSyncEnabled ?? false}
|
||||
onChange={(checked) =>
|
||||
handleToggleGroupSync(provider, checked)
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</SettingRow>
|
||||
)}
|
||||
{provider.isActive &&
|
||||
provider.groupSyncSupported &&
|
||||
provider.groupSyncUsesClaim &&
|
||||
provider.settings?.groupSyncEnabled && (
|
||||
<SettingRow
|
||||
label={t("Group claim")}
|
||||
name={`groupClaim-${provider.name}`}
|
||||
description={t(
|
||||
"The claim in the provider response that contains group names (e.g. groups, roles)"
|
||||
)}
|
||||
border={false}
|
||||
</Flex>
|
||||
</SettingRow>
|
||||
{showGroupSync && (
|
||||
<SettingRow
|
||||
label={t("Group sync")}
|
||||
name={`groupSync-${provider.name}`}
|
||||
description={
|
||||
provider.groupSyncRequiresSetup &&
|
||||
!provider.settings?.groupSyncEnabled
|
||||
? t(
|
||||
"An administrator of {{ authProvider }} must approve group access for this workspace",
|
||||
{ authProvider: provider.displayName }
|
||||
)
|
||||
: t(
|
||||
"Sync group memberships from {{ authProvider }} on each sign-in",
|
||||
{ authProvider: provider.displayName }
|
||||
)
|
||||
}
|
||||
border={
|
||||
!(
|
||||
provider.settings?.groupSyncEnabled &&
|
||||
provider.groupSyncUsesClaim
|
||||
)
|
||||
}
|
||||
>
|
||||
<Input
|
||||
id={`groupClaim-${provider.name}`}
|
||||
defaultValue={provider.settings?.groupClaim ?? "groups"}
|
||||
placeholder="groups"
|
||||
onBlur={(ev: React.FocusEvent<HTMLInputElement>) => {
|
||||
const value = ev.target.value.trim();
|
||||
if (value !== (provider.settings?.groupClaim ?? "")) {
|
||||
void handleGroupClaimChange(provider, value);
|
||||
{provider.groupSyncRequiresSetup &&
|
||||
!provider.settings?.groupSyncEnabled ? (
|
||||
<Button
|
||||
onClick={() => handleToggleGroupSync(provider, true)}
|
||||
neutral
|
||||
>
|
||||
{t("Set up group sync")}
|
||||
</Button>
|
||||
) : (
|
||||
<Switch
|
||||
id={`groupSync-${provider.name}`}
|
||||
checked={provider.settings?.groupSyncEnabled ?? false}
|
||||
onChange={(checked) =>
|
||||
handleToggleGroupSync(provider, checked)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
/>
|
||||
)}
|
||||
</SettingRow>
|
||||
)}
|
||||
</React.Fragment>
|
||||
))}
|
||||
{showGroupSync &&
|
||||
provider.groupSyncUsesClaim &&
|
||||
provider.settings?.groupSyncEnabled && (
|
||||
<SettingRow
|
||||
label={t("Group claim")}
|
||||
name={`groupClaim-${provider.name}`}
|
||||
description={t(
|
||||
"The claim in the provider response that contains group names (e.g. groups, roles)"
|
||||
)}
|
||||
border={false}
|
||||
>
|
||||
<Input
|
||||
id={`groupClaim-${provider.name}`}
|
||||
defaultValue={provider.settings?.groupClaim ?? "groups"}
|
||||
placeholder="groups"
|
||||
onBlur={(ev: React.FocusEvent<HTMLInputElement>) => {
|
||||
const value = ev.target.value.trim();
|
||||
if (value !== (provider.settings?.groupClaim ?? "")) {
|
||||
void handleGroupClaimChange(provider, value);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</SettingRow>
|
||||
)}
|
||||
</React.Fragment>
|
||||
);
|
||||
})}
|
||||
<SettingRow
|
||||
label={
|
||||
<Flex gap={8} align="center">
|
||||
|
||||
@@ -59,6 +59,14 @@ allow(User, "audit", Team, (actor, team) =>
|
||||
)
|
||||
);
|
||||
|
||||
allow(User, "syncGroups", Team, (actor, team) =>
|
||||
and(
|
||||
//
|
||||
isTeamAdmin(actor, team),
|
||||
teamHasEntitlement(team, PlanFeature.GroupSync)
|
||||
)
|
||||
);
|
||||
|
||||
allow(User, ["createTemplate", "updateTemplate"], Team, (actor, team) =>
|
||||
and(
|
||||
//
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { vi } from "vitest";
|
||||
import { Plan } from "@shared/types";
|
||||
import {
|
||||
AuthenticationProvider,
|
||||
Group,
|
||||
GroupUser,
|
||||
Team,
|
||||
UserAuthentication,
|
||||
} from "@server/models";
|
||||
import { buildUser } from "@server/test/factories";
|
||||
@@ -38,6 +40,10 @@ async function setup() {
|
||||
describe("SyncUserGroupsTask", () => {
|
||||
const schedule = mockTaskSchedule();
|
||||
|
||||
beforeEach(() => {
|
||||
vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Business);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
@@ -104,6 +110,17 @@ describe("SyncUserGroupsTask", () => {
|
||||
expect(fetchUserGroups).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not sync groups when the team is not entitled to group sync", async () => {
|
||||
const { authentication, fetchUserGroups } = await setup();
|
||||
vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Community);
|
||||
|
||||
await new SyncUserGroupsTask().perform({
|
||||
userAuthenticationId: authentication.id,
|
||||
});
|
||||
|
||||
expect(fetchUserGroups).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw when the provider fails so the task is retried", async () => {
|
||||
const { authentication, fetchUserGroups } = await setup();
|
||||
fetchUserGroups.mockRejectedValue(new Error("provider unavailable"));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { JobOptions } from "bull";
|
||||
import { PlanFeature } from "@shared/types";
|
||||
import groupsSyncer from "@server/commands/groupsSyncer";
|
||||
import { createContext } from "@server/context";
|
||||
import Logger from "@server/logging/Logger";
|
||||
@@ -8,6 +9,7 @@ import {
|
||||
User,
|
||||
UserAuthentication,
|
||||
} from "@server/models";
|
||||
import { teamHasEntitlement } from "@server/policies/utils";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { PluginManager } from "@server/utils/PluginManager";
|
||||
import { BaseTask, TaskPriority } from "./base/BaseTask";
|
||||
@@ -21,8 +23,8 @@ type Props = {
|
||||
* Fetches a user's group memberships from the authentication provider linked
|
||||
* to the given user authentication and synchronizes them with internal
|
||||
* groups. This is a no-op when group sync is not enabled for the provider,
|
||||
* no access token is stored, or the provider plugin does not support group
|
||||
* sync.
|
||||
* the team is not entitled to group sync, no access token is stored, or the
|
||||
* provider plugin does not support group sync.
|
||||
*/
|
||||
export default class SyncUserGroupsTask extends BaseTask<Props> {
|
||||
/**
|
||||
@@ -80,7 +82,7 @@ export default class SyncUserGroupsTask extends BaseTask<Props> {
|
||||
}
|
||||
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
if (!team) {
|
||||
if (!team || !teamHasEntitlement(team, PlanFeature.GroupSync)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { MockInstance } from "vitest";
|
||||
import { vi } from "vitest";
|
||||
import sharedEnv from "@shared/env";
|
||||
import { Plan } from "@shared/types";
|
||||
import env from "@server/env";
|
||||
import { AuthenticationProvider } from "@server/models";
|
||||
import { AuthenticationProvider, Team } from "@server/models";
|
||||
import { buildUser, buildAdmin, buildTeam } from "@server/test/factories";
|
||||
import { getTestServer, setSelfHosted } from "@server/test/support";
|
||||
import { PluginManager } from "@server/utils/PluginManager";
|
||||
@@ -61,6 +63,18 @@ describe("#authenticationProviders.info", () => {
|
||||
});
|
||||
|
||||
describe("#authenticationProviders.update", () => {
|
||||
let planSpy: MockInstance<() => Plan>;
|
||||
|
||||
beforeEach(() => {
|
||||
planSpy = vi
|
||||
.spyOn(Team.prototype, "plan", "get")
|
||||
.mockReturnValue(Plan.Business);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
planSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("requires the admin setup flow before enabling group sync", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
@@ -97,6 +111,65 @@ describe("#authenticationProviders.update", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("requires the group sync entitlement to enable group sync", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
const provider = await team.$create("authenticationProvider", {
|
||||
name: "oidc",
|
||||
providerId: randomUUID(),
|
||||
});
|
||||
|
||||
planSpy.mockReturnValue(Plan.Community);
|
||||
const getGroupSyncProvider = vi
|
||||
.spyOn(PluginManager, "getGroupSyncProvider")
|
||||
.mockReturnValue({
|
||||
useGroupClaim: true,
|
||||
fetchUserGroups: async () => [],
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await server.post(
|
||||
"/api/authenticationProviders.update",
|
||||
user,
|
||||
{
|
||||
body: {
|
||||
id: provider.id,
|
||||
settings: { groupSyncEnabled: true },
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
expect(res.status).toEqual(403);
|
||||
const reloaded = await AuthenticationProvider.findByPk(provider.id);
|
||||
expect(reloaded?.settings?.groupSyncEnabled).not.toBe(true);
|
||||
} finally {
|
||||
getGroupSyncProvider.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("allows disabling group sync without the group sync entitlement", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
const provider = await team.$create("authenticationProvider", {
|
||||
name: "oidc",
|
||||
providerId: randomUUID(),
|
||||
settings: { groupSyncEnabled: true },
|
||||
});
|
||||
|
||||
planSpy.mockReturnValue(Plan.Community);
|
||||
|
||||
const res = await server.post("/api/authenticationProviders.update", user, {
|
||||
body: {
|
||||
id: provider.id,
|
||||
settings: { groupSyncEnabled: false },
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
const reloaded = await AuthenticationProvider.findByPk(provider.id);
|
||||
expect(reloaded?.settings?.groupSyncEnabled).toBe(false);
|
||||
});
|
||||
|
||||
it("should not allow admins to disable when last authentication provider", async () => {
|
||||
const team = await buildTeam({
|
||||
guestSignin: false,
|
||||
@@ -163,6 +236,18 @@ describe("#authenticationProviders.update", () => {
|
||||
});
|
||||
|
||||
describe("#authenticationProviders.startGroupSync", () => {
|
||||
let planSpy: MockInstance<() => Plan>;
|
||||
|
||||
beforeEach(() => {
|
||||
planSpy = vi
|
||||
.spyOn(Team.prototype, "plan", "get")
|
||||
.mockReturnValue(Plan.Business);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
planSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("returns the provider setup URL for a workspace admin", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
@@ -198,6 +283,38 @@ describe("#authenticationProviders.startGroupSync", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("requires the group sync entitlement", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
const provider = await team.$create("authenticationProvider", {
|
||||
name: "azure",
|
||||
providerId: randomUUID(),
|
||||
});
|
||||
const startGroupSync = vi
|
||||
.fn()
|
||||
.mockResolvedValue("https://example.com/consent");
|
||||
planSpy.mockReturnValue(Plan.Community);
|
||||
const getGroupSyncProvider = vi
|
||||
.spyOn(PluginManager, "getGroupSyncProvider")
|
||||
.mockReturnValue({
|
||||
useGroupClaim: false,
|
||||
startGroupSync,
|
||||
fetchUserGroups: async () => [],
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await server.post(
|
||||
"/api/authenticationProviders.startGroupSync",
|
||||
user,
|
||||
{ body: { id: provider.id } }
|
||||
);
|
||||
expect(res.status).toEqual(403);
|
||||
expect(startGroupSync).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
getGroupSyncProvider.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects providers without an admin setup flow", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
|
||||
@@ -57,6 +57,8 @@ router.post(
|
||||
settings?.groupSyncEnabled === true &&
|
||||
!authenticationProvider.settings?.groupSyncEnabled
|
||||
) {
|
||||
authorize(user, "syncGroups", user.team);
|
||||
|
||||
const groupSyncProvider = PluginManager.getGroupSyncProvider(
|
||||
authenticationProvider.name
|
||||
);
|
||||
@@ -106,6 +108,7 @@ router.post(
|
||||
ctx.input.body.id
|
||||
);
|
||||
authorize(user, "update", authenticationProvider);
|
||||
authorize(user, "syncGroups", user.team);
|
||||
|
||||
const groupSyncProvider = PluginManager.getGroupSyncProvider(
|
||||
authenticationProvider.name
|
||||
|
||||
Reference in new issue
Block a user