Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PS-817] Add Generate Password Shortcut to MV3 #3575

Merged
merged 19 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/browser/src/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MainBackground from "./background/main.background";
import { onAlarmListener } from "./listeners/onAlarmListener";
import { onCommandListener } from "./listeners/onCommandListener";
import { onInstallListener } from "./listeners/onInstallListener";

Expand All @@ -7,6 +8,7 @@ const manifest = chrome.runtime.getManifest();
if (manifest.manifest_version === 3) {
chrome.commands.onCommand.addListener(onCommandListener);
chrome.runtime.onInstalled.addListener(onInstallListener);
chrome.alarms.onAlarm.addListener(onAlarmListener);
} else {
const bitwardenMain = ((window as any).bitwardenMain = new MainBackground());
bitwardenMain.bootstrap().then(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AutofillService as AbstractAutofillService } from "../../services/abstractions/autofill.service";
import AutofillService from "../../services/autofill.service";

import { cipherServiceFactory, CipherServiceInitOptions } from "./cipher-service.factory";
import { eventServiceFactory, EventServiceInitOptions } from "./event-service.factory";
import { CachedServices, factory, FactoryOptions } from "./factory-options";
import { logServiceFactory, LogServiceInitOptions } from "./log-service.factory";
import { stateServiceFactory, StateServiceInitOptions } from "./state-service.factory";
import { totpServiceFactory, TotpServiceInitOptions } from "./totp-service.factory";

type AutofillServiceFactoryOptions = FactoryOptions;

export type AutofillServiceInitOptions = AutofillServiceFactoryOptions &
CipherServiceInitOptions &
StateServiceInitOptions &
TotpServiceInitOptions &
EventServiceInitOptions &
LogServiceInitOptions;

export function autofillServiceFactory(
cache: { autofillService?: AbstractAutofillService } & CachedServices,
opts: AutofillServiceInitOptions
): Promise<AbstractAutofillService> {
return factory(
cache,
"autofillService",
opts,
async () =>
new AutofillService(
await cipherServiceFactory(cache, opts),
await stateServiceFactory(cache, opts),
await totpServiceFactory(cache, opts),
await eventServiceFactory(cache, opts),
await logServiceFactory(cache, opts)
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function cipherServiceFactory(
await apiServiceFactory(cache, opts),
await fileUploadServiceFactory(cache, opts),
await i18nServiceFactory(cache, opts),
opts.cipherServiceOptions.searchServiceFactory === undefined
opts.cipherServiceOptions?.searchServiceFactory === undefined
justindbaur marked this conversation as resolved.
Show resolved Hide resolved
? () => cache.searchService
: opts.cipherServiceOptions.searchServiceFactory,
await logServiceFactory(cache, opts),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CryptoService as AbstractCryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { CryptoService } from "@bitwarden/common/services/crypto.service";

import { BrowserCryptoService } from "../../services/browserCrypto.service";

import {
cryptoFunctionServiceFactory,
Expand Down Expand Up @@ -32,7 +33,7 @@ export function cryptoServiceFactory(
"cryptoService",
opts,
async () =>
new CryptoService(
new BrowserCryptoService(
await cryptoFunctionServiceFactory(cache, opts),
await encryptServiceFactory(cache, opts),
await platformUtilsServiceFactory(cache, opts),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { EventService as AbstractEventService } from "@bitwarden/common/abstractions/event.service";
import { EventService } from "@bitwarden/common/services/event.service";
import { NoopEventService } from "@bitwarden/common/services/noopEvent.service";

import { apiServiceFactory, ApiServiceInitOptions } from "./api-service.factory";
import { cipherServiceFactory, CipherServiceInitOptions } from "./cipher-service.factory";
import { CachedServices, factory, FactoryOptions } from "./factory-options";
import { logServiceFactory, LogServiceInitOptions } from "./log-service.factory";
import {
organizationServiceFactory,
OrganizationServiceInitOptions,
} from "./organization-service.factory";
import { stateServiceFactory, StateServiceInitOptions } from "./state-service.factory";

type EventServiceFactoryOptions = FactoryOptions & {
eventServiceOptions: {
useNoopService: boolean;
justindbaur marked this conversation as resolved.
Show resolved Hide resolved
};
};

export type EventServiceInitOptions = EventServiceFactoryOptions &
ApiServiceInitOptions &
CipherServiceInitOptions &
StateServiceInitOptions &
LogServiceInitOptions &
OrganizationServiceInitOptions;

export function eventServiceFactory(
cache: { eventService?: AbstractEventService } & CachedServices,
opts: EventServiceInitOptions
): Promise<AbstractEventService> {
return factory(cache, "eventService", opts, async () => {
if (opts.eventServiceOptions.useNoopService) {
return new NoopEventService();
}

return new EventService(
await apiServiceFactory(cache, opts),
await cipherServiceFactory(cache, opts),
await stateServiceFactory(cache, opts),
await logServiceFactory(cache, opts),
await organizationServiceFactory(cache, opts)
);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { PasswordGenerationService as AbstractPasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
import { PasswordGenerationService } from "@bitwarden/common/services/passwordGeneration.service";

import { cryptoServiceFactory, CryptoServiceInitOptions } from "./crypto-service.factory";
import { CachedServices, factory, FactoryOptions } from "./factory-options";
import { policyServiceFactory, PolicyServiceInitOptions } from "./policy-service.factory";
import { stateServiceFactory, StateServiceInitOptions } from "./state-service.factory";

type PasswordGenerationServiceFactoryOptions = FactoryOptions;

export type PasswordGenerationServiceInitOptions = PasswordGenerationServiceFactoryOptions &
CryptoServiceInitOptions &
PolicyServiceInitOptions &
StateServiceInitOptions;

export function passwordGenerationServiceFactory(
cache: { passwordGenerationService?: AbstractPasswordGenerationService } & CachedServices,
opts: PasswordGenerationServiceInitOptions
): Promise<AbstractPasswordGenerationService> {
return factory(
cache,
"passwordGenerationService",
opts,
async () =>
new PasswordGenerationService(
await cryptoServiceFactory(cache, opts),
await policyServiceFactory(cache, opts),
await stateServiceFactory(cache, opts)
)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TotpService as AbstractTotpService } from "@bitwarden/common/abstractions/totp.service";
import { TotpService } from "@bitwarden/common/services/totp.service";

import {
cryptoFunctionServiceFactory,
CryptoFunctionServiceInitOptions,
} from "./crypto-function-service.factory";
import { CachedServices, factory, FactoryOptions } from "./factory-options";
import { logServiceFactory, LogServiceInitOptions } from "./log-service.factory";

type TotpServiceFactoryOptions = FactoryOptions;

export type TotpServiceInitOptions = TotpServiceFactoryOptions &
CryptoFunctionServiceInitOptions &
LogServiceInitOptions;

export function totpServiceFactory(
cache: { totpService?: AbstractTotpService } & CachedServices,
opts: TotpServiceInitOptions
): Promise<AbstractTotpService> {
return factory(
cache,
"totpService",
opts,
async () =>
new TotpService(
await cryptoFunctionServiceFactory(cache, opts),
await logServiceFactory(cache, opts)
)
);
}
9 changes: 9 additions & 0 deletions apps/browser/src/browser/sendTabsMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TabMessage } from "../types/tab-messages";

export const sendTabsMessage = <T = unknown>(
justindbaur marked this conversation as resolved.
Show resolved Hide resolved
tabId: number,
message: TabMessage,
responseCallback?: (response: T) => void
) => {
chrome.tabs.sendMessage<TabMessage, T>(tabId, message, responseCallback);
};
23 changes: 23 additions & 0 deletions apps/browser/src/content/miscUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TabMessage } from "../types/tab-messages";

async function copyText(text: string) {
await window.navigator.clipboard.writeText(text);
}

async function onMessageListener(
msg: TabMessage,
sender: chrome.runtime.MessageSender,
responseCallback: (response: unknown) => void
) {
switch (msg.command) {
case "copyText":
await copyText(msg.text);
break;
case "clearClipboard":
await copyText("\u0000");
break;
default:
}
}

chrome.runtime.onMessage.addListener(onMessageListener);
15 changes: 15 additions & 0 deletions apps/browser/src/listeners/onAlarmListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { sendTabsMessage } from "../browser/sendTabsMessage";

export const onAlarmListener = async (alarm: chrome.alarms.Alarm) => {
switch (alarm.name) {
case "clearClipboard": {
const tabs = await chrome.tabs.query({
active: true,
});
if (tabs && tabs.length > 0) {
sendTabsMessage(tabs[0].id, { command: "clearClipboard" });
}
break;
}
}
};
Loading