Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/service/json-rpc-verification-client.ts
2500 views
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
} from "@gitpod/public-api/lib/gitpod/v1/verification_pb";
16
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
17
import { getGitpodService } from "./service";
18
import { validate as uuidValidate } from "uuid";
19
20
export class JsonRpcVerificationClient implements PromiseClient<typeof VerificationService> {
21
async sendPhoneNumberVerificationToken(
22
request: PartialMessage<SendPhoneNumberVerificationTokenRequest>,
23
_options?: CallOptions | undefined,
24
): Promise<SendPhoneNumberVerificationTokenResponse> {
25
if (!request.phoneNumber) {
26
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "phoneNumber is required");
27
}
28
const info = await getGitpodService().server.sendPhoneNumberVerificationToken(request.phoneNumber);
29
return new SendPhoneNumberVerificationTokenResponse({
30
verificationId: info.verificationId,
31
});
32
}
33
34
async verifyPhoneNumberVerificationToken(
35
request: PartialMessage<VerifyPhoneNumberVerificationTokenRequest>,
36
_options?: CallOptions | undefined,
37
): Promise<VerifyPhoneNumberVerificationTokenResponse> {
38
if (!request.phoneNumber) {
39
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "phoneNumber is required");
40
}
41
if (!request.verificationId || !uuidValidate(request.verificationId)) {
42
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "verificationId is required");
43
}
44
if (!request.token) {
45
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "token is required");
46
}
47
const info = await getGitpodService().server.verifyPhoneNumberVerificationToken(
48
request.phoneNumber,
49
request.token,
50
request.verificationId,
51
);
52
return new VerifyPhoneNumberVerificationTokenResponse({
53
verified: info,
54
});
55
}
56
}
57
58