mirror of
https://github.com/bitwarden/browser
synced 2026-02-22 04:14:04 +00:00
Prefer static classes for pure functions
This commit is contained in:
@@ -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