mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 08:43:33 +00:00
* Rename service-factory folder * Move cryptographic service factories * Move crypto models * Move crypto services * Move domain base class * Platform code owners * Move desktop log services * Move log files * Establish component library ownership * Move background listeners * Move background background * Move localization to Platform * Move browser alarms to Platform * Move browser state to Platform * Move CLI state to Platform * Move Desktop native concerns to Platform * Move flag and misc to Platform * Lint fixes * Move electron state to platform * Move web state to Platform * Move lib state to Platform * Fix broken tests * Rename interface to idiomatic TS * `npm run prettier` 🤖 * Resolve review feedback * Set platform as owners of web core and shared * Expand moved services * Fix test types --------- Co-authored-by: Hinton <hinton@users.noreply.github.com>
89 lines
3.4 KiB
TypeScript
89 lines
3.4 KiB
TypeScript
import { UserVerificationApiServiceAbstraction } from "../../../abstractions/userVerification/userVerification-api.service.abstraction";
|
|
import { UserVerificationService as UserVerificationServiceAbstraction } from "../../../abstractions/userVerification/userVerification.service.abstraction";
|
|
import { CryptoService } from "../../../platform/abstractions/crypto.service";
|
|
import { I18nService } from "../../../platform/abstractions/i18n.service";
|
|
import { Verification } from "../../../types/verification";
|
|
import { VerificationType } from "../../enums/verification-type";
|
|
import { SecretVerificationRequest } from "../../models/request/secret-verification.request";
|
|
import { VerifyOTPRequest } from "../../models/request/verify-otp.request";
|
|
|
|
/**
|
|
* Used for general-purpose user verification throughout the app.
|
|
* Use it to verify the input collected by UserVerificationComponent.
|
|
*/
|
|
export class UserVerificationService implements UserVerificationServiceAbstraction {
|
|
constructor(
|
|
private cryptoService: CryptoService,
|
|
private i18nService: I18nService,
|
|
private userVerificationApiService: UserVerificationApiServiceAbstraction
|
|
) {}
|
|
|
|
/**
|
|
* Create a new request model to be used for server-side verification
|
|
* @param verification User-supplied verification data (Master Password or OTP)
|
|
* @param requestClass The request model to create
|
|
* @param alreadyHashed Whether the master password is already hashed
|
|
*/
|
|
async buildRequest<T extends SecretVerificationRequest>(
|
|
verification: Verification,
|
|
requestClass?: new () => T,
|
|
alreadyHashed?: boolean
|
|
) {
|
|
this.validateInput(verification);
|
|
|
|
const request =
|
|
requestClass != null ? new requestClass() : (new SecretVerificationRequest() as T);
|
|
|
|
if (verification.type === VerificationType.OTP) {
|
|
request.otp = verification.secret;
|
|
} else {
|
|
request.masterPasswordHash = alreadyHashed
|
|
? verification.secret
|
|
: await this.cryptoService.hashPassword(verification.secret, null);
|
|
}
|
|
|
|
return request;
|
|
}
|
|
|
|
/**
|
|
* Used to verify the Master Password client-side, or send the OTP to the server for verification (with no other data)
|
|
* Generally used for client-side verification only.
|
|
* @param verification User-supplied verification data (Master Password or OTP)
|
|
*/
|
|
async verifyUser(verification: Verification): Promise<boolean> {
|
|
this.validateInput(verification);
|
|
|
|
if (verification.type === VerificationType.OTP) {
|
|
const request = new VerifyOTPRequest(verification.secret);
|
|
try {
|
|
await this.userVerificationApiService.postAccountVerifyOTP(request);
|
|
} catch (e) {
|
|
throw new Error(this.i18nService.t("invalidVerificationCode"));
|
|
}
|
|
} else {
|
|
const passwordValid = await this.cryptoService.compareAndUpdateKeyHash(
|
|
verification.secret,
|
|
null
|
|
);
|
|
if (!passwordValid) {
|
|
throw new Error(this.i18nService.t("invalidMasterPassword"));
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
async requestOTP() {
|
|
await this.userVerificationApiService.postAccountRequestOTP();
|
|
}
|
|
|
|
private validateInput(verification: Verification) {
|
|
if (verification?.secret == null || verification.secret === "") {
|
|
if (verification.type === VerificationType.OTP) {
|
|
throw new Error(this.i18nService.t("verificationCodeRequired"));
|
|
} else {
|
|
throw new Error(this.i18nService.t("masterPasswordRequired"));
|
|
}
|
|
}
|
|
}
|
|
}
|