2022-09-22 14:51:14 +02:00
|
|
|
import { Jsonify } from "type-fest";
|
|
|
|
|
2022-08-16 14:05:03 +02:00
|
|
|
import { AbstractCachedStorageService } from "@bitwarden/common/abstractions/storage.service";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { GlobalState } from "@bitwarden/common/models/domain/global-state";
|
|
|
|
import { StorageOptions } from "@bitwarden/common/models/domain/storage-options";
|
2022-06-27 19:38:12 +02:00
|
|
|
import {
|
|
|
|
StateService as BaseStateService,
|
|
|
|
withPrototype,
|
|
|
|
} from "@bitwarden/common/services/state.service";
|
2022-01-27 22:22:51 +01:00
|
|
|
|
|
|
|
import { Account } from "../models/account";
|
|
|
|
import { BrowserComponentState } from "../models/browserComponentState";
|
|
|
|
import { BrowserGroupingsComponentState } from "../models/browserGroupingsComponentState";
|
|
|
|
import { BrowserSendComponentState } from "../models/browserSendComponentState";
|
2022-02-24 18:14:04 +01:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
import { StateService as StateServiceAbstraction } from "./abstractions/state.service";
|
|
|
|
|
2022-02-03 19:55:06 +01:00
|
|
|
export class StateService
|
|
|
|
extends BaseStateService<GlobalState, Account>
|
|
|
|
implements StateServiceAbstraction
|
|
|
|
{
|
2022-09-22 14:51:14 +02:00
|
|
|
async getFromSessionMemory<T>(key: string, deserializer?: (obj: Jsonify<T>) => T): Promise<T> {
|
2022-08-16 14:05:03 +02:00
|
|
|
return this.memoryStorageService instanceof AbstractCachedStorageService
|
2022-09-22 14:51:14 +02:00
|
|
|
? await this.memoryStorageService.getBypassCache<T>(key, { deserializer: deserializer })
|
2022-08-16 14:05:03 +02:00
|
|
|
: await this.memoryStorageService.get<T>(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setInSessionMemory(key: string, value: any): Promise<void> {
|
|
|
|
await this.memoryStorageService.save(key, value);
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
async addAccount(account: Account) {
|
|
|
|
// Apply browser overrides to default account values
|
|
|
|
account = new Account(account);
|
|
|
|
await super.addAccount(account);
|
|
|
|
}
|
|
|
|
|
2022-02-15 23:06:35 +01:00
|
|
|
async getIsAuthenticated(options?: StorageOptions): Promise<boolean> {
|
|
|
|
// Firefox Private Mode can clash with non-Private Mode because they both read from the same onDiskOptions
|
|
|
|
// Check that there is an account in memory before considering the user authenticated
|
|
|
|
return (
|
|
|
|
(await super.getIsAuthenticated(options)) &&
|
2022-06-27 19:38:12 +02:00
|
|
|
(await this.getAccount(await this.defaultInMemoryOptions())) != null
|
2022-02-15 23:06:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:38:12 +02:00
|
|
|
@withPrototype(BrowserGroupingsComponentState)
|
2022-01-27 22:22:51 +01:00
|
|
|
async getBrowserGroupingComponentState(
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<BrowserGroupingsComponentState> {
|
2022-06-27 19:38:12 +02:00
|
|
|
return (
|
|
|
|
await this.getAccount(this.reconcileOptions(options, await this.defaultInMemoryOptions()))
|
|
|
|
)?.groupings;
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserGroupingComponentState(
|
|
|
|
value: BrowserGroupingsComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
2022-06-27 19:38:12 +02:00
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
2022-01-27 22:22:51 +01:00
|
|
|
);
|
|
|
|
account.groupings = value;
|
2022-06-27 19:38:12 +02:00
|
|
|
await this.saveAccount(
|
|
|
|
account,
|
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
|
|
|
);
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:38:12 +02:00
|
|
|
@withPrototype(BrowserComponentState)
|
2022-01-27 22:22:51 +01:00
|
|
|
async getBrowserCipherComponentState(options?: StorageOptions): Promise<BrowserComponentState> {
|
2022-06-27 19:38:12 +02:00
|
|
|
return (
|
|
|
|
await this.getAccount(this.reconcileOptions(options, await this.defaultInMemoryOptions()))
|
|
|
|
)?.ciphers;
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserCipherComponentState(
|
|
|
|
value: BrowserComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
2022-06-27 19:38:12 +02:00
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
2022-01-27 22:22:51 +01:00
|
|
|
);
|
|
|
|
account.ciphers = value;
|
2022-06-27 19:38:12 +02:00
|
|
|
await this.saveAccount(
|
|
|
|
account,
|
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
|
|
|
);
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:38:12 +02:00
|
|
|
@withPrototype(BrowserSendComponentState)
|
2022-01-27 22:22:51 +01:00
|
|
|
async getBrowserSendComponentState(options?: StorageOptions): Promise<BrowserSendComponentState> {
|
2022-06-27 19:38:12 +02:00
|
|
|
return (
|
|
|
|
await this.getAccount(this.reconcileOptions(options, await this.defaultInMemoryOptions()))
|
|
|
|
)?.send;
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserSendComponentState(
|
|
|
|
value: BrowserSendComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
2022-06-27 19:38:12 +02:00
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
2022-01-27 22:22:51 +01:00
|
|
|
);
|
|
|
|
account.send = value;
|
2022-06-27 19:38:12 +02:00
|
|
|
await this.saveAccount(
|
|
|
|
account,
|
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
|
|
|
);
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
2022-06-27 19:38:12 +02:00
|
|
|
|
|
|
|
@withPrototype(BrowserComponentState)
|
2022-01-27 22:22:51 +01:00
|
|
|
async getBrowserSendTypeComponentState(options?: StorageOptions): Promise<BrowserComponentState> {
|
2022-06-27 19:38:12 +02:00
|
|
|
return (
|
|
|
|
await this.getAccount(this.reconcileOptions(options, await this.defaultInMemoryOptions()))
|
|
|
|
)?.sendType;
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserSendTypeComponentState(
|
|
|
|
value: BrowserComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
2022-06-27 19:38:12 +02:00
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
2022-01-27 22:22:51 +01:00
|
|
|
);
|
|
|
|
account.sendType = value;
|
2022-06-27 19:38:12 +02:00
|
|
|
await this.saveAccount(
|
|
|
|
account,
|
|
|
|
this.reconcileOptions(options, await this.defaultInMemoryOptions())
|
|
|
|
);
|
2022-01-27 22:22:51 +01:00
|
|
|
}
|
|
|
|
}
|