2018-04-06 05:09:49 +02:00
|
|
|
import { Location } from '@angular/common';
|
|
|
|
import {
|
2018-08-20 23:40:39 +02:00
|
|
|
ChangeDetectorRef,
|
2018-04-06 05:09:49 +02:00
|
|
|
Component,
|
2018-08-20 23:40:39 +02:00
|
|
|
NgZone,
|
2018-04-06 05:09:49 +02:00
|
|
|
} from '@angular/core';
|
|
|
|
import {
|
|
|
|
ActivatedRoute,
|
|
|
|
Router,
|
|
|
|
} from '@angular/router';
|
|
|
|
|
2021-04-22 16:40:31 +02:00
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
2018-04-06 05:09:49 +02:00
|
|
|
import { AuditService } from 'jslib/abstractions/audit.service';
|
|
|
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
2019-07-09 19:56:13 +02:00
|
|
|
import { EventService } from 'jslib/abstractions/event.service';
|
2018-04-06 05:09:49 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2020-08-24 15:10:15 +02:00
|
|
|
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
2021-05-03 20:56:38 +02:00
|
|
|
import { PasswordRepromptService } from 'jslib/abstractions/passwordReprompt.service';
|
2018-04-06 05:09:49 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
|
|
|
import { TokenService } from 'jslib/abstractions/token.service';
|
|
|
|
import { TotpService } from 'jslib/abstractions/totp.service';
|
2018-08-29 05:17:39 +02:00
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
2020-08-24 15:10:15 +02:00
|
|
|
import { Cipher } from 'jslib/models/domain';
|
|
|
|
import { LoginUriView } from 'jslib/models/view';
|
2018-04-06 05:09:49 +02:00
|
|
|
|
2018-08-20 23:40:39 +02:00
|
|
|
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
|
|
|
|
|
2018-04-06 05:09:49 +02:00
|
|
|
import { ViewComponent as BaseViewComponent } from 'jslib/angular/components/view.component';
|
2020-08-24 15:10:15 +02:00
|
|
|
import { BrowserApi } from '../../browser/browserApi';
|
|
|
|
import { AutofillService } from '../../services/abstractions/autofill.service';
|
|
|
|
import { PopupUtilsService } from '../services/popup-utils.service';
|
|
|
|
|
2021-03-19 02:07:22 +01:00
|
|
|
import { CipherType } from 'jslib/enums';
|
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
const BroadcasterSubscriptionId = 'ChildViewComponent';
|
2018-04-06 05:09:49 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-vault-view',
|
2018-04-06 17:48:45 +02:00
|
|
|
templateUrl: 'view.component.html',
|
2018-04-06 05:09:49 +02:00
|
|
|
})
|
2018-08-20 23:40:39 +02:00
|
|
|
export class ViewComponent extends BaseViewComponent {
|
2018-07-25 05:23:44 +02:00
|
|
|
showAttachments = true;
|
2020-08-24 15:10:15 +02:00
|
|
|
pageDetails: any[] = [];
|
2020-08-24 16:17:15 +02:00
|
|
|
tab: any;
|
|
|
|
loadPageDetailsTimeout: number;
|
|
|
|
inPopout = false;
|
2021-03-19 02:07:22 +01:00
|
|
|
cipherType = CipherType;
|
2018-04-14 20:56:30 +02:00
|
|
|
|
2018-04-06 05:09:49 +02:00
|
|
|
constructor(cipherService: CipherService, totpService: TotpService,
|
2018-10-03 06:21:22 +02:00
|
|
|
tokenService: TokenService, i18nService: I18nService,
|
2018-04-06 05:09:49 +02:00
|
|
|
cryptoService: CryptoService, platformUtilsService: PlatformUtilsService,
|
|
|
|
auditService: AuditService, private route: ActivatedRoute,
|
2018-08-20 23:40:39 +02:00
|
|
|
private router: Router, private location: Location,
|
|
|
|
broadcasterService: BroadcasterService, ngZone: NgZone,
|
2019-07-09 19:56:13 +02:00
|
|
|
changeDetectorRef: ChangeDetectorRef, userService: UserService,
|
2021-05-03 20:56:38 +02:00
|
|
|
eventService: EventService, private autofillService: AutofillService,
|
|
|
|
private messagingService: MessagingService, private popupUtilsService: PopupUtilsService,
|
|
|
|
apiService: ApiService, passwordRepromptService: PasswordRepromptService) {
|
2018-10-03 06:21:22 +02:00
|
|
|
super(cipherService, totpService, tokenService, i18nService, cryptoService, platformUtilsService,
|
2021-05-03 20:56:38 +02:00
|
|
|
auditService, window, broadcasterService, ngZone, changeDetectorRef, userService, eventService,
|
|
|
|
apiService, passwordRepromptService);
|
2018-04-06 05:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-08-24 15:10:15 +02:00
|
|
|
this.inPopout = this.popupUtilsService.inPopout(window);
|
2021-03-02 19:31:52 +01:00
|
|
|
const queryParamsSub = this.route.queryParams.subscribe(async params => {
|
2018-04-06 05:09:49 +02:00
|
|
|
if (params.cipherId) {
|
|
|
|
this.cipherId = params.cipherId;
|
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.load();
|
2019-01-17 05:30:39 +01:00
|
|
|
if (queryParamsSub != null) {
|
|
|
|
queryParamsSub.unsubscribe();
|
|
|
|
}
|
2018-04-06 05:09:49 +02:00
|
|
|
});
|
2020-08-24 16:17:15 +02:00
|
|
|
|
2018-08-20 23:40:39 +02:00
|
|
|
super.ngOnInit();
|
2020-08-24 15:10:15 +02:00
|
|
|
|
|
|
|
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
|
|
|
|
this.ngZone.run(async () => {
|
|
|
|
switch (message.command) {
|
|
|
|
case 'collectPageDetailsResponse':
|
|
|
|
if (message.sender === BroadcasterSubscriptionId) {
|
|
|
|
this.pageDetails.push({
|
|
|
|
frameId: message.webExtSender.frameId,
|
|
|
|
tab: message.tab,
|
|
|
|
details: message.details,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2020-08-24 16:17:15 +02:00
|
|
|
case 'tabChanged':
|
|
|
|
case 'windowChanged':
|
|
|
|
if (this.loadPageDetailsTimeout != null) {
|
|
|
|
window.clearTimeout(this.loadPageDetailsTimeout);
|
|
|
|
}
|
|
|
|
this.loadPageDetailsTimeout = window.setTimeout(() => this.loadPageDetails(), 500);
|
|
|
|
break;
|
2020-08-24 15:10:15 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
super.ngOnDestroy();
|
2020-08-24 16:17:15 +02:00
|
|
|
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await super.load();
|
|
|
|
await this.loadPageDetails();
|
2018-04-06 05:09:49 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
async edit() {
|
2020-04-13 16:26:11 +02:00
|
|
|
if (this.cipher.isDeleted) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-03 20:56:38 +02:00
|
|
|
if (!await super.edit()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-06 05:09:49 +02:00
|
|
|
this.router.navigate(['/edit-cipher'], { queryParams: { cipherId: this.cipher.id } });
|
2021-05-03 20:56:38 +02:00
|
|
|
return true;
|
2018-04-06 05:09:49 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
async clone() {
|
2020-04-13 16:26:11 +02:00
|
|
|
if (this.cipher.isDeleted) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-03 20:56:38 +02:00
|
|
|
|
|
|
|
if (!await super.clone()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:08:13 +01:00
|
|
|
this.router.navigate(['/clone-cipher'], {
|
|
|
|
queryParams: {
|
|
|
|
cloneMode: true,
|
|
|
|
cipherId: this.cipher.id,
|
|
|
|
},
|
|
|
|
});
|
2021-05-03 20:56:38 +02:00
|
|
|
return true;
|
2020-02-04 22:08:13 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
async share() {
|
|
|
|
if (!await super.share()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:06:31 +01:00
|
|
|
if (this.cipher.organizationId == null) {
|
|
|
|
this.router.navigate(['/share-cipher'], { replaceUrl: true, queryParams: { cipherId: this.cipher.id } });
|
|
|
|
}
|
2021-05-03 20:56:38 +02:00
|
|
|
return true;
|
2020-12-17 18:06:31 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
async fillCipher() {
|
2020-08-24 16:17:15 +02:00
|
|
|
const didAutofill = await this.doAutofill();
|
2020-08-24 15:10:15 +02:00
|
|
|
if (didAutofill) {
|
|
|
|
this.platformUtilsService.showToast('success', null,
|
|
|
|
this.i18nService.t('autoFillSuccess'));
|
2020-08-24 16:17:15 +02:00
|
|
|
}
|
2020-08-24 15:10:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fillCipherAndSave() {
|
2020-08-24 16:17:15 +02:00
|
|
|
const didAutofill = await this.doAutofill();
|
2020-08-24 15:10:15 +02:00
|
|
|
|
|
|
|
if (didAutofill) {
|
2020-08-24 16:17:15 +02:00
|
|
|
if (this.tab == null) {
|
2020-08-24 15:10:15 +02:00
|
|
|
throw new Error('No tab found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.cipher.login.uris == null) {
|
|
|
|
this.cipher.login.uris = [];
|
|
|
|
} else {
|
2021-03-02 19:31:52 +01:00
|
|
|
if (this.cipher.login.uris.some(uri => uri.uri === this.tab.url)) {
|
2020-08-24 15:10:15 +02:00
|
|
|
this.platformUtilsService.showToast('success', null,
|
2020-08-24 16:17:15 +02:00
|
|
|
this.i18nService.t('autoFillSuccessAndSavedUri'));
|
2020-08-24 15:10:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
const loginUri = new LoginUriView();
|
|
|
|
loginUri.uri = this.tab.url;
|
2020-08-24 15:10:15 +02:00
|
|
|
this.cipher.login.uris.push(loginUri);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const cipher: Cipher = await this.cipherService.encrypt(this.cipher);
|
|
|
|
await this.cipherService.saveWithServer(cipher);
|
|
|
|
this.platformUtilsService.showToast('success', null,
|
2020-08-24 16:17:15 +02:00
|
|
|
this.i18nService.t('autoFillSuccessAndSavedUri'));
|
2020-08-24 15:10:15 +02:00
|
|
|
this.messagingService.send('editedCipher');
|
|
|
|
} catch {
|
|
|
|
this.platformUtilsService.showToast('error', null,
|
|
|
|
this.i18nService.t('unexpectedError'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
async restore() {
|
|
|
|
if (!this.cipher.isDeleted) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (await super.restore()) {
|
|
|
|
this.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete() {
|
|
|
|
if (await super.delete()) {
|
|
|
|
this.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.location.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadPageDetails() {
|
|
|
|
this.pageDetails = [];
|
|
|
|
this.tab = await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
if (this.tab == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BrowserApi.tabSendMessage(this.tab, {
|
|
|
|
command: 'collectPageDetails',
|
|
|
|
tab: this.tab,
|
|
|
|
sender: BroadcasterSubscriptionId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async doAutofill() {
|
2021-05-03 20:56:38 +02:00
|
|
|
if (!await this.promptPassword()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
if (this.pageDetails == null || this.pageDetails.length === 0) {
|
|
|
|
this.platformUtilsService.showToast('error', null,
|
|
|
|
this.i18nService.t('autofillError'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.totpCode = await this.autofillService.doAutoFill({
|
|
|
|
cipher: this.cipher,
|
|
|
|
pageDetails: this.pageDetails,
|
|
|
|
doc: window.document,
|
2020-09-21 21:41:06 +02:00
|
|
|
fillNewPassword: true,
|
2020-08-24 15:10:15 +02:00
|
|
|
});
|
|
|
|
if (this.totpCode != null) {
|
|
|
|
this.platformUtilsService.copyToClipboard(this.totpCode, { window: window });
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
this.platformUtilsService.showToast('error', null,
|
|
|
|
this.i18nService.t('autofillError'));
|
|
|
|
this.changeDetectorRef.detectChanges();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-06 05:09:49 +02:00
|
|
|
}
|