2021-11-09 17:01:22 +01:00
|
|
|
import { UserVerificationService as UserVerificationServiceAbstraction } from '../abstractions/userVerification.service';
|
|
|
|
|
|
|
|
import { ApiService } from '../abstractions/api.service';
|
|
|
|
import { CryptoService } from '../abstractions/crypto.service';
|
|
|
|
import { I18nService } from '../abstractions/i18n.service';
|
|
|
|
|
|
|
|
import { VerificationType } from '../enums/verificationType';
|
|
|
|
|
|
|
|
import { VerifyOTPRequest } from '../models/request/account/verifyOTPRequest';
|
|
|
|
import { SecretVerificationRequest } from '../models/request/secretVerificationRequest';
|
|
|
|
|
|
|
|
import { Verification } from '../types/verification';
|
|
|
|
|
|
|
|
export class UserVerificationService implements UserVerificationServiceAbstraction {
|
|
|
|
constructor(private cryptoService: CryptoService, private i18nService: I18nService,
|
2021-11-15 22:53:57 +01:00
|
|
|
private apiService: ApiService) { }
|
2021-11-09 17:01:22 +01:00
|
|
|
|
|
|
|
async buildRequest<T extends SecretVerificationRequest>(verification: Verification,
|
|
|
|
requestClass?: new () => T, alreadyHashed?: boolean) {
|
2021-11-15 22:53:57 +01:00
|
|
|
this.validateInput(verification);
|
2021-11-09 17:01:22 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
async verifyUser(verification: Verification): Promise<boolean> {
|
2021-11-15 22:53:57 +01:00
|
|
|
this.validateInput(verification);
|
2021-11-09 17:01:22 +01:00
|
|
|
|
|
|
|
if (verification.type === VerificationType.OTP) {
|
|
|
|
const request = new VerifyOTPRequest(verification.secret);
|
|
|
|
try {
|
|
|
|
await this.apiService.postAccountVerifyOTP(request);
|
|
|
|
} catch (e) {
|
2021-11-15 22:53:57 +01:00
|
|
|
throw new Error(this.i18nService.t('invalidVerificationCode'));
|
2021-11-09 17:01:22 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const passwordValid = await this.cryptoService.compareAndUpdateKeyHash(verification.secret, null);
|
|
|
|
if (!passwordValid) {
|
2021-11-15 22:53:57 +01:00
|
|
|
throw new Error(this.i18nService.t('invalidMasterPassword'));
|
2021-11-09 17:01:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-11-15 22:53:57 +01:00
|
|
|
|
|
|
|
async requestOTP() {
|
|
|
|
await this.apiService.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('masterPassRequired'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 17:01:22 +01:00
|
|
|
}
|