2020-06-04 19:24:11 +02:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
import KeychainService from './keychain/KeychainService';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Setting from '../models/Setting';
|
|
|
|
import uuid from '../uuid';
|
2021-08-12 17:54:10 +02:00
|
|
|
import { migrateLocalSyncInfo } from './synchronizer/syncInfoUtils';
|
2024-08-08 20:53:43 +02:00
|
|
|
import KeychainServiceDriverBase from './keychain/KeychainServiceDriverBase';
|
2024-09-02 13:26:43 +02:00
|
|
|
import shim from '../shim';
|
2024-08-08 20:53:43 +02:00
|
|
|
|
|
|
|
type KeychainServiceDriverConstructor = new (appId: string, clientId: string)=> KeychainServiceDriverBase;
|
2020-06-03 18:07:50 +02:00
|
|
|
|
|
|
|
// This function takes care of initialising both the keychain service and settings.
|
|
|
|
//
|
|
|
|
// Loading the settings became more complicated with the keychain integration. This is because
|
|
|
|
// the settings needs a keychain service, and the keychain service needs a clientId, which
|
|
|
|
// is set dynamically and saved to the settings.
|
|
|
|
// In other words, it's not possible to load the settings without the KS service and it's not
|
|
|
|
// possible to initialise the KS service without the settings.
|
|
|
|
// The solution is to fetch just the client ID directly from the database.
|
2024-08-08 20:53:43 +02:00
|
|
|
export async function loadKeychainServiceAndSettings(keychainServiceDrivers: KeychainServiceDriverConstructor[]) {
|
2020-06-03 18:07:50 +02:00
|
|
|
const clientIdSetting = await Setting.loadOne('clientId');
|
|
|
|
const clientId = clientIdSetting ? clientIdSetting.value : uuid.create();
|
2024-09-02 13:26:43 +02:00
|
|
|
|
|
|
|
// Temporary workaround: For a short time, pre-release versions of Joplin Portable encrypted
|
|
|
|
// saved keys using the keychain. This can break sync when transferring Joplin between devices.
|
|
|
|
// To prevent secure keys from being lost, we enable read-only keychain access in portable mode.
|
|
|
|
if (shim.isPortable()) {
|
|
|
|
KeychainService.instance().readOnly = true;
|
|
|
|
}
|
|
|
|
|
2024-08-08 20:53:43 +02:00
|
|
|
await KeychainService.instance().initialize(
|
|
|
|
keychainServiceDrivers.map(Driver => new Driver(Setting.value('appId'), clientId)),
|
|
|
|
);
|
2020-06-03 18:07:50 +02:00
|
|
|
Setting.setKeychainService(KeychainService.instance());
|
|
|
|
await Setting.load();
|
2021-08-12 17:54:10 +02:00
|
|
|
|
|
|
|
// This is part of the migration to the new sync target info. It needs to be
|
|
|
|
// set as early as possible since it's used to tell if E2EE is enabled, it
|
|
|
|
// contains the master keys, etc. Once it has been set, it becomes a noop
|
|
|
|
// on future calls.
|
|
|
|
await migrateLocalSyncInfo(Setting.db());
|
|
|
|
|
2020-06-03 18:07:50 +02:00
|
|
|
if (!clientIdSetting) Setting.setValue('clientId', clientId);
|
|
|
|
await KeychainService.instance().detectIfKeychainSupported();
|
|
|
|
}
|