Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

linuxSecretStorage.ts

69 lines2.0 KB

1export type LinuxPasswordStorePreference =
2 | "auto"
3 | "gnome-libsecret"
4 | "kwallet"
5 | "kwallet5"
6 | "kwallet6";
7export type LinuxPasswordStoreSwitch = Exclude<LinuxPasswordStorePreference, "auto">;
8​
9export const DEFAULT_LINUX_PASSWORD_STORE: LinuxPasswordStorePreference = "auto";
10​
11// Chromium matches desktop names case-sensitively and stops at the first recognized value.
12const ELECTRON_LIBSECRET_DESKTOPS = new Set([
13 "Deepin",
14 "GNOME",
15 "Pantheon",
16 "UKUI",
17 "Unity",
18 "X-Cinnamon",
19 "XFCE",
20]);
21// Chromium selects a KWallet generation for KDE from KDE_SESSION_VERSION, so it needs no help.
22const ELECTRON_KDE_DESKTOP = "KDE";
23// Chromium recognizes LXQt and still selects basic text for it, so it does need a forced backend.
24const ELECTRON_UNPROTECTED_DESKTOPS = new Set(["LXQt"]);
25​
26export function normalizeLinuxPasswordStorePreference(
27 value: unknown,
28): LinuxPasswordStorePreference {
29 return value === "gnome-libsecret" ||
30 value === "kwallet" ||
31 value === "kwallet5" ||
32 value === "kwallet6"
33 ? value
34 : DEFAULT_LINUX_PASSWORD_STORE;
36​
37export function resolveLinuxPasswordStoreSwitch(input: {
38 readonly preference: LinuxPasswordStorePreference;
39 readonly env: NodeJS.ProcessEnv;
40}): LinuxPasswordStoreSwitch | null {
41 if (input.preference !== "auto") {
42 return input.preference;
43 }
44​
45 return electronSelectsProtectedBackend(input.env) ? null : "gnome-libsecret";
47​
48// DESKTOP_SESSION and legacy KDE markers can be stale, so only trust exact XDG_CURRENT_DESKTOP values.
49​
50function electronSelectsProtectedBackend(env: NodeJS.ProcessEnv): boolean {
51 for (const name of splitDesktopNameList(env.XDG_CURRENT_DESKTOP)) {
52 const trimmed = name.trim();
53 if (trimmed.length === 0) {
54 continue;
55 }
56 if (trimmed === ELECTRON_KDE_DESKTOP || ELECTRON_LIBSECRET_DESKTOPS.has(trimmed)) {
57 return true;
58 }
59 if (ELECTRON_UNPROTECTED_DESKTOPS.has(trimmed)) {
60 return false;
61 }
62 }
63​
64 return false;
66​
67function splitDesktopNameList(value: string | undefined): string[] {
68 return value?.split(":") ?? [];