mirror of
https://github.com/bitwarden/browser
synced 2025-12-19 17:53:39 +00:00
* Rename service-factory folder * Move cryptographic service factories * Move crypto models * Move crypto services * Move domain base class * Platform code owners * Move desktop log services * Move log files * Establish component library ownership * Move background listeners * Move background background * Move localization to Platform * Move browser alarms to Platform * Move browser state to Platform * Move CLI state to Platform * Move Desktop native concerns to Platform * Move flag and misc to Platform * Lint fixes * Move electron state to platform * Move web state to Platform * Move lib state to Platform * Fix broken tests * Rename interface to idiomatic TS * `npm run prettier` 🤖 * Resolve review feedback * Set platform as owners of web core and shared * Expand moved services * Fix test types --------- Co-authored-by: Hinton <hinton@users.noreply.github.com>
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { Jsonify } from "type-fest";
|
|
|
|
import { Decryptable } from "../../interfaces/decryptable.interface";
|
|
import { SymmetricCryptoKey } from "../../models/domain/symmetric-crypto-key";
|
|
import { ConsoleLogService } from "../console-log.service";
|
|
import { ContainerService } from "../container.service";
|
|
import { WebCryptoFunctionService } from "../web-crypto-function.service";
|
|
|
|
import { EncryptServiceImplementation } from "./encrypt.service.implementation";
|
|
import { getClassInitializer } from "./get-class-initializer";
|
|
|
|
const workerApi: Worker = self as any;
|
|
|
|
let inited = false;
|
|
let encryptService: EncryptServiceImplementation;
|
|
|
|
/**
|
|
* Bootstrap the worker environment with services required for decryption
|
|
*/
|
|
export function init() {
|
|
const cryptoFunctionService = new WebCryptoFunctionService(self);
|
|
const logService = new ConsoleLogService(false);
|
|
encryptService = new EncryptServiceImplementation(cryptoFunctionService, logService, true);
|
|
|
|
const bitwardenContainerService = new ContainerService(null, encryptService);
|
|
bitwardenContainerService.attachToGlobal(self);
|
|
|
|
inited = true;
|
|
}
|
|
|
|
/**
|
|
* Listen for messages and decrypt their contents
|
|
*/
|
|
workerApi.addEventListener("message", async (event: { data: string }) => {
|
|
if (!inited) {
|
|
init();
|
|
}
|
|
|
|
const request: {
|
|
id: string;
|
|
items: Jsonify<Decryptable<any>>[];
|
|
key: Jsonify<SymmetricCryptoKey>;
|
|
} = JSON.parse(event.data);
|
|
|
|
const key = SymmetricCryptoKey.fromJSON(request.key);
|
|
const items = request.items.map((jsonItem) => {
|
|
const initializer = getClassInitializer<Decryptable<any>>(jsonItem.initializerKey);
|
|
return initializer(jsonItem);
|
|
});
|
|
const result = await encryptService.decryptItems(items, key);
|
|
|
|
workerApi.postMessage({
|
|
id: request.id,
|
|
items: JSON.stringify(result),
|
|
});
|
|
});
|