mirror of
https://github.com/outline/outline.git
synced 2026-11-29 16:40:27 +03:00
feat: Allow admins to reconnect an authentication provider (#14649)
Reconnecting updates the existing provider row in place so that member authentications stay linked when the external domain or tenant changes.
This commit is contained in:
1 parent
6d82287a08
commit
0171534400
5 files changed
+192
-22
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import { observer } from "mobx-react";
|
||||
import { useMemo } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import type AuthenticationProvider from "~/models/AuthenticationProvider";
|
||||
import Button from "~/components/Button";
|
||||
import { ConnectedIcon } from "~/components/Icons/ConnectedIcon";
|
||||
import { DropdownMenu } from "~/components/Menu/DropdownMenu";
|
||||
import { createAction } from "~/actions";
|
||||
import { useMenuAction } from "~/hooks/useMenuAction";
|
||||
|
||||
interface Props {
|
||||
/** The connected authentication provider to show the menu for. */
|
||||
provider: AuthenticationProvider;
|
||||
/** Callback when the user chooses to reconnect the provider. */
|
||||
onReconnect: (name: string) => void;
|
||||
/** Callback when the user chooses to disconnect the provider. */
|
||||
onDisconnect: (provider: AuthenticationProvider) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* A "Connected" button with a menu to reconnect or disconnect an
|
||||
* authentication provider.
|
||||
*/
|
||||
export const AuthenticationProviderMenu = observer(
|
||||
function AuthenticationProviderMenu({
|
||||
provider,
|
||||
onReconnect,
|
||||
onDisconnect,
|
||||
}: Props) {
|
||||
const { t } = useTranslation();
|
||||
const section = "Authentication";
|
||||
|
||||
const actions = useMemo(
|
||||
() => [
|
||||
createAction({
|
||||
name: t("Reconnect"),
|
||||
section,
|
||||
perform: () => onReconnect(provider.name),
|
||||
}),
|
||||
createAction({
|
||||
name: `${t("Disconnect")}…`,
|
||||
section,
|
||||
dangerous: true,
|
||||
perform: () => onDisconnect(provider),
|
||||
}),
|
||||
],
|
||||
[t, provider, onReconnect, onDisconnect]
|
||||
);
|
||||
|
||||
const rootAction = useMenuAction(actions);
|
||||
|
||||
return (
|
||||
<DropdownMenu action={rootAction} ariaLabel={t("Connected")}>
|
||||
<Button icon={<ConnectedIcon />} neutral disclosure>
|
||||
{t("Connected")}
|
||||
</Button>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
);
|
||||
@@ -31,6 +31,7 @@ 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();
|
||||
@@ -228,23 +229,21 @@ function Authentication() {
|
||||
<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>
|
||||
{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>
|
||||
|
||||
@@ -833,6 +833,99 @@ describe("accountProvisioner", () => {
|
||||
const providers = await team.$get("authenticationProviders");
|
||||
expect(providers.find((p) => p.name === "google")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should reconnect an existing authentication provider in place", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const team = admin.team;
|
||||
const [existingProvider] = await team.$get("authenticationProviders");
|
||||
const member = await buildUser({ teamId: team.id });
|
||||
const [memberAuth] = await member.$get("authentications");
|
||||
const providerId = randomUUID();
|
||||
|
||||
await accountProvisioner(createContext({ ip, user: admin }), {
|
||||
user: {
|
||||
name: admin.name,
|
||||
email: admin.email!,
|
||||
},
|
||||
team: {
|
||||
teamId: team.id,
|
||||
subdomain: team.subdomain!,
|
||||
},
|
||||
authenticationProvider: {
|
||||
name: existingProvider.name,
|
||||
providerId,
|
||||
},
|
||||
authentication: {
|
||||
providerId: randomUUID(),
|
||||
scopes: ["read"],
|
||||
},
|
||||
});
|
||||
|
||||
const providers = await team.$get("authenticationProviders");
|
||||
expect(providers.length).toEqual(1);
|
||||
expect(providers[0].id).toEqual(existingProvider.id);
|
||||
expect(providers[0].providerId).toEqual(providerId);
|
||||
|
||||
// A member signs in with the same external user id and a new email.
|
||||
const newEmail = faker.internet.email();
|
||||
const { user, isNewUser } = await accountProvisioner(ctx, {
|
||||
user: {
|
||||
name: member.name,
|
||||
email: newEmail,
|
||||
emailVerified: true,
|
||||
},
|
||||
team: {
|
||||
teamId: team.id,
|
||||
subdomain: team.subdomain!,
|
||||
},
|
||||
authenticationProvider: {
|
||||
name: existingProvider.name,
|
||||
providerId,
|
||||
},
|
||||
authentication: {
|
||||
providerId: memberAuth.providerId,
|
||||
scopes: ["read"],
|
||||
},
|
||||
});
|
||||
|
||||
expect(user.id).toEqual(member.id);
|
||||
expect(user.email).toEqual(newEmail);
|
||||
expect(isNewUser).toEqual(false);
|
||||
});
|
||||
|
||||
it("should add an authentication provider when the team has several of the same type", async () => {
|
||||
const team = await buildTeam({
|
||||
authenticationProviders: [
|
||||
{ name: "google", providerId: faker.internet.domainName() },
|
||||
{ name: "google", providerId: faker.internet.domainName() },
|
||||
],
|
||||
});
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const providerId = faker.internet.domainName();
|
||||
|
||||
await accountProvisioner(createContext({ ip, user: admin }), {
|
||||
user: {
|
||||
name: admin.name,
|
||||
email: admin.email!,
|
||||
},
|
||||
team: {
|
||||
teamId: team.id,
|
||||
subdomain: team.subdomain!,
|
||||
},
|
||||
authenticationProvider: {
|
||||
name: "google",
|
||||
providerId,
|
||||
},
|
||||
authentication: {
|
||||
providerId: randomUUID(),
|
||||
scopes: ["read"],
|
||||
},
|
||||
});
|
||||
|
||||
const providers = await team.$get("authenticationProviders");
|
||||
expect(providers.length).toEqual(3);
|
||||
expect(providers.map((p) => p.providerId)).toContain(providerId);
|
||||
});
|
||||
});
|
||||
|
||||
describe("self hosted", () => {
|
||||
|
||||
@@ -103,17 +103,34 @@ async function accountProvisioner(
|
||||
const actor = ctx.state.auth?.user;
|
||||
|
||||
// If the user is already logged in and is an admin of the team then we
|
||||
// allow them to connect a new authentication provider.
|
||||
// allow them to connect or reconnect an authentication provider.
|
||||
if (actor && actor.teamId === teamParams.teamId && actor.isAdmin) {
|
||||
const team = actor.team;
|
||||
const authenticationProvider = await AuthenticationProvider.findOne({
|
||||
const providers = await AuthenticationProvider.findAll({
|
||||
where: {
|
||||
...authenticationProviderParams,
|
||||
name: authenticationProviderParams.name,
|
||||
teamId: team.id,
|
||||
},
|
||||
});
|
||||
const isConnected = providers.some(
|
||||
(provider) =>
|
||||
provider.providerId === authenticationProviderParams.providerId
|
||||
);
|
||||
|
||||
if (!authenticationProvider) {
|
||||
if (!isConnected && providers.length === 1) {
|
||||
// Update in place so that existing user authentications stay linked.
|
||||
await sequelize.transaction((transaction) =>
|
||||
providers[0].updateWithCtx(
|
||||
createContext({
|
||||
user: actor,
|
||||
authType: ctx.context.auth?.type,
|
||||
ip: ctx.context.ip,
|
||||
transaction,
|
||||
}),
|
||||
{ providerId: authenticationProviderParams.providerId }
|
||||
)
|
||||
);
|
||||
} else if (!isConnected) {
|
||||
await team.$create<AuthenticationProvider>(
|
||||
"authenticationProvider",
|
||||
authenticationProviderParams
|
||||
|
||||
@@ -894,6 +894,8 @@
|
||||
"Developer": "Developer",
|
||||
"Applications": "Applications",
|
||||
"API key": "API key",
|
||||
"Reconnect": "Reconnect",
|
||||
"Connected": "Connected",
|
||||
"Show path to document": "Show path to document",
|
||||
"Comment options": "Comment options",
|
||||
"Enable viewer insights": "Enable viewer insights",
|
||||
@@ -1306,7 +1308,6 @@
|
||||
"Sign in": "Sign in",
|
||||
"Allow members to sign in with {{ authProvider }}": "Allow members to sign in with {{ authProvider }}",
|
||||
"Connect {{ authProvider }} to allow members to sign in": "Connect {{ authProvider }} to allow members to sign in",
|
||||
"Connected": "Connected",
|
||||
"Disabled": "Disabled",
|
||||
"Connect": "Connect",
|
||||
"Group sync": "Group sync",
|
||||
|
||||
Reference in new issue
Block a user