Path: blob/main/components/dashboard/src/service/json-rpc-authprovider-client.ts
2500 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { PartialMessage } from "@bufbuild/protobuf";7import { PromiseClient } from "@connectrpc/connect";8import { AuthProviderService } from "@gitpod/public-api/lib/gitpod/v1/authprovider_connect";9import {10CreateAuthProviderRequest,11CreateAuthProviderResponse,12DeleteAuthProviderRequest,13DeleteAuthProviderResponse,14GetAuthProviderRequest,15GetAuthProviderResponse,16ListAuthProviderDescriptionsRequest,17ListAuthProviderDescriptionsResponse,18ListAuthProvidersRequest,19ListAuthProvidersResponse,20UpdateAuthProviderRequest,21UpdateAuthProviderResponse,22} from "@gitpod/public-api/lib/gitpod/v1/authprovider_pb";23import { converter } from "./public-api";24import { getGitpodService } from "./service";25import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";2627export class JsonRpcAuthProviderClient implements PromiseClient<typeof AuthProviderService> {28async createAuthProvider(request: PartialMessage<CreateAuthProviderRequest>): Promise<CreateAuthProviderResponse> {29const ownerId = request.owner?.case === "ownerId" ? request.owner.value : undefined;30const organizationId = request.owner?.case === "organizationId" ? request.owner.value : undefined;3132if (!organizationId && !ownerId) {33throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId or ownerId is required");34}35if (!request.type) {36throw new ApplicationError(ErrorCodes.BAD_REQUEST, "type is required");37}38if (!request.host) {39throw new ApplicationError(ErrorCodes.BAD_REQUEST, "host is required");40}4142if (organizationId) {43const result = await getGitpodService().server.createOrgAuthProvider({44entry: {45organizationId,46host: request.host,47type: converter.fromAuthProviderType(request.type),48clientId: request.oauth2Config?.clientId,49clientSecret: request.oauth2Config?.clientSecret,50},51});52return new CreateAuthProviderResponse({ authProvider: converter.toAuthProvider(result) });53}54if (ownerId) {55const result = await getGitpodService().server.updateOwnAuthProvider({56entry: {57host: request.host,58ownerId,59type: converter.fromAuthProviderType(request.type),60clientId: request.oauth2Config?.clientId,61clientSecret: request.oauth2Config?.clientSecret,62},63});64return new CreateAuthProviderResponse({ authProvider: converter.toAuthProvider(result) });65}6667throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId or ownerId is required");68}6970async getAuthProvider(request: PartialMessage<GetAuthProviderRequest>): Promise<GetAuthProviderResponse> {71if (!request.authProviderId) {72throw new ApplicationError(ErrorCodes.BAD_REQUEST, "authProviderId is required");73}7475const provider = await getGitpodService().server.getAuthProvider(request.authProviderId);76return new GetAuthProviderResponse({77authProvider: converter.toAuthProvider(provider),78});79}8081async listAuthProviders(request: PartialMessage<ListAuthProvidersRequest>): Promise<ListAuthProvidersResponse> {82if (!request.id?.case) {83throw new ApplicationError(ErrorCodes.BAD_REQUEST, "id is required");84}85const organizationId = request.id.case === "organizationId" ? request.id.value : undefined;86const userId = request.id.case === "userId" ? request.id.value : undefined;8788if (!organizationId && !userId) {89throw new ApplicationError(ErrorCodes.BAD_REQUEST, "organizationId or userId is required");90}9192const authProviders = !!organizationId93? await getGitpodService().server.getOrgAuthProviders({94organizationId,95})96: await getGitpodService().server.getOwnAuthProviders();97const response = new ListAuthProvidersResponse({98authProviders: authProviders.map(converter.toAuthProvider.bind(converter)),99});100return response;101}102103async listAuthProviderDescriptions(104request: PartialMessage<ListAuthProviderDescriptionsRequest>,105): Promise<ListAuthProviderDescriptionsResponse> {106const aps = await getGitpodService().server.getAuthProviders();107return new ListAuthProviderDescriptionsResponse({108descriptions: aps.map((ap) => converter.toAuthProviderDescription(ap)),109});110}111112async updateAuthProvider(request: PartialMessage<UpdateAuthProviderRequest>): Promise<UpdateAuthProviderResponse> {113if (!request.authProviderId) {114throw new ApplicationError(ErrorCodes.BAD_REQUEST, "authProviderId is required");115}116const clientId = request?.clientId || "";117const clientSecret = request?.clientSecret || "";118if (!clientId && !clientSecret) {119throw new ApplicationError(ErrorCodes.BAD_REQUEST, "clientId or clientSecret are required");120}121122const entry = await getGitpodService().server.updateAuthProvider(request.authProviderId, {123clientId,124clientSecret,125});126return new UpdateAuthProviderResponse({127authProvider: converter.toAuthProvider(entry),128});129}130131async deleteAuthProvider(request: PartialMessage<DeleteAuthProviderRequest>): Promise<DeleteAuthProviderResponse> {132if (!request.authProviderId) {133throw new ApplicationError(ErrorCodes.BAD_REQUEST, "authProviderId is required");134}135await getGitpodService().server.deleteAuthProvider(request.authProviderId);136return new DeleteAuthProviderResponse();137}138}139140141