Browse Source

Move delta tokens out of secure storage (#246)

pull/247/head
Robyn MacCallum 4 years ago committed by GitHub
parent
commit
8374103a15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 78
      src/services/state.service.ts
  2. 43
      src/services/stateMigration.service.ts

78
src/services/state.service.ts

@ -238,50 +238,6 @@ export class StateService @@ -238,50 +238,6 @@ export class StateService
);
}
async getUserDelta(options?: StorageOptions): Promise<string> {
options = this.reconcileOptions(options, await this.defaultSecureStorageOptions());
if (options?.userId == null) {
return null;
}
return await this.secureStorageService.get<string>(
`${options.userId}_${SecureStorageKeys.userDelta}`
);
}
async setUserDelta(value: string, options?: StorageOptions): Promise<void> {
options = this.reconcileOptions(options, await this.defaultSecureStorageOptions());
if (options?.userId == null) {
return;
}
await this.secureStorageService.save(
`${options.userId}_${SecureStorageKeys.userDelta}`,
value,
options
);
}
async getGroupDelta(options?: StorageOptions): Promise<string> {
options = this.reconcileOptions(options, await this.defaultSecureStorageOptions());
if (options?.userId == null) {
return null;
}
return await this.secureStorageService.get<string>(
`${options.userId}_${SecureStorageKeys.groupDelta}`
);
}
async setGroupDelta(value: string, options?: StorageOptions): Promise<void> {
options = this.reconcileOptions(options, await this.defaultSecureStorageOptions());
if (options?.userId == null) {
return;
}
await this.secureStorageService.save(
`${options.userId}_${SecureStorageKeys.groupDelta}`,
value,
options
);
}
async getConfiguration(type: DirectoryType): Promise<IConfiguration> {
switch (type) {
case DirectoryType.Ldap:
@ -514,6 +470,40 @@ export class StateService @@ -514,6 +470,40 @@ export class StateService
await this.saveAccount(account, this.reconcileOptions(options, this.defaultInMemoryOptions));
}
async getUserDelta(options?: StorageOptions): Promise<string> {
return (
await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))
)?.directorySettings?.userDelta;
}
async setUserDelta(value: string, options?: StorageOptions): Promise<void> {
const account = await this.getAccount(
this.reconcileOptions(options, await this.defaultOnDiskOptions())
);
account.directorySettings.userDelta = value;
await this.saveAccount(
account,
this.reconcileOptions(options, await this.defaultOnDiskOptions())
);
}
async getGroupDelta(options?: StorageOptions): Promise<string> {
return (
await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))
)?.directorySettings?.groupDelta;
}
async setGroupDelta(value: string, options?: StorageOptions): Promise<void> {
const account = await this.getAccount(
this.reconcileOptions(options, await this.defaultOnDiskOptions())
);
account.directorySettings.groupDelta = value;
await this.saveAccount(
account,
this.reconcileOptions(options, await this.defaultOnDiskOptions())
);
}
async clearSyncSettings(hashToo = false) {
await this.setUserDelta(null);
await this.setGroupDelta(null);

43
src/services/stateMigration.service.ts

@ -19,8 +19,6 @@ const SecureStorageKeys: { [key: string]: any } = { @@ -19,8 +19,6 @@ const SecureStorageKeys: { [key: string]: any } = {
directoryConfigPrefix: "directoryConfig_",
sync: "syncConfig",
directoryType: "directoryType",
userDelta: "userDeltaToken",
groupDelta: "groupDeltaToken",
organizationId: "organizationId",
};
@ -33,10 +31,17 @@ const Keys: { [key: string]: any } = { @@ -33,10 +31,17 @@ const Keys: { [key: string]: any } = {
lastSyncHash: "lastSyncHash",
syncingDir: "syncingDir",
syncConfig: "syncConfig",
userDelta: "userDeltaToken",
groupDelta: "groupDeltaToken",
tempDirectoryConfigs: "tempDirectoryConfigs",
tempDirectorySettings: "tempDirectorySettings",
};
const StateKeys = {
global: "global",
authenticatedAccounts: "authenticatedAccounts",
};
const ClientKeys: { [key: string]: any } = {
clientIdOld: "clientId",
clientId: "apikey_clientId",
@ -53,6 +58,8 @@ export class StateMigrationService extends BaseStateMigrationService { @@ -53,6 +58,8 @@ export class StateMigrationService extends BaseStateMigrationService {
await this.migrateClientKeys();
await this.migrateStateFrom1To2();
break;
case StateVersion.Two:
await this.migrateStateFrom2To3();
}
currentStateVersion += 1;
}
@ -116,6 +123,8 @@ export class StateMigrationService extends BaseStateMigrationService { @@ -116,6 +123,8 @@ export class StateMigrationService extends BaseStateMigrationService {
lastSyncHash: await this.get<string>(Keys.lastSyncHash),
syncingDir: await this.get<boolean>(Keys.syncingDir),
sync: await this.get<SyncConfiguration>(Keys.syncConfig),
userDelta: await this.get<string>(Keys.userDelta),
groupDelta: await this.get<string>(Keys.groupDelta),
};
// (userId == null) = no authed account, stored data temporarily to be applied and cleared on next auth
@ -155,4 +164,34 @@ export class StateMigrationService extends BaseStateMigrationService { @@ -155,4 +164,34 @@ export class StateMigrationService extends BaseStateMigrationService {
}
}
}
protected async migrateStateFrom2To3(useSecureStorageForSecrets = true): Promise<void> {
if (useSecureStorageForSecrets) {
const authenticatedUserIds = await this.get<string[]>(StateKeys.authenticatedAccounts);
await Promise.all(
authenticatedUserIds.map(async (userId) => {
const account = await this.get<Account>(userId);
// Fix for userDelta and groupDelta being put into secure storage when they should not have
if (await this.secureStorageService.has(`${userId}_${Keys.userDelta}`)) {
account.directorySettings.userDelta = await this.secureStorageService.get(
`${userId}_${Keys.userDelta}`
);
await this.secureStorageService.remove(`${userId}_${Keys.userDelta}`);
}
if (await this.secureStorageService.has(`${userId}_${Keys.groupDelta}`)) {
account.directorySettings.groupDelta = await this.secureStorageService.get(
`${userId}_${Keys.groupDelta}`
);
await this.secureStorageService.remove(`${userId}_${Keys.groupDelta}`);
}
await this.set(userId, account);
})
);
}
const globals = await this.getGlobals();
globals.stateVersion = StateVersion.Three;
await this.set(StateKeys.global, globals);
}
}

Loading…
Cancel
Save