bitwarden-estensione-browser/src/app/accounts/settings.component.ts

365 lines
16 KiB
TypeScript
Raw Normal View History

2018-02-10 05:41:29 +01:00
import {
Component,
OnInit,
} from '@angular/core';
2021-09-15 20:02:46 +02:00
import { FormControl } from '@angular/forms';
import { debounceTime } from 'rxjs/operators';
2018-02-10 05:41:29 +01:00
import { DeviceType } from 'jslib-common/enums/deviceType';
2018-05-08 15:40:33 +02:00
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { StateService } from 'jslib-common/abstractions/state.service';
import { StorageService } from 'jslib-common/abstractions/storage.service';
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
2018-02-11 05:24:22 +01:00
import { ConstantsService } from 'jslib-common/services/constants.service';
2018-05-04 19:16:12 +02:00
import { ModalService } from 'jslib-angular/services/modal.service';
import { ElectronConstants } from 'jslib-electron/electronConstants';
2018-02-10 05:41:29 +01:00
import { Utils } from 'jslib-common/misc/utils';
import { isWindowsStore } from 'jslib-electron/utils';
2018-07-24 04:45:58 +02:00
import { SetPinComponent } from '../components/set-pin.component';
2018-02-10 05:41:29 +01:00
@Component({
selector: 'app-settings',
templateUrl: 'settings.component.html',
2018-02-10 05:41:29 +01:00
})
export class SettingsComponent implements OnInit {
vaultTimeoutAction: string;
2019-02-13 16:06:58 +01:00
pin: boolean = null;
2018-02-11 05:24:22 +01:00
disableFavicons: boolean = false;
enableBrowserIntegration: boolean = false;
enableBrowserIntegrationFingerprint: boolean = false;
2018-05-04 19:16:12 +02:00
enableMinToTray: boolean = false;
enableCloseToTray: boolean = false;
2018-05-05 06:29:02 +02:00
enableTray: boolean = false;
2018-05-08 15:40:33 +02:00
showMinToTray: boolean = false;
2019-02-02 18:27:06 +01:00
startToTray: boolean = false;
minimizeOnCopyToClipboard: boolean = false;
locale: string;
vaultTimeouts: any[];
2018-04-25 05:53:20 +02:00
localeOptions: any[];
2018-05-30 05:28:58 +02:00
theme: string;
themeOptions: any[];
2019-02-27 17:29:31 +01:00
clearClipboard: number;
clearClipboardOptions: any[];
supportsBiometric: boolean;
biometric: boolean;
biometricText: string;
noAutoPromptBiometrics: boolean;
noAutoPromptBiometricsText: string;
alwaysShowDock: boolean;
showAlwaysShowDock: boolean = false;
2020-11-25 20:43:29 +01:00
openAtLogin: boolean;
requireEnableTray: boolean = false;
2018-02-10 05:41:29 +01:00
enableTrayText: string;
enableTrayDescText: string;
enableMinToTrayText: string;
enableMinToTrayDescText: string;
enableCloseToTrayText: string;
enableCloseToTrayDescText: string;
startToTrayText: string;
startToTrayDescText: string;
2021-09-15 20:02:46 +02:00
vaultTimeout: FormControl = new FormControl(null);
constructor(private i18nService: I18nService, private platformUtilsService: PlatformUtilsService,
private storageService: StorageService, private vaultTimeoutService: VaultTimeoutService,
2019-02-13 16:06:58 +01:00
private stateService: StateService, private messagingService: MessagingService,
2021-09-15 20:02:46 +02:00
private cryptoService: CryptoService, private modalService: ModalService) {
const isMac = this.platformUtilsService.getDevice() === DeviceType.MacOsDesktop;
// Workaround to avoid ghosting trays https://github.com/electron/electron/issues/17622
this.requireEnableTray = this.platformUtilsService.getDevice() === DeviceType.LinuxDesktop;
const trayKey = isMac ? 'enableMenuBar' : 'enableTray';
this.enableTrayText = this.i18nService.t(trayKey);
this.enableTrayDescText = this.i18nService.t(trayKey + 'Desc');
const minToTrayKey = isMac ? 'enableMinToMenuBar' : 'enableMinToTray';
2021-02-03 19:21:22 +01:00
this.enableMinToTrayText = this.i18nService.t(minToTrayKey);
this.enableMinToTrayDescText = this.i18nService.t(minToTrayKey + 'Desc');
const closeToTrayKey = isMac ? 'enableCloseToMenuBar' : 'enableCloseToTray';
2021-02-03 19:21:22 +01:00
this.enableCloseToTrayText = this.i18nService.t(closeToTrayKey);
this.enableCloseToTrayDescText = this.i18nService.t(closeToTrayKey + 'Desc');
const startToTrayKey = isMac ? 'startToMenuBar' : 'startToTray';
2021-02-03 19:21:22 +01:00
this.startToTrayText = this.i18nService.t(startToTrayKey);
this.startToTrayDescText = this.i18nService.t(startToTrayKey + 'Desc');
this.vaultTimeouts = [
2018-02-11 05:24:22 +01:00
// { name: i18nService.t('immediately'), value: 0 },
{ name: i18nService.t('oneMinute'), value: 1 },
{ name: i18nService.t('fiveMinutes'), value: 5 },
{ name: i18nService.t('fifteenMinutes'), value: 15 },
{ name: i18nService.t('thirtyMinutes'), value: 30 },
{ name: i18nService.t('oneHour'), value: 60 },
{ name: i18nService.t('fourHours'), value: 240 },
2018-02-11 06:09:47 +01:00
{ name: i18nService.t('onIdle'), value: -4 },
2018-02-11 05:24:22 +01:00
{ name: i18nService.t('onSleep'), value: -3 },
2019-03-19 21:12:26 +01:00
];
if (this.platformUtilsService.getDevice() !== DeviceType.LinuxDesktop) {
this.vaultTimeouts.push({ name: i18nService.t('onLocked'), value: -2 });
2019-03-19 21:12:26 +01:00
}
this.vaultTimeouts = this.vaultTimeouts.concat([
2018-02-11 05:24:22 +01:00
{ name: i18nService.t('onRestart'), value: -1 },
{ name: i18nService.t('never'), value: null },
2019-03-19 21:12:26 +01:00
]);
2021-09-15 20:02:46 +02:00
this.vaultTimeout.valueChanges.pipe(debounceTime(500)).subscribe(() => {
this.saveVaultTimeoutOptions();
});
2018-07-24 04:45:58 +02:00
const localeOptions: any[] = [];
2021-02-03 19:21:22 +01:00
i18nService.supportedTranslationLocales.forEach(locale => {
let name = locale;
if (i18nService.localeNames.has(locale)) {
name += (' - ' + i18nService.localeNames.get(locale));
}
localeOptions.push({ name: name, value: locale });
});
2018-07-24 04:45:58 +02:00
localeOptions.sort(Utils.getSortFunction(i18nService, 'name'));
localeOptions.splice(0, 0, { name: i18nService.t('default'), value: null });
this.localeOptions = localeOptions;
2018-05-30 05:28:58 +02:00
this.themeOptions = [
{ name: i18nService.t('default'), value: null },
{ name: i18nService.t('light'), value: 'light' },
{ name: i18nService.t('dark'), value: 'dark' },
{ name: 'Nord', value: 'nord' },
2018-05-30 05:28:58 +02:00
];
2019-02-27 17:29:31 +01:00
this.clearClipboardOptions = [
{ name: i18nService.t('never'), value: null },
{ name: i18nService.t('tenSeconds'), value: 10 },
{ name: i18nService.t('twentySeconds'), value: 20 },
{ name: i18nService.t('thirtySeconds'), value: 30 },
{ name: i18nService.t('oneMinute'), value: 60 },
{ name: i18nService.t('twoMinutes'), value: 120 },
{ name: i18nService.t('fiveMinutes'), value: 300 },
];
2018-02-10 05:41:29 +01:00
}
2018-02-11 05:24:22 +01:00
async ngOnInit() {
this.showMinToTray = this.platformUtilsService.getDevice() !== DeviceType.LinuxDesktop;
2021-09-15 20:02:46 +02:00
this.vaultTimeout.setValue(await this.vaultTimeoutService.getVaultTimeout());
this.vaultTimeoutAction = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
const pinSet = await this.vaultTimeoutService.isPinLockSet();
2019-02-14 20:00:09 +01:00
this.pin = pinSet[0] || pinSet[1];
2018-02-11 05:24:22 +01:00
this.disableFavicons = await this.storageService.get<boolean>(ConstantsService.disableFaviconKey);
this.enableBrowserIntegration = await this.storageService.get<boolean>(
ElectronConstants.enableBrowserIntegration);
this.enableBrowserIntegrationFingerprint = await this.storageService.get<boolean>(ElectronConstants.enableBrowserIntegrationFingerprint);
2018-05-05 06:29:02 +02:00
this.enableMinToTray = await this.storageService.get<boolean>(ElectronConstants.enableMinimizeToTrayKey);
this.enableCloseToTray = await this.storageService.get<boolean>(ElectronConstants.enableCloseToTrayKey);
2018-05-05 06:29:02 +02:00
this.enableTray = await this.storageService.get<boolean>(ElectronConstants.enableTrayKey);
2019-02-02 18:27:06 +01:00
this.startToTray = await this.storageService.get<boolean>(ElectronConstants.enableStartToTrayKey);
2018-04-25 05:53:20 +02:00
this.locale = await this.storageService.get<string>(ConstantsService.localeKey);
2018-05-30 05:28:58 +02:00
this.theme = await this.storageService.get<string>(ConstantsService.themeKey);
2019-02-27 17:29:31 +01:00
this.clearClipboard = await this.storageService.get<number>(ConstantsService.clearClipboardKey);
this.minimizeOnCopyToClipboard = await this.storageService.get<boolean>(
ElectronConstants.minimizeOnCopyToClipboardKey);
this.supportsBiometric = await this.platformUtilsService.supportsBiometric();
this.biometric = await this.vaultTimeoutService.isBiometricLockSet();
this.biometricText = await this.storageService.get<string>(ConstantsService.biometricText);
2021-09-20 10:41:57 +02:00
this.noAutoPromptBiometrics = await this.storageService.get<boolean>(ConstantsService.disableAutoBiometricsPromptKey);
this.noAutoPromptBiometricsText = await this.storageService.get<string>(ElectronConstants.noAutoPromptBiometricsText);
this.alwaysShowDock = await this.storageService.get<boolean>(ElectronConstants.alwaysShowDock);
this.showAlwaysShowDock = this.platformUtilsService.getDevice() === DeviceType.MacOsDesktop;
2020-11-25 20:43:29 +01:00
this.openAtLogin = await this.storageService.get<boolean>(ElectronConstants.openAtLogin);
2018-02-11 05:24:22 +01:00
}
2018-02-10 05:41:29 +01:00
async saveVaultTimeoutOptions() {
if (this.vaultTimeoutAction === 'logOut') {
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('vaultTimeoutLogOutConfirmation'),
this.i18nService.t('vaultTimeoutLogOutConfirmationTitle'),
this.i18nService.t('yes'), this.i18nService.t('cancel'), 'warning');
if (!confirmed) {
this.vaultTimeoutAction = 'lock';
return;
}
}
2021-09-15 20:02:46 +02:00
// Avoid saving 0 since it's useless as a timeout value.
if (this.vaultTimeout.value === 0) {
return;
}
if (!this.vaultTimeout.valid) {
this.platformUtilsService.showToast('error', null, this.i18nService.t('vaultTimeoutTooLarge'));
return;
}
await this.vaultTimeoutService.setVaultTimeoutOptions(this.vaultTimeout.value, this.vaultTimeoutAction);
2018-02-12 21:06:39 +01:00
}
2019-02-13 16:06:58 +01:00
async updatePin() {
if (this.pin) {
const ref = this.modalService.open(SetPinComponent, { allowMultipleModals: true });
2019-02-14 20:00:09 +01:00
if (ref == null) {
2019-02-13 16:06:58 +01:00
this.pin = false;
return;
2019-02-13 16:06:58 +01:00
}
this.pin = await ref.onClosedPromise();
2019-02-13 16:06:58 +01:00
}
if (!this.pin) {
2019-08-29 16:01:59 +02:00
await this.cryptoService.clearPinProtectedKey();
await this.vaultTimeoutService.clear();
2019-02-13 16:06:58 +01:00
}
}
async updateBiometric() {
const current = this.biometric;
if (this.biometric) {
this.biometric = false;
} else if (this.supportsBiometric) {
this.biometric = await this.platformUtilsService.authenticateBiometric();
}
if (this.biometric === current) {
return;
}
if (this.biometric) {
await this.storageService.save(ConstantsService.biometricUnlockKey, true);
} else {
await this.storageService.remove(ConstantsService.biometricUnlockKey);
2021-09-20 10:41:57 +02:00
await this.storageService.remove(ConstantsService.disableAutoBiometricsPromptKey);
this.noAutoPromptBiometrics = false;
}
this.vaultTimeoutService.biometricLocked = false;
await this.cryptoService.toggleKey();
}
async updateNoAutoPromptBiometrics() {
if (!this.biometric) {
this.noAutoPromptBiometrics = false;
}
if (this.noAutoPromptBiometrics) {
2021-09-20 10:41:57 +02:00
await this.storageService.save(ConstantsService.disableAutoBiometricsPromptKey, true);
} else {
2021-09-20 10:41:57 +02:00
await this.storageService.remove(ConstantsService.disableAutoBiometricsPromptKey);
}
}
2018-02-12 21:06:39 +01:00
async saveFavicons() {
2018-02-11 05:24:22 +01:00
await this.storageService.save(ConstantsService.disableFaviconKey, this.disableFavicons);
2018-02-12 21:06:39 +01:00
await this.stateService.save(ConstantsService.disableFaviconKey, this.disableFavicons);
this.messagingService.send('refreshCiphers');
}
2018-05-04 19:16:12 +02:00
async saveMinToTray() {
2018-05-05 06:29:02 +02:00
await this.storageService.save(ElectronConstants.enableMinimizeToTrayKey, this.enableMinToTray);
2018-02-12 21:06:39 +01:00
}
async saveCloseToTray() {
if (this.requireEnableTray) {
this.enableTray = true;
2020-12-16 09:38:28 +01:00
await this.storageService.save(ElectronConstants.enableTrayKey, this.enableTray);
}
await this.storageService.save(ElectronConstants.enableCloseToTrayKey, this.enableCloseToTray);
}
2018-05-05 06:29:02 +02:00
async saveTray() {
if (this.requireEnableTray && !this.enableTray && (this.startToTray || this.enableCloseToTray)) {
const confirm = await this.platformUtilsService.showDialog(
this.i18nService.t('confirmTrayDesc'), this.i18nService.t('confirmTrayTitle'),
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
if (confirm) {
this.startToTray = false;
2020-12-16 09:38:28 +01:00
await this.storageService.save(ElectronConstants.enableStartToTrayKey, this.startToTray);
this.enableCloseToTray = false;
2020-12-16 09:38:28 +01:00
await this.storageService.save(ElectronConstants.enableCloseToTrayKey, this.enableCloseToTray);
} else {
this.enableTray = true;
}
return;
}
2018-05-05 06:29:02 +02:00
await this.storageService.save(ElectronConstants.enableTrayKey, this.enableTray);
this.messagingService.send(this.enableTray ? 'showTray' : 'removeTray');
}
2019-02-02 18:27:06 +01:00
async saveStartToTray() {
if (this.requireEnableTray) {
this.enableTray = true;
2020-12-16 09:38:28 +01:00
await this.storageService.save(ElectronConstants.enableTrayKey, this.enableTray);
}
2019-02-02 18:27:06 +01:00
await this.storageService.save(ElectronConstants.enableStartToTrayKey, this.startToTray);
}
2018-04-25 05:53:20 +02:00
async saveLocale() {
await this.storageService.save(ConstantsService.localeKey, this.locale);
}
2018-05-30 05:28:58 +02:00
async saveTheme() {
await this.storageService.save(ConstantsService.themeKey, this.theme);
2018-05-31 04:28:04 +02:00
window.setTimeout(() => window.location.reload(), 200);
2018-05-30 05:28:58 +02:00
}
async saveMinOnCopyToClipboard() {
await this.storageService.save(ElectronConstants.minimizeOnCopyToClipboardKey, this.minimizeOnCopyToClipboard);
}
2019-02-27 17:29:31 +01:00
async saveClearClipboard() {
await this.storageService.save(ConstantsService.clearClipboardKey, this.clearClipboard);
}
async saveAlwaysShowDock() {
await this.storageService.save(ElectronConstants.alwaysShowDock, this.alwaysShowDock);
2020-11-20 14:05:50 +01:00
}
2020-11-25 20:43:29 +01:00
async saveOpenAtLogin() {
this.storageService.save(ElectronConstants.openAtLogin, this.openAtLogin);
this.messagingService.send(this.openAtLogin ? 'addOpenAtLogin' : 'removeOpenAtLogin');
2020-11-23 15:04:35 +01:00
}
async saveBrowserIntegration() {
2020-12-22 17:16:12 +01:00
if (process.platform === 'darwin' && !this.platformUtilsService.isMacAppStore()) {
await this.platformUtilsService.showDialog(
this.i18nService.t('browserIntegrationMasOnlyDesc'),
this.i18nService.t('browserIntegrationMasOnlyTitle'),
this.i18nService.t('ok'), null, 'warning');
2020-12-22 17:16:12 +01:00
this.enableBrowserIntegration = false;
return;
} else if (isWindowsStore()) {
await this.platformUtilsService.showDialog(
this.i18nService.t('browserIntegrationWindowsStoreDesc'),
this.i18nService.t('browserIntegrationWindowsStoreTitle'),
this.i18nService.t('ok'), null, 'warning');
this.enableBrowserIntegration = false;
return;
}
await this.storageService.save(ElectronConstants.enableBrowserIntegration, this.enableBrowserIntegration);
this.messagingService.send(this.enableBrowserIntegration ? 'enableBrowserIntegration' : 'disableBrowserIntegration');
if (!this.enableBrowserIntegration) {
this.enableBrowserIntegrationFingerprint = false;
this.saveBrowserIntegrationFingerprint();
}
}
async saveBrowserIntegrationFingerprint() {
await this.storageService.save(ElectronConstants.enableBrowserIntegrationFingerprint, this.enableBrowserIntegrationFingerprint);
}
2018-02-10 05:41:29 +01:00
}