|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { CallOptions, PromiseClient } from "@connectrpc/connect"; |
| 8 | +import { PartialMessage } from "@bufbuild/protobuf"; |
| 9 | +import { VerificationService } from "@gitpod/public-api/lib/gitpod/v1/verification_connect"; |
| 10 | +import { |
| 11 | + SendPhoneNumberVerificationTokenRequest, |
| 12 | + SendPhoneNumberVerificationTokenResponse, |
| 13 | + VerifyPhoneNumberVerificationTokenRequest, |
| 14 | + VerifyPhoneNumberVerificationTokenResponse, |
| 15 | + ListBlockedRepositoriesRequest, |
| 16 | + ListBlockedRepositoriesResponse, |
| 17 | + CreateBlockedRepositoryRequest, |
| 18 | + CreateBlockedRepositoryResponse, |
| 19 | + DeleteBlockedRepositoryRequest, |
| 20 | + DeleteBlockedRepositoryResponse, |
| 21 | + ListBlockedEmailDomainsRequest, |
| 22 | + ListBlockedEmailDomainsResponse, |
| 23 | + CreateBlockedEmailDomainRequest, |
| 24 | + CreateBlockedEmailDomainResponse, |
| 25 | +} from "@gitpod/public-api/lib/gitpod/v1/verification_pb"; |
| 26 | +import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; |
| 27 | +import { getGitpodService } from "./service"; |
| 28 | +import { validate as uuidValidate } from "uuid"; |
| 29 | +import { converter } from "./public-api"; |
| 30 | +import { PaginationResponse } from "@gitpod/public-api/lib/gitpod/v1/pagination_pb"; |
| 31 | + |
| 32 | +export class JsonRpcVerificationClient implements PromiseClient<typeof VerificationService> { |
| 33 | + async sendPhoneNumberVerificationToken( |
| 34 | + request: PartialMessage<SendPhoneNumberVerificationTokenRequest>, |
| 35 | + _options?: CallOptions | undefined, |
| 36 | + ): Promise<SendPhoneNumberVerificationTokenResponse> { |
| 37 | + if (!request.phoneNumber) { |
| 38 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "phoneNumber is required"); |
| 39 | + } |
| 40 | + const info = await getGitpodService().server.sendPhoneNumberVerificationToken(request.phoneNumber); |
| 41 | + return new SendPhoneNumberVerificationTokenResponse({ |
| 42 | + verificationId: info.verificationId, |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + async verifyPhoneNumberVerificationToken( |
| 47 | + request: PartialMessage<VerifyPhoneNumberVerificationTokenRequest>, |
| 48 | + _options?: CallOptions | undefined, |
| 49 | + ): Promise<VerifyPhoneNumberVerificationTokenResponse> { |
| 50 | + if (!request.phoneNumber) { |
| 51 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "phoneNumber is required"); |
| 52 | + } |
| 53 | + if (!request.verificationId || !uuidValidate(request.verificationId)) { |
| 54 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "verificationId is required"); |
| 55 | + } |
| 56 | + if (!request.token) { |
| 57 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "token is required"); |
| 58 | + } |
| 59 | + const info = await getGitpodService().server.verifyPhoneNumberVerificationToken( |
| 60 | + request.phoneNumber, |
| 61 | + request.token, |
| 62 | + request.verificationId, |
| 63 | + ); |
| 64 | + return new VerifyPhoneNumberVerificationTokenResponse({ |
| 65 | + verified: info, |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + async listBlockedRepositories( |
| 70 | + request: PartialMessage<ListBlockedRepositoriesRequest>, |
| 71 | + _options?: CallOptions | undefined, |
| 72 | + ): Promise<ListBlockedRepositoriesResponse> { |
| 73 | + // dashboard params is constant, no need to implement |
| 74 | + const info = await getGitpodService().server.adminGetBlockedRepositories({ |
| 75 | + limit: 100, |
| 76 | + offset: 0, |
| 77 | + orderBy: "urlRegexp", |
| 78 | + orderDir: "asc", |
| 79 | + searchTerm: request.searchTerm, |
| 80 | + }); |
| 81 | + return new ListBlockedRepositoriesResponse({ |
| 82 | + blockedRepositories: info.rows.map((item) => converter.toBlockedRepository(item)), |
| 83 | + pagination: new PaginationResponse(), |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + async createBlockedRepository( |
| 88 | + request: PartialMessage<CreateBlockedRepositoryRequest>, |
| 89 | + _options?: CallOptions | undefined, |
| 90 | + ): Promise<CreateBlockedRepositoryResponse> { |
| 91 | + if (!request.urlRegexp) { |
| 92 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "urlRegexp is required"); |
| 93 | + } |
| 94 | + if (!request.blockUser) { |
| 95 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "blockUser is required"); |
| 96 | + } |
| 97 | + const info = await getGitpodService().server.adminCreateBlockedRepository(request.urlRegexp, request.blockUser); |
| 98 | + return new CreateBlockedRepositoryResponse({ |
| 99 | + blockedRepository: converter.toBlockedRepository(info), |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + async deleteBlockedRepository( |
| 104 | + request: PartialMessage<DeleteBlockedRepositoryRequest>, |
| 105 | + _options?: CallOptions | undefined, |
| 106 | + ): Promise<DeleteBlockedRepositoryResponse> { |
| 107 | + if (!request.blockedRepositoryId) { |
| 108 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "blockedRepositoryId is required"); |
| 109 | + } |
| 110 | + await getGitpodService().server.adminDeleteBlockedRepository(request.blockedRepositoryId); |
| 111 | + return new DeleteBlockedRepositoryResponse(); |
| 112 | + } |
| 113 | + |
| 114 | + async listBlockedEmailDomains( |
| 115 | + request: PartialMessage<ListBlockedEmailDomainsRequest>, |
| 116 | + _options?: CallOptions | undefined, |
| 117 | + ): Promise<ListBlockedEmailDomainsResponse> { |
| 118 | + const info = await getGitpodService().server.adminGetBlockedEmailDomains(); |
| 119 | + return new ListBlockedEmailDomainsResponse({ |
| 120 | + blockedEmailDomains: info.map((item) => converter.toBlockedEmailDomain(item)), |
| 121 | + pagination: new PaginationResponse(), |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + async createBlockedEmailDomain( |
| 126 | + request: PartialMessage<CreateBlockedEmailDomainRequest>, |
| 127 | + _options?: CallOptions | undefined, |
| 128 | + ): Promise<CreateBlockedEmailDomainResponse> { |
| 129 | + if (!request.domain) { |
| 130 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "domain is required"); |
| 131 | + } |
| 132 | + if (request.negative === undefined) { |
| 133 | + throw new ApplicationError(ErrorCodes.BAD_REQUEST, "negative is required"); |
| 134 | + } |
| 135 | + await getGitpodService().server.adminSaveBlockedEmailDomain({ |
| 136 | + domain: request.domain, |
| 137 | + negative: request.negative, |
| 138 | + }); |
| 139 | + // There's no way to get blockedEmailDomain, just ignore it since dashboard don't care about the response data |
| 140 | + return new CreateBlockedEmailDomainResponse({}); |
| 141 | + } |
| 142 | +} |
0 commit comments