From db0deb69978cf32e803a9866384cc41360d944be Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 2 Apr 2025 09:45:56 -0400 Subject: [PATCH] debugging --- app/actions/definitions/oauthClients.tsx | 26 ++++++ app/hooks/useSettingsConfig.ts | 9 +++ app/models/OAuthClient.ts | 59 ++++++++++++++ app/scenes/OAuth/Authorize.tsx | 5 +- app/scenes/Settings/Applications.tsx | 73 +++++++++++++++++ .../components/OAuthClientListItem.tsx | 80 +++++++++++++++++++ app/stores/OAuthClientsStore.ts | 16 ++++ app/stores/RootStore.ts | 3 + plugins/oauth/server/api/oauthClients.ts | 37 +++++++++ plugins/oauth/server/api/schema.ts | 35 ++++++++ plugins/oauth/server/index.ts | 5 ++ plugins/oauth/server/models/OAuthClient.ts | 34 +++++--- plugins/oauth/server/oauth.ts | 11 ++- .../oauth/server/presenters/oauthClient.ts | 18 +++++ 14 files changed, 394 insertions(+), 17 deletions(-) create mode 100644 app/actions/definitions/oauthClients.tsx create mode 100644 app/models/OAuthClient.ts create mode 100644 app/scenes/Settings/Applications.tsx create mode 100644 app/scenes/Settings/components/OAuthClientListItem.tsx create mode 100644 app/stores/OAuthClientsStore.ts create mode 100644 plugins/oauth/server/api/oauthClients.ts create mode 100644 plugins/oauth/server/api/schema.ts create mode 100644 plugins/oauth/server/presenters/oauthClient.ts diff --git a/app/actions/definitions/oauthClients.tsx b/app/actions/definitions/oauthClients.tsx new file mode 100644 index 0000000000..db0fbc61ae --- /dev/null +++ b/app/actions/definitions/oauthClients.tsx @@ -0,0 +1,26 @@ +import { PlusIcon } from "outline-icons"; +import * as React from "react"; +import stores from "~/stores"; +import OAuthClientNew from "~/scenes/OAuthClientNew"; +import { createAction } from ".."; +import { SettingsSection } from "../sections"; + +export const createOAuthClient = createAction({ + name: ({ t }) => t("New App"), + analyticsName: "New App", + section: SettingsSection, + icon: , + keywords: "create", + // TODO + // visible: () => + // stores.policies.abilities(stores.auth.team?.id || "").createOAuthClient, + perform: ({ t, event }) => { + event?.preventDefault(); + event?.stopPropagation(); + + stores.dialogs.openModal({ + title: t("New App"), + content: , + }); + }, +}); diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index 89935f8c80..0f6afe5760 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -29,6 +29,7 @@ import useCurrentUser from "./useCurrentUser"; 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 Details = lazy(() => import("~/scenes/Settings/Details")); const Export = lazy(() => import("~/scenes/Settings/Export")); @@ -152,6 +153,14 @@ const useSettingsConfig = () => { group: t("Workspace"), icon: CodeIcon, }, + { + name: t("Applications"), + path: settingsPath("applications"), + component: Applications, + enabled: true, // can.listApiKeys, TODO + group: t("Workspace"), + icon: CodeIcon, + }, { name: t("Shared Links"), path: settingsPath("shares"), diff --git a/app/models/OAuthClient.ts b/app/models/OAuthClient.ts new file mode 100644 index 0000000000..3b40ac2af8 --- /dev/null +++ b/app/models/OAuthClient.ts @@ -0,0 +1,59 @@ +import { observable } from "mobx"; +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"; + + /** The human-readable name of this app */ + @Field + @observable + name: string; + + /** A short description of this app */ + @Field + @observable + description: string | null; + + /** The name of the developer of this app */ + @Field + @observable + developerName: string | null; + + /** The URL of the developer of this app */ + @Field + @observable + developerUrl: string | null; + + /** The URL of the avatar of the developer of this app */ + @Field + @observable + avatarUrl: string | null; + + /** The developer facing ID of this app */ + @Field + clientId: string; + + /** The developer facing secret of this app */ + @Field + clientSecret: string; + + /** Whether this app is published (available to other workspaces) */ + @Field + @observable + published: boolean; + + /** A list of valid redirect URIs for this app */ + @Field + @observable + redirectUris: string[]; + + @Relation(() => User) + createdBy: User; + + createdById: string; +} + +export default OAuthClient; diff --git a/app/scenes/OAuth/Authorize.tsx b/app/scenes/OAuth/Authorize.tsx index cfa44c410f..7ff8de56cb 100644 --- a/app/scenes/OAuth/Authorize.tsx +++ b/app/scenes/OAuth/Authorize.tsx @@ -12,8 +12,9 @@ function Authorize() { - - + + + diff --git a/app/scenes/Settings/Applications.tsx b/app/scenes/Settings/Applications.tsx new file mode 100644 index 0000000000..6ba9cdc97c --- /dev/null +++ b/app/scenes/Settings/Applications.tsx @@ -0,0 +1,73 @@ +import { observer } from "mobx-react"; +import { CodeIcon } from "outline-icons"; +import * as React from "react"; +import { useTranslation, Trans } from "react-i18next"; +import OAuthClient from "~/models/OAuthClient"; +import { Action } from "~/components/Actions"; +import Button from "~/components/Button"; +import Heading from "~/components/Heading"; +import PaginatedList from "~/components/PaginatedList"; +import Scene from "~/components/Scene"; +import Text from "~/components/Text"; +import { createOAuthClient } from "~/actions/definitions/oauthClients"; +import useActionContext from "~/hooks/useActionContext"; +import useCurrentTeam from "~/hooks/useCurrentTeam"; +import usePolicy from "~/hooks/usePolicy"; +import useStores from "~/hooks/useStores"; +import OAuthClientListItem from "./components/OAuthClientListItem"; + +function Applications() { + const team = useCurrentTeam(); + const { t } = useTranslation(); + const { oauthClients } = useStores(); + const can = usePolicy(team); + const context = useActionContext(); + + return ( + } + actions={ + <> + {can.createApiKey && ( + + + + )} + + *** + + {/* */} + + } + /> + ); +}; + +export default observer(OAuthClientListItem); diff --git a/app/stores/OAuthClientsStore.ts b/app/stores/OAuthClientsStore.ts new file mode 100644 index 0000000000..b896f09164 --- /dev/null +++ b/app/stores/OAuthClientsStore.ts @@ -0,0 +1,16 @@ +import OAuthClient from "~/models/OAuthClient"; +import RootStore from "./RootStore"; +import Store, { RPCAction } from "./base/Store"; + +export default class OAuthClientsStore extends Store { + actions = [ + RPCAction.List, + RPCAction.Create, + RPCAction.Update, + RPCAction.Delete, + ]; + + constructor(rootStore: RootStore) { + super(rootStore, OAuthClient); + } +} diff --git a/app/stores/RootStore.ts b/app/stores/RootStore.ts index f874340763..c08153c6a3 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 OAuthClientsStore from "./OAuthClientsStore"; import PinsStore from "./PinsStore"; import PoliciesStore from "./PoliciesStore"; import RevisionsStore from "./RevisionsStore"; @@ -48,6 +49,7 @@ export default class RootStore { integrations: IntegrationsStore; memberships: MembershipsStore; notifications: NotificationsStore; + oauthClients: OAuthClientsStore; presence: DocumentPresenceStore; pins: PinsStore; policies: PoliciesStore; @@ -78,6 +80,7 @@ export default class RootStore { this.registerStore(IntegrationsStore); this.registerStore(MembershipsStore); this.registerStore(NotificationsStore); + this.registerStore(OAuthClientsStore); this.registerStore(PinsStore); this.registerStore(PoliciesStore); this.registerStore(RevisionsStore); diff --git a/plugins/oauth/server/api/oauthClients.ts b/plugins/oauth/server/api/oauthClients.ts new file mode 100644 index 0000000000..efa27881e2 --- /dev/null +++ b/plugins/oauth/server/api/oauthClients.ts @@ -0,0 +1,37 @@ +import auth from "@server/middlewares/authentication"; +import { transaction } from "@server/middlewares/transaction"; +import validate from "@server/middlewares/validate"; +import { authorize } from "@server/policies"; +import { APIContext } from "@server/types"; +import Router from "koa-router"; +import OAuthClient from "../models/OAuthClient"; +import presentOAuthClient from "../presenters/oauthClient"; +import * as T from "./schema"; + +const router = new Router(); + +router.post( + "oauthClients.create", + auth(), + validate(T.OAuthClientsCreateSchema), + transaction(), + async (ctx: APIContext) => { + const input = ctx.input.body; + const { user } = ctx.state.auth; + + // TODO: Switch policy + authorize(user, "createApiKey", user.team); + + const oauthClient = await OAuthClient.createWithCtx(ctx, { + ...input, + teamId: user.teamId, + createdById: user.id, + }); + + ctx.body = { + data: presentOAuthClient(oauthClient), + }; + } +); + +export default router; diff --git a/plugins/oauth/server/api/schema.ts b/plugins/oauth/server/api/schema.ts new file mode 100644 index 0000000000..224f6518dc --- /dev/null +++ b/plugins/oauth/server/api/schema.ts @@ -0,0 +1,35 @@ +import { BaseSchema } from "@server/routes/api/schema"; +import { z } from "zod"; + +export const OAuthClientsCreateSchema = BaseSchema.extend({ + body: z.object({ + /** OAuth client name */ + name: z.string(), + + /** OAuth client description */ + description: z.string().nullish(), + + /** OAuth client developer name */ + developerName: z.string().nullish(), + + /** OAuth client developer url */ + developerUrl: z.string().nullish(), + + /** OAuth client avatar url */ + avatarUrl: z.string().nullish(), + + /** OAuth client redirect uri */ + redirectUris: z + .array(z.string().url()) + .min(1, { message: "At least one redirect uri is required" }) + .max(10, { message: "A maximum of 10 redirect uris are allowed" }) + .refine((uris) => uris.every((uri) => uri.length <= 255), { + message: "Redirect uri must be less than 255 characters", + }), + + /** OAuth client published */ + published: z.boolean().default(false), + }), +}); + +export type OAuthClientsCreateReq = z.infer; diff --git a/plugins/oauth/server/index.ts b/plugins/oauth/server/index.ts index 7c98fe3a02..a00b439d94 100644 --- a/plugins/oauth/server/index.ts +++ b/plugins/oauth/server/index.ts @@ -1,4 +1,5 @@ import { PluginManager, Hook } from "@server/utils/PluginManager"; +import oauthClients from "./api/oauthClients"; import OAuthAuthentication from "./models/OAuthAuthentication"; import OAuthAuthorizationCode from "./models/OAuthAuthorizationCode"; import OAuthClient from "./models/OAuthClient"; @@ -24,4 +25,8 @@ PluginManager.add([ app: oauth, }, }, + { + type: Hook.API, + value: oauthClients, + }, ]); diff --git a/plugins/oauth/server/models/OAuthClient.ts b/plugins/oauth/server/models/OAuthClient.ts index 98bc5c03c4..722fb29913 100644 --- a/plugins/oauth/server/models/OAuthClient.ts +++ b/plugins/oauth/server/models/OAuthClient.ts @@ -1,4 +1,18 @@ -import { ArrayNotEmpty, ArrayUnique, IsUrl } from "class-validator"; +import Team from "@server/models/Team"; +import User from "@server/models/User"; +import ParanoidModel from "@server/models/base/ParanoidModel"; +import Encrypted from "@server/models/decorators/Encrypted"; +import Fix from "@server/models/decorators/Fix"; +import IsUrlOrRelativePath from "@server/models/validators/IsUrlOrRelativePath"; +import NotContainsUrl from "@server/models/validators/NotContainsUrl"; +import { OAuthClientValidation } from "@shared/validations"; +import { + ArrayMaxSize, + ArrayMinSize, + ArrayNotEmpty, + ArrayUnique, + IsUrl, +} from "class-validator"; import rs from "randomstring"; import { InferAttributes, InferCreationAttributes } from "sequelize"; import { @@ -11,14 +25,6 @@ import { BeforeCreate, AllowNull, } from "sequelize-typescript"; -import { OAuthClientValidation } from "@shared/validations"; -import Team from "@server/models/Team"; -import User from "@server/models/User"; -import ParanoidModel from "@server/models/base/ParanoidModel"; -import Encrypted from "@server/models/decorators/Encrypted"; -import Fix from "@server/models/decorators/Fix"; -import IsUrlOrRelativePath from "@server/models/validators/IsUrlOrRelativePath"; -import NotContainsUrl from "@server/models/validators/NotContainsUrl"; @Table({ tableName: "oauth_clients", @@ -29,7 +35,7 @@ class OAuthClient extends ParanoidModel< InferAttributes, Partial> > { - public static secretPrefix = "ol_secret_"; + public static clientSecretPrefix = "ol_sk_"; @NotContainsUrl @Length({ max: OAuthClientValidation.maxNameLength }) @@ -72,7 +78,8 @@ class OAuthClient extends ParanoidModel< @ArrayNotEmpty() @ArrayUnique() - @Length({ max: OAuthClientValidation.maxRedirectUriLength }) + @ArrayMinSize(1) + @ArrayMaxSize(10) @IsUrl( { require_tld: false, @@ -106,10 +113,11 @@ class OAuthClient extends ParanoidModel< @BeforeCreate public static async generateCredentials(model: OAuthClient) { model.clientId = rs.generate({ - length: 32, + length: 20, charset: "alphanumeric", + capitalization: "lowercase", }); - model.clientSecret = `${OAuthClient.secretPrefix}${rs.generate(32)}`; + model.clientSecret = `${OAuthClient.clientSecretPrefix}${rs.generate(32)}`; } } diff --git a/plugins/oauth/server/oauth.ts b/plugins/oauth/server/oauth.ts index 7da880a2e2..dfe78e23d8 100644 --- a/plugins/oauth/server/oauth.ts +++ b/plugins/oauth/server/oauth.ts @@ -1,8 +1,8 @@ import OAuth2Server from "@node-oauth/oauth2-server"; +import auth from "@server/middlewares/authentication"; import Koa from "koa"; import bodyParser from "koa-body"; import Router from "koa-router"; -import auth from "@server/middlewares/authentication"; import { OAuthInterface } from "./interface"; const app = new Koa(); @@ -17,7 +17,14 @@ router.post("/authorize", auth(), async (ctx) => { const request = new OAuth2Server.Request(ctx.request); const response = new OAuth2Server.Response(ctx.response); - const authorizationCode = await oauth.authorize(request, response); + const authorizationCode = await oauth.authorize(request, response, { + authenticateHandler: { + handle: async () => { + const { user } = ctx.state.auth; + return user; + }, + }, + }); ctx.body = { code: authorizationCode }; }); diff --git a/plugins/oauth/server/presenters/oauthClient.ts b/plugins/oauth/server/presenters/oauthClient.ts new file mode 100644 index 0000000000..48dbb72928 --- /dev/null +++ b/plugins/oauth/server/presenters/oauthClient.ts @@ -0,0 +1,18 @@ +import OAuthClient from "../models/OAuthClient"; + +export default function presentOAuthClient(apiKey: OAuthClient) { + return { + id: apiKey.id, + name: apiKey.name, + description: apiKey.description, + developerName: apiKey.developerName, + developerUrl: apiKey.developerUrl, + avatarUrl: apiKey.avatarUrl, + clientId: apiKey.clientId, + clientSecret: apiKey.clientSecret, + redirectUris: apiKey.redirectUris, + published: apiKey.published, + createdAt: apiKey.createdAt, + updatedAt: apiKey.updatedAt, + }; +}