-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
969cb86
commit 6d50167
Showing
12 changed files
with
209 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { User } from "@gitpod/gitpod-protocol"; | ||
import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; | ||
import { inject, injectable, postConstruct } from "inversify"; | ||
import { Config } from "../config"; | ||
import { Twilio } from "twilio"; | ||
import { ServiceContext } from "twilio/lib/rest/verify/v2/service"; | ||
import { WorkspaceDB } from "@gitpod/gitpod-db/lib"; | ||
|
||
@injectable() | ||
export class PhoneVerificationService { | ||
@inject(Config) protected config: Config; | ||
@inject(WorkspaceDB) protected workspaceDB: WorkspaceDB; | ||
|
||
protected verifyService: ServiceContext; | ||
|
||
@postConstruct() | ||
protected initialize(): void { | ||
if (this.config.phoneVerification) { | ||
const client = new Twilio( | ||
this.config.phoneVerification.accountSID, | ||
this.config.phoneVerification.authToken, | ||
); | ||
this.verifyService = client.verify.v2.services(this.config.phoneVerification.serviceName); | ||
} | ||
} | ||
|
||
public async needsVerification(user: User): Promise<boolean> { | ||
if (!this.config.phoneVerification) { | ||
return false; | ||
} | ||
if (!!user.additionalData?.profile?.verifiedPhoneNumber) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public async sendVerificationToken(phoneNumber: string): Promise<void> { | ||
if (!this.verifyService) { | ||
throw new Error("No verification service configured."); | ||
} | ||
const verification = await this.verifyService.verifications.create({ to: phoneNumber, channel: "sms" }); | ||
log.info("Verification code sent", { phoneNumber, status: verification.status }); | ||
} | ||
|
||
public async verifyVerificationToken(phoneNumber: string, oneTimePassword: string): Promise<boolean> { | ||
if (!this.verifyService) { | ||
throw new Error("No verification service configured."); | ||
} | ||
const verification_check = await this.verifyService.verificationChecks.create({ | ||
to: phoneNumber, | ||
code: oneTimePassword, | ||
}); | ||
return verification_check.status === "approved"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.