1
0
mirror of https://github.com/bitwarden/jslib synced 2025-12-18 17:23:30 +00:00

Move decrypt to FolderView

This commit is contained in:
Hinton
2022-04-20 10:50:51 +02:00
parent fc67f99521
commit 708bfcb026
7 changed files with 64 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { FolderService } from "jslib-common/abstractions/folder.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { LogService } from "jslib-common/abstractions/log.service";
@@ -19,10 +20,11 @@ export class FolderAddEditComponent implements OnInit {
deletePromise: Promise<any>;
constructor(
protected cryptoService: CryptoService,
protected folderService: FolderService,
protected i18nService: I18nService,
protected platformUtilsService: PlatformUtilsService,
private logService: LogService
protected logService: LogService,
protected platformUtilsService: PlatformUtilsService
) {}
async ngOnInit() {
@@ -88,7 +90,7 @@ export class FolderAddEditComponent implements OnInit {
this.editMode = true;
this.title = this.i18nService.t("editFolder");
const folder = await this.folderService.get(this.folderId);
this.folder = await this.folderService.decrypt(folder);
this.folder = await FolderView.fromFolder(this.cryptoService, folder);
} else {
this.title = this.i18nService.t("addFolder");
}

View File

@@ -1,4 +1,11 @@
import Substitute from "@fluffy-spoon/substitute";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { FolderData } from "jslib-common/models/data/folderData";
import { Folder } from "jslib-common/models/domain/folder";
import { FolderView } from "jslib-common/models/view/folderView";
import { mockEnc } from "../utils";
describe("Folder", () => {
it("Convert", () => {
@@ -15,4 +22,21 @@ describe("Folder", () => {
revisionDate: new Date("2022-01-31T12:00:00.000Z"),
});
});
it("Decrypt", async () => {
const folder: Folder = {
id: "id",
name: mockEnc("encName"),
revisionDate: new Date("2022-01-31T12:00:00.000Z"),
};
const cryptoService = Substitute.for<CryptoService>();
const view = await FolderView.fromFolder(cryptoService, folder);
expect(view).toEqual({
id: "id",
name: "encName",
revisionDate: new Date("2022-01-31T12:00:00.000Z"),
});
});
});

View File

@@ -111,20 +111,4 @@ describe("FolderService", () => {
expect(folders).toHaveLength(0);
});
});
it("Decrypt", async () => {
const folder: Folder = {
id: "id",
name: mockEnc("encName"),
revisionDate: new Date("2022-01-31T12:00:00.000Z"),
};
const view = await folderService.decrypt(folder);
expect(view).toEqual({
id: "id",
name: "encName",
revisionDate: new Date("2022-01-31T12:00:00.000Z"),
});
});
});

View File

@@ -7,7 +7,6 @@ import { FolderView } from "../models/view/folderView";
export abstract class FolderService {
clearCache: (userId?: string) => Promise<void>;
encrypt: (model: FolderView, key?: SymmetricCryptoKey) => Promise<Folder>;
decrypt: (model: Folder, key?: SymmetricCryptoKey) => Promise<FolderView>;
get: (id: string) => Promise<Folder>;
getAll: () => Promise<Folder[]>;
getAllDecrypted: () => Promise<FolderView[]>;

View File

@@ -81,7 +81,7 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer {
const folder = FolderWithIdExport.toDomain(f);
if (folder != null) {
folder.id = null;
const view = await this.folderService.decrypt(folder);
const view = await FolderView.fromFolder(this.cryptoService, folder);
groupingsMap.set(f.id, this.result.folders.length);
this.result.folders.push(view);
}

View File

@@ -96,11 +96,10 @@ export class EncString {
}
}
/**
* @deprecated As of 2022-04-20, use {@link decryptWithCryptoService} instead
*/
async decrypt(orgId: string, key: SymmetricCryptoKey = null): Promise<string> {
if (this.decryptedValue != null) {
return this.decryptedValue;
}
let cryptoService: CryptoService;
const containerService = (Utils.global as any).bitwardenContainerService;
if (containerService) {
@@ -109,8 +108,20 @@ export class EncString {
throw new Error("global bitwardenContainerService not initialized.");
}
return this.decryptWithCryptoService(cryptoService, orgId, key);
}
async decryptWithCryptoService(
cryptoService: CryptoService,
orgId?: string,
key: SymmetricCryptoKey = null
): Promise<string> {
if (this.decryptedValue != null) {
return this.decryptedValue;
}
try {
if (key == null) {
if (orgId != null && key == null) {
key = await cryptoService.getOrgKey(orgId);
}
this.decryptedValue = await cryptoService.decryptToUtf8(this, key);

View File

@@ -1,9 +1,24 @@
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { Folder } from "../domain/folder";
import { SymmetricCryptoKey } from "../domain/symmetricCryptoKey";
import { ITreeNodeObject } from "../domain/treeNode";
import { View } from "./view";
export class FolderView implements View, ITreeNodeObject {
export class FolderView implements ITreeNodeObject {
id: string = null;
name: string = null;
revisionDate: Date = null;
static async fromFolder(
cryptoService: CryptoService,
folder: Folder,
key?: SymmetricCryptoKey
): Promise<FolderView> {
const view = new FolderView();
view.id = folder.id;
view.name = await folder.name.decryptWithCryptoService(cryptoService, null, key);
view.revisionDate = folder.revisionDate;
return view;
}
}