1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-09-21 03:01:37 +02:00
bitwarden-desktop/src/app/vault/view.component.ts

116 lines
3.0 KiB
TypeScript
Raw Normal View History

2018-01-24 04:21:14 +01:00
import * as template from './view.component.html';
import {
Component,
2018-01-24 23:41:57 +01:00
EventEmitter,
2018-01-24 04:21:14 +01:00
Input,
OnChanges,
2018-01-25 05:26:40 +01:00
OnDestroy,
2018-01-24 23:41:57 +01:00
Output,
2018-01-24 04:21:14 +01:00
} from '@angular/core';
2018-01-25 05:26:40 +01:00
import { CipherType } from 'jslib/enums/cipherType';
2018-01-24 06:06:05 +01:00
import { CipherService } from 'jslib/abstractions/cipher.service';
2018-01-25 05:26:40 +01:00
import { TokenService } from 'jslib/abstractions/token.service';
import { TotpService } from 'jslib/abstractions/totp.service';
2018-01-24 06:06:05 +01:00
2018-01-24 18:20:01 +01:00
import { CipherView } from 'jslib/models/view/cipherView';
2018-01-24 04:21:14 +01:00
@Component({
selector: 'app-vault-view',
template: template,
})
2018-01-25 05:26:40 +01:00
export class ViewComponent implements OnChanges, OnDestroy {
2018-01-24 04:21:14 +01:00
@Input() cipherId: string;
2018-01-24 23:41:57 +01:00
@Output() onEditCipher = new EventEmitter<string>();
2018-01-24 18:20:01 +01:00
cipher: CipherView;
2018-01-25 05:26:40 +01:00
showPassword: boolean;
isPremium: boolean;
totpCode: string;
totpCodeFormatted: string;
totpDash: number;
totpSec: number;
totpLow: boolean;
private totpInterval: NodeJS.Timer;
2018-01-24 04:21:14 +01:00
2018-01-25 05:26:40 +01:00
constructor(private cipherService: CipherService, private totpService: TotpService,
private tokenService: TokenService) {
2018-01-24 04:21:14 +01:00
}
2018-01-24 06:06:05 +01:00
async ngOnChanges() {
2018-01-25 05:26:40 +01:00
this.showPassword = false;
2018-01-24 06:06:05 +01:00
const cipher = await this.cipherService.get(this.cipherId);
this.cipher = await cipher.decrypt();
2018-01-25 05:26:40 +01:00
this.isPremium = this.tokenService.getPremium();
if (this.cipher.type == CipherType.Login && this.cipher.login.totp &&
(cipher.organizationUseTotp || this.isPremium)) {
await this.totpUpdateCode();
await this.totpTick();
if (this.totpInterval) {
clearInterval(this.totpInterval);
}
this.totpInterval = setInterval(async () => {
await this.totpTick();
}, 1000);
}
}
ngOnDestroy() {
if (this.totpInterval) {
clearInterval(this.totpInterval);
}
2018-01-24 04:21:14 +01:00
}
2018-01-24 23:41:57 +01:00
edit() {
this.onEditCipher.emit(this.cipher.id);
}
2018-01-25 05:26:40 +01:00
togglePassword() {
this.showPassword = !this.showPassword;
}
launch() {
// TODO
}
copy(value: string) {
// TODO
}
private async totpUpdateCode() {
if (this.cipher.type !== CipherType.Login || this.cipher.login.totp == null) {
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) {
clearInterval(this.totpInterval);
}
}
}
private async totpTick() {
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-01-24 04:21:14 +01:00
}