bitwarden-estensione-browser/apps/web/src/app/settings/two-factor-yubikey.componen...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
3.3 KiB
TypeScript
Raw Normal View History

import { Component } from "@angular/core";
2018-06-27 19:45:11 +02:00
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification.service";
import { TwoFactorProviderType } from "@bitwarden/common/enums/twoFactorProviderType";
import { UpdateTwoFactorYubioOtpRequest } from "@bitwarden/common/models/request/updateTwoFactorYubioOtpRequest";
import { TwoFactorYubiKeyResponse } from "@bitwarden/common/models/response/twoFactorYubiKeyResponse";
2018-06-27 19:45:11 +02:00
import { TwoFactorBaseComponent } from "./two-factor-base.component";
2018-06-27 19:45:11 +02:00
@Component({
selector: "app-two-factor-yubikey",
templateUrl: "two-factor-yubikey.component.html",
})
export class TwoFactorYubiKeyComponent extends TwoFactorBaseComponent {
2018-07-18 23:10:26 +02:00
type = TwoFactorProviderType.Yubikey;
2018-06-27 19:45:11 +02:00
keys: any[];
2018-07-21 15:50:50 +02:00
nfc = false;
2018-06-27 19:45:11 +02:00
formPromise: Promise<any>;
disablePromise: Promise<any>;
constructor(
apiService: ApiService,
i18nService: I18nService,
2021-12-07 20:41:45 +01:00
platformUtilsService: PlatformUtilsService,
logService: LogService,
userVerificationService: UserVerificationService
) {
2021-12-07 20:41:45 +01:00
super(apiService, i18nService, platformUtilsService, logService, userVerificationService);
}
2018-06-27 19:45:11 +02:00
auth(authResponse: any) {
super.auth(authResponse);
this.processResponse(authResponse.response);
2018-06-27 19:45:11 +02:00
}
async submit() {
const request = await this.buildRequestModel(UpdateTwoFactorYubioOtpRequest);
2018-06-27 19:45:11 +02:00
request.key1 = this.keys != null && this.keys.length > 0 ? this.keys[0].key : null;
request.key2 = this.keys != null && this.keys.length > 1 ? this.keys[1].key : null;
request.key3 = this.keys != null && this.keys.length > 2 ? this.keys[2].key : null;
request.key4 = this.keys != null && this.keys.length > 3 ? this.keys[3].key : null;
request.key5 = this.keys != null && this.keys.length > 4 ? this.keys[4].key : null;
request.nfc = this.nfc;
return super.enable(async () => {
2018-06-27 19:45:11 +02:00
this.formPromise = this.apiService.putTwoFactorYubiKey(request);
const response = await this.formPromise;
await this.processResponse(response);
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast("success", null, this.i18nService.t("yubikeysUpdated"));
});
2018-06-27 19:45:11 +02:00
}
disable() {
return super.disable(this.disablePromise);
2018-06-27 19:45:11 +02:00
}
remove(key: any) {
key.existingKey = null;
key.key = null;
}
private processResponse(response: TwoFactorYubiKeyResponse) {
this.enabled = response.enabled;
this.keys = [
{ key: response.key1, existingKey: this.padRight(response.key1) },
{ key: response.key2, existingKey: this.padRight(response.key2) },
{ key: response.key3, existingKey: this.padRight(response.key3) },
{ key: response.key4, existingKey: this.padRight(response.key4) },
{ key: response.key5, existingKey: this.padRight(response.key5) },
];
this.nfc = response.nfc || !response.enabled;
}
private padRight(str: string, character = "•", size = 44) {
if (str == null || character == null || str.length >= size) {
return str;
}
const max = (size - str.length) / character.length;
for (let i = 0; i < max; i++) {
str += character;
}
return str;
2021-12-17 15:57:11 +01:00
}
2018-06-27 19:45:11 +02:00
}