diff --git a/app/menus/AuthenticationProviderMenu.tsx b/app/menus/AuthenticationProviderMenu.tsx new file mode 100644 index 0000000000..2eda72c812 --- /dev/null +++ b/app/menus/AuthenticationProviderMenu.tsx @@ -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 ( + + + + ); + } +); diff --git a/app/scenes/Settings/Authentication.tsx b/app/scenes/Settings/Authentication.tsx index 71e701064c..bc2400dafa 100644 --- a/app/scenes/Settings/Authentication.tsx +++ b/app/scenes/Settings/Authentication.tsx @@ -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() { {provider.isConnected ? ( - + {provider.isEnabled ? ( + + ) : ( + + )} {provider.providerId} diff --git a/server/commands/accountProvisioner.test.ts b/server/commands/accountProvisioner.test.ts index 99615a120f..ec596b0915 100644 --- a/server/commands/accountProvisioner.test.ts +++ b/server/commands/accountProvisioner.test.ts @@ -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", () => { diff --git a/server/commands/accountProvisioner.ts b/server/commands/accountProvisioner.ts index 3b1de34b6e..0161bdf2f8 100644 --- a/server/commands/accountProvisioner.ts +++ b/server/commands/accountProvisioner.ts @@ -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", authenticationProviderParams diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index d505c52ac1..aa8539d5aa 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -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",