Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(input: { key: Buffer, preset: EncryptionAdapterBundle.KeyBasedPreset }) {
this.logger.info("constructor()");
const encryptionAdapter = new EncryptionAdapterBundle.EncryptionAdapter(input);
this.read = async (data: Buffer) => {
this.logger.info(`read() buffer.length: ${data.length}`);
const {header: {serialization}} = persistencePartsUtil.split(data);
const decryptedData = await encryptionAdapter.read(data);
if (serialization && serialization.type === "msgpack") {
this.logger.verbose(`"msgpack.decode" start`);
const decoded = msgpack.decode(decryptedData) as FsDb;
this.logger.verbose(`"msgpack.decode" end`);
return decoded;
}
const readableStream = bufferToStream(decryptedData);
export async function buildSettingsAdapter(
{configStore}: Context,
password: string,
): Promise {
return new EncryptionAdapter(
{password, preset: (await configStore.readExisting()).encryptionPreset},
{keyDerivationCache: true, keyDerivationCacheLimit: 3},
);
}
readSettings: async (t) => {
const {
endpoints,
mocks: {
"src/electron-main/keytar": {setPassword: setPasswordSpy, deletePassword: deletePasswordSpy},
"src/electron-main/storage-upgrade": {upgradeSettings: upgradeSettingsSpy},
"src/electron-main/session": {initSessionByAccount: initSessionByAccountMock},
},
} = t.context;
t.false(await t.context.ctx.settingsStore.readable(), "settings file does not exist");
t.falsy(t.context.ctx.settingsStore.adapter, "adapter is not set");
const initial = await readConfigAndSettings(endpoints, {password: OPTIONS.masterPassword, savePassword: false});
t.is(0, upgradeSettingsSpy.callCount);
t.is(t.context.ctx.settingsStore.adapter && t.context.ctx.settingsStore.adapter.constructor.name,
EncryptionAdapter.name,
"adapter is an EncryptionAdapter",
);
const initialExpected = {...t.context.ctx.initialStores.settings, ...{_rev: 0}};
t.deepEqual(initial, initialExpected, "checking initial settings file");
t.true(await t.context.ctx.settingsStore.readable(), "settings file exists");
t.is(setPasswordSpy.callCount, 0);
t.is(deletePasswordSpy.callCount, 1);
t.true(deletePasswordSpy.alwaysCalledWithExactly());
t.is(initial.accounts.length, initSessionByAccountMock.callCount);
for (const {login} of initial.accounts) {
t.true(initSessionByAccountMock.calledWithExactly(t.context.ctx, login));
}
const initialUpdated = await t.context.ctx.settingsStore.write(initial);
const initialUpdatedExpected = {...initial, ...initialExpected, ...{_rev: initialExpected._rev + 1}};
t.deepEqual(initialUpdated, initialUpdatedExpected, "saved initial settings file");