Repository navigation
Expand file tree
/
Copy pathAuthBridge.qml
More file actions
133 lines (121 loc) · 4.03 KB
/
Copy pathAuthBridge.qml
File metadata and controls
133 lines (121 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import QtQuick
import Quickshell.Io
QtObject {
id: root
property bool running: false
property string error: ""
property string token: ""
property string stdoutBuffer: ""
property string stderrBuffer: ""
property bool outputOverflowed: false
// A token-exchange result or error message is at most a few hundred
// bytes; this bounds what the shared shell process will hold onto
// regardless of what the helper script actually does.
readonly property int maxBufferBytes: 65536
signal success(string token)
signal failed(string message)
function start() {
if (running) return;
running = true;
error = "";
token = "";
stdoutBuffer = "";
stderrBuffer = "";
outputOverflowed = false;
try {
var scriptPath = String(Qt.resolvedUrl("bin/keeply-auth"));
if (scriptPath.startsWith("file://")) {
scriptPath = scriptPath.substring(7);
}
authProcess.command = ["python3", scriptPath];
authProcess.running = true;
} catch (e) {
running = false;
error = "Could not start auth helper: " + e;
failed(error);
}
}
function cancel() {
if (authProcess.running) {
authProcess.signal(2); // SIGINT
}
running = false;
error = "Cancelled";
failed(error);
}
// Bounds what a runaway or hostile helper process can make this
// long-lived shell process hold in memory, independent of whatever the
// script itself does. Terminates the process on overflow rather than
// just discarding further output, so it can't keep running indefinitely.
function appendStdout(value) {
if (root.outputOverflowed) return;
var next = root.stdoutBuffer + value;
if (next.length > root.maxBufferBytes) {
root.outputOverflowed = true;
root.stdoutBuffer = next.substring(0, root.maxBufferBytes);
if (authProcess.running) authProcess.signal(15); // SIGTERM
return;
}
root.stdoutBuffer = next;
}
function appendStderr(value) {
if (root.outputOverflowed) return;
var next = root.stderrBuffer + value;
if (next.length > root.maxBufferBytes) {
root.outputOverflowed = true;
root.stderrBuffer = next.substring(0, root.maxBufferBytes);
if (authProcess.running) authProcess.signal(15); // SIGTERM
return;
}
root.stderrBuffer = next;
}
property Process authProcess: Process {
stdout: SplitParser {
// An empty splitMarker delivers whatever a single OS read returns,
// instead of the default "\n" behavior of buffering an entire line
// internally before onRead ever fires once. appendStdout already
// accumulates and caps across multiple calls, so this only changes
// how early each chunk is checked, catching a helper that emits one
// huge unbroken line before the default delimiter search ever would.
splitMarker: ""
onRead: function(value) {
root.appendStdout(value);
}
}
stderr: SplitParser {
splitMarker: ""
onRead: function(value) {
root.appendStderr(value);
}
}
onRunningChanged: {
if (!running) {
root.running = false;
if (root.outputOverflowed) {
root.error = "Auth helper produced unexpectedly large output";
root.failed(root.error);
} else if (root.stdoutBuffer.trim().length > 0) {
try {
var result = JSON.parse(root.stdoutBuffer.trim());
if (result.accessToken) {
root.token = result.accessToken;
root.success(root.token);
} else if (result.error) {
root.error = result.error;
root.failed(root.error);
}
} catch (e) {
root.error = "Invalid response from auth helper";
root.failed(root.error);
}
} else if (root.stderrBuffer.trim().length > 0) {
root.error = root.stderrBuffer.trim();
root.failed(root.error);
} else {
root.error = "Auth helper exited unexpectedly";
root.failed(root.error);
}
}
}
}
}