bitwarden-estensione-browser/src/popup/settings/settings.component.ts

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

417 lines
14 KiB
TypeScript
Raw Normal View History

2018-04-10 22:20:49 +02:00
import { Component, ElementRef, OnInit, ViewChild } from "@angular/core";
2021-09-14 13:36:34 +02:00
import { FormControl } from "@angular/forms";
2018-04-10 22:20:49 +02:00
import { Router } from "@angular/router";
2021-09-14 13:36:34 +02:00
import Swal from "sweetalert2/src/sweetalert2.js";
2018-04-10 22:20:49 +02:00
import { BrowserApi } from "../../browser/browserApi";
import { DeviceType } from "jslib-common/enums/deviceType";
2018-04-10 22:20:49 +02:00
import { ConstantsService } from "jslib-common/services/constants.service";
2018-04-10 22:20:49 +02:00
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { EnvironmentService } from "jslib-common/abstractions/environment.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
import { MessagingService } from "jslib-common/abstractions/messaging.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { StorageService } from "jslib-common/abstractions/storage.service";
import { UserService } from "jslib-common/abstractions/user.service";
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
import { PopupUtilsService } from "../services/popup-utils.service";
2018-04-10 22:20:49 +02:00
import { ModalService } from "jslib-angular/services/modal.service";
import { SetPinComponent } from "../components/set-pin.component";
2018-04-10 22:20:49 +02:00
const RateUrls = {
2018-07-09 15:12:41 +02:00
[DeviceType.ChromeExtension]:
2018-04-10 22:20:49 +02:00
"https://chrome.google.com/webstore/detail/bitwarden-free-password-m/nngceckbapebfimnlniiiahkandclblb/reviews",
2018-07-09 15:12:41 +02:00
[DeviceType.FirefoxExtension]:
2018-04-10 22:20:49 +02:00
"https://addons.mozilla.org/en-US/firefox/addon/bitwarden-password-manager/#reviews",
2018-07-09 15:12:41 +02:00
[DeviceType.OperaExtension]:
2018-04-10 22:20:49 +02:00
"https://addons.opera.com/en/extensions/details/bitwarden-free-password-manager/#feedback-container",
2018-07-09 15:12:41 +02:00
[DeviceType.EdgeExtension]:
"https://microsoftedge.microsoft.com/addons/detail/jbkfoedolllekgbhcbcoahefnbanhhlh",
2018-07-09 15:12:41 +02:00
[DeviceType.VivaldiExtension]:
2018-04-10 22:20:49 +02:00
"https://chrome.google.com/webstore/detail/bitwarden-free-password-m/nngceckbapebfimnlniiiahkandclblb/reviews",
2019-08-21 16:05:17 +02:00
[DeviceType.SafariExtension]: "https://apps.apple.com/app/bitwarden/id1352778147",
2018-04-10 22:20:49 +02:00
};
2018-04-09 23:35:16 +02:00
@Component({
selector: "app-settings",
templateUrl: "settings.component.html",
})
2018-04-10 22:20:49 +02:00
export class SettingsComponent implements OnInit {
@ViewChild("vaultTimeoutActionSelect", { read: ElementRef, static: true })
vaultTimeoutActionSelectRef: ElementRef;
vaultTimeouts: any[];
vaultTimeoutActions: any[];
vaultTimeoutAction: string;
2019-02-13 05:53:04 +01:00
pin: boolean = null;
2021-01-25 21:23:18 +01:00
supportsBiometric: boolean;
biometric: boolean = false;
disableAutoBiometricsPrompt = true;
previousVaultTimeout: number = null;
showChangeMasterPass = true;
2021-12-21 15:43:35 +01:00
2021-09-14 13:36:34 +02:00
vaultTimeout: FormControl = new FormControl(null);
2021-12-21 15:43:35 +01:00
2018-04-10 22:20:49 +02:00
constructor(
private platformUtilsService: PlatformUtilsService,
private i18nService: I18nService,
private vaultTimeoutService: VaultTimeoutService,
private storageService: StorageService,
public messagingService: MessagingService,
private router: Router,
private environmentService: EnvironmentService,
private cryptoService: CryptoService,
2021-09-14 13:36:34 +02:00
private userService: UserService,
private popupUtilsService: PopupUtilsService,
2021-12-07 20:42:18 +01:00
private modalService: ModalService,
private keyConnectorService: KeyConnectorService
2021-12-21 15:43:35 +01:00
) {}
2018-04-10 22:20:49 +02:00
async ngOnInit() {
2020-09-15 16:50:45 +02:00
const showOnLocked =
!this.platformUtilsService.isFirefox() && !this.platformUtilsService.isSafari();
2021-12-21 15:43:35 +01:00
this.vaultTimeouts = [
2018-04-10 22:20:49 +02:00
{ name: this.i18nService.t("immediately"), value: 0 },
{ name: this.i18nService.t("oneMinute"), value: 1 },
{ name: this.i18nService.t("fiveMinutes"), value: 5 },
{ name: this.i18nService.t("fifteenMinutes"), value: 15 },
{ name: this.i18nService.t("thirtyMinutes"), value: 30 },
{ name: this.i18nService.t("oneHour"), value: 60 },
{ name: this.i18nService.t("fourHours"), value: 240 },
// { name: i18nService.t('onIdle'), value: -4 },
// { name: i18nService.t('onSleep'), value: -3 },
2021-12-21 15:43:35 +01:00
];
if (showOnLocked) {
this.vaultTimeouts.push({ name: this.i18nService.t("onLocked"), value: -2 });
2018-04-10 22:20:49 +02:00
}
this.vaultTimeouts.push({ name: this.i18nService.t("onRestart"), value: -1 });
this.vaultTimeouts.push({ name: this.i18nService.t("never"), value: null });
2021-09-14 13:36:34 +02:00
this.vaultTimeoutActions = [
{ name: this.i18nService.t("lock"), value: "lock" },
{ name: this.i18nService.t("logOut"), value: "logOut" },
];
2019-02-13 05:53:04 +01:00
let timeout = await this.vaultTimeoutService.getVaultTimeout();
if (timeout != null) {
if (timeout === -2 && !showOnLocked) {
timeout = -1;
2021-12-21 15:43:35 +01:00
}
this.vaultTimeout.setValue(timeout);
2018-04-10 22:20:49 +02:00
}
2021-09-14 13:36:34 +02:00
this.previousVaultTimeout = this.vaultTimeout.value;
this.vaultTimeout.valueChanges.subscribe((value) => {
2021-12-07 20:42:18 +01:00
this.saveVaultTimeout(value);
2021-12-21 15:43:35 +01:00
});
2021-12-07 20:42:18 +01:00
const action = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
2021-09-14 13:36:34 +02:00
this.vaultTimeoutAction = action == null ? "lock" : action;
2021-12-21 15:43:35 +01:00
2021-09-14 13:36:34 +02:00
const pinSet = await this.vaultTimeoutService.isPinLockSet();
this.pin = pinSet[0] || pinSet[1];
2021-12-21 15:43:35 +01:00
2021-09-14 13:36:34 +02:00
this.supportsBiometric = await this.platformUtilsService.supportsBiometric();
this.biometric = await this.vaultTimeoutService.isBiometricLockSet();
this.disableAutoBiometricsPrompt =
(await this.storageService.get<boolean>(ConstantsService.disableAutoBiometricsPromptKey)) ??
true;
2021-12-07 20:42:18 +01:00
this.showChangeMasterPass = !(await this.keyConnectorService.getUsesKeyConnector());
2021-09-14 13:36:34 +02:00
}
2021-12-21 15:43:35 +01:00
async saveVaultTimeout(newValue: number) {
2019-02-13 05:53:04 +01:00
if (newValue == null) {
const confirmed = await this.platformUtilsService.showDialog(
2019-02-13 05:53:04 +01:00
this.i18nService.t("neverLockWarning"),
2021-12-21 15:43:35 +01:00
null,
this.i18nService.t("yes"),
2019-08-29 15:41:04 +02:00
this.i18nService.t("cancel"),
"warning"
);
if (!confirmed) {
this.vaultTimeout.setValue(this.previousVaultTimeout);
2021-12-21 15:43:35 +01:00
return;
2019-02-13 05:53:04 +01:00
}
}
if (!this.vaultTimeout.valid) {
this.platformUtilsService.showToast("error", null, this.i18nService.t("vaultTimeoutToLarge"));
return;
}
this.previousVaultTimeout = this.vaultTimeout.value;
await this.vaultTimeoutService.setVaultTimeoutOptions(
this.vaultTimeout.value,
this.vaultTimeoutAction
);
if (this.previousVaultTimeout == null) {
this.messagingService.send("bgReseedStorage");
2018-04-10 22:20:49 +02:00
}
2021-12-21 15:43:35 +01:00
}
2018-04-10 22:20:49 +02:00
async saveVaultTimeoutAction(newValue: string) {
if (newValue === "logOut") {
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t("vaultTimeoutLogOutConfirmation"),
this.i18nService.t("vaultTimeoutLogOutConfirmationTitle"),
this.i18nService.t("yes"),
this.i18nService.t("cancel"),
2021-12-21 15:43:35 +01:00
"warning"
2018-04-10 22:20:49 +02:00
);
if (!confirmed) {
this.vaultTimeoutActions.forEach((option: any, i) => {
if (option.value === this.vaultTimeoutAction) {
2018-04-10 22:20:49 +02:00
this.vaultTimeoutActionSelectRef.nativeElement.value =
i + ": " + this.vaultTimeoutAction;
2021-12-21 15:43:35 +01:00
}
2018-04-10 22:20:49 +02:00
});
2021-12-21 15:43:35 +01:00
return;
2018-04-10 22:20:49 +02:00
}
}
if (!this.vaultTimeout.valid) {
this.platformUtilsService.showToast("error", null, this.i18nService.t("vaultTimeoutToLarge"));
return;
}
this.vaultTimeoutAction = newValue;
2021-09-14 13:36:34 +02:00
await this.vaultTimeoutService.setVaultTimeoutOptions(
this.vaultTimeout.value,
this.vaultTimeoutAction
2021-12-21 15:43:35 +01:00
);
}
2018-04-10 22:20:49 +02:00
async updatePin() {
if (this.pin) {
const ref = this.modalService.open(SetPinComponent, { allowMultipleModals: true });
2021-12-21 15:43:35 +01:00
if (ref == null) {
2018-04-10 22:20:49 +02:00
this.pin = false;
2021-12-21 15:43:35 +01:00
return;
}
2018-04-10 22:20:49 +02:00
this.pin = await ref.onClosedPromise();
} else {
await this.cryptoService.clearPinProtectedKey();
await this.vaultTimeoutService.clear();
}
2021-12-21 15:43:35 +01:00
}
2018-04-10 22:20:49 +02:00
async updateBiometric() {
2021-01-25 21:23:18 +01:00
if (this.biometric && this.supportsBiometric) {
let granted;
2021-12-21 15:43:35 +01:00
try {
granted = await BrowserApi.requestPermission({ permissions: ["nativeMessaging"] });
} catch (e) {
// tslint:disable-next-line
console.error(e);
2021-12-21 15:43:35 +01:00
if (this.platformUtilsService.isFirefox() && this.popupUtilsService.inSidebar(window)) {
await this.platformUtilsService.showDialog(
this.i18nService.t("nativeMessaginPermissionSidebarDesc"),
this.i18nService.t("nativeMessaginPermissionSidebarTitle"),
this.i18nService.t("ok"),
2021-12-21 15:43:35 +01:00
null
);
this.biometric = false;
return;
}
2021-12-21 15:43:35 +01:00
}
if (!granted) {
await this.platformUtilsService.showDialog(
this.i18nService.t("nativeMessaginPermissionErrorDesc"),
this.i18nService.t("nativeMessaginPermissionErrorTitle"),
this.i18nService.t("ok"),
2021-12-21 15:43:35 +01:00
null
);
this.biometric = false;
2021-12-21 15:43:35 +01:00
return;
}
const submitted = Swal.fire({
heightAuto: false,
buttonsStyling: false,
titleText: this.i18nService.t("awaitDesktop"),
text: this.i18nService.t("awaitDesktopDesc"),
icon: "info",
iconHtml: '<i class="swal-custom-icon fa fa-info-circle text-info"></i>',
showCancelButton: true,
cancelButtonText: this.i18nService.t("cancel"),
showConfirmButton: false,
allowOutsideClick: false,
2021-12-21 15:43:35 +01:00
});
await this.storageService.save(ConstantsService.biometricAwaitingAcceptance, true);
await this.cryptoService.toggleKey();
2021-12-21 15:43:35 +01:00
await Promise.race([
2021-03-02 19:31:52 +01:00
submitted.then((result) => {
if (result.dismiss === Swal.DismissReason.cancel) {
this.biometric = false;
this.storageService.remove(ConstantsService.biometricAwaitingAcceptance);
2021-12-21 15:43:35 +01:00
}
}),
2020-09-15 16:50:45 +02:00
this.platformUtilsService
2021-03-02 19:31:52 +01:00
.authenticateBiometric()
.then((result) => {
this.biometric = result;
2021-12-21 15:43:35 +01:00
Swal.close();
if (this.biometric === false) {
2020-10-21 17:18:04 +02:00
this.platformUtilsService.showToast(
2021-12-21 15:43:35 +01:00
"error",
2020-10-21 17:18:04 +02:00
this.i18nService.t("errorEnableBiometricTitle"),
this.i18nService.t("errorEnableBiometricDesc")
2021-12-21 15:43:35 +01:00
);
}
})
2021-03-02 19:31:52 +01:00
.catch((e) => {
// Handle connection errors
this.biometric = false;
2021-12-21 15:43:35 +01:00
}),
]);
} else {
await this.storageService.remove(ConstantsService.biometricUnlockKey);
this.vaultTimeoutService.biometricLocked = false;
}
2021-12-21 15:43:35 +01:00
}
async updateAutoBiometricsPrompt() {
await this.storageService.save(
ConstantsService.disableAutoBiometricsPromptKey,
this.disableAutoBiometricsPrompt
2021-12-21 15:43:35 +01:00
);
}
2018-04-10 22:20:49 +02:00
async lock() {
await this.vaultTimeoutService.lock(true);
2021-12-21 15:43:35 +01:00
}
async logOut() {
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t("logOutConfirmation"),
this.i18nService.t("logOut"),
2018-04-10 22:20:49 +02:00
this.i18nService.t("yes"),
this.i18nService.t("cancel")
);
if (confirmed) {
2018-06-25 14:06:38 +02:00
this.messagingService.send("logout");
2018-04-10 22:20:49 +02:00
}
2021-12-21 15:43:35 +01:00
}
2018-04-10 22:20:49 +02:00
async changePassword() {
const confirmed = await this.platformUtilsService.showDialog(
2018-04-10 22:20:49 +02:00
this.i18nService.t("changeMasterPasswordConfirmation"),
this.i18nService.t("changeMasterPassword"),
this.i18nService.t("yes"),
this.i18nService.t("cancel")
2021-12-21 15:43:35 +01:00
);
2018-04-10 22:20:49 +02:00
if (confirmed) {
BrowserApi.createNewTab("https://help.bitwarden.com/article/change-your-master-password/");
}
2021-12-21 15:43:35 +01:00
}
2018-04-14 04:18:21 +02:00
async twoStep() {
const confirmed = await this.platformUtilsService.showDialog(
2018-04-10 22:20:49 +02:00
this.i18nService.t("twoStepLoginConfirmation"),
this.i18nService.t("twoStepLogin"),
this.i18nService.t("yes"),
this.i18nService.t("cancel")
2021-12-21 15:43:35 +01:00
);
if (confirmed) {
2018-04-14 04:18:21 +02:00
BrowserApi.createNewTab("https://help.bitwarden.com/article/setup-two-step-login/");
}
2021-12-21 15:43:35 +01:00
}
2018-04-12 23:28:33 +02:00
async share() {
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t("learnOrgConfirmation"),
this.i18nService.t("learnOrg"),
2018-04-10 22:20:49 +02:00
this.i18nService.t("yes"),
2018-04-12 23:28:33 +02:00
this.i18nService.t("cancel")
2021-12-21 15:43:35 +01:00
);
if (confirmed) {
2018-04-12 23:28:33 +02:00
BrowserApi.createNewTab("https://help.bitwarden.com/article/what-is-an-organization/");
}
2021-12-21 15:43:35 +01:00
}
async webVault() {
const url = this.environmentService.getWebVaultUrl();
BrowserApi.createNewTab(url);
2021-12-21 15:43:35 +01:00
}
import() {
2018-04-10 22:20:49 +02:00
BrowserApi.createNewTab("https://help.bitwarden.com/article/import-data/");
2021-12-21 15:43:35 +01:00
}
export() {
2018-04-14 04:18:21 +02:00
this.router.navigate(["/export"]);
2021-12-21 15:43:35 +01:00
}
help() {
2018-04-10 22:20:49 +02:00
BrowserApi.createNewTab("https://help.bitwarden.com/");
2021-12-21 15:43:35 +01:00
}
2018-04-12 23:28:33 +02:00
about() {
2018-04-19 15:45:51 +02:00
const year = new Date().getFullYear();
2018-04-12 23:28:33 +02:00
const versionText = document.createTextNode(
this.i18nService.t("version") + ": " + BrowserApi.getApplicationVersion()
);
const div = document.createElement("div");
2019-10-08 23:04:44 +02:00
div.innerHTML =
`<p class="text-center"><i class="fa fa-shield fa-3x" aria-hidden="true"></i></p>
2020-02-19 04:35:28 +01:00
<p class="text-center"><b>Bitwarden</b><br>&copy; Bitwarden Inc. 2015-` +
year +
`</p>`;
2018-04-12 23:28:33 +02:00
div.appendChild(versionText);
2021-12-21 15:43:35 +01:00
Swal.fire({
heightAuto: false,
buttonsStyling: false,
html: div,
showConfirmButton: false,
showCancelButton: true,
cancelButtonText: this.i18nService.t("close"),
2021-12-21 15:43:35 +01:00
});
}
2018-11-16 17:08:36 +01:00
async fingerprint() {
const fingerprint = await this.cryptoService.getFingerprint(await this.userService.getUserId());
const p = document.createElement("p");
p.innerText = this.i18nService.t("yourAccountsFingerprint") + ":";
const p2 = document.createElement("p");
p2.innerText = fingerprint.join("-");
const div = document.createElement("div");
div.appendChild(p);
div.appendChild(p2);
2021-12-21 15:43:35 +01:00
const result = await Swal.fire({
heightAuto: false,
buttonsStyling: false,
html: div,
showCancelButton: true,
cancelButtonText: this.i18nService.t("close"),
showConfirmButton: true,
confirmButtonText: this.i18nService.t("learnMore"),
2018-04-12 23:28:33 +02:00
});
2021-12-21 15:43:35 +01:00
if (result.value) {
2018-11-16 17:17:16 +01:00
this.platformUtilsService.launchUri("https://help.bitwarden.com/article/fingerprint-phrase/");
2018-04-12 23:28:33 +02:00
}
2021-12-21 15:43:35 +01:00
}
2018-04-12 23:28:33 +02:00
2018-11-16 17:08:36 +01:00
rate() {
const deviceType = this.platformUtilsService.getDevice();
BrowserApi.createNewTab((RateUrls as any)[deviceType]);
2018-04-10 22:20:49 +02:00
}
}