2018-10-19 18:44:52 +02:00
|
|
|
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';
|
2019-07-12 16:41:18 +02:00
|
|
|
import { EventType } from 'jslib/enums/eventType';
|
2018-06-20 05:40:51 +02:00
|
|
|
|
2018-06-06 23:25:57 +02:00
|
|
|
import { AuditService } from 'jslib/abstractions/audit.service';
|
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
2018-10-19 18:44:52 +02:00
|
|
|
import { CollectionService } from 'jslib/abstractions/collection.service';
|
2019-07-12 16:41:18 +02:00
|
|
|
import { EventService } from 'jslib/abstractions/event.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',
|
|
|
|
})
|
2018-10-19 18:44:52 +02:00
|
|
|
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,
|
2018-10-19 18:44:52 +02:00
|
|
|
userService: UserService, collectionService: CollectionService,
|
|
|
|
protected totpService: TotpService, protected passwordGenerationService: PasswordGenerationService,
|
2019-07-12 16:41:18 +02:00
|
|
|
protected messagingService: MessagingService, eventService: EventService) {
|
2018-10-19 18:44:52 +02:00
|
|
|
super(cipherService, folderService, i18nService, platformUtilsService, auditService, stateService,
|
2019-07-12 16:41:18 +02:00
|
|
|
userService, collectionService, messagingService, eventService);
|
2018-06-06 23:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 05:23:14 +02:00
|
|
|
async ngOnInit() {
|
2018-10-19 18:44:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:33:04 +02:00
|
|
|
this.platformUtilsService.eventTrack('Launched Login URI');
|
2019-01-07 16:33:27 +01:00
|
|
|
this.platformUtilsService.launchUri(uri.launchUri);
|
2018-06-07 23:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
copy(value: string, typeI18nKey: string, aType: string) {
|
|
|
|
if (value == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:33:04 +02:00
|
|
|
this.platformUtilsService.eventTrack('Copied ' + aType);
|
2018-08-17 18:25:21 +02:00
|
|
|
this.platformUtilsService.copyToClipboard(value, { window: window });
|
2018-10-03 16:33:04 +02:00
|
|
|
this.platformUtilsService.showToast('info', null,
|
2018-06-07 23:12:11 +02:00
|
|
|
this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey)));
|
2019-07-12 16:41:18 +02:00
|
|
|
|
|
|
|
if (this.editMode) {
|
|
|
|
if (typeI18nKey === 'password') {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, this.cipherId);
|
|
|
|
} else if (typeI18nKey === 'securityCode') {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedCardCode, this.cipherId);
|
|
|
|
} else if (aType === 'H_Field') {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientCopiedHiddenField, this.cipherId);
|
|
|
|
}
|
|
|
|
}
|
2018-06-07 23:12:11 +02:00
|
|
|
}
|
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-12-14 20:48:12 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 20:48:12 +01:00
|
|
|
upgradeOrganization() {
|
2018-07-18 15:21:23 +02:00
|
|
|
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
|
|
|
}
|