mirror of
https://github.com/bitwarden/browser
synced 2026-01-02 00:23:35 +00:00
Feature/password protected export (#612)
* Add password protected export * Run prettier * Test password protected export service * Create type for known import type strings * Test import service changes * Test bitwarden password importer * Run prettier * Remove unnecessary class properties * Run prettier * Tslint fixes * Add KdfType to password protected export * Linter fixes * run prettier
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
||||
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
||||
|
||||
import { ApiService } from "jslib-common/abstractions/api.service";
|
||||
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
||||
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
||||
import { CryptoFunctionService } from "jslib-common/abstractions/cryptoFunction.service";
|
||||
import { FolderService } from "jslib-common/abstractions/folder.service";
|
||||
|
||||
import { ExportService } from "jslib-common/services/export.service";
|
||||
@@ -13,6 +14,9 @@ import { Login } from "jslib-common/models/domain/login";
|
||||
import { CipherWithIds as CipherExport } from "jslib-common/models/export/cipherWithIds";
|
||||
|
||||
import { CipherType } from "jslib-common/enums/cipherType";
|
||||
import { KdfType } from "jslib-common/enums/kdfType";
|
||||
|
||||
import { Utils } from "jslib-common/misc/utils";
|
||||
import { CipherView } from "jslib-common/models/view/cipherView";
|
||||
import { LoginView } from "jslib-common/models/view/loginView";
|
||||
|
||||
@@ -85,12 +89,14 @@ function expectEqualCiphers(ciphers: CipherView[] | Cipher[], jsonResult: string
|
||||
describe("ExportService", () => {
|
||||
let exportService: ExportService;
|
||||
let apiService: SubstituteOf<ApiService>;
|
||||
let cryptoFunctionService: SubstituteOf<CryptoFunctionService>;
|
||||
let cipherService: SubstituteOf<CipherService>;
|
||||
let folderService: SubstituteOf<FolderService>;
|
||||
let cryptoService: SubstituteOf<CryptoService>;
|
||||
|
||||
beforeEach(() => {
|
||||
apiService = Substitute.for<ApiService>();
|
||||
cryptoFunctionService = Substitute.for<CryptoFunctionService>();
|
||||
cipherService = Substitute.for<CipherService>();
|
||||
folderService = Substitute.for<FolderService>();
|
||||
cryptoService = Substitute.for<CryptoService>();
|
||||
@@ -98,7 +104,13 @@ describe("ExportService", () => {
|
||||
folderService.getAllDecrypted().resolves([]);
|
||||
folderService.getAll().resolves([]);
|
||||
|
||||
exportService = new ExportService(folderService, cipherService, apiService, cryptoService);
|
||||
exportService = new ExportService(
|
||||
folderService,
|
||||
cipherService,
|
||||
apiService,
|
||||
cryptoService,
|
||||
cryptoFunctionService
|
||||
);
|
||||
});
|
||||
|
||||
it("exports unecrypted user ciphers", async () => {
|
||||
@@ -132,4 +144,68 @@ describe("ExportService", () => {
|
||||
|
||||
expectEqualCiphers(UserCipherDomains.slice(0, 2), actual);
|
||||
});
|
||||
|
||||
describe("password protected export", () => {
|
||||
let exportString: string;
|
||||
let exportObject: any;
|
||||
let mac: SubstituteOf<EncString>;
|
||||
let data: SubstituteOf<EncString>;
|
||||
const password = "password";
|
||||
const salt = "salt";
|
||||
|
||||
describe("export json object", () => {
|
||||
beforeEach(async () => {
|
||||
mac = Substitute.for<EncString>();
|
||||
data = Substitute.for<EncString>();
|
||||
|
||||
mac.encryptedString = "mac";
|
||||
data.encryptedString = "encData";
|
||||
|
||||
spyOn(Utils, "fromBufferToB64").and.returnValue(salt);
|
||||
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
|
||||
|
||||
exportString = await exportService.getPasswordProtectedExport(password);
|
||||
exportObject = JSON.parse(exportString);
|
||||
});
|
||||
|
||||
it("specifies it is encrypted", () => {
|
||||
expect(exportObject.encrypted).toBe(true);
|
||||
});
|
||||
|
||||
it("specifies it's password protected", () => {
|
||||
expect(exportObject.passwordProtected).toBe(true);
|
||||
});
|
||||
|
||||
it("specifies format", () => {
|
||||
expect(exportObject).toEqual(jasmine.objectContaining({ format: jasmine.any(String) }));
|
||||
});
|
||||
|
||||
it("specifies salt", () => {
|
||||
expect(exportObject.salt).toEqual("salt");
|
||||
});
|
||||
|
||||
it("specifies kdfIterations", () => {
|
||||
expect(exportObject.kdfIterations).toEqual(100000);
|
||||
});
|
||||
|
||||
it("has kdfType", () => {
|
||||
expect(exportObject.kdfType).toEqual(KdfType.PBKDF2_SHA256);
|
||||
});
|
||||
|
||||
it("has a mac property", () => {
|
||||
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(mac);
|
||||
expect(exportObject.encKeyValidation_DO_NOT_EDIT).toEqual(mac.encryptedString);
|
||||
});
|
||||
|
||||
it("has data property", () => {
|
||||
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(data);
|
||||
expect(exportObject.data).toEqual(data.encryptedString);
|
||||
});
|
||||
|
||||
it("encrypts the data property", async () => {
|
||||
const unencrypted = await exportService.getExport();
|
||||
expect(exportObject.data).not.toEqual(unencrypted);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
74
spec/common/services/import.service.spec.ts
Normal file
74
spec/common/services/import.service.spec.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import Substitute, { Arg, SubstituteOf } from "@fluffy-spoon/substitute";
|
||||
import { ApiService } from "jslib-common/abstractions/api.service";
|
||||
|
||||
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
||||
import { CollectionService } from "jslib-common/abstractions/collection.service";
|
||||
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 { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
||||
import { BitwardenPasswordProtectedImporter } from "jslib-common/importers/bitwardenPasswordProtectedImporter";
|
||||
|
||||
import { Importer } from "jslib-common/importers/importer";
|
||||
import { Utils } from "jslib-common/misc/utils";
|
||||
|
||||
import { ImportService } from "jslib-common/services/import.service";
|
||||
|
||||
describe("ImportService", () => {
|
||||
let importService: ImportService;
|
||||
let cipherService: SubstituteOf<CipherService>;
|
||||
let folderService: SubstituteOf<FolderService>;
|
||||
let apiService: SubstituteOf<ApiService>;
|
||||
let i18nService: SubstituteOf<I18nService>;
|
||||
let collectionService: SubstituteOf<CollectionService>;
|
||||
let platformUtilsService: SubstituteOf<PlatformUtilsService>;
|
||||
let cryptoService: SubstituteOf<CryptoService>;
|
||||
|
||||
beforeEach(() => {
|
||||
cipherService = Substitute.for<CipherService>();
|
||||
folderService = Substitute.for<FolderService>();
|
||||
apiService = Substitute.for<ApiService>();
|
||||
i18nService = Substitute.for<I18nService>();
|
||||
collectionService = Substitute.for<CollectionService>();
|
||||
platformUtilsService = Substitute.for<PlatformUtilsService>();
|
||||
cryptoService = Substitute.for<CryptoService>();
|
||||
|
||||
importService = new ImportService(
|
||||
cipherService,
|
||||
folderService,
|
||||
apiService,
|
||||
i18nService,
|
||||
collectionService,
|
||||
platformUtilsService,
|
||||
cryptoService
|
||||
);
|
||||
});
|
||||
|
||||
describe("getImporterInstance", () => {
|
||||
describe("Get bitPasswordProtected importer", () => {
|
||||
let importer: Importer;
|
||||
const organizationId = Utils.newGuid();
|
||||
const password = Utils.newGuid();
|
||||
|
||||
beforeEach(() => {
|
||||
importer = importService.getImporter(
|
||||
"bitwardenpasswordprotected",
|
||||
organizationId,
|
||||
password
|
||||
);
|
||||
});
|
||||
|
||||
it("returns an instance of BitwardenPasswordProtectedImporter", () => {
|
||||
expect(importer).toBeInstanceOf(BitwardenPasswordProtectedImporter);
|
||||
});
|
||||
|
||||
it("has the appropriate organization Id", () => {
|
||||
expect(importer.organizationId).toEqual(organizationId);
|
||||
});
|
||||
|
||||
it("has the appropriate password", () => {
|
||||
expect(Object.entries(importer)).toEqual(jasmine.arrayContaining([["password", password]]));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user