2018-04-06 05:09:49 +02:00
|
|
|
import { Location } from "@angular/common";
|
|
|
|
import { ChangeDetectorRef, Component, NgZone } from "@angular/core";
|
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-10-14 23:58:59 +02:00
|
|
|
import { first } from "rxjs/operators";
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { AuditService } from "jslib-common/abstractions/audit.service";
|
2021-12-06 12:21:07 +01:00
|
|
|
import { BroadcasterService } from "jslib-common/abstractions/broadcaster.service";
|
2021-06-07 19:25:37 +02:00
|
|
|
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
|
|
|
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
|
|
|
import { EventService } from "jslib-common/abstractions/event.service";
|
|
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
2021-10-21 11:10:46 +02:00
|
|
|
import { LogService } from "jslib-common/abstractions/log.service";
|
2021-06-07 19:25:37 +02:00
|
|
|
import { MessagingService } from "jslib-common/abstractions/messaging.service";
|
|
|
|
import { PasswordRepromptService } from "jslib-common/abstractions/passwordReprompt.service";
|
|
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
|
|
|
import { TokenService } from "jslib-common/abstractions/token.service";
|
|
|
|
import { TotpService } from "jslib-common/abstractions/totp.service";
|
|
|
|
import { UserService } from "jslib-common/abstractions/user.service";
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-09-17 15:44:27 +02:00
|
|
|
import { Cipher } from "jslib-common/models/domain/cipher";
|
|
|
|
import { LoginUriView } from "jslib-common/models/view/loginUriView";
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-09-17 15:44:27 +02:00
|
|
|
import { CipherType } from "jslib-common/enums/cipherType";
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-06-07 19:25:37 +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-12-21 15:43:35 +01:00
|
|
|
|
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;
|
2021-12-21 15:43:35 +01: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,
|
2018-10-03 06:21:22 +02:00
|
|
|
apiService: ApiService,
|
2021-05-03 20:56:38 +02:00
|
|
|
passwordRepromptService: PasswordRepromptService,
|
|
|
|
logService: LogService
|
2021-12-21 15:43:35 +01:00
|
|
|
) {
|
|
|
|
super(
|
2020-08-24 15:10:15 +02:00
|
|
|
cipherService,
|
2018-10-03 06:21:22 +02:00
|
|
|
totpService,
|
|
|
|
tokenService,
|
|
|
|
i18nService,
|
2021-10-21 11:10:46 +02:00
|
|
|
cryptoService,
|
|
|
|
platformUtilsService,
|
|
|
|
auditService,
|
2021-12-21 15:43:35 +01:00
|
|
|
window,
|
2021-10-21 11:10:46 +02:00
|
|
|
broadcasterService,
|
2021-12-21 15:43:35 +01:00
|
|
|
ngZone,
|
2021-10-21 11:10:46 +02:00
|
|
|
changeDetectorRef,
|
2018-10-03 06:21:22 +02:00
|
|
|
userService,
|
2021-05-03 20:56:38 +02:00
|
|
|
eventService,
|
2021-10-21 11:10:46 +02:00
|
|
|
apiService,
|
|
|
|
passwordRepromptService,
|
|
|
|
logService
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-21 11:10:46 +02:00
|
|
|
ngOnInit() {
|
2018-10-03 06:21:22 +02:00
|
|
|
this.inPopout = this.popupUtilsService.inPopout(window);
|
|
|
|
this.route.queryParams.pipe(first()).subscribe(async (params) => {
|
|
|
|
if (params.cipherId) {
|
|
|
|
this.cipherId = params.cipherId;
|
|
|
|
} else {
|
|
|
|
this.close();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 05:09:49 +02:00
|
|
|
await this.load();
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
super.ngOnInit();
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
|
|
|
|
this.ngZone.run(async () => {
|
2020-08-24 15:10:15 +02:00
|
|
|
switch (message.command) {
|
2021-05-03 20:56:38 +02:00
|
|
|
case "collectPageDetailsResponse":
|
|
|
|
if (message.sender === BroadcasterSubscriptionId) {
|
|
|
|
this.pageDetails.push({
|
|
|
|
frameId: message.webExtSender.frameId,
|
|
|
|
tab: message.tab,
|
|
|
|
details: message.details,
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2020-08-24 16:17:15 +02:00
|
|
|
case "tabChanged":
|
2021-05-03 20:56:38 +02:00
|
|
|
case "windowChanged":
|
|
|
|
if (this.loadPageDetailsTimeout != null) {
|
|
|
|
window.clearTimeout(this.loadPageDetailsTimeout);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-10-21 11:10:46 +02:00
|
|
|
this.loadPageDetailsTimeout = window.setTimeout(() => this.loadPageDetails(), 500);
|
|
|
|
break;
|
2021-12-21 15:43:35 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
super.ngOnDestroy();
|
2021-10-21 11:10:46 +02:00
|
|
|
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-21 11:10:46 +02:00
|
|
|
async load() {
|
2020-08-24 16:17:15 +02:00
|
|
|
await super.load();
|
|
|
|
await this.loadPageDetails();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
async edit() {
|
2020-04-13 16:26:11 +02:00
|
|
|
if (this.cipher.isDeleted) {
|
2021-10-21 11:10:46 +02:00
|
|
|
return false;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
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 } });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
async clone() {
|
2020-08-24 16:17:15 +02:00
|
|
|
if (this.cipher.isDeleted) {
|
|
|
|
return false;
|
2020-08-24 15:10:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(await super.clone())) {
|
2020-08-24 16:17:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.router.navigate(["/clone-cipher"], {
|
2020-12-17 18:06:31 +01:00
|
|
|
queryParams: {
|
2020-02-04 22:08:13 +01:00
|
|
|
cloneMode: true,
|
|
|
|
cipherId: this.cipher.id,
|
2021-12-21 15:43:35 +01:00
|
|
|
},
|
|
|
|
});
|
2021-05-03 20:56:38 +02:00
|
|
|
return true;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
async share() {
|
|
|
|
if (!(await super.share())) {
|
|
|
|
return false;
|
2018-04-06 05:09:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 16:26:11 +02:00
|
|
|
if (this.cipher.organizationId == null) {
|
2018-04-06 05:09:49 +02:00
|
|
|
this.router.navigate(["/share-cipher"], {
|
2020-12-17 18:06:31 +01:00
|
|
|
replaceUrl: true,
|
2018-04-06 05:09:49 +02:00
|
|
|
queryParams: { cipherId: this.cipher.id },
|
|
|
|
});
|
|
|
|
}
|
2021-05-03 20:56:38 +02:00
|
|
|
return true;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2018-04-06 05:09:49 +02:00
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
async fillCipher() {
|
|
|
|
const didAutofill = await this.doAutofill();
|
|
|
|
if (didAutofill) {
|
2020-02-04 22:08:13 +01:00
|
|
|
this.platformUtilsService.showToast("success", null, this.i18nService.t("autoFillSuccess"));
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
async fillCipherAndSave() {
|
|
|
|
const didAutofill = await this.doAutofill();
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
if (didAutofill) {
|
2021-05-03 20:56:38 +02:00
|
|
|
if (this.tab == null) {
|
|
|
|
throw new Error("No tab found.");
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-12-17 18:06:31 +01:00
|
|
|
if (this.cipher.login.uris == null) {
|
|
|
|
this.cipher.login.uris = [];
|
|
|
|
} else {
|
|
|
|
if (this.cipher.login.uris.some((uri) => uri.uri === this.tab.url)) {
|
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("autoFillSuccessAndSavedUri")
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
const loginUri = new LoginUriView();
|
2021-05-03 20:56:38 +02:00
|
|
|
loginUri.uri = this.tab.url;
|
2020-08-24 15:10:15 +02:00
|
|
|
this.cipher.login.uris.push(loginUri);
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
try {
|
2020-08-24 15:10:15 +02:00
|
|
|
const cipher: Cipher = await this.cipherService.encrypt(this.cipher);
|
|
|
|
await this.cipherService.saveWithServer(cipher);
|
|
|
|
this.platformUtilsService.showToast(
|
2021-12-21 15:43:35 +01:00
|
|
|
"success",
|
|
|
|
null,
|
2020-08-24 16:17:15 +02:00
|
|
|
this.i18nService.t("autoFillSuccessAndSavedUri")
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
2020-08-24 15:10:15 +02:00
|
|
|
this.messagingService.send("editedCipher");
|
|
|
|
} catch {
|
|
|
|
this.platformUtilsService.showToast("error", null, this.i18nService.t("unexpectedError"));
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-12-17 18:06:31 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-12-17 18:06:31 +01:00
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
async restore() {
|
|
|
|
if (!this.cipher.isDeleted) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-02 19:31:52 +01:00
|
|
|
if (await super.restore()) {
|
2020-08-24 15:10:15 +02:00
|
|
|
this.close();
|
2020-08-24 16:17:15 +02:00
|
|
|
return true;
|
2020-08-24 15:10:15 +02:00
|
|
|
}
|
2021-05-03 20:56:38 +02:00
|
|
|
return false;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2020-08-24 15:10:15 +02:00
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
async delete() {
|
|
|
|
if (await super.delete()) {
|
|
|
|
this.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
close() {
|
|
|
|
this.location.back();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
private async loadPageDetails() {
|
|
|
|
this.pageDetails = [];
|
|
|
|
this.tab = await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
if (this.tab == null) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2020-08-24 16:17:15 +02:00
|
|
|
}
|
|
|
|
BrowserApi.tabSendMessage(this.tab, {
|
|
|
|
command: "collectPageDetails",
|
|
|
|
tab: this.tab,
|
|
|
|
sender: BroadcasterSubscriptionId,
|
|
|
|
});
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:17:15 +02:00
|
|
|
private async doAutofill() {
|
2021-05-03 20:56:38 +02:00
|
|
|
if (!(await this.promptPassword())) {
|
2020-08-24 15:10:15 +02:00
|
|
|
return false;
|
2020-08-24 16:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pageDetails == null || this.pageDetails.length === 0) {
|
|
|
|
this.platformUtilsService.showToast("error", null, this.i18nService.t("autofillError"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-08-24 15:10:15 +02:00
|
|
|
return true;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2018-04-06 05:09:49 +02:00
|
|
|
}
|