Skip to content

Commit

Permalink
[PM-5439] TOTP to clipboard does not work with autofill overlay (#7427)
Browse files Browse the repository at this point in the history
* [PM-5439] TOTP to Clipboard Does not Work with Autofill Overlay

* [PM-5439] Adding jest tests to validate totp copy action when filling using the overlay
  • Loading branch information
cagonzalezcs committed Jan 9, 2024
1 parent 0b3b039 commit b144f5b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions apps/browser/src/autofill/background/overlay.background.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { CipherService } from "@bitwarden/common/vault/services/cipher.service";

import { BrowserApi } from "../../platform/browser/browser-api";
import BrowserPlatformUtilsService from "../../platform/services/browser-platform-utils.service";
import { BrowserStateService } from "../../platform/services/browser-state.service";
import { SHOW_AUTOFILL_BUTTON } from "../constants";
import {
Expand Down Expand Up @@ -47,6 +48,7 @@ describe("OverlayBackground", () => {
const settingsService = mock<SettingsService>();
const stateService = mock<BrowserStateService>();
const i18nService = mock<I18nService>();
const platformUtilsService = mock<BrowserPlatformUtilsService>();
const initOverlayElementPorts = (options = { initList: true, initButton: true }) => {
const { initList, initButton } = options;
if (initButton) {
Expand All @@ -71,6 +73,7 @@ describe("OverlayBackground", () => {
settingsService,
stateService,
i18nService,
platformUtilsService,
);
overlayBackground.init();
});
Expand Down Expand Up @@ -1263,6 +1266,24 @@ describe("OverlayBackground", () => {
]).entries(),
);
});

it("copies the cipher's totp code to the clipboard after filling", async () => {
const cipher1 = mock<CipherView>({ id: "overlay-cipher-1" });
overlayBackground["overlayLoginCiphers"] = new Map([["overlay-cipher-1", cipher1]]);
isPasswordRepromptRequiredSpy.mockResolvedValue(false);
const copyToClipboardSpy = jest
.spyOn(overlayBackground["platformUtilsService"], "copyToClipboard")
.mockImplementation();
doAutoFillSpy.mockReturnValueOnce("totp-code");

sendPortMessage(listPortSpy, {
command: "fillSelectedListItem",
overlayCipherId: "overlay-cipher-2",
});
await flushPromises();

expect(copyToClipboardSpy).toHaveBeenCalledWith("totp-code", { window });
});
});

describe("getNewVaultItemDetails", () => {
Expand Down
8 changes: 7 additions & 1 deletion apps/browser/src/autofill/background/overlay.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { ThemeType } from "@bitwarden/common/platform/enums";
import { Utils } from "@bitwarden/common/platform/misc/utils";
Expand Down Expand Up @@ -91,6 +92,7 @@ class OverlayBackground implements OverlayBackgroundInterface {
private settingsService: SettingsService,
private stateService: StateService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
) {
this.iconsServerUrl = this.environmentService.getIconsUrl();
}
Expand Down Expand Up @@ -227,14 +229,18 @@ class OverlayBackground implements OverlayBackgroundInterface {
if (await this.autofillService.isPasswordRepromptRequired(cipher, sender.tab)) {
return;
}
await this.autofillService.doAutoFill({
const totpCode = await this.autofillService.doAutoFill({
tab: sender.tab,
cipher: cipher,
pageDetails: this.pageDetailsForTab[sender.tab.id],
fillNewPassword: true,
allowTotpAutofill: true,
});

if (totpCode) {
this.platformUtilsService.copyToClipboard(totpCode, { window });
}

this.overlayLoginCiphers = new Map([[overlayCipherId, cipher], ...this.overlayLoginCiphers]);
}

Expand Down
1 change: 1 addition & 0 deletions apps/browser/src/background/main.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ export default class MainBackground {
this.settingsService,
this.stateService,
this.i18nService,
this.platformUtilsService,
);
this.filelessImporterBackground = new FilelessImporterBackground(
this.configService,
Expand Down

0 comments on commit b144f5b

Please sign in to comment.