diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index fb51cc3442..ece9989b0e 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -30,7 +30,7 @@ import usePolicy from "./usePolicy"; const ApiKeys = lazy(() => import("~/scenes/Settings/ApiKeys")); const Applications = lazy(() => import("~/scenes/Settings/Applications")); -const PersonalApiKeys = lazy(() => import("~/scenes/Settings/PersonalApiKeys")); +const APIAndApps = lazy(() => import("~/scenes/Settings/APIAndApps")); const Details = lazy(() => import("~/scenes/Settings/Details")); const Export = lazy(() => import("~/scenes/Settings/Export")); const Features = lazy(() => import("~/scenes/Settings/Features")); @@ -88,12 +88,12 @@ const useSettingsConfig = () => { icon: EmailIcon, }, { - name: t("Authorizations"), - path: settingsPath("authorizations"), - component: PersonalApiKeys, - enabled: can.createApiKey && !can.listApiKeys, + name: t("API & Apps"), + path: settingsPath("api-and-apps"), + component: APIAndApps, + enabled: true, group: t("Account"), - icon: CodeIcon, + icon: PadlockIcon, }, // Workspace { diff --git a/app/menus/OAuthAuthenticationMenu.tsx b/app/menus/OAuthAuthenticationMenu.tsx new file mode 100644 index 0000000000..8b826655c7 --- /dev/null +++ b/app/menus/OAuthAuthenticationMenu.tsx @@ -0,0 +1,57 @@ +import { observer } from "mobx-react"; +import * as React from "react"; +import { useTranslation } from "react-i18next"; +import { useMenuState } from "reakit/Menu"; +import OAuthAuthentication from "~/models/oauth/OAuthAuthentication"; +import ConfirmationDialog from "~/components/ConfirmationDialog"; +import ContextMenu from "~/components/ContextMenu"; +import MenuItem from "~/components/ContextMenu/MenuItem"; +import OverflowMenuButton from "~/components/ContextMenu/OverflowMenuButton"; +import useStores from "~/hooks/useStores"; + +type Props = { + /** The OAuthAuthentication to associate with the menu */ + oauthAuthentication: OAuthAuthentication; +}; + +function OAuthAuthenticationMenu({ oauthAuthentication }: Props) { + const menu = useMenuState({ + modal: true, + }); + const { dialogs } = useStores(); + const { t } = useTranslation(); + + const handleRevoke = React.useCallback(() => { + dialogs.openModal({ + title: t("Revoke {{ appName }}", { + appName: oauthAuthentication.oauthClient.name, + }), + content: ( + { + await oauthAuthentication.deleteAll(); + dialogs.closeAllModals(); + }} + submitText={t("Revoke")} + savingText={`${t("Revoking")}…`} + danger + > + {t("Are you sure you want to revoke access?")} + + ), + }); + }, [t, dialogs, oauthAuthentication]); + + return ( + <> + + + + {t("Revoke")} + + + + ); +} + +export default observer(OAuthAuthenticationMenu); diff --git a/app/models/oauth/OAuthAuthentication.ts b/app/models/oauth/OAuthAuthentication.ts index a34d1e7523..adb1da698c 100644 --- a/app/models/oauth/OAuthAuthentication.ts +++ b/app/models/oauth/OAuthAuthentication.ts @@ -1,4 +1,5 @@ -import { observable } from "mobx"; +import { action, observable } from "mobx"; +import { client } from "~/utils/ApiClient"; import User from "../User"; import ParanoidModel from "../base/ParanoidModel"; import Field from "../decorators/Field"; @@ -24,6 +25,16 @@ class OAuthAuthentication extends ParanoidModel { oauthClientId: string; lastActiveAt: string; + + @action + public async deleteAll() { + await client.post(`/${this.store.apiEndpoint}.delete`, { + oauthClientId: this.oauthClientId, + scope: this.scope, + }); + + return this.store.remove(this.id); + } } export default OAuthAuthentication; diff --git a/app/scenes/Settings/PersonalApiKeys.tsx b/app/scenes/Settings/APIAndApps.tsx similarity index 61% rename from app/scenes/Settings/PersonalApiKeys.tsx rename to app/scenes/Settings/APIAndApps.tsx index 0b795a48d7..1ec53c1adb 100644 --- a/app/scenes/Settings/PersonalApiKeys.tsx +++ b/app/scenes/Settings/APIAndApps.tsx @@ -1,5 +1,5 @@ import { observer } from "mobx-react"; -import { CodeIcon } from "outline-icons"; +import { PadlockIcon } from "outline-icons"; import * as React from "react"; import { useTranslation, Trans } from "react-i18next"; import ApiKey from "~/models/ApiKey"; @@ -11,6 +11,7 @@ import PaginatedList from "~/components/PaginatedList"; import Scene from "~/components/Scene"; import Text from "~/components/Text"; import { createApiKey } from "~/actions/definitions/apiKeys"; +import env from "~/env"; import useActionContext from "~/hooks/useActionContext"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import useCurrentUser from "~/hooks/useCurrentUser"; @@ -19,18 +20,19 @@ import useStores from "~/hooks/useStores"; import ApiKeyListItem from "./components/ApiKeyListItem"; import OAuthAuthenticationListItem from "./components/OAuthAuthenticationListItem"; -function PersonalApiKeys() { +function APIAndApps() { const team = useCurrentTeam(); const user = useCurrentUser(); const { t } = useTranslation(); const { apiKeys, oauthAuthentications } = useStores(); const can = usePolicy(team); const context = useActionContext(); + const appName = env.APP_NAME; return ( } + title={t("API & Apps")} + icon={} actions={ <> {can.createApiKey && ( @@ -46,28 +48,33 @@ function PersonalApiKeys() { } > - {t("API")} - - - ), - }} - /> - + {t("API & Apps")} +

{t("API keys")}

+ {can.createApiKey ? ( + + + ), + }} + /> + + ) : ( + + {t("API keys have been disabled by an admin for your account")} + + )} fetch={apiKeys.fetchPage} items={apiKeys.personalApiKeys} options={{ userId: user.id }} - heading={

{t("Personal keys")}

} renderItem={(apiKey) => ( )} @@ -75,7 +82,17 @@ function PersonalApiKeys() { {t("Authentications")}} + heading={ + <> +

{t("Application access")}

+ + {t( + "Manage which third-party and internal applications have been granted access to your {{ appName }} account.", + { appName } + )} + + + } renderItem={(oauthAuthentication: OAuthAuthentication) => ( { const subtitle = ( <> - {oauthAuthentication.lastActiveAt && ( - - {t("Accessed data")}{" "} - - )} + {oauthAuthentication.lastActiveAt ? ( + <> + {t("Last active")}{" "} + + {OAuthScopeHelper.normalizeScopes(oauthAuthentication.scope, t).join( ", " )} @@ -46,6 +51,9 @@ const OAuthAuthenticationListItem = ({ oauthAuthentication }: Props) => { } title={oauthAuthentication.oauthClient.name} subtitle={subtitle} + actions={ + + } /> ); }; diff --git a/server/models/oauth/OAuthAuthentication.ts b/server/models/oauth/OAuthAuthentication.ts index ed86bacb79..06865bcdb7 100644 --- a/server/models/oauth/OAuthAuthentication.ts +++ b/server/models/oauth/OAuthAuthentication.ts @@ -12,7 +12,6 @@ import { BelongsTo, ForeignKey, Table, - BeforeCreate, IsDate, Unique, } from "sequelize-typescript"; @@ -115,13 +114,6 @@ class OAuthAuthentication extends ParanoidModel< return this.save({ silent: true }); }; - // hooks - - @BeforeCreate - static async setLastActiveAt(model: OAuthAuthentication) { - model.lastActiveAt = new Date(); - } - // instance methods /** Checks if the authentication has access to the given path */ diff --git a/server/routes/api/oauthAuthentications/oauthAuthentications.ts b/server/routes/api/oauthAuthentications/oauthAuthentications.ts index 7830b8b1e5..351647b33d 100644 --- a/server/routes/api/oauthAuthentications/oauthAuthentications.ts +++ b/server/routes/api/oauthAuthentications/oauthAuthentications.ts @@ -34,6 +34,7 @@ router.post( FROM oauth_authentications oa INNER JOIN oauth_clients oc ON oc.id = oa."oauthClientId" WHERE oa."userId" = :userId + AND oa."deletedAt" IS NULL ORDER BY oa."oauthClientId", oa."scope", oa."lastActiveAt", oa."createdAt" DESC LIMIT :limit OFFSET :offset `, diff --git a/server/routes/api/oauthAuthentications/schema.ts b/server/routes/api/oauthAuthentications/schema.ts index 9a7f8d33e2..9dce6369f9 100644 --- a/server/routes/api/oauthAuthentications/schema.ts +++ b/server/routes/api/oauthAuthentications/schema.ts @@ -12,7 +12,7 @@ export type OAuthAuthenticationsListReq = z.infer< export const OAuthAuthenticationsDeleteSchema = BaseSchema.extend({ body: z.object({ oauthClientId: z.string(), - scope: z.string().optional(), + scope: z.array(z.string()).optional(), }), }); diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 8dd90ea1f1..5576e578f2 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -520,7 +520,7 @@ "Unsubscribed from document": "Unsubscribed from document", "Unsubscribed from collection": "Unsubscribed from collection", "Account": "Account", - "Authorizations": "Authorizations", + "API & Apps": "API & Apps", "Details": "Details", "Security": "Security", "Features": "Features", @@ -561,6 +561,9 @@ "New child document": "New child document", "Save in workspace": "Save in workspace", "Notification settings": "Notification settings", + "Revoke {{ appName }}": "Revoke {{ appName }}", + "Revoking": "Revoking", + "Are you sure you want to revoke access?": "Are you sure you want to revoke access?", "Delete app": "Delete app", "Revision options": "Revision options", "Share link revoked": "Share link revoked", @@ -875,6 +878,11 @@ "Something went wrong": "Something went wrong", "Please try again or contact support if the problem persists": "Please try again or contact support if the problem persists", "No documents found for your search filters.": "No documents found for your search filters.", + "Create personal API keys to authenticate with the API and programatically control\n your workspace's data. For more details see the developer documentation.": "Create personal API keys to authenticate with the API and programatically control\n your workspace's data. For more details see the developer documentation.", + "{t(\"API keys have been disabled by an admin for your account\")}": "{t(\"API keys have been disabled by an admin for your account\")}", + "API keys have been disabled by an admin for your account": "API keys have been disabled by an admin for your account", + "Application access": "Application access", + "Manage which third-party and internal applications have been granted access to your {{ appName }} account.": "Manage which third-party and internal applications have been granted access to your {{ appName }} account.", "API": "API", "API keys can be used to authenticate with the API and programatically control\n your workspace's data. For more details see the developer documentation.": "API keys can be used to authenticate with the API and programatically control\n your workspace's data. For more details see the developer documentation.", "Client secret rotated": "Client secret rotated", @@ -900,7 +908,6 @@ "Restricted scope": "Restricted scope", "API key copied to clipboard": "API key copied to clipboard", "Copied": "Copied", - "Revoking": "Revoking", "Are you sure you want to revoke the {{ tokenName }} token?": "Are you sure you want to revoke the {{ tokenName }} token?", "Disconnect integration": "Disconnect integration", "Connected": "Connected", @@ -964,7 +971,7 @@ "Drag and drop the zip file from the Markdown export option in {{appName}}, or click to upload": "Drag and drop the zip file from the Markdown export option in {{appName}}, or click to upload", "Last active": "Last active", "Guest": "Guest", - "Accessed data": "Accessed data", + "Never used": "Never used", "Are you sure you want to delete the {{ appName }} application? This cannot be undone.": "Are you sure you want to delete the {{ appName }} application? This cannot be undone.", "Shared by": "Shared by", "Date shared": "Date shared", @@ -1050,9 +1057,6 @@ "Unsubscription successful. Your notification settings were updated": "Unsubscription successful. Your notification settings were updated", "Manage when and where you receive email notifications.": "Manage when and where you receive email notifications.", "The email integration is currently disabled. Please set the associated environment variables and restart the server to enable notifications.": "The email integration is currently disabled. Please set the associated environment variables and restart the server to enable notifications.", - "Create personal API keys to authenticate with the API and programatically control\n your workspace's data. API keys have the same permissions as your user account.\n For more details see the developer documentation.": "Create personal API keys to authenticate with the API and programatically control\n your workspace's data. API keys have the same permissions as your user account.\n For more details see the developer documentation.", - "Personal keys": "Personal keys", - "Authentications": "Authentications", "Preferences saved": "Preferences saved", "Delete account": "Delete account", "Manage settings that affect your personal experience.": "Manage settings that affect your personal experience.",