diff --git a/plugins/webhooks/server/api/webhookSubscriptions.ts b/plugins/webhooks/server/api/webhookSubscriptions.ts index 00f3da7aaf..7be68a0fa5 100644 --- a/plugins/webhooks/server/api/webhookSubscriptions.ts +++ b/plugins/webhooks/server/api/webhookSubscriptions.ts @@ -8,7 +8,7 @@ import validate from "@server/middlewares/validate"; import { WebhookSubscription } from "@server/models"; import { authorize } from "@server/policies"; import pagination from "@server/routes/api/middlewares/pagination"; -import { APIContext } from "@server/types"; +import { APIContext, AuthenticationType } from "@server/types"; import presentWebhookSubscription from "../presenters/webhookSubscription"; import * as T from "./schema"; @@ -41,7 +41,10 @@ router.post( router.post( "webhookSubscriptions.create", - auth({ role: UserRole.Admin }), + auth({ + role: UserRole.Admin, + type: [AuthenticationType.API, AuthenticationType.APP], + }), validate(T.WebhookSubscriptionsCreateSchema), transaction(), async (ctx: APIContext) => { @@ -68,7 +71,10 @@ router.post( router.post( "webhookSubscriptions.delete", - auth({ role: UserRole.Admin }), + auth({ + role: UserRole.Admin, + type: [AuthenticationType.API, AuthenticationType.APP], + }), validate(T.WebhookSubscriptionsDeleteSchema), transaction(), async (ctx: APIContext) => { @@ -94,7 +100,10 @@ router.post( router.post( "webhookSubscriptions.update", - auth({ role: UserRole.Admin }), + auth({ + role: UserRole.Admin, + type: [AuthenticationType.API, AuthenticationType.APP], + }), validate(T.WebhookSubscriptionsUpdateSchema), transaction(), async (ctx: APIContext) => { diff --git a/server/middlewares/authentication.test.ts b/server/middlewares/authentication.test.ts index 273f540a30..e5cd020c33 100644 --- a/server/middlewares/authentication.test.ts +++ b/server/middlewares/authentication.test.ts @@ -8,6 +8,7 @@ import { buildApiKey, buildOAuthAuthentication, } from "@server/test/factories"; +import { AuthenticationType } from "@server/types"; import auth from "./authentication"; describe("Authentication middleware", () => { @@ -51,6 +52,30 @@ describe("Authentication middleware", () => { expect(e.message).toBe("Invalid token"); } }); + + it("should return error if AuthenticationType mismatches", async () => { + const state = {} as DefaultState; + const user = await buildUser(); + const authMiddleware = auth({ + type: AuthenticationType.API, + }); + + try { + await authMiddleware( + { + // @ts-expect-error mock request + request: { + get: jest.fn(() => `Bearer ${user.getJwtToken()}`), + }, + state, + cache: {}, + }, + jest.fn() + ); + } catch (e) { + expect(e.message).toBe("Invalid authentication type"); + } + }); }); describe("with API key", () => { diff --git a/server/middlewares/authentication.ts b/server/middlewares/authentication.ts index 9c5d17202c..b8a8588b33 100644 --- a/server/middlewares/authentication.ts +++ b/server/middlewares/authentication.ts @@ -20,7 +20,7 @@ type AuthenticationOptions = { /** Role requuired to access the route. */ role?: UserRole; /** Type of authentication required to access the route. */ - type?: AuthenticationType; + type?: AuthenticationType | AuthenticationType[]; /** Authentication is parsed, but optional. */ optional?: boolean; }; @@ -168,7 +168,12 @@ export default function auth(options: AuthenticationOptions = {}) { throw AuthorizationError(`${capitalize(options.role)} role required`); } - if (options.type && type !== options.type) { + if ( + options.type && + (Array.isArray(options.type) + ? !options.type.includes(type) + : type !== options.type) + ) { throw AuthorizationError(`Invalid authentication type`); } diff --git a/server/routes/api/apiKeys/apiKeys.ts b/server/routes/api/apiKeys/apiKeys.ts index a1ae85db68..c9a551261e 100644 --- a/server/routes/api/apiKeys/apiKeys.ts +++ b/server/routes/api/apiKeys/apiKeys.ts @@ -15,7 +15,10 @@ const router = new Router(); router.post( "apiKeys.create", - auth({ role: UserRole.Member, type: AuthenticationType.APP }), + auth({ + role: UserRole.Member, + type: AuthenticationType.APP, + }), validate(T.APIKeysCreateSchema), transaction(), async (ctx: APIContext) => { @@ -90,7 +93,10 @@ router.post( router.post( "apiKeys.delete", - auth({ role: UserRole.Member }), + auth({ + role: UserRole.Member, + type: AuthenticationType.APP, + }), validate(T.APIKeysDeleteSchema), transaction(), async (ctx: APIContext) => {