bitwarden-estensione-browser/src/app/vault/add-edit.component.ts

157 lines
5.7 KiB
TypeScript
Raw Normal View History

import { Component } from '@angular/core';
2018-06-06 23:25:57 +02:00
2018-06-20 05:40:51 +02:00
import { CipherType } from 'jslib/enums/cipherType';
2018-06-06 23:25:57 +02:00
import { AuditService } from 'jslib/abstractions/audit.service';
import { CipherService } from 'jslib/abstractions/cipher.service';
import { CollectionService } from 'jslib/abstractions/collection.service';
2018-06-06 23:25:57 +02:00
import { FolderService } from 'jslib/abstractions/folder.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-07-18 15:21:23 +02:00
import { MessagingService } from 'jslib/abstractions/messaging.service';
2018-06-21 00:16:20 +02:00
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
2018-06-06 23:25:57 +02:00
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { StateService } from 'jslib/abstractions/state.service';
2018-06-20 05:40:51 +02:00
import { TotpService } from 'jslib/abstractions/totp.service';
2018-08-29 05:17:58 +02:00
import { UserService } from 'jslib/abstractions/user.service';
2018-06-06 23:25:57 +02:00
import { AddEditComponent as BaseAddEditComponent } from 'jslib/angular/components/add-edit.component';
2018-06-07 23:12:11 +02:00
import { LoginUriView } from 'jslib/models/view/loginUriView';
2018-06-06 23:25:57 +02:00
@Component({
selector: 'app-vault-add-edit',
templateUrl: 'add-edit.component.html',
})
export class AddEditComponent extends BaseAddEditComponent {
2018-08-29 05:17:58 +02:00
canAccessPremium: boolean;
2018-06-20 05:40:51 +02:00
totpCode: string;
totpCodeFormatted: string;
totpDash: number;
totpSec: number;
totpLow: boolean;
2018-07-31 04:02:01 +02:00
showRevisionDate = false;
hasPasswordHistory = false;
2018-07-30 14:48:48 +02:00
viewingPasswordHistory = false;
2018-06-20 05:40:51 +02:00
2018-07-05 15:42:50 +02:00
protected totpInterval: number;
2018-06-20 05:40:51 +02:00
2018-06-06 23:25:57 +02:00
constructor(cipherService: CipherService, folderService: FolderService,
i18nService: I18nService, platformUtilsService: PlatformUtilsService,
2018-06-20 05:40:51 +02:00
auditService: AuditService, stateService: StateService,
userService: UserService, collectionService: CollectionService,
protected totpService: TotpService, protected passwordGenerationService: PasswordGenerationService,
protected messagingService: MessagingService) {
super(cipherService, folderService, i18nService, platformUtilsService, auditService, stateService,
userService, collectionService);
2018-06-06 23:25:57 +02:00
}
2018-06-07 05:23:14 +02:00
async ngOnInit() {
await super.ngOnInit();
await this.load();
2018-07-31 04:02:01 +02:00
this.showRevisionDate = this.cipher.passwordRevisionDisplayDate != null;
this.hasPasswordHistory = this.cipher.hasPasswordHistory;
2018-06-20 05:40:51 +02:00
this.cleanUp();
2018-08-29 05:17:58 +02:00
this.canAccessPremium = await this.userService.canAccessPremium();
2018-06-20 05:40:51 +02:00
if (this.cipher.type === CipherType.Login && this.cipher.login.totp &&
2018-08-29 05:17:58 +02:00
(this.cipher.organizationUseTotp || this.canAccessPremium)) {
2018-06-20 05:40:51 +02:00
await this.totpUpdateCode();
2018-07-31 17:26:04 +02:00
const interval = this.totpService.getTimeInterval(this.cipher.login.totp);
await this.totpTick(interval);
2018-06-20 05:40:51 +02:00
this.totpInterval = window.setInterval(async () => {
2018-07-31 17:26:04 +02:00
await this.totpTick(interval);
2018-06-20 05:40:51 +02:00
}, 1000);
}
2018-06-06 23:25:57 +02:00
}
2018-06-07 23:12:11 +02:00
toggleFavorite() {
this.cipher.favorite = !this.cipher.favorite;
}
launch(uri: LoginUriView) {
if (!uri.canLaunch) {
return;
}
this.platformUtilsService.eventTrack('Launched Login URI');
2018-06-07 23:12:11 +02:00
this.platformUtilsService.launchUri(uri.uri);
}
copy(value: string, typeI18nKey: string, aType: string) {
if (value == null) {
return;
}
this.platformUtilsService.eventTrack('Copied ' + aType);
2018-08-17 18:25:21 +02:00
this.platformUtilsService.copyToClipboard(value, { window: window });
this.platformUtilsService.showToast('info', null,
2018-06-07 23:12:11 +02:00
this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey)));
}
2018-06-20 05:40:51 +02:00
2018-06-21 00:16:20 +02:00
async generatePassword(): Promise<boolean> {
const confirmed = await super.generatePassword();
if (confirmed) {
const options = await this.passwordGenerationService.getOptions();
this.cipher.login.password = await this.passwordGenerationService.generatePassword(options);
}
return confirmed;
}
2018-07-18 15:21:23 +02:00
async premiumRequired() {
2018-08-29 05:17:58 +02:00
if (!this.canAccessPremium) {
2018-07-18 15:21:23 +02:00
this.messagingService.send('premiumRequired');
return;
}
}
async upgradeOrganization() {
this.messagingService.send('upgradeOrganization', { organizationId: this.cipher.organizationId });
}
2018-07-30 14:48:48 +02:00
viewHistory() {
this.viewingPasswordHistory = !this.viewingPasswordHistory;
}
2018-07-05 15:42:50 +02:00
protected cleanUp() {
2018-06-20 05:40:51 +02:00
if (this.totpInterval) {
window.clearInterval(this.totpInterval);
}
}
2018-07-05 15:42:50 +02:00
protected async totpUpdateCode() {
2018-06-20 05:40:51 +02:00
if (this.cipher == null || this.cipher.type !== CipherType.Login || this.cipher.login.totp == null) {
if (this.totpInterval) {
window.clearInterval(this.totpInterval);
}
return;
}
this.totpCode = await this.totpService.getCode(this.cipher.login.totp);
if (this.totpCode != null) {
2018-07-31 17:26:04 +02:00
if (this.totpCode.length > 4) {
const half = Math.floor(this.totpCode.length / 2);
this.totpCodeFormatted = this.totpCode.substring(0, half) + ' ' + this.totpCode.substring(half);
} else {
this.totpCodeFormatted = this.totpCode;
}
2018-06-20 05:40:51 +02:00
} else {
this.totpCodeFormatted = null;
if (this.totpInterval) {
window.clearInterval(this.totpInterval);
}
}
}
2018-07-31 17:26:04 +02:00
private async totpTick(intervalSeconds: number) {
2018-06-20 05:40:51 +02:00
const epoch = Math.round(new Date().getTime() / 1000.0);
2018-07-31 17:26:04 +02:00
const mod = epoch % intervalSeconds;
2018-06-20 05:40:51 +02:00
2018-07-31 17:26:04 +02:00
this.totpSec = intervalSeconds - mod;
this.totpDash = +(Math.round((((78.6 / intervalSeconds) * mod) + 'e+2') as any) + 'e-2');
2018-06-20 05:40:51 +02:00
this.totpLow = this.totpSec <= 7;
if (mod === 0) {
await this.totpUpdateCode();
}
}
2018-06-06 23:25:57 +02:00
}