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

134 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-06-06 23:25:57 +02:00
import {
Component,
2018-06-07 05:23:14 +02:00
OnInit,
2018-06-06 23:25:57 +02:00
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
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 { FolderService } from 'jslib/abstractions/folder.service';
import { I18nService } from 'jslib/abstractions/i18n.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 { TokenService } from 'jslib/abstractions/token.service';
import { TotpService } from 'jslib/abstractions/totp.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',
})
2018-06-07 05:23:14 +02:00
export class AddEditComponent extends BaseAddEditComponent implements OnInit {
2018-06-20 05:40:51 +02:00
isPremium: boolean;
totpCode: string;
totpCodeFormatted: string;
totpDash: number;
totpSec: number;
totpLow: boolean;
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,
analytics: Angulartics2, toasterService: ToasterService,
2018-06-20 05:40:51 +02:00
auditService: AuditService, stateService: StateService,
2018-07-05 15:42:50 +02:00
protected tokenService: TokenService, protected totpService: TotpService,
protected passwordGenerationService: PasswordGenerationService) {
2018-06-06 23:25:57 +02:00
super(cipherService, folderService, i18nService, platformUtilsService, analytics,
toasterService, auditService, stateService);
}
2018-06-07 05:23:14 +02:00
async ngOnInit() {
2018-06-06 23:25:57 +02:00
await super.load();
2018-06-20 05:40:51 +02:00
this.cleanUp();
this.isPremium = this.tokenService.getPremium();
if (this.cipher.type === CipherType.Login && this.cipher.login.totp &&
(this.cipher.organizationUseTotp || this.isPremium)) {
await this.totpUpdateCode();
await this.totpTick();
this.totpInterval = window.setInterval(async () => {
await this.totpTick();
}, 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.analytics.eventTrack.next({ action: 'Launched Login URI' });
this.platformUtilsService.launchUri(uri.uri);
}
copy(value: string, typeI18nKey: string, aType: string) {
if (value == null) {
return;
}
this.analytics.eventTrack.next({ action: 'Copied ' + aType });
this.platformUtilsService.copyToClipboard(value, { doc: window.document });
this.toasterService.popAsync('info', null,
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-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) {
this.totpCodeFormatted = this.totpCode.substring(0, 3) + ' ' + this.totpCode.substring(3);
} else {
this.totpCodeFormatted = null;
if (this.totpInterval) {
window.clearInterval(this.totpInterval);
}
}
}
2018-07-05 15:42:50 +02:00
protected async totpTick() {
2018-06-20 05:40:51 +02:00
const epoch = Math.round(new Date().getTime() / 1000.0);
const mod = epoch % 30;
this.totpSec = 30 - mod;
this.totpDash = +(Math.round(((2.62 * mod) + 'e+2') as any) + 'e-2');
this.totpLow = this.totpSec <= 7;
if (mod === 0) {
await this.totpUpdateCode();
}
}
2018-06-06 23:25:57 +02:00
}