Files
homelab/dotfiles/quickshell/HyprChrome/Widgets/Polkit/ProtonPass.qml
T
darmanandClaude Opus 5.5 acd08afaad feat(terra): Proton Pass picker in the polkit/askpass dialog
A collapsed panel under the dialog lists logins from every vault via
pass-cli, one tab per vault with overflow into a dropdown; picking one
fetches its password into the field. Listing never touches secrets, and
the `darman@<config>` items are `custom`, not `login`, so both types are
listed. Adds a show/hide toggle to the password field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 18:49:36 +02:00

177 lines
6.6 KiB
QML

pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import Quickshell.Io
// Proton Pass logins for the polkit and askpass prompts, via pass-cli.
//
// Listing never touches a secret: `item list` without --show-secrets returns
// ids and titles only. A password is fetched on its own, for one item, after
// the user picks it — and read through a SplitParser rather than a
// StdioCollector, which would keep it in its `text` property afterwards.
//
// `item list` refuses to run without a vault, so "all vaults" is one call per
// vault. Each takes ~0.8s, hence run in parallel by a single shell script.
QtObject {
id: root
// {shareId, id, title, vault}, unsorted — the dialog orders and groups them.
property var entries: []
// Why the list is empty or incomplete; "" when it is fine.
property string error: ""
property string fetchError: ""
readonly property bool loading: vaults.running || items.running
readonly property bool fetching: fetcher.running
// Shown inside the picker.
readonly property string listStatus: root.loading
? (root.entries.length === 0 ? "LOADING VAULTS…" : "REFRESHING…")
: root.error
// Shown under the field, picker open or not.
readonly property string fetchStatus: root.fetching ? "FETCHING PASSWORD…" : root.fetchError
// Bumped per request; a fetch that lands after it changed is dropped, so a
// slow fetch cannot fill a password into the NEXT request's field.
property int generation: 0
property int fetchGeneration: -1
property bool fetchGotLine: false
property var vaultNames: ({})
signal fetched(string secret)
function refresh() {
if (root.loading)
return;
root.error = "";
vaults.running = true;
}
function invalidate() {
root.generation++;
root.fetchError = "";
}
function fetch(entry) {
if (!entry || root.fetching)
return;
root.fetchError = "";
root.fetchGeneration = root.generation;
root.fetchGotLine = false;
fetcher.command = ["pass-cli", "item", "view", "--share-id", entry.shareId,
"--item-id", entry.id, "--field", "password", "--output", "human"];
fetcher.running = true;
}
function describe(output) {
const text = output.replace(/\x1b\[[0-9;]*m/g, "");
if (/not found/.test(text))
return "PASS-CLI NOT INSTALLED";
if (/authenticated client|no session/.test(text))
return "PASS-CLI LOGGED OUT — RUN pass-cli login";
const lines = text.trim().split("\n");
return lines[lines.length - 1].toUpperCase();
}
// Prints the vault JSON or `ERR <output>`: one stream, so success and
// failure need no ordering between exit code and stdout.
readonly property var vaults: Process {
id: vaults
command: ["sh", "-c",
"out=$(pass-cli vault list --output json 2>&1) && printf '%s' \"$out\" || printf 'ERR %s' \"$out\""]
stdout: StdioCollector {
onStreamFinished: {
const body = this.text;
if (body.startsWith("ERR ")) {
root.error = root.describe(body.slice(4));
return;
}
let list;
try {
list = JSON.parse(body).vaults || [];
} catch (e) {
root.error = "UNREADABLE VAULT LIST";
return;
}
const names = {};
for (const v of list)
names[v.share_id] = v.name;
root.vaultNames = names;
if (list.length === 0) {
root.entries = [];
root.error = "NO VAULTS";
return;
}
items.command = ["sh", "-c", items.script, "sh"].concat(list.map(v => v.share_id));
items.running = true;
}
}
}
// One `item list` per vault in parallel, each to its own file, then
// printed back in order under an `@@<share_id>` line — concurrent writers
// on one pipe would interleave.
//
// No --filter-type: it takes ONE type, and the `darman@<config>` machine
// logins scripts/deploy reads are `custom` items, not `login`.
readonly property var items: Process {
id: items
readonly property string script: "d=$(mktemp -d \"${XDG_RUNTIME_DIR:-/tmp}/qs-pass.XXXXXX\") || exit 1;"
+ " trap 'rm -rf \"$d\"' EXIT; i=0;"
+ " for s; do i=$((i+1)); pass-cli item list --share-id \"$s\""
+ " --filter-state active --output json > \"$d/$i\" 2>/dev/null & done; wait; i=0;"
+ " for s; do i=$((i+1)); printf '\\n@@%s\\n' \"$s\"; cat \"$d/$i\"; done"
stdout: StdioCollector {
onStreamFinished: {
const found = [];
let failed = 0;
for (const chunk of this.text.split(/^@@/m)) {
const nl = chunk.indexOf("\n");
const share = nl < 0 ? "" : chunk.slice(0, nl).trim();
if (share === "")
continue;
try {
for (const it of JSON.parse(chunk.slice(nl + 1)).items || [])
if (it.item_type === "login" || it.item_type === "custom")
found.push({ shareId: share, id: it.id, title: it.title, vault: root.vaultNames[share] || "" });
} catch (e) {
failed++;
}
}
root.entries = found;
root.error = failed === 0 ? (found.length === 0 ? "NO LOGINS IN ANY VAULT" : "")
: failed + " VAULT" + (failed === 1 ? "" : "S") + " FAILED TO LIST";
}
}
}
readonly property var fetcher: Process {
id: fetcher
stdout: SplitParser {
splitMarker: "\n"
onRead: line => {
if (root.fetchGotLine)
return;
root.fetchGotLine = true;
if (root.fetchGeneration !== root.generation)
return;
root.fetchError = line === "" ? "THAT ITEM HAS NO PASSWORD" : "";
if (line !== "")
root.fetched(line);
}
}
onExited: (exitCode, exitStatus) => {
if (!root.fetchGotLine && root.fetchGeneration === root.generation)
root.fetchError = exitCode === 0 ? "THAT ITEM HAS NO PASSWORD" : "COULD NOT READ THAT ITEM";
}
}
}