Files
outline/app/scenes/Settings/Authentication.tsx
T
Tom MoorandClaude Fable 5.1 abc47b442a feat: Sync group descriptions and re-sync groups on SSO validation (#13889)
* feat: Sync group descriptions and re-sync groups on SSO validation

- Sync group descriptions from group sync providers alongside names
- Lock name and description edits for externally synced groups at the
  API layer and in the edit dialog
- Move group sync into a SyncUserGroupsTask, scheduled from login and
  from ValidateSSOAccessTask after a fresh provider validation
- Add authenticationProviders.startGroupSync for providers that require
  an admin consent flow before group sync can be enabled

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: Make group sync setup generic and deduplicate sync jobs

- Expose groupSyncRequiresSetup from the provider list and use it in the
  settings scene instead of checking for the azure provider name
- Rename the setup redirect query param to groupSync
- Give SyncUserGroupsTask a job id per user authentication so concurrent
  syncs for the same authentication are not queued twice

* Use existing hook

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-25 19:49:26 -04:00

454 lines
14 KiB
TypeScript

import { observer } from "mobx-react";
import { EmailIcon, PadlockIcon } from "outline-icons";
import * as React from "react";
import { useTranslation, Trans } from "react-i18next";
import { toast } from "sonner";
import { errToString } from "@shared/utils/error";
import ConfirmationDialog from "~/components/ConfirmationDialog";
import Flex from "~/components/Flex";
import Heading from "~/components/Heading";
import Input from "~/components/Input";
import { InputSelect } from "~/components/InputSelect";
import type AuthenticationProvider from "~/models/AuthenticationProvider";
import PluginIcon from "~/components/PluginIcon";
import Scene from "~/components/Scene";
import Switch from "~/components/Switch";
import Text from "~/components/Text";
import env from "~/env";
import useConsumeQueryParam from "~/hooks/useConsumeQueryParam";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import useRequest from "~/hooks/useRequest";
import useStores from "~/hooks/useStores";
import SettingRow from "./components/SettingRow";
import { setPostLoginPath } from "~/hooks/useLastVisitedPath";
import { getRedirectUrl, toRelative } from "~/utils/urls";
import { settingsPath } from "~/utils/routeHelpers";
import DomainManagement from "./components/DomainManagement";
import Button from "~/components/Button";
import { ConnectedIcon } from "~/components/Icons/ConnectedIcon";
import { client } from "~/utils/ApiClient";
import { useTheme } from "styled-components";
import { VStack } from "~/components/primitives/VStack";
function Authentication() {
const { authenticationProviders, dialogs } = useStores();
const team = useCurrentTeam();
const { t } = useTranslation();
const theme = useTheme();
const {
data: providers,
loading,
request,
} = useRequest(authenticationProviders.fetchPage);
React.useEffect(() => {
if (!providers && !loading) {
void request();
}
}, [loading, providers, request]);
const groupSyncResult = useConsumeQueryParam("groupSync");
React.useEffect(() => {
if (!groupSyncResult) {
return;
}
if (groupSyncResult === "connected") {
toast.success(t("Group sync connected"));
} else {
toast.error(t("Could not connect group sync"));
}
}, [groupSyncResult, t]);
const handleGuestSigninChange = React.useCallback(
async (checked: boolean) => {
try {
await team.save({ guestSignin: checked });
toast.success(t("Settings saved"));
} catch (err) {
toast.error(errToString(err));
}
},
[team, t]
);
const handleToggleProvider = React.useCallback(
async (provider: AuthenticationProvider, isEnabled: boolean) => {
try {
await provider.save({ isEnabled });
toast.success(t("Settings saved"));
} catch (err) {
toast.error(errToString(err));
}
},
[t]
);
const handleRemoveProvider = React.useCallback(
async (provider: AuthenticationProvider) => {
dialogs.openModal({
title: t("Are you sure?"),
content: (
<ConfirmationDialog
onSubmit={async () => {
await provider.delete();
toast.success(t("Settings saved"));
}}
savingText={`${t("Removing")}…`}
danger
>
{t(
"Removing this authentication provider will prevent members from signing in with {{ authProvider }}.",
{
authProvider: provider.displayName,
}
)}
</ConfirmationDialog>
),
});
},
[dialogs, t]
);
const handleConnectProvider = React.useCallback((name: string) => {
setPostLoginPath(settingsPath("authentication"));
// Start the flow on the current workspace origin so the signed-in actor is
// captured from the host-scoped session before bouncing to the apex.
window.location.href = toRelative(getRedirectUrl(`/auth/${name}`));
}, []);
const handleToggleGroupSync = React.useCallback(
(provider: AuthenticationProvider, checked: boolean) => {
if (checked) {
void (async () => {
try {
if (provider.groupSyncRequiresSetup) {
const result = await client.post<{ data: { url: string } }>(
"/authenticationProviders.startGroupSync",
{ id: provider.id }
);
window.location.href = result.data.url;
return;
}
await provider.save({
settings: {
...provider.settings,
groupSyncEnabled: true,
},
});
toast.success(t("Settings saved"));
} catch (err) {
toast.error(errToString(err));
}
})();
} else {
dialogs.openModal({
title: t("Disable group sync"),
content: (
<DisableGroupSyncDialog
provider={provider}
onSubmit={dialogs.closeAllModals}
/>
),
});
}
},
[t, dialogs]
);
const handleGroupClaimChange = React.useCallback(
async (provider: AuthenticationProvider, groupClaim: string) => {
try {
await provider.save({
settings: {
...provider.settings,
groupClaim,
},
});
toast.success(t("Settings saved"));
} catch (err) {
toast.error(errToString(err));
}
},
[t]
);
const showSuccessMessage = React.useMemo(
() => () => toast.success(t("Settings saved")),
[t]
);
return (
<Scene title={t("Authentication")} icon={<PadlockIcon />}>
<Heading>{t("Authentication")}</Heading>
<Text as="p" type="secondary">
<Trans>
Manage how members sign-in to your workspace and which authentication
providers are enabled.
</Trans>
</Text>
<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">
<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
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}
>
<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">
<EmailIcon /> {t("Email")}
</Flex>
}
name="guestSignin"
description={
env.EMAIL_ENABLED
? t("Allow members to sign-in using their email address")
: t("The server must have SMTP configured to enable this setting")
}
border={false}
>
<Switch
id="guestSignin"
checked={team.guestSignin}
onChange={handleGuestSigninChange}
disabled={!env.EMAIL_ENABLED}
/>
</SettingRow>
<SettingRow
label={
<Flex gap={8} align="center">
<PadlockIcon /> {t("Passkeys")}
</Flex>
}
name="passkeysEnabled"
description={t("Allow members to sign-in with a WebAuthn passkey")}
>
<Switch
id="passkeysEnabled"
checked={team.passkeysEnabled}
onChange={async (checked) => {
try {
await team.save({ passkeysEnabled: checked });
toast.success(t("Settings saved"));
} catch (err) {
toast.error(errToString(err));
}
}}
/>
</SettingRow>
<Heading as="h2">{t("Restrictions")}</Heading>
<DomainManagement onSuccess={showSuccessMessage} />
</Scene>
);
}
const DisableGroupSyncDialog = observer(function DisableGroupSyncDialog({
provider,
onSubmit,
}: {
provider: AuthenticationProvider;
onSubmit: () => void;
}) {
const { t } = useTranslation();
const [action, setAction] = React.useState("keep");
const [isSaving, setIsSaving] = React.useState(false);
const options = React.useMemo(
() => [
{
type: "item" as const,
label: t("Keep synced groups"),
description: t("Groups will remain but no longer update"),
value: "keep",
},
{
type: "item" as const,
label: t("Delete synced groups"),
description: t("Remove all groups created by sync"),
value: "delete",
},
],
[t]
);
const handleSubmit = React.useCallback(
async (ev: React.SyntheticEvent) => {
ev.preventDefault();
setIsSaving(true);
try {
await provider.save({
settings: {
...provider.settings,
groupSyncEnabled: false,
},
});
if (action === "delete") {
await client.post("/groups.deleteAll", {
authenticationProviderId: provider.id,
});
}
toast.success(t("Settings saved"));
onSubmit();
} catch (err) {
toast.error(errToString(err));
} finally {
setIsSaving(false);
}
},
[provider, action, onSubmit, t]
);
return (
<form onSubmit={handleSubmit}>
<Flex gap={12} column>
<Text type="secondary">
{t(
"Group memberships will no longer be synced from {{ authProvider }} when members sign in.",
{ authProvider: provider.displayName }
)}
</Text>
<InputSelect
label={t("Existing groups")}
options={options}
value={action}
onChange={setAction}
/>
<Flex justify="flex-end">
<Button type="submit" disabled={isSaving} danger>
{isSaving ? `${t("Disabling")}…` : t("Disable")}
</Button>
</Flex>
</Flex>
</form>
);
});
export default observer(Authentication);