mirror of
https://github.com/bitwarden/browser
synced 2026-02-12 06:23:38 +00:00
Prefer static classes for pure functions
This commit is contained in:
@@ -92,7 +92,6 @@ import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platfor
|
||||
import { KeyGenerationService as KeyGenerationServiceAbstraction } from "@bitwarden/common/platform/abstractions/key-generation.service";
|
||||
import { LogService as LogServiceAbstraction } from "@bitwarden/common/platform/abstractions/log.service";
|
||||
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||
import { SdkPureClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
|
||||
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
|
||||
import { StateService as StateServiceAbstraction } from "@bitwarden/common/platform/abstractions/state.service";
|
||||
@@ -259,10 +258,7 @@ import { LocalBackedSessionStorageService } from "../platform/services/local-bac
|
||||
import { BackgroundPlatformUtilsService } from "../platform/services/platform-utils/background-platform-utils.service";
|
||||
import { BrowserPlatformUtilsService } from "../platform/services/platform-utils/browser-platform-utils.service";
|
||||
import { PopupViewCacheBackgroundService } from "../platform/services/popup-view-cache-background.service";
|
||||
import {
|
||||
BrowserSdkClientFactory,
|
||||
BrowserSdkPureClientFactory,
|
||||
} from "../platform/services/sdk/browser-sdk-client-factory";
|
||||
import { BrowserSdkClientFactory } from "../platform/services/sdk/browser-sdk-client-factory";
|
||||
import { BrowserSdkLoadService } from "../platform/services/sdk/browser-sdk-load.service";
|
||||
import { BackgroundTaskSchedulerService } from "../platform/services/task-scheduler/background-task-scheduler.service";
|
||||
import { BackgroundMemoryStorageService } from "../platform/storage/background-memory-storage.service";
|
||||
@@ -384,7 +380,6 @@ export default class MainBackground {
|
||||
autoSubmitLoginBackground: AutoSubmitLoginBackground;
|
||||
sdkService: SdkService;
|
||||
sdkLoadService: SdkLoadService;
|
||||
pureSdkClientFactory: SdkPureClientFactory;
|
||||
cipherAuthorizationService: CipherAuthorizationService;
|
||||
inlineMenuFieldQualificationService: InlineMenuFieldQualificationService;
|
||||
|
||||
@@ -502,19 +497,13 @@ export default class MainBackground {
|
||||
return derivedKey;
|
||||
});
|
||||
|
||||
this.pureSdkClientFactory = new BrowserSdkPureClientFactory();
|
||||
this.largeObjectMemoryStorageForStateProviders = new LocalBackedSessionStorageService(
|
||||
sessionKey,
|
||||
this.storageService,
|
||||
// For local backed session storage, we expect that the encrypted data on disk will persist longer than the encryption key in memory
|
||||
// and failures to decrypt because of that are completely expected. For this reason, we pass in `false` to the `EncryptServiceImplementation`
|
||||
// so that MAC failures are not logged.
|
||||
new EncryptServiceImplementation(
|
||||
this.pureSdkClientFactory,
|
||||
this.cryptoFunctionService,
|
||||
this.logService,
|
||||
false,
|
||||
),
|
||||
new EncryptServiceImplementation(this.cryptoFunctionService, this.logService, false),
|
||||
this.platformUtilsService,
|
||||
this.logService,
|
||||
);
|
||||
@@ -550,18 +539,8 @@ export default class MainBackground {
|
||||
);
|
||||
|
||||
this.encryptService = BrowserApi.isManifestVersion(2)
|
||||
? new EncryptServiceImplementation(
|
||||
this.pureSdkClientFactory,
|
||||
this.cryptoFunctionService,
|
||||
this.logService,
|
||||
true,
|
||||
)
|
||||
: new EncryptServiceImplementation(
|
||||
this.pureSdkClientFactory,
|
||||
this.cryptoFunctionService,
|
||||
this.logService,
|
||||
true,
|
||||
);
|
||||
? new EncryptServiceImplementation(this.cryptoFunctionService, this.logService, true)
|
||||
: new EncryptServiceImplementation(this.cryptoFunctionService, this.logService, true);
|
||||
|
||||
this.singleUserStateProvider = new DefaultSingleUserStateProvider(
|
||||
storageServiceProvider,
|
||||
|
||||
@@ -1,16 +1,5 @@
|
||||
import {
|
||||
SdkClientFactory,
|
||||
SdkPureClientFactory,
|
||||
} from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import type { BitwardenClient, BitwardenPure } from "@bitwarden/sdk-internal";
|
||||
|
||||
export class BrowserSdkPureClientFactory implements SdkPureClientFactory {
|
||||
async createPureSdkClient(): Promise<BitwardenPure> {
|
||||
const instance = (globalThis as any).init_pure();
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
import { SdkClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import type { BitwardenClient } from "@bitwarden/sdk-internal";
|
||||
|
||||
/**
|
||||
* SDK client factory with a js fallback for when WASM is not supported.
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import * as sdk from "@bitwarden/sdk-internal";
|
||||
import * as wasm from "@bitwarden/sdk-internal/bitwarden_wasm_internal_bg.wasm.js";
|
||||
|
||||
(globalThis as any).init_pure = () => {
|
||||
(sdk as any).init(wasm);
|
||||
|
||||
return new sdk.BitwardenPure();
|
||||
};
|
||||
(globalThis as any).init_sdk = (...args: ConstructorParameters<typeof sdk.BitwardenClient>) => {
|
||||
(sdk as any).init(wasm);
|
||||
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import * as sdk from "@bitwarden/sdk-internal";
|
||||
import * as wasm from "@bitwarden/sdk-internal/bitwarden_wasm_internal_bg.wasm";
|
||||
|
||||
(globalThis as any).init_pure = () => {
|
||||
(sdk as any).init(wasm);
|
||||
|
||||
return new sdk.BitwardenPure();
|
||||
};
|
||||
(globalThis as any).init_sdk = (...args: ConstructorParameters<typeof sdk.BitwardenClient>) => {
|
||||
(sdk as any).init(wasm);
|
||||
|
||||
|
||||
@@ -70,10 +70,7 @@ import { KeyGenerationService } from "@bitwarden/common/platform/abstractions/ke
|
||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||
import { MessagingService as MessagingServiceAbstraction } from "@bitwarden/common/platform/abstractions/messaging.service";
|
||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||
import {
|
||||
SdkClientFactory,
|
||||
SdkPureClientFactory,
|
||||
} from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
|
||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
||||
import {
|
||||
@@ -87,10 +84,7 @@ import { flagEnabled } from "@bitwarden/common/platform/misc/flags";
|
||||
import { TaskSchedulerService } from "@bitwarden/common/platform/scheduling";
|
||||
import { ConsoleLogService } from "@bitwarden/common/platform/services/console-log.service";
|
||||
import { ContainerService } from "@bitwarden/common/platform/services/container.service";
|
||||
import {
|
||||
NoopSdkClientFactory,
|
||||
NoopSdkPureClientFactory,
|
||||
} from "@bitwarden/common/platform/services/sdk/noop-sdk-client-factory";
|
||||
import { NoopSdkClientFactory } from "@bitwarden/common/platform/services/sdk/noop-sdk-client-factory";
|
||||
import { StorageServiceProvider } from "@bitwarden/common/platform/services/storage-service.provider";
|
||||
import { WebCryptoFunctionService } from "@bitwarden/common/platform/services/web-crypto-function.service";
|
||||
import {
|
||||
@@ -151,10 +145,7 @@ import BrowserMemoryStorageService from "../../platform/services/browser-memory-
|
||||
import { BrowserScriptInjectorService } from "../../platform/services/browser-script-injector.service";
|
||||
import I18nService from "../../platform/services/i18n.service";
|
||||
import { ForegroundPlatformUtilsService } from "../../platform/services/platform-utils/foreground-platform-utils.service";
|
||||
import {
|
||||
BrowserSdkClientFactory,
|
||||
BrowserSdkPureClientFactory,
|
||||
} from "../../platform/services/sdk/browser-sdk-client-factory";
|
||||
import { BrowserSdkClientFactory } from "../../platform/services/sdk/browser-sdk-client-factory";
|
||||
import { BrowserSdkLoadService } from "../../platform/services/sdk/browser-sdk-load.service";
|
||||
import { ForegroundTaskSchedulerService } from "../../platform/services/task-scheduler/foreground-task-scheduler.service";
|
||||
import { BrowserStorageServiceProvider } from "../../platform/storage/browser-storage-service.provider";
|
||||
@@ -600,12 +591,6 @@ const safeProviders: SafeProvider[] = [
|
||||
flagEnabled("sdk") ? new BrowserSdkClientFactory() : new NoopSdkClientFactory(),
|
||||
deps: [],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: SdkPureClientFactory,
|
||||
useFactory: (logService) =>
|
||||
flagEnabled("sdk") ? new BrowserSdkPureClientFactory() : new NoopSdkPureClientFactory(),
|
||||
deps: [LogService],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: LoginEmailService,
|
||||
useClass: LoginEmailService,
|
||||
|
||||
@@ -66,10 +66,7 @@ import { I18nService as I18nServiceAbstraction } from "@bitwarden/common/platfor
|
||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
|
||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||
import {
|
||||
SdkClientFactory,
|
||||
SdkPureClientFactory,
|
||||
} from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
|
||||
import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service";
|
||||
import { ThemeType } from "@bitwarden/common/platform/enums";
|
||||
@@ -78,10 +75,7 @@ import { MemoryStorageService } from "@bitwarden/common/platform/services/memory
|
||||
// eslint-disable-next-line import/no-restricted-paths -- Implementation for memory storage
|
||||
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
||||
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
||||
import {
|
||||
NoopSdkClientFactory,
|
||||
NoopSdkPureClientFactory,
|
||||
} from "@bitwarden/common/platform/services/sdk/noop-sdk-client-factory";
|
||||
import { NoopSdkClientFactory } from "@bitwarden/common/platform/services/sdk/noop-sdk-client-factory";
|
||||
import { NoopSdkLoadService } from "@bitwarden/common/platform/services/sdk/noop-sdk-load.service";
|
||||
import { StorageServiceProvider } from "@bitwarden/common/platform/services/storage-service.provider";
|
||||
/* eslint-disable import/no-restricted-paths -- Implementation for memory storage */
|
||||
@@ -118,7 +112,7 @@ import { WebProcessReloadService } from "../key-management/services/web-process-
|
||||
import { WebBiometricsService } from "../key-management/web-biometric.service";
|
||||
import { WebEnvironmentService } from "../platform/web-environment.service";
|
||||
import { WebMigrationRunner } from "../platform/web-migration-runner";
|
||||
import { WebSdkClientFactory, WebSdkPureClientFactory } from "../platform/web-sdk-client-factory";
|
||||
import { WebSdkClientFactory } from "../platform/web-sdk-client-factory";
|
||||
import { WebSdkLoadService } from "../platform/web-sdk-load.service";
|
||||
import { WebStorageServiceProvider } from "../platform/web-storage-service.provider";
|
||||
|
||||
@@ -307,11 +301,6 @@ const safeProviders: SafeProvider[] = [
|
||||
useClass: flagEnabled("sdk") ? WebSdkClientFactory : NoopSdkClientFactory,
|
||||
deps: [],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: SdkPureClientFactory,
|
||||
useClass: flagEnabled("sdk") ? WebSdkPureClientFactory : NoopSdkPureClientFactory,
|
||||
deps: [],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: ProcessReloadServiceAbstraction,
|
||||
useClass: WebProcessReloadService,
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
import {
|
||||
SdkClientFactory,
|
||||
SdkPureClientFactory,
|
||||
} from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import * as sdk from "@bitwarden/sdk-internal";
|
||||
|
||||
export class WebSdkPureClientFactory implements SdkPureClientFactory {
|
||||
async createPureSdkClient(): Promise<sdk.BitwardenPure> {
|
||||
return Promise.resolve(new sdk.BitwardenPure());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SDK client factory with a js fallback for when WASM is not supported.
|
||||
*/
|
||||
|
||||
@@ -160,10 +160,7 @@ import { KeyGenerationService as KeyGenerationServiceAbstraction } from "@bitwar
|
||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||
import { MessagingService as MessagingServiceAbstraction } from "@bitwarden/common/platform/abstractions/messaging.service";
|
||||
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||
import {
|
||||
SdkClientFactory,
|
||||
SdkPureClientFactory,
|
||||
} from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
|
||||
import { StateService as StateServiceAbstraction } from "@bitwarden/common/platform/abstractions/state.service";
|
||||
import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service";
|
||||
@@ -890,7 +887,7 @@ const safeProviders: SafeProvider[] = [
|
||||
safeProvider({
|
||||
provide: EncryptService,
|
||||
useClass: EncryptServiceImplementation,
|
||||
deps: [SdkPureClientFactory, CryptoFunctionServiceAbstraction, LogService, LOG_MAC_FAILURES],
|
||||
deps: [CryptoFunctionServiceAbstraction, LogService, LOG_MAC_FAILURES],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: BulkEncryptService,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { BitwardenClient, BitwardenPure } from "@bitwarden/sdk-internal";
|
||||
import type { BitwardenClient } from "@bitwarden/sdk-internal";
|
||||
|
||||
/**
|
||||
* Factory for creating SDK clients.
|
||||
@@ -8,7 +8,3 @@ export abstract class SdkClientFactory {
|
||||
...args: ConstructorParameters<typeof BitwardenClient>
|
||||
): Promise<BitwardenClient>;
|
||||
}
|
||||
|
||||
export abstract class SdkPureClientFactory {
|
||||
abstract createPureSdkClient(): Promise<BitwardenPure>;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// FIXME: Update this file to be type safe and remove this and next line
|
||||
// @ts-strict-ignore
|
||||
|
||||
// eslint-disable-next-line no-restricted-imports -- TODO MDG: fix this
|
||||
import { SdkPureClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory";
|
||||
import { BitwardenPure } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { Utils } from "../../../platform/misc/utils";
|
||||
import { CryptoFunctionService } from "../../abstractions/crypto-function.service";
|
||||
@@ -19,7 +18,6 @@ import { SymmetricCryptoKey } from "../../models/domain/symmetric-crypto-key";
|
||||
|
||||
export class EncryptServiceImplementation implements EncryptService {
|
||||
constructor(
|
||||
protected readonly sdkPureClientFactory: SdkPureClientFactory,
|
||||
protected cryptoFunctionService: CryptoFunctionService,
|
||||
protected logService: LogService,
|
||||
protected logMacFailures: boolean,
|
||||
@@ -81,8 +79,7 @@ export class EncryptServiceImplementation implements EncryptService {
|
||||
|
||||
key = this.resolveLegacyKey(key, encString);
|
||||
|
||||
const pure = await this.sdkPureClientFactory.createPureSdkClient();
|
||||
const decrypted = pure.crypto().symmetric_decrypt(encString.encryptedString, key.keyB64);
|
||||
const decrypted = BitwardenPure.symmetric_decrypt(encString.encryptedString, key.keyB64);
|
||||
|
||||
return decrypted;
|
||||
}
|
||||
@@ -98,16 +95,16 @@ export class EncryptServiceImplementation implements EncryptService {
|
||||
|
||||
key = this.resolveLegacyKey(key, encThing);
|
||||
|
||||
const pure = await this.sdkPureClientFactory.createPureSdkClient();
|
||||
const encString = new EncString(
|
||||
encThing.encryptionType,
|
||||
Utils.fromBufferToB64(encThing.dataBytes),
|
||||
Utils.fromBufferToB64(encThing.ivBytes),
|
||||
Utils.fromBufferToB64(encThing.macBytes),
|
||||
);
|
||||
const decrypted = pure
|
||||
.crypto()
|
||||
.symmetric_decrypt_to_bytes(encString.encryptedString, key.keyB64);
|
||||
const decrypted = BitwardenPure.symmetric_decrypt_to_bytes(
|
||||
encString.encryptedString,
|
||||
key.keyB64,
|
||||
);
|
||||
|
||||
return decrypted ?? null;
|
||||
}
|
||||
@@ -175,9 +172,8 @@ export class EncryptServiceImplementation implements EncryptService {
|
||||
}
|
||||
|
||||
private async aesEncrypt(data: Uint8Array, key: SymmetricCryptoKey): Promise<EncryptedObject> {
|
||||
const pure = await this.sdkPureClientFactory.createPureSdkClient();
|
||||
const encString = new EncString(
|
||||
pure.crypto().symmetric_encrypt(Utils.fromBufferToUtf8(data), key.keyB64),
|
||||
BitwardenPure.symmetric_encrypt(Utils.fromBufferToUtf8(data), key.keyB64),
|
||||
);
|
||||
const obj = new EncryptedObject();
|
||||
obj.key = key;
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
// eslint-disable-next-line no-restricted-imports -- TODO MDG: this is a bug in the restricted import rule
|
||||
import { DefaultSdkPureClientFactory } from "@bitwarden/common/platform/services/sdk/default-sdk-client-factory";
|
||||
|
||||
import { Decryptable } from "../../interfaces/decryptable.interface";
|
||||
import { SymmetricCryptoKey } from "../../models/domain/symmetric-crypto-key";
|
||||
import { ConsoleLogService } from "../console-log.service";
|
||||
@@ -25,13 +22,7 @@ let encryptService: EncryptServiceImplementation;
|
||||
export function init() {
|
||||
const cryptoFunctionService = new WebCryptoFunctionService(self);
|
||||
const logService = new ConsoleLogService(false);
|
||||
const pureSdkFactory = new DefaultSdkPureClientFactory();
|
||||
encryptService = new EncryptServiceImplementation(
|
||||
pureSdkFactory,
|
||||
cryptoFunctionService,
|
||||
logService,
|
||||
true,
|
||||
);
|
||||
encryptService = new EncryptServiceImplementation(cryptoFunctionService, logService, true);
|
||||
|
||||
const bitwardenContainerService = new ContainerService(null, encryptService);
|
||||
bitwardenContainerService.attachToGlobal(self);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// FIXME: Update this file to be type safe and remove this and next line
|
||||
// @ts-strict-ignore
|
||||
import { mockReset, mock } from "jest-mock-extended";
|
||||
|
||||
import { makeStaticByteArray } from "../../../spec";
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
import * as sdk from "@bitwarden/sdk-internal";
|
||||
import * as module from "@bitwarden/sdk-internal/bitwarden_wasm_internal_bg.wasm";
|
||||
|
||||
import { SdkClientFactory, SdkPureClientFactory } from "../../abstractions/sdk/sdk-client-factory";
|
||||
|
||||
export class DefaultSdkPureClientFactory implements SdkPureClientFactory {
|
||||
async createPureSdkClient(): Promise<sdk.BitwardenPure> {
|
||||
(sdk as any).init(module);
|
||||
|
||||
return Promise.resolve(new sdk.BitwardenPure());
|
||||
}
|
||||
}
|
||||
import { SdkClientFactory } from "../../abstractions/sdk/sdk-client-factory";
|
||||
|
||||
/**
|
||||
* Directly imports the Bitwarden SDK and initializes it.
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// FIXME: Update this file to be type safe and remove this and next line
|
||||
// @ts-strict-ignore
|
||||
import { mock, MockProxy } from "jest-mock-extended";
|
||||
import { BehaviorSubject, firstValueFrom, of } from "rxjs";
|
||||
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
import type { BitwardenClient } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { SdkClientFactory, SdkPureClientFactory } from "../../abstractions/sdk/sdk-client-factory";
|
||||
|
||||
export class NoopSdkPureClientFactory implements SdkPureClientFactory {
|
||||
createPureSdkClient(): Promise<never> {
|
||||
return Promise.reject(new Error("SDK not available"));
|
||||
}
|
||||
}
|
||||
import { SdkClientFactory } from "../../abstractions/sdk/sdk-client-factory";
|
||||
|
||||
/**
|
||||
* Noop SDK client factory.
|
||||
|
||||
Reference in New Issue
Block a user