From b9a1522abc882b5671a44a143762793c93739a9c Mon Sep 17 00:00:00 2001 From: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Date: Thu, 8 Aug 2024 15:45:22 +0300 Subject: [PATCH] chore: oAuth Client Guard apv2 (#16130) * chore: oAuh Client Guard apv2 * fixup! chore: oAuh Client Guard apv2 * fixup! Merge branch 'oauth-client-guard-' of github.com:calcom/cal.com into oauth-client-guard- * fixup! fixup! Merge branch 'oauth-client-guard-' of github.com:calcom/cal.com into oauth-client-guard- * fixup! fixup! fixup! Merge branch 'oauth-client-guard-' of github.com:calcom/cal.com into oauth-client-guard- --- .../oauth-client-users.controller.e2e-spec.ts | 11 +++++- .../oauth-client-users.controller.ts | 3 +- .../oauth-clients/oauth-clients.controller.ts | 5 +++ .../oauth-flow.controller.e2e-spec.ts | 1 + .../guards/oauth-client-guard.ts | 38 +++++++++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 apps/api/v2/src/modules/oauth-clients/guards/oauth-client-guard.ts diff --git a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts index 9fac542ed5..4665fa2896 100644 --- a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts +++ b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.e2e-spec.ts @@ -112,9 +112,16 @@ describe("OAuth Client Users Endpoints", () => { await profilesRepositoryFixture.create({ uid: "asd-asd", - username: userEmail, - user: { connect: { id: platformAdmin.id } }, + username: platformAdminEmail, organization: { connect: { id: organization.id } }, + movedFromUser: { + connect: { + id: platformAdmin.id, + }, + }, + user: { + connect: { id: platformAdmin.id }, + }, }); await app.init(); diff --git a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts index 9bfed76a9e..b81124117b 100644 --- a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts +++ b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/oauth-client-users.controller.ts @@ -6,6 +6,7 @@ import { GetManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth- import { GetManagedUsersOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/get-managed-users.output"; import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-client-users/outputs/managed-user.output"; import { KeysResponseDto } from "@/modules/oauth-clients/controllers/oauth-flow/responses/KeysResponse.dto"; +import { OAuthClientGuard } from "@/modules/oauth-clients/guards/oauth-client-guard"; import { OAuthClientRepository } from "@/modules/oauth-clients/oauth-client.repository"; import { OAuthClientUsersService } from "@/modules/oauth-clients/services/oauth-clients-users.service"; import { TokensRepository } from "@/modules/tokens/tokens.repository"; @@ -37,7 +38,7 @@ import { Pagination } from "@calcom/platform-types"; path: "/v2/oauth-clients/:clientId/users", version: API_VERSIONS_VALUES, }) -@UseGuards(ApiAuthGuard) +@UseGuards(ApiAuthGuard, OAuthClientGuard) @DocsTags("Managed users") export class OAuthClientUsersController { private readonly logger = new Logger("UserController"); diff --git a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-clients/oauth-clients.controller.ts b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-clients/oauth-clients.controller.ts index f80dccf14a..8521a2b342 100644 --- a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-clients/oauth-clients.controller.ts +++ b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-clients/oauth-clients.controller.ts @@ -9,6 +9,7 @@ import { ManagedUserOutput } from "@/modules/oauth-clients/controllers/oauth-cli import { CreateOAuthClientResponseDto } from "@/modules/oauth-clients/controllers/oauth-clients/responses/CreateOAuthClientResponse.dto"; import { GetOAuthClientResponseDto } from "@/modules/oauth-clients/controllers/oauth-clients/responses/GetOAuthClientResponse.dto"; import { GetOAuthClientsResponseDto } from "@/modules/oauth-clients/controllers/oauth-clients/responses/GetOAuthClientsResponse.dto"; +import { OAuthClientGuard } from "@/modules/oauth-clients/guards/oauth-client-guard"; import { UpdateOAuthClientInput } from "@/modules/oauth-clients/inputs/update-oauth-client.input"; import { OAuthClientRepository } from "@/modules/oauth-clients/oauth-client.repository"; import { OrganizationsRepository } from "@/modules/organizations/organizations.repository"; @@ -109,6 +110,7 @@ export class OAuthClientsController { @HttpCode(HttpStatus.OK) @MembershipRoles([MembershipRole.ADMIN, MembershipRole.OWNER, MembershipRole.MEMBER]) @DocsOperation({ description: AUTH_DOCUMENTATION }) + @UseGuards(OAuthClientGuard) async getOAuthClientById(@Param("clientId") clientId: string): Promise { const client = await this.oauthClientRepository.getOAuthClient(clientId); if (!client) { @@ -121,6 +123,7 @@ export class OAuthClientsController { @HttpCode(HttpStatus.OK) @MembershipRoles([MembershipRole.ADMIN, MembershipRole.OWNER, MembershipRole.MEMBER]) @DocsOperation({ description: AUTH_DOCUMENTATION }) + @UseGuards(OAuthClientGuard) async getOAuthClientManagedUsersById( @Param("clientId") clientId: string, @Query() queryParams: Pagination @@ -139,6 +142,7 @@ export class OAuthClientsController { @HttpCode(HttpStatus.OK) @MembershipRoles([MembershipRole.ADMIN, MembershipRole.OWNER]) @DocsOperation({ description: AUTH_DOCUMENTATION }) + @UseGuards(OAuthClientGuard) async updateOAuthClient( @Param("clientId") clientId: string, @Body() body: UpdateOAuthClientInput @@ -152,6 +156,7 @@ export class OAuthClientsController { @HttpCode(HttpStatus.OK) @MembershipRoles([MembershipRole.ADMIN, MembershipRole.OWNER]) @DocsOperation({ description: AUTH_DOCUMENTATION }) + @UseGuards(OAuthClientGuard) async deleteOAuthClient(@Param("clientId") clientId: string): Promise { this.logger.log(`Deleting OAuth Client with ID: ${clientId}`); const client = await this.oauthClientRepository.deleteOAuthClient(clientId); diff --git a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-flow/oauth-flow.controller.e2e-spec.ts b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-flow/oauth-flow.controller.e2e-spec.ts index 8f4574d1e2..ee3adfa09d 100644 --- a/apps/api/v2/src/modules/oauth-clients/controllers/oauth-flow/oauth-flow.controller.e2e-spec.ts +++ b/apps/api/v2/src/modules/oauth-clients/controllers/oauth-flow/oauth-flow.controller.e2e-spec.ts @@ -97,6 +97,7 @@ describe("OAuthFlow Endpoints", () => { uid: "asd-asd", username: userEmail, user: { connect: { id: user.id } }, + movedFromUser: { connect: { id: user.id } }, organization: { connect: { id: organization.id } }, }); oAuthClient = await createOAuthClient(organization.id); diff --git a/apps/api/v2/src/modules/oauth-clients/guards/oauth-client-guard.ts b/apps/api/v2/src/modules/oauth-clients/guards/oauth-client-guard.ts new file mode 100644 index 0000000000..029af7f5d1 --- /dev/null +++ b/apps/api/v2/src/modules/oauth-clients/guards/oauth-client-guard.ts @@ -0,0 +1,38 @@ +import { GetUserReturnType } from "@/modules/auth/decorators/get-user/get-user.decorator"; +import { OAuthClientRepository } from "@/modules/oauth-clients/oauth-client.repository"; +import { + Injectable, + CanActivate, + ExecutionContext, + ForbiddenException, + NotFoundException, +} from "@nestjs/common"; +import { Request } from "express"; + +@Injectable() +export class OAuthClientGuard implements CanActivate { + constructor(private oAuthClientRepository: OAuthClientRepository) {} + + async canActivate(context: ExecutionContext): Promise { + const request = context.switchToHttp().getRequest(); + const user: GetUserReturnType = request.user; + const organizationId = user?.movedToProfile?.organizationId || user?.organizationId; + const oAuthClientId = request.params.clientId; + + if (!oAuthClientId) { + throw new ForbiddenException("No OAuth client associated with the request."); + } + + if (!user || !organizationId) { + throw new ForbiddenException("No organization associated with the user."); + } + + const oAuthClient = await this.oAuthClientRepository.getOAuthClient(oAuthClientId); + + if (!oAuthClient) { + throw new NotFoundException("OAuth client not found."); + } + + return Boolean(user.isSystemAdmin || oAuthClient.organizationId === organizationId); + } +}