From 2504b051eceedda314d2e2c99ae03d4cc5191cc2 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 21 Apr 2025 19:45:58 -0400 Subject: [PATCH] wip --- .../OAuthClient/OAuthClientForm.tsx | 2 +- app/hooks/useSettingsConfig.ts | 4 +- app/menus/OAuthClientMenu.tsx | 2 +- app/models/oauth/OAuthAuthentication.ts | 29 ++++++++++++++ app/models/{ => oauth}/OAuthClient.ts | 8 ++-- app/scenes/Settings/Application.tsx | 2 +- app/scenes/Settings/Applications.tsx | 2 +- app/scenes/Settings/PersonalApiKeys.tsx | 15 ++++++- .../OAuthAuthenticationListItem.tsx | 39 +++++++++++++++++++ .../components/OAuthClientDeleteDialog.tsx | 2 +- .../components/OAuthClientListItem.tsx | 2 +- app/stores/OAuthAuthenticationsStore.ts | 11 ++++++ app/stores/OAuthClientsStore.ts | 4 +- app/stores/RootStore.ts | 3 ++ server/presenters/oauthClient.ts | 1 + .../oauthAuthentications.ts | 2 +- 16 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 app/models/oauth/OAuthAuthentication.ts rename app/models/{ => oauth}/OAuthClient.ts (91%) create mode 100644 app/scenes/Settings/components/OAuthAuthenticationListItem.tsx create mode 100644 app/stores/OAuthAuthenticationsStore.ts diff --git a/app/components/OAuthClient/OAuthClientForm.tsx b/app/components/OAuthClient/OAuthClientForm.tsx index 74dc48e6e7..921c71c707 100644 --- a/app/components/OAuthClient/OAuthClientForm.tsx +++ b/app/components/OAuthClient/OAuthClientForm.tsx @@ -3,7 +3,7 @@ import * as React from "react"; import { Controller, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { OAuthClientValidation } from "@shared/validations"; -import OAuthClient from "~/models/OAuthClient"; +import OAuthClient from "~/models/oauth/OAuthClient"; import ImageInput from "~/scenes/Settings/components/ImageInput"; import Button from "~/components/Button"; import Flex from "~/components/Flex"; diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index 31ca673011..dcbde3d92b 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -90,8 +90,8 @@ const useSettingsConfig = () => { icon: EmailIcon, }, { - name: t("API Keys"), - path: settingsPath("personal-api-keys"), + name: t("Authorizations"), + path: settingsPath("authorizations"), component: PersonalApiKeys, enabled: can.createApiKey && !can.listApiKeys, group: t("Account"), diff --git a/app/menus/OAuthClientMenu.tsx b/app/menus/OAuthClientMenu.tsx index d92f0e7562..e2177c3b8e 100644 --- a/app/menus/OAuthClientMenu.tsx +++ b/app/menus/OAuthClientMenu.tsx @@ -2,7 +2,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { useMenuState } from "reakit/Menu"; -import OAuthClient from "~/models/OAuthClient"; +import OAuthClient from "~/models/oauth/OAuthClient"; import OAuthClientDeleteDialog from "~/scenes/Settings/components/OAuthClientDeleteDialog"; import ContextMenu from "~/components/ContextMenu"; import OverflowMenuButton from "~/components/ContextMenu/OverflowMenuButton"; diff --git a/app/models/oauth/OAuthAuthentication.ts b/app/models/oauth/OAuthAuthentication.ts new file mode 100644 index 0000000000..a34d1e7523 --- /dev/null +++ b/app/models/oauth/OAuthAuthentication.ts @@ -0,0 +1,29 @@ +import { observable } from "mobx"; +import User from "../User"; +import ParanoidModel from "../base/ParanoidModel"; +import Field from "../decorators/Field"; +import Relation from "../decorators/Relation"; +import OAuthClient from "./OAuthClient"; + +class OAuthAuthentication extends ParanoidModel { + static modelName = "OAuthAuthentication"; + + /** A list of scopes that this authentication has access to */ + @Field + @observable + scope: string[]; + + @Relation(() => User) + user: User; + + userId: string; + + @Relation(() => OAuthClient) + oauthClient: OAuthClient; + + oauthClientId: string; + + lastActiveAt: string; +} + +export default OAuthAuthentication; diff --git a/app/models/OAuthClient.ts b/app/models/oauth/OAuthClient.ts similarity index 91% rename from app/models/OAuthClient.ts rename to app/models/oauth/OAuthClient.ts index babf519e9b..fe22707ea8 100644 --- a/app/models/OAuthClient.ts +++ b/app/models/oauth/OAuthClient.ts @@ -3,10 +3,10 @@ import { observable, runInAction } from "mobx"; import queryString from "query-string"; import env from "~/env"; import { client } from "~/utils/ApiClient"; -import User from "./User"; -import ParanoidModel from "./base/ParanoidModel"; -import Field from "./decorators/Field"; -import Relation from "./decorators/Relation"; +import User from "../User"; +import ParanoidModel from "../base/ParanoidModel"; +import Field from "../decorators/Field"; +import Relation from "../decorators/Relation"; class OAuthClient extends ParanoidModel { static modelName = "OAuthClient"; diff --git a/app/scenes/Settings/Application.tsx b/app/scenes/Settings/Application.tsx index 55818ca36b..84b781726d 100644 --- a/app/scenes/Settings/Application.tsx +++ b/app/scenes/Settings/Application.tsx @@ -6,7 +6,7 @@ import { useTranslation } from "react-i18next"; import { useParams } from "react-router-dom"; import { toast } from "sonner"; import { OAuthClientValidation } from "@shared/validations"; -import OAuthClient from "~/models/OAuthClient"; +import OAuthClient from "~/models/oauth/OAuthClient"; import Breadcrumb from "~/components/Breadcrumb"; import Button from "~/components/Button"; import ConfirmationDialog from "~/components/ConfirmationDialog"; diff --git a/app/scenes/Settings/Applications.tsx b/app/scenes/Settings/Applications.tsx index 58b2bb847d..af8d11414e 100644 --- a/app/scenes/Settings/Applications.tsx +++ b/app/scenes/Settings/Applications.tsx @@ -2,7 +2,7 @@ import { observer } from "mobx-react"; import { InternetIcon } from "outline-icons"; import * as React from "react"; import { useTranslation, Trans } from "react-i18next"; -import OAuthClient from "~/models/OAuthClient"; +import OAuthClient from "~/models/oauth/OAuthClient"; import { Action } from "~/components/Actions"; import Button from "~/components/Button"; import Heading from "~/components/Heading"; diff --git a/app/scenes/Settings/PersonalApiKeys.tsx b/app/scenes/Settings/PersonalApiKeys.tsx index b0bfaf6c62..b7a5201d61 100644 --- a/app/scenes/Settings/PersonalApiKeys.tsx +++ b/app/scenes/Settings/PersonalApiKeys.tsx @@ -3,6 +3,7 @@ import { CodeIcon } from "outline-icons"; import * as React from "react"; import { useTranslation, Trans } from "react-i18next"; import ApiKey from "~/models/ApiKey"; +import OAuthAuthentication from "~/models/oauth/OAuthAuthentication"; import { Action } from "~/components/Actions"; import Button from "~/components/Button"; import Heading from "~/components/Heading"; @@ -16,12 +17,13 @@ import useCurrentUser from "~/hooks/useCurrentUser"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import ApiKeyListItem from "./components/ApiKeyListItem"; +import OAuthAuthenticationListItem from "./components/OAuthAuthenticationListItem"; function PersonalApiKeys() { const team = useCurrentTeam(); const user = useCurrentUser(); const { t } = useTranslation(); - const { apiKeys } = useStores(); + const { apiKeys, oauthAuthentications } = useStores(); const can = usePolicy(team); const context = useActionContext(); @@ -70,6 +72,17 @@ function PersonalApiKeys() { )} /> + {t("Authentications")}} + renderItem={(oauthAuthentication: OAuthAuthentication) => ( + + )} + /> ); } diff --git a/app/scenes/Settings/components/OAuthAuthenticationListItem.tsx b/app/scenes/Settings/components/OAuthAuthenticationListItem.tsx new file mode 100644 index 0000000000..85c3684033 --- /dev/null +++ b/app/scenes/Settings/components/OAuthAuthenticationListItem.tsx @@ -0,0 +1,39 @@ +import { observer } from "mobx-react"; +import * as React from "react"; +import { useTranslation } from "react-i18next"; +import OAuthAuthentication from "~/models/oauth/OAuthAuthentication"; +import ListItem from "~/components/List/Item"; +import Text from "~/components/Text"; +import Time from "~/components/Time"; + +type Props = { + /** The OAuthAuthentication to display */ + oauthAuthentication: OAuthAuthentication; +}; + +const OAuthAuthenticationListItem = ({ oauthAuthentication }: Props) => { + const { t } = useTranslation(); + + const subtitle = ( + <> + {oauthAuthentication.lastActiveAt && ( + + {t("Last accessed")}{" "} + + )} + {oauthAuthentication.scope.join(", ")} + + ); + + return ( + + ); +}; + +export default observer(OAuthAuthenticationListItem); diff --git a/app/scenes/Settings/components/OAuthClientDeleteDialog.tsx b/app/scenes/Settings/components/OAuthClientDeleteDialog.tsx index c16462b447..b84ae082ca 100644 --- a/app/scenes/Settings/components/OAuthClientDeleteDialog.tsx +++ b/app/scenes/Settings/components/OAuthClientDeleteDialog.tsx @@ -1,6 +1,6 @@ import * as React from "react"; import { useTranslation } from "react-i18next"; -import OAuthClient from "~/models/OAuthClient"; +import OAuthClient from "~/models/oauth/OAuthClient"; import ConfirmationDialog from "~/components/ConfirmationDialog"; type Props = { diff --git a/app/scenes/Settings/components/OAuthClientListItem.tsx b/app/scenes/Settings/components/OAuthClientListItem.tsx index ee7f827361..825a357cbe 100644 --- a/app/scenes/Settings/components/OAuthClientListItem.tsx +++ b/app/scenes/Settings/components/OAuthClientListItem.tsx @@ -2,7 +2,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; -import OAuthClient from "~/models/OAuthClient"; +import OAuthClient from "~/models/oauth/OAuthClient"; import { Avatar, AvatarSize } from "~/components/Avatar"; import { AvatarVariant } from "~/components/Avatar/Avatar"; import ListItem from "~/components/List/Item"; diff --git a/app/stores/OAuthAuthenticationsStore.ts b/app/stores/OAuthAuthenticationsStore.ts new file mode 100644 index 0000000000..ba30209326 --- /dev/null +++ b/app/stores/OAuthAuthenticationsStore.ts @@ -0,0 +1,11 @@ +import OAuthAuthentication from "~/models/oauth/OAuthAuthentication"; +import RootStore from "./RootStore"; +import Store from "./base/Store"; + +export default class OAuthAuthenticationsStore extends Store { + apiEndpoint = "oauthAuthentications"; + + constructor(rootStore: RootStore) { + super(rootStore, OAuthAuthentication); + } +} diff --git a/app/stores/OAuthClientsStore.ts b/app/stores/OAuthClientsStore.ts index 5ed13dd88b..a1fd075c95 100644 --- a/app/stores/OAuthClientsStore.ts +++ b/app/stores/OAuthClientsStore.ts @@ -1,8 +1,10 @@ -import OAuthClient from "~/models/OAuthClient"; +import OAuthClient from "~/models/oauth/OAuthClient"; import RootStore from "./RootStore"; import Store from "./base/Store"; export default class OAuthClientsStore extends Store { + apiEndpoint = "oauthClients"; + constructor(rootStore: RootStore) { super(rootStore, OAuthClient); } diff --git a/app/stores/RootStore.ts b/app/stores/RootStore.ts index 264080df28..eeef2dfac3 100644 --- a/app/stores/RootStore.ts +++ b/app/stores/RootStore.ts @@ -18,6 +18,7 @@ import ImportsStore from "./ImportsStore"; import IntegrationsStore from "./IntegrationsStore"; import MembershipsStore from "./MembershipsStore"; import NotificationsStore from "./NotificationsStore"; +import OAuthAuthenticationsStore from "./OAuthAuthenticationsStore"; import OAuthClientsStore from "./OAuthClientsStore"; import PinsStore from "./PinsStore"; import PoliciesStore from "./PoliciesStore"; @@ -49,6 +50,7 @@ export default class RootStore { integrations: IntegrationsStore; memberships: MembershipsStore; notifications: NotificationsStore; + oauthAuthentications: OAuthAuthenticationsStore; oauthClients: OAuthClientsStore; presence: DocumentPresenceStore; pins: PinsStore; @@ -80,6 +82,7 @@ export default class RootStore { this.registerStore(IntegrationsStore); this.registerStore(MembershipsStore); this.registerStore(NotificationsStore); + this.registerStore(OAuthAuthenticationsStore, "oauthAuthentications"); this.registerStore(OAuthClientsStore, "oauthClients"); this.registerStore(PinsStore); this.registerStore(PoliciesStore); diff --git a/server/presenters/oauthClient.ts b/server/presenters/oauthClient.ts index 08fc9505e6..40e466f91f 100644 --- a/server/presenters/oauthClient.ts +++ b/server/presenters/oauthClient.ts @@ -31,6 +31,7 @@ export default function presentOAuthClient(oauthClient: OAuthClient) { */ export function presentPublishedOAuthClient(oauthClient: OAuthClient) { return { + id: oauthClient.id, name: oauthClient.name, description: oauthClient.description, developerName: oauthClient.developerName, diff --git a/server/routes/api/oauthAuthentications/oauthAuthentications.ts b/server/routes/api/oauthAuthentications/oauthAuthentications.ts index 15f0cd8f6e..7830b8b1e5 100644 --- a/server/routes/api/oauthAuthentications/oauthAuthentications.ts +++ b/server/routes/api/oauthAuthentications/oauthAuthentications.ts @@ -34,7 +34,7 @@ router.post( FROM oauth_authentications oa INNER JOIN oauth_clients oc ON oc.id = oa."oauthClientId" WHERE oa."userId" = :userId - ORDER BY oa."oauthClientId", oa."scope", oa."createdAt" DESC + ORDER BY oa."oauthClientId", oa."scope", oa."lastActiveAt", oa."createdAt" DESC LIMIT :limit OFFSET :offset `, {