mirror of
https://github.com/outline/outline.git
synced 2026-12-02 09:42:07 +03:00
Reconnecting updates the existing provider row in place so that member authentications stay linked when the external domain or tenant changes.
466 lines
15 KiB
TypeScript
466 lines
15 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 usePolicy from "~/hooks/usePolicy";
|
|
import useRequest from "~/hooks/useRequest";
|
|
import useStores from "~/hooks/useStores";
|
|
import SettingRow from "./components/SettingRow";
|
|
import { SettingsSection } from "./components/SettingsSection";
|
|
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";
|
|
import { AuthenticationProviderMenu } from "~/menus/AuthenticationProviderMenu";
|
|
|
|
function Authentication() {
|
|
const { authenticationProviders, dialogs } = useStores();
|
|
const team = useCurrentTeam();
|
|
const can = usePolicy(team);
|
|
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>
|
|
|
|
<SettingsSection title={t("Sign in")}>
|
|
{authenticationProviders.orderedData.map((provider) => {
|
|
// Group sync can still be disabled after losing the entitlement.
|
|
const showGroupSync =
|
|
provider.isActive &&
|
|
provider.groupSyncSupported &&
|
|
(!!can.externalGroupSync || !!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">
|
|
{provider.isEnabled ? (
|
|
<AuthenticationProviderMenu
|
|
provider={provider}
|
|
onReconnect={handleConnectProvider}
|
|
onDisconnect={handleRemoveProvider}
|
|
/>
|
|
) : (
|
|
<Button
|
|
icon={<ConnectedIcon color={theme.textSecondary} />}
|
|
onClick={() => handleToggleProvider(provider, true)}
|
|
neutral
|
|
>
|
|
{t("Disabled")}
|
|
</Button>
|
|
)}
|
|
<Text type="tertiary" size="small">
|
|
{provider.providerId}
|
|
</Text>
|
|
</VStack>
|
|
) : (
|
|
<Button
|
|
onClick={() => handleConnectProvider(provider.name)}
|
|
neutral
|
|
>
|
|
{t("Connect")}
|
|
</Button>
|
|
)}
|
|
</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
|
|
)
|
|
}
|
|
>
|
|
{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>
|
|
)}
|
|
{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">
|
|
<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")
|
|
}
|
|
>
|
|
<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>
|
|
</SettingsSection>
|
|
|
|
<SettingsSection title={t("Restrictions")}>
|
|
<DomainManagement onSuccess={showSuccessMessage} />
|
|
</SettingsSection>
|
|
</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);
|