bitwarden-desktop/src/main/desktopCredentialStorageLis...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-04-05 16:54:44 +02:00
import { ipcMain } from "electron";
import { deletePassword, getPassword, setPassword } from "keytar";
2022-04-05 16:54:44 +02:00
import { BiometricMain } from "./biometric/biometric.main";
const AuthRequiredSuffix = "_biometric";
const AuthenticatedActions = ["getPassword"];
export class DesktopCredentialStorageListener {
constructor(private serviceName: string, private biometricService: BiometricMain) {}
init() {
ipcMain.on("keytar", async (event: any, message: any) => {
try {
let serviceName = this.serviceName;
message.keySuffix = "_" + (message.keySuffix ?? "");
if (message.keySuffix !== "_") {
serviceName += message.keySuffix;
}
const authenticationRequired =
AuthenticatedActions.includes(message.action) && AuthRequiredSuffix === message.keySuffix;
const authenticated = !authenticationRequired || (await this.authenticateBiometric());
let val: string | boolean = null;
if (authenticated && message.action && message.key) {
if (message.action === "getPassword") {
val = await getPassword(serviceName, message.key);
2022-04-05 16:54:44 +02:00
} else if (message.action === "hasPassword") {
const result = await getPassword(serviceName, message.key);
2022-04-05 16:54:44 +02:00
val = result != null;
} else if (message.action === "setPassword" && message.value) {
await setPassword(serviceName, message.key, message.value);
2022-04-05 16:54:44 +02:00
} else if (message.action === "deletePassword") {
await deletePassword(serviceName, message.key);
2022-04-05 16:54:44 +02:00
}
}
event.returnValue = val;
} catch {
event.returnValue = null;
}
});
}
private async authenticateBiometric(): Promise<boolean> {
if (this.biometricService) {
return await this.biometricService.authenticateBiometric();
}
return false;
}
}