1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-25 12:15:18 +01:00

pm-5273 Update references

This commit is contained in:
Carlos Gonçalves 2024-03-09 09:53:14 +00:00
parent b6b7ea283c
commit 1145201549
No known key found for this signature in database
GPG Key ID: 8147F618E732EF25
16 changed files with 54 additions and 45 deletions

View File

@ -710,7 +710,7 @@ describe("NotificationBackground", () => {
);
tabSendMessageSpy = jest.spyOn(BrowserApi, "tabSendMessage").mockImplementation();
editItemSpy = jest.spyOn(notificationBackground as any, "editItem");
setAddEditCipherInfoSpy = jest.spyOn(stateService, "setAddEditCipherInfo");
setAddEditCipherInfoSpy = jest.spyOn(cipherService, "setAddEditCipherInfo");
openAddEditVaultItemPopoutSpy = jest.spyOn(
notificationBackground as any,
"openAddEditVaultItemPopout",

View File

@ -575,14 +575,14 @@ export default class NotificationBackground {
}
/**
* Sets the add/edit cipher info in the state service
* Sets the add/edit cipher info in the cipher service
* and opens the add/edit vault item popout.
*
* @param cipherView - The cipher to edit
* @param senderTab - The tab that the message was sent from
*/
private async editItem(cipherView: CipherView, senderTab: chrome.tabs.Tab) {
await this.stateService.setAddEditCipherInfo({
await this.cipherService.setAddEditCipherInfo({
cipher: cipherView,
collectionIds: cipherView.collectionIds,
});

View File

@ -562,7 +562,7 @@ describe("OverlayBackground", () => {
beforeEach(() => {
sender = mock<chrome.runtime.MessageSender>({ tab: { id: 1 } });
jest
.spyOn(overlayBackground["stateService"], "setAddEditCipherInfo")
.spyOn(overlayBackground["cipherService"], "setAddEditCipherInfo")
.mockImplementation();
jest.spyOn(overlayBackground as any, "openAddEditVaultItemPopout").mockImplementation();
});
@ -570,7 +570,7 @@ describe("OverlayBackground", () => {
it("will not open the add edit popout window if the message does not have a login cipher provided", () => {
sendExtensionRuntimeMessage({ command: "autofillOverlayAddNewVaultItem" }, sender);
expect(overlayBackground["stateService"].setAddEditCipherInfo).not.toHaveBeenCalled();
expect(overlayBackground["cipherService"].setAddEditCipherInfo).not.toHaveBeenCalled();
expect(overlayBackground["openAddEditVaultItemPopout"]).not.toHaveBeenCalled();
});
@ -591,7 +591,7 @@ describe("OverlayBackground", () => {
);
await flushPromises();
expect(overlayBackground["stateService"].setAddEditCipherInfo).toHaveBeenCalled();
expect(overlayBackground["cipherService"].setAddEditCipherInfo).toHaveBeenCalled();
expect(BrowserApi.sendMessage).toHaveBeenCalledWith(
"inlineAutofillMenuRefreshAddEditCipher",
);

View File

@ -626,7 +626,7 @@ class OverlayBackground implements OverlayBackgroundInterface {
cipherView.type = CipherType.Login;
cipherView.login = loginView;
await this.stateService.setAddEditCipherInfo({
await this.cipherService.setAddEditCipherInfo({
cipher: cipherView,
collectionIds: cipherView.collectionIds,
});

View File

@ -16,6 +16,7 @@ import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broa
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { DialogService, SimpleDialogOptions } from "@bitwarden/components";
import { BrowserApi } from "../platform/browser/browser-api";
@ -46,6 +47,7 @@ export class AppComponent implements OnInit, OnDestroy {
private i18nService: I18nService,
private router: Router,
private stateService: BrowserStateService,
private cipherService: CipherService,
private messagingService: MessagingService,
private changeDetectorRef: ChangeDetectorRef,
private ngZone: NgZone,
@ -166,7 +168,7 @@ export class AppComponent implements OnInit, OnDestroy {
await this.clearComponentStates();
}
if (url.startsWith("/tabs/")) {
await this.stateService.setAddEditCipherInfo(null);
await this.cipherService.setAddEditCipherInfo(null);
}
(window as any).previousPopupUrl = url;

View File

@ -9,6 +9,7 @@ import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/pl
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
import { UsernameGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/username";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { AddEditCipherInfo } from "@bitwarden/common/vault/types/add-edit-cipher-info";
@ -26,6 +27,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
platformUtilsService: PlatformUtilsService,
i18nService: I18nService,
stateService: StateService,
cipherService: CipherService,
route: ActivatedRoute,
logService: LogService,
private location: Location,
@ -35,6 +37,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
usernameGenerationService,
platformUtilsService,
stateService,
cipherService,
i18nService,
logService,
route,
@ -43,7 +46,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
}
async ngOnInit() {
this.addEditCipherInfo = await this.stateService.getAddEditCipherInfo();
this.addEditCipherInfo = await this.cipherService.getAddEditCipherInfo();
if (this.addEditCipherInfo != null) {
this.cipherState = this.addEditCipherInfo.cipher;
}
@ -64,7 +67,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
this.addEditCipherInfo.cipher = this.cipherState;
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.stateService.setAddEditCipherInfo(this.addEditCipherInfo);
this.cipherService.setAddEditCipherInfo(this.addEditCipherInfo);
this.close();
}

View File

@ -304,7 +304,7 @@ export class AddEditComponent extends BaseAddEditComponent {
}
private saveCipherState() {
return this.stateService.setAddEditCipherInfo({
return this.cipherService.setAddEditCipherInfo({
cipher: this.cipher,
collectionIds:
this.collections == null

View File

@ -10,6 +10,7 @@ import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/pl
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
import { UsernameGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/username";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { GeneratorComponent } from "./generator.component";
@ -54,6 +55,10 @@ describe("GeneratorComponent", () => {
provide: LogService,
useValue: mock<LogService>(),
},
{
provide: CipherService,
useValue: mock<CipherService>(),
},
],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();

View File

@ -8,6 +8,7 @@ import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/pl
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
import { UsernameGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/username";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@Component({
selector: "app-generator",
@ -18,6 +19,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
passwordGenerationService: PasswordGenerationServiceAbstraction,
usernameGenerationService: UsernameGenerationServiceAbstraction,
stateService: StateService,
cipherService: CipherService,
platformUtilsService: PlatformUtilsService,
i18nService: I18nService,
route: ActivatedRoute,
@ -28,6 +30,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
usernameGenerationService,
platformUtilsService,
stateService,
cipherService,
i18nService,
logService,
route,

View File

@ -8,6 +8,7 @@ import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/pl
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/password";
import { UsernameGenerationServiceAbstraction } from "@bitwarden/common/tools/generator/username";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { DialogService } from "@bitwarden/components";
import { PasswordGeneratorHistoryComponent } from "./password-generator-history.component";
@ -21,6 +22,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
passwordGenerationService: PasswordGenerationServiceAbstraction,
usernameGenerationService: UsernameGenerationServiceAbstraction,
stateService: StateService,
cipherService: CipherService,
platformUtilsService: PlatformUtilsService,
i18nService: I18nService,
logService: LogService,
@ -32,6 +34,7 @@ export class GeneratorComponent extends BaseGeneratorComponent {
usernameGenerationService,
platformUtilsService,
stateService,
cipherService,
i18nService,
logService,
route,

View File

@ -19,6 +19,7 @@ import {
UsernameGeneratorOptions,
} from "@bitwarden/common/tools/generator/username";
import { EmailForwarderOptions } from "@bitwarden/common/tools/models/domain/email-forwarder-options";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@Directive()
export class GeneratorComponent implements OnInit {
@ -57,6 +58,7 @@ export class GeneratorComponent implements OnInit {
protected usernameGenerationService: UsernameGenerationServiceAbstraction,
protected platformUtilsService: PlatformUtilsService,
protected stateService: StateService,
protected cipherService: CipherService,
protected i18nService: I18nService,
protected logService: LogService,
protected route: ActivatedRoute,

View File

@ -678,7 +678,7 @@ export class AddEditComponent implements OnInit, OnDestroy {
}
async loadAddEditCipherInfo(): Promise<boolean> {
const addEditCipherInfo: any = await this.stateService.getAddEditCipherInfo();
const addEditCipherInfo: any = await this.cipherService.getAddEditCipherInfo();
const loadedSavedInfo = addEditCipherInfo != null;
if (loadedSavedInfo) {
@ -691,7 +691,7 @@ export class AddEditComponent implements OnInit, OnDestroy {
}
}
await this.stateService.setAddEditCipherInfo(null);
await this.cipherService.setAddEditCipherInfo(null);
return loadedSavedInfo;
}

View File

@ -20,7 +20,6 @@ import { UriMatchType } from "../../vault/enums";
import { CipherData } from "../../vault/models/data/cipher.data";
import { LocalData } from "../../vault/models/data/local.data";
import { CipherView } from "../../vault/models/view/cipher.view";
import { AddEditCipherInfo } from "../../vault/types/add-edit-cipher-info";
import { KdfType, ThemeType } from "../enums";
import { ServerConfigData } from "../models/data/server-config.data";
import {
@ -61,8 +60,6 @@ export abstract class StateService<T extends Account = Account> {
getAccessToken: (options?: StorageOptions) => Promise<string>;
setAccessToken: (value: string, options?: StorageOptions) => Promise<void>;
getAddEditCipherInfo: (options?: StorageOptions) => Promise<AddEditCipherInfo>;
setAddEditCipherInfo: (value: AddEditCipherInfo, options?: StorageOptions) => Promise<void>;
getAlwaysShowDock: (options?: StorageOptions) => Promise<boolean>;
setAlwaysShowDock: (value: boolean, options?: StorageOptions) => Promise<void>;
getApiKeyClientId: (options?: StorageOptions) => Promise<string>;

View File

@ -24,7 +24,6 @@ import { UriMatchType } from "../../vault/enums";
import { CipherData } from "../../vault/models/data/cipher.data";
import { LocalData } from "../../vault/models/data/local.data";
import { CipherView } from "../../vault/models/view/cipher.view";
import { AddEditCipherInfo } from "../../vault/types/add-edit-cipher-info";
import { EnvironmentService } from "../abstractions/environment.service";
import { LogService } from "../abstractions/log.service";
import {
@ -271,34 +270,6 @@ export class StateService<
await this.saveAccount(account, options);
}
async getAddEditCipherInfo(options?: StorageOptions): Promise<AddEditCipherInfo> {
const account = await this.getAccount(
this.reconcileOptions(options, await this.defaultInMemoryOptions()),
);
// ensure prototype on cipher
const raw = account?.data?.addEditCipherInfo;
return raw == null
? null
: {
cipher:
raw?.cipher.toJSON != null
? raw.cipher
: CipherView.fromJSON(raw?.cipher as Jsonify<CipherView>),
collectionIds: raw?.collectionIds,
};
}
async setAddEditCipherInfo(value: AddEditCipherInfo, options?: StorageOptions): Promise<void> {
const account = await this.getAccount(
this.reconcileOptions(options, await this.defaultInMemoryOptions()),
);
account.data.addEditCipherInfo = value;
await this.saveAccount(
account,
this.reconcileOptions(options, await this.defaultInMemoryOptions()),
);
}
async getAlwaysShowDock(options?: StorageOptions): Promise<boolean> {
return (
(await this.getGlobals(this.reconcileOptions(options, await this.defaultOnDiskOptions())))

View File

@ -6,6 +6,7 @@ import { Cipher } from "../models/domain/cipher";
import { Field } from "../models/domain/field";
import { CipherView } from "../models/view/cipher.view";
import { FieldView } from "../models/view/field.view";
import { AddEditCipherInfo } from "../types/add-edit-cipher-info";
export abstract class CipherService {
clearCache: (userId?: string) => Promise<void>;
@ -89,4 +90,6 @@ export abstract class CipherService {
) => Promise<void>;
getKeyForCipherKeyDecryption: (cipher: Cipher) => Promise<any>;
decryptCiphers: (ciphers: Cipher[]) => Promise<CipherView[]>;
getAddEditCipherInfo: () => Promise<AddEditCipherInfo>;
setAddEditCipherInfo: (value: AddEditCipherInfo) => Promise<void>;
}

View File

@ -59,6 +59,7 @@ import { AttachmentView } from "../models/view/attachment.view";
import { CipherView } from "../models/view/cipher.view";
import { FieldView } from "../models/view/field.view";
import { PasswordHistoryView } from "../models/view/password-history.view";
import { AddEditCipherInfo } from "../types/add-edit-cipher-info";
import { DECRYPTED_CIPHERS, ENCRYPTED_CIPHERS } from "./key-state/ciphers.state";
@ -68,6 +69,12 @@ const CIPHERS_DISK_KEY = new KeyDefinition<Record<string, LocalData>>(CIPHERS_DI
deserializer: (obj) => obj,
});
const ADD_EDIT_CIPHER_INFO_KEY = new KeyDefinition<AddEditCipherInfo>(
CIPHERS_DISK,
"addEditCipherInfo",
{ deserializer: (obj) => obj },
);
export class CipherService implements CipherServiceAbstraction {
private sortedCiphersCache: SortedCiphersCache = new SortedCiphersCache(
this.sortCiphersByLastUsed,
@ -76,10 +83,12 @@ export class CipherService implements CipherServiceAbstraction {
localData$: Observable<Record<string, LocalData>>;
ciphers$: Observable<Record<string, CipherData>>;
cipherViews$: Observable<CipherView[]>;
addEditCipherInfo$: Observable<AddEditCipherInfo>;
private localDataState: ActiveUserState<Record<string, LocalData>>;
private encryptedCiphersState: ActiveUserState<Record<string, CipherData>>;
private decryptedCiphersState: DerivedState<CipherView[]>;
private addEditCipherInfoState: ActiveUserState<AddEditCipherInfo>;
constructor(
private cryptoService: CryptoService,
@ -101,10 +110,12 @@ export class CipherService implements CipherServiceAbstraction {
DECRYPTED_CIPHERS,
{ cipherService: this },
);
this.addEditCipherInfoState = this.stateProvider.getActive(ADD_EDIT_CIPHER_INFO_KEY);
this.localData$ = this.localDataState.state$;
this.ciphers$ = this.encryptedCiphersState.state$;
this.cipherViews$ = this.decryptedCiphersState.state$;
this.addEditCipherInfo$ = this.addEditCipherInfoState.state$;
}
async getDecryptedCipherCache(): Promise<CipherView[]> {
@ -1045,6 +1056,15 @@ export class CipherService implements CipherServiceAbstraction {
);
}
async getAddEditCipherInfo(): Promise<AddEditCipherInfo> {
const info = await firstValueFrom(this.addEditCipherInfo$);
return info;
}
async setAddEditCipherInfo(value: AddEditCipherInfo) {
await this.addEditCipherInfoState.update(() => value);
}
// Helpers
// In the case of a cipher that is being shared with an organization, we want to decrypt the