From 0dd1aeba9f4d9bcb2c0171bca3615eb6284277f8 Mon Sep 17 00:00:00 2001 From: Daniel James Smith <2670567+djsmith85@users.noreply.github.com> Date: Tue, 3 Oct 2023 18:33:49 +0200 Subject: [PATCH] [PM-4031] Add libs/importer to browser and desktop (#6373) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Import libs/importer and instantiate ImportService * Create ImportApi and ImportService factories * Add libs/importer to desktop * [PM-4075] Setup Feature Flag for Browser Fileless Import (#6391) * Update apps/browser/src/tools/background/service_factories/import-api-service.factory.ts Co-authored-by: ✨ Audrey ✨ * Created non-exported ServiceCache-type for ImportApiServiceFactory --------- Co-authored-by: Daniel James Smith Co-authored-by: Cesar Gonzalez Co-authored-by: ✨ Audrey ✨ --- .../browser/src/background/main.background.ts | 20 ++++++ .../src/popup/services/services.module.ts | 6 ++ .../import-api-service.factory.ts | 26 ++++++++ .../import-service.factory.ts | 61 +++++++++++++++++++ apps/browser/tsconfig.json | 1 + apps/desktop/tsconfig.json | 1 + .../src/services/jslib-services.module.ts | 23 +++++++ libs/common/src/enums/feature-flag.enum.ts | 1 + tsconfig.eslint.json | 1 + 9 files changed, 140 insertions(+) create mode 100644 apps/browser/src/tools/background/service_factories/import-api-service.factory.ts create mode 100644 apps/browser/src/tools/background/service_factories/import-service.factory.ts diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 7a9863f3a7..b3edd50aa0 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -103,6 +103,12 @@ import { VaultExportService, VaultExportServiceAbstraction, } from "@bitwarden/exporter/vault-export"; +import { + ImportApiServiceAbstraction, + ImportApiService, + ImportServiceAbstraction, + ImportService, +} from "@bitwarden/importer"; import { BrowserOrganizationService } from "../admin-console/services/browser-organization.service"; import { BrowserPolicyService } from "../admin-console/services/browser-policy.service"; @@ -172,6 +178,8 @@ export default class MainBackground { containerService: ContainerService; auditService: AuditServiceAbstraction; authService: AuthServiceAbstraction; + importApiService: ImportApiServiceAbstraction; + importService: ImportServiceAbstraction; exportService: VaultExportServiceAbstraction; searchService: SearchServiceAbstraction; notificationsService: NotificationsServiceAbstraction; @@ -527,6 +535,18 @@ export default class MainBackground { this.userVerificationService ); this.auditService = new AuditService(this.cryptoFunctionService, this.apiService); + + this.importApiService = new ImportApiService(this.apiService); + + this.importService = new ImportService( + this.cipherService, + this.folderService, + this.importApiService, + this.i18nService, + this.collectionService, + this.cryptoService + ); + this.exportService = new VaultExportService( this.folderService, this.cipherService, diff --git a/apps/browser/src/popup/services/services.module.ts b/apps/browser/src/popup/services/services.module.ts index 0ba396af92..c0597e29e4 100644 --- a/apps/browser/src/popup/services/services.module.ts +++ b/apps/browser/src/popup/services/services.module.ts @@ -85,6 +85,7 @@ import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.serv import { FolderApiService } from "@bitwarden/common/vault/services/folder/folder-api.service"; import { DialogService } from "@bitwarden/components"; import { VaultExportServiceAbstraction } from "@bitwarden/exporter/vault-export"; +import { ImportServiceAbstraction } from "@bitwarden/importer"; import { BrowserOrganizationService } from "../../admin-console/services/browser-organization.service"; import { BrowserPolicyService } from "../../admin-console/services/browser-policy.service"; @@ -368,6 +369,11 @@ function getBgService(service: keyof MainBackground) { useFactory: getBgService("autofillService"), deps: [], }, + { + provide: ImportServiceAbstraction, + useFactory: getBgService("importService"), + deps: [], + }, { provide: VaultExportServiceAbstraction, useFactory: getBgService("exportService"), diff --git a/apps/browser/src/tools/background/service_factories/import-api-service.factory.ts b/apps/browser/src/tools/background/service_factories/import-api-service.factory.ts new file mode 100644 index 0000000000..4344647bbe --- /dev/null +++ b/apps/browser/src/tools/background/service_factories/import-api-service.factory.ts @@ -0,0 +1,26 @@ +import { ImportApiService, ImportApiServiceAbstraction } from "@bitwarden/importer"; + +import { + ApiServiceInitOptions, + apiServiceFactory, +} from "../../../platform/background/service-factories/api-service.factory"; +import { + FactoryOptions, + CachedServices, + factory, +} from "../../../platform/background/service-factories/factory-options"; + +export type ImportApiServiceInitOptions = FactoryOptions & ApiServiceInitOptions; +type ServiceCache = { importApiService?: ImportApiServiceAbstraction } & CachedServices; + +export function importApiServiceFactory( + cache: ServiceCache, + opts: ImportApiServiceInitOptions +): Promise { + return factory( + cache, + "importApiService", + opts, + async () => new ImportApiService(await apiServiceFactory(cache, opts)) + ); +} diff --git a/apps/browser/src/tools/background/service_factories/import-service.factory.ts b/apps/browser/src/tools/background/service_factories/import-service.factory.ts new file mode 100644 index 0000000000..3dc9bbd4f0 --- /dev/null +++ b/apps/browser/src/tools/background/service_factories/import-service.factory.ts @@ -0,0 +1,61 @@ +import { ImportService, ImportServiceAbstraction } from "@bitwarden/importer"; + +import { + cryptoServiceFactory, + CryptoServiceInitOptions, +} from "../../../platform/background/service-factories/crypto-service.factory"; +import { + CachedServices, + factory, + FactoryOptions, +} from "../../../platform/background/service-factories/factory-options"; +import { + i18nServiceFactory, + I18nServiceInitOptions, +} from "../../../platform/background/service-factories/i18n-service.factory"; +import { + cipherServiceFactory, + CipherServiceInitOptions, +} from "../../../vault/background/service_factories/cipher-service.factory"; +import { + collectionServiceFactory, + CollectionServiceInitOptions, +} from "../../../vault/background/service_factories/collection-service.factory"; +import { + folderServiceFactory, + FolderServiceInitOptions, +} from "../../../vault/background/service_factories/folder-service.factory"; + +import { importApiServiceFactory, ImportApiServiceInitOptions } from "./import-api-service.factory"; + +type ImportServiceFactoryOptions = FactoryOptions; + +export type ImportServiceInitOptions = ImportServiceFactoryOptions & + CipherServiceInitOptions & + FolderServiceInitOptions & + ImportApiServiceInitOptions & + I18nServiceInitOptions & + CollectionServiceInitOptions & + CryptoServiceInitOptions; + +export function importServiceFactory( + cache: { + importService?: ImportServiceAbstraction; + } & CachedServices, + opts: ImportServiceInitOptions +): Promise { + return factory( + cache, + "importService", + opts, + async () => + new ImportService( + await cipherServiceFactory(cache, opts), + await folderServiceFactory(cache, opts), + await importApiServiceFactory(cache, opts), + await i18nServiceFactory(cache, opts), + await collectionServiceFactory(cache, opts), + await cryptoServiceFactory(cache, opts) + ) + ); +} diff --git a/apps/browser/tsconfig.json b/apps/browser/tsconfig.json index 9624939c87..ecb2f996e5 100644 --- a/apps/browser/tsconfig.json +++ b/apps/browser/tsconfig.json @@ -15,6 +15,7 @@ "@bitwarden/common/*": ["../../libs/common/src/*"], "@bitwarden/components": ["../../libs/components/src"], "@bitwarden/exporter/*": ["../../libs/exporter/src/*"], + "@bitwarden/importer": ["../../libs/importer/src"], "@bitwarden/vault": ["../../libs/auth/src"] }, "useDefineForClassFields": false diff --git a/apps/desktop/tsconfig.json b/apps/desktop/tsconfig.json index d587997f9e..67bfba6442 100644 --- a/apps/desktop/tsconfig.json +++ b/apps/desktop/tsconfig.json @@ -15,6 +15,7 @@ "@bitwarden/common/*": ["../../libs/common/src/*"], "@bitwarden/components": ["../../libs/components/src"], "@bitwarden/exporter/*": ["../../libs/exporter/src/*"], + "@bitwarden/importer": ["../../libs/importer/src"], "@bitwarden/vault": ["../../libs/vault/src"] }, "useDefineForClassFields": false diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index 5c19d0fe77..bbea250bcd 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -150,6 +150,12 @@ import { VaultExportService, VaultExportServiceAbstraction, } from "@bitwarden/exporter/vault-export"; +import { + ImportApiService, + ImportApiServiceAbstraction, + ImportService, + ImportServiceAbstraction, +} from "@bitwarden/importer"; import { AuthGuard } from "../auth/guards/auth.guard"; import { UnauthGuard } from "../auth/guards/unauth.guard"; @@ -485,6 +491,23 @@ import { AbstractThemingService } from "./theming/theming.service.abstraction"; STATE_SERVICE_USE_CACHE, ], }, + { + provide: ImportApiServiceAbstraction, + useClass: ImportApiService, + deps: [ApiServiceAbstraction], + }, + { + provide: ImportServiceAbstraction, + useClass: ImportService, + deps: [ + CipherServiceAbstraction, + FolderServiceAbstraction, + ImportApiServiceAbstraction, + I18nServiceAbstraction, + CollectionServiceAbstraction, + CryptoServiceAbstraction, + ], + }, { provide: VaultExportServiceAbstraction, useClass: VaultExportService, diff --git a/libs/common/src/enums/feature-flag.enum.ts b/libs/common/src/enums/feature-flag.enum.ts index f87a8ef52c..9ca25ab287 100644 --- a/libs/common/src/enums/feature-flag.enum.ts +++ b/libs/common/src/enums/feature-flag.enum.ts @@ -3,6 +3,7 @@ export enum FeatureFlag { DisplayLowKdfIterationWarningFlag = "display-kdf-iteration-warning", TrustedDeviceEncryption = "trusted-device-encryption", AutofillV2 = "autofill-v2", + BrowserFilelessImport = "browser-fileless-import", } // Replace this with a type safe lookup of the feature flag values in PM-2282 diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json index 1a435e93c3..d96a144ebc 100644 --- a/tsconfig.eslint.json +++ b/tsconfig.eslint.json @@ -20,6 +20,7 @@ "@bitwarden/common/*": ["./libs/common/src/*"], "@bitwarden/components": ["./libs/components/src"], "@bitwarden/exporter/*": ["./libs/exporter/src/*"], + "@bitwarden/importer": ["./libs/importer/src"], "@bitwarden/node/*": ["./libs/node/src/*"], "@bitwarden/vault": ["./libs/vault/src"] },