@x-tech-laboratories/x-app 0.126.0 → 0.127.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/app/daemon.js +1 -1
- package/dist/app/define.d.ts +8 -0
- package/dist/app/define.js +1 -0
- package/dist/app/desktop.js +47 -11
- package/dist/app/preload.js +1 -0
- package/dist/app/protocol.d.ts +1 -0
- package/dist/app/server.d.ts +2 -0
- package/dist/app/server.js +6 -2
- package/dist/app/server_fetch.d.ts +7 -0
- package/dist/app/server_fetch.js +37 -0
- package/dist/app/web/hosts.js +5 -0
- package/dist/app/web/portal.js +3 -0
- package/dist/app/web/settings_page.d.ts +3 -0
- package/dist/app/web/settings_page.js +6 -1
- package/dist/server/account_store.js +10 -1
- package/dist/server/passwords.d.ts +2 -2
- package/dist/server/passwords.js +5 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -275,6 +275,14 @@ An app whose work needs a computer, such as one that runs programs on checkouts,
|
|
|
275
275
|
services keep them through `context.files`, the server and daemon serve them at `/api/files/<name>` to those signed
|
|
276
276
|
in, and the page shows one from `page().file_src(name)`: the server's route, the desktop's `<id>-file://<name>` from
|
|
277
277
|
this computer or the server in use, or a blob on the phone.
|
|
278
|
+
- **A desktop portal**: `desktop: "portal"` makes the desktop app a window onto a server chosen under Connections, as
|
|
279
|
+
`phone: "portal"` does the phone: it runs no daemon, keeps no data and keeps its own switches in its profile.
|
|
280
|
+
- **Pages on REST routes**: `page().fetch("api/runs")` reaches the app's own server routes from every host: the
|
|
281
|
+
browser's own sign-in, and on a portal the server chosen under Connections with this device's token, the desktop's
|
|
282
|
+
through its main process (`app/server_fetch` does the rewriting).
|
|
283
|
+
- **Sign-in from the config**: `admin_name` names the admin a bare hash in the accounts file signs in as, and
|
|
284
|
+
`proxy_user_header` (with `trusted_proxies`, 127.0.0.1 and ::1 unless set) signs in whoever a reverse proxy on those
|
|
285
|
+
addresses names; `define_app`'s `check_access` checks a user's access when an admin sets it, such as a list of grants.
|
|
278
286
|
- **Keys in a settings export**: `render_app`'s `exports_secrets: true` offers "Include keys and tokens" on exporting
|
|
279
287
|
the settings, for an app whose settings hold other services' keys.
|
|
280
288
|
- **Links to other apps**: `run_desktop`'s `external_schemes`, such as `["stremio:", "steam:"]`, opens those links in
|
package/dist/app/daemon.js
CHANGED
|
@@ -126,7 +126,7 @@ export async function start_daemon(app, options) {
|
|
|
126
126
|
default_port: app.ports.server,
|
|
127
127
|
build: (admins, https) => build_server(app, {
|
|
128
128
|
database: opened.database,
|
|
129
|
-
accounts: new AccountStore({ database: accounts_database, app_name: app.name, declared: admins, ...(app.token_prefix ? { token_prefix: app.token_prefix } : {}) }),
|
|
129
|
+
accounts: new AccountStore({ database: accounts_database, app_name: app.name, declared: admins, ...(app.token_prefix ? { token_prefix: app.token_prefix } : {}), ...(app.check_access ? { check_access: app.check_access } : {}) }),
|
|
130
130
|
web_folder,
|
|
131
131
|
changes,
|
|
132
132
|
backup_methods: opened.backup_methods,
|
package/dist/app/define.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { type Caller } from "../caller.ts";
|
|
|
5
5
|
import { type CertificateMethods } from "../certificate_protocol.ts";
|
|
6
6
|
import { type ConnectionMethods } from "../connection_protocol.ts";
|
|
7
7
|
import { type Bridge } from "../electron/bridge.ts";
|
|
8
|
+
import type { FetchMethod } from "../electron/fetch.ts";
|
|
8
9
|
import { type EventsBridge } from "../electron/events.ts";
|
|
9
10
|
import type { History } from "../history.ts";
|
|
10
11
|
import type { ScopeSpec } from "../server/declared_tokens.ts";
|
|
@@ -94,8 +95,10 @@ export interface AppOptions<Features extends readonly Feature[]> {
|
|
|
94
95
|
synced_theme?: boolean;
|
|
95
96
|
host_tables?: Readonly<Record<string, HostTable>>;
|
|
96
97
|
remote_access?: boolean;
|
|
98
|
+
desktop?: "daemon" | "portal";
|
|
97
99
|
phone?: "device" | "portal";
|
|
98
100
|
roles?: readonly string[];
|
|
101
|
+
check_access?: (value: unknown) => unknown;
|
|
99
102
|
sync?: boolean;
|
|
100
103
|
declared_tokens?: {
|
|
101
104
|
key: string;
|
|
@@ -133,6 +136,9 @@ export interface AppBridges<Methods extends Record<keyof Methods, (...args: neve
|
|
|
133
136
|
events: EventsBridge<AppEvents>;
|
|
134
137
|
host: Bridge<HostAbilityMethods>;
|
|
135
138
|
remote_access: Bridge<RemoteAccessMethods>;
|
|
139
|
+
fetch: Bridge<{
|
|
140
|
+
http_fetch: FetchMethod;
|
|
141
|
+
}>;
|
|
136
142
|
host_tables: Readonly<Record<string, Bridge<Record<string, (...args: never[]) => unknown>>>>;
|
|
137
143
|
}
|
|
138
144
|
export declare function define_app<const Features extends readonly Feature[]>(options: AppOptions<Features>): {
|
|
@@ -174,8 +180,10 @@ export declare function define_app<const Features extends readonly Feature[]>(op
|
|
|
174
180
|
};
|
|
175
181
|
synced_theme?: boolean;
|
|
176
182
|
remote_access?: boolean;
|
|
183
|
+
desktop?: "daemon" | "portal";
|
|
177
184
|
phone?: "device" | "portal";
|
|
178
185
|
roles?: readonly string[];
|
|
186
|
+
check_access?: (value: unknown) => unknown;
|
|
179
187
|
sync?: boolean;
|
|
180
188
|
declared_tokens?: {
|
|
181
189
|
key: string;
|
package/dist/app/define.js
CHANGED
|
@@ -266,6 +266,7 @@ export function define_app(options) {
|
|
|
266
266
|
local_mcp: define_bridge(`${prefix}_local_mcp`, local_mcp_method_names),
|
|
267
267
|
events: define_events(`${prefix}_events`),
|
|
268
268
|
host: define_bridge(`${prefix}_host`, host_ability_method_names),
|
|
269
|
+
fetch: define_bridge(`${prefix}_fetch`, ["http_fetch"]),
|
|
269
270
|
// Letting other devices reach this computer's daemon, answered over its socket alone.
|
|
270
271
|
remote_access: define_bridge(`${prefix}_remote`, remote_access_method_names),
|
|
271
272
|
host_tables: Object.fromEntries(Object.entries(options.host_tables ?? {}).map(([name, table]) => [name, define_bridge(`${prefix}_${name}`, Object.keys(table.api))])),
|
package/dist/app/desktop.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// work to this computer's daemon or to a server chosen under Settings → Connections. The app hands in Electron, so this
|
|
3
3
|
// module builds without it.
|
|
4
4
|
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
5
|
-
import { unwatchFile, watchFile } from "node:fs";
|
|
5
|
+
import { existsSync, readFileSync, unwatchFile, watchFile } from "node:fs";
|
|
6
6
|
import { homedir, hostname } from "node:os";
|
|
7
7
|
import { writeFile } from "node:fs/promises";
|
|
8
8
|
import { extname, join, resolve } from "node:path";
|
|
@@ -24,9 +24,11 @@ import { peer_listening_method_names, sync_device_method_names } from "../sync_p
|
|
|
24
24
|
import { route_of_link } from "./links.js";
|
|
25
25
|
import { remote_access_method_names } from "../remote_access_protocol.js";
|
|
26
26
|
import { file_response, register_file_scheme } from "../electron/file_protocol.js";
|
|
27
|
+
import { create_main_fetch } from "../electron/fetch.js";
|
|
28
|
+
import { no_server_text, server_headers, server_url } from "./server_fetch.js";
|
|
27
29
|
import { folder_file_store } from "../node_file_store.js";
|
|
28
30
|
import { read_plugin_file } from "../plugin_folder.js";
|
|
29
|
-
import { file_scheme_of, host_ability_method_names, notice_event, notice_is_on, notice_withdrawn_event } from "./protocol.js";
|
|
31
|
+
import { file_scheme_of, host_ability_method_names, notice_event, read_desktop_settings, notice_is_on, notice_withdrawn_event } from "./protocol.js";
|
|
30
32
|
// The Save dialog's options for a file name: Downloads with that name, offering files of its kind.
|
|
31
33
|
export function save_dialog_options(file_name, downloads) {
|
|
32
34
|
const extension = extname(file_name).slice(1);
|
|
@@ -41,6 +43,8 @@ export function run_desktop(app, electron, options) {
|
|
|
41
43
|
const start = options.start_daemon ?? start_daemon;
|
|
42
44
|
const call_socket = options.call_daemon ?? call_daemon;
|
|
43
45
|
const write_autostart = options.set_autostart ?? set_autostart;
|
|
46
|
+
// A portal runs no daemon and keeps no data: it works on a server chosen under Connections.
|
|
47
|
+
const portal = app.desktop === "portal";
|
|
44
48
|
// ── Profile ──────────────────────────────────────────────────────────
|
|
45
49
|
// Dev runs set this so they never touch the installed app's data.
|
|
46
50
|
const own_profile = env[`${prefix}_USER_DATA`];
|
|
@@ -61,6 +65,8 @@ export function run_desktop(app, electron, options) {
|
|
|
61
65
|
// Opening the app replaces a daemon another build left running, such as one from before an upgrade. Only opening does:
|
|
62
66
|
// two windows of different builds that both replaced on reconnecting would stop each other's daemon in turn.
|
|
63
67
|
function ensure_daemon(replace_other_build = false) {
|
|
68
|
+
if (portal)
|
|
69
|
+
return Promise.resolve("portal");
|
|
64
70
|
// Only the user's own daemon reads what this computer's configuration declares, unless a run names a file itself.
|
|
65
71
|
const declared = is_own_profile ? {} : { [`${prefix}_DECLARED_FILE`]: env[`${prefix}_DECLARED_FILE`] ?? "" };
|
|
66
72
|
const own_env = options.daemon_env?.({ path: profile, is_own_profile }) ?? {};
|
|
@@ -83,7 +89,7 @@ export function run_desktop(app, electron, options) {
|
|
|
83
89
|
trust_certificates();
|
|
84
90
|
watchFile(certificates_file, { interval: 2000 }, trust_certificates);
|
|
85
91
|
// This computer's daemon, and servers each signed in once with a token of this device's own.
|
|
86
|
-
const connections = create_connections({ storage: connection_file_storage(join(profile, "connections.json")), offers_local:
|
|
92
|
+
const connections = create_connections({ storage: connection_file_storage(join(profile, "connections.json")), offers_local: !portal, device_name: `${app.name} on ${hostname()}` });
|
|
87
93
|
const link = create_daemon_link({ connections, socket_path });
|
|
88
94
|
const windows = () => BrowserWindow.getAllWindows();
|
|
89
95
|
// A daemon elsewhere tells of its notices as events; this computer shows them by its own switches, as its own
|
|
@@ -104,9 +110,11 @@ export function run_desktop(app, electron, options) {
|
|
|
104
110
|
const router = create_daemon_router({
|
|
105
111
|
connections,
|
|
106
112
|
link,
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
113
|
+
...(portal ? {} : {
|
|
114
|
+
call_local: (path, args, headers) => call_socket(socket_path, path, args, headers),
|
|
115
|
+
restart_local: () => ensure_daemon(),
|
|
116
|
+
}),
|
|
117
|
+
local_tables: portal ? [] : [...default_local_tables, "peer_listening", "remote", ...own_tables],
|
|
110
118
|
is_read_only: app.is_read_only,
|
|
111
119
|
on_change: (change) => {
|
|
112
120
|
app.bridges.events.emit(windows(), "changed", change);
|
|
@@ -153,14 +161,38 @@ export function run_desktop(app, electron, options) {
|
|
|
153
161
|
if (synced_files && electron.protocol)
|
|
154
162
|
register_file_scheme(electron.protocol, file_scheme_of(app.id));
|
|
155
163
|
// This computer's own settings, always from its own daemon.
|
|
156
|
-
|
|
157
|
-
|
|
164
|
+
// A portal keeps them in the profile itself, as it runs no daemon to keep them.
|
|
165
|
+
const portal_settings_file = join(profile, "desktop.json");
|
|
166
|
+
async function get_desktop_settings() {
|
|
167
|
+
if (!portal)
|
|
168
|
+
return call("daemon/get_desktop_settings");
|
|
169
|
+
return read_desktop_settings(existsSync(portal_settings_file) ? JSON.parse(readFileSync(portal_settings_file, "utf8")) : null, app.desktop_settings);
|
|
170
|
+
}
|
|
171
|
+
async function set_desktop_setting(key, value) {
|
|
172
|
+
if (!portal)
|
|
173
|
+
return void (await call("daemon/set_desktop_setting", [key, value]));
|
|
174
|
+
await writeFile(portal_settings_file, JSON.stringify(read_desktop_settings({ ...(await get_desktop_settings()), [key]: value }, app.desktop_settings), null, 2));
|
|
175
|
+
}
|
|
176
|
+
// The page's request to its server's routes, sent to the server the window works on with this device's token.
|
|
177
|
+
const main_fetch = create_main_fetch({
|
|
178
|
+
get hosts() {
|
|
179
|
+
const daemon = connections.current();
|
|
180
|
+
return daemon?.kind === "remote" ? [new URL(daemon.address).host] : [];
|
|
181
|
+
},
|
|
182
|
+
allow_http: true,
|
|
183
|
+
});
|
|
184
|
+
function http_fetch(request) {
|
|
185
|
+
const daemon = connections.current();
|
|
186
|
+
if (daemon?.kind !== "remote")
|
|
187
|
+
throw new Error(no_server_text);
|
|
188
|
+
return main_fetch({ ...request, url: server_url(daemon.address, request.url), headers: server_headers(request.headers, daemon.token) });
|
|
158
189
|
}
|
|
159
190
|
// The tray as its setting says, and, for the user's own profile only, the login entry; a dev run never writes it.
|
|
160
191
|
async function apply_window_settings() {
|
|
161
192
|
const settings = await get_desktop_settings();
|
|
162
193
|
tray?.set_shown(settings.show_tray_icon);
|
|
163
|
-
|
|
194
|
+
// A portal has no daemon to start at login.
|
|
195
|
+
if (!is_own_profile || portal)
|
|
164
196
|
return;
|
|
165
197
|
write_autostart({ id: `${app.id}-daemon`, name: `${app.name} in the background`, comment: `Sync and backups for ${app.name}`, icon: icon_path, command: own_command(["--daemon"]) }, settings.start_at_login && settings.keep_running_in_background);
|
|
166
198
|
}
|
|
@@ -171,6 +203,8 @@ export function run_desktop(app, electron, options) {
|
|
|
171
203
|
};
|
|
172
204
|
// The daemon's services keep quiet, such as about notifications, while a window is in front.
|
|
173
205
|
function tell_in_front() {
|
|
206
|
+
if (portal)
|
|
207
|
+
return;
|
|
174
208
|
const in_front = windows().some((each) => each.isFocused() && each.isVisible());
|
|
175
209
|
void call("daemon/set_window_in_front", [in_front]).catch(() => undefined);
|
|
176
210
|
}
|
|
@@ -213,7 +247,8 @@ export function run_desktop(app, electron, options) {
|
|
|
213
247
|
// Stops the daemon as well, so nothing runs until the app is opened again.
|
|
214
248
|
async function quit_completely() {
|
|
215
249
|
router.quitting();
|
|
216
|
-
|
|
250
|
+
if (!portal)
|
|
251
|
+
await call("daemon/stop_daemon").catch(() => undefined);
|
|
217
252
|
electron.app.quit();
|
|
218
253
|
}
|
|
219
254
|
// ── Tray ───────────────────────────────────────────────────────────
|
|
@@ -274,12 +309,13 @@ export function run_desktop(app, electron, options) {
|
|
|
274
309
|
if (app.remote_access)
|
|
275
310
|
bridges.remote_access.handle(ipcMain, forwarded(remote_access_method_names, "remote"), handle_options);
|
|
276
311
|
bridges.host.handle(ipcMain, forwarded(host_ability_method_names, "host"), handle_options);
|
|
312
|
+
bridges.fetch.handle(ipcMain, { http_fetch }, handle_options);
|
|
277
313
|
for (const [name, bridge] of Object.entries(bridges.host_tables))
|
|
278
314
|
bridge.handle(ipcMain, forwarded(Object.keys(app.host_tables[name].api), name), handle_options);
|
|
279
315
|
const desktop = {
|
|
280
316
|
get_desktop_settings,
|
|
281
317
|
set_desktop_setting: async (key, value) => {
|
|
282
|
-
await
|
|
318
|
+
await set_desktop_setting(String(key), value);
|
|
283
319
|
await apply_window_settings();
|
|
284
320
|
},
|
|
285
321
|
quit_completely,
|
package/dist/app/preload.js
CHANGED
|
@@ -11,6 +11,7 @@ export function run_preload(app, { contextBridge, ipcRenderer }) {
|
|
|
11
11
|
if (app.remote_access)
|
|
12
12
|
bridges.remote_access.expose(contextBridge, ipcRenderer, { envelopes: true });
|
|
13
13
|
bridges.host.expose(contextBridge, ipcRenderer, { envelopes: true });
|
|
14
|
+
bridges.fetch.expose(contextBridge, ipcRenderer, { envelopes: true });
|
|
14
15
|
for (const bridge of Object.values(bridges.host_tables))
|
|
15
16
|
bridge.expose(contextBridge, ipcRenderer, { envelopes: true });
|
|
16
17
|
bridges.events.expose(contextBridge, ipcRenderer);
|
package/dist/app/protocol.d.ts
CHANGED
|
@@ -91,6 +91,7 @@ export interface PageApi<Methods extends Record<keyof Methods, (...args: never[]
|
|
|
91
91
|
};
|
|
92
92
|
reread_abilities?: () => Promise<Pick<PageApi, "admin" | "tables">>;
|
|
93
93
|
server_theme?: ServerTheme;
|
|
94
|
+
fetch?: typeof fetch;
|
|
94
95
|
file_src?: (name: string) => Promise<string>;
|
|
95
96
|
plugin_base?: string;
|
|
96
97
|
read_plugin_file?: (path: string) => Promise<string>;
|
package/dist/app/server.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { DatabaseSync } from "node:sqlite";
|
|
|
2
2
|
import { type FastifyInstance } from "fastify";
|
|
3
3
|
import { type MethodSpec } from "../api_spec.ts";
|
|
4
4
|
import { type BackupMethods } from "../backups.ts";
|
|
5
|
+
import { type ProxySignIn } from "../server/account_sign_in.ts";
|
|
5
6
|
import { OidcClient } from "../server/oidc.ts";
|
|
6
7
|
import { AccountStore } from "../server/account_store.ts";
|
|
7
8
|
import { type Channel } from "../server/notify/channels.ts";
|
|
@@ -51,6 +52,7 @@ export interface ServerOptions {
|
|
|
51
52
|
plugins_folder?: string | null;
|
|
52
53
|
files?: SyncFileStore | null;
|
|
53
54
|
second_factor_from?: string | null;
|
|
55
|
+
proxy?: ProxySignIn | null;
|
|
54
56
|
}
|
|
55
57
|
export declare const backup_tools: Record<"list_backups" | "create_backup" | "get_backup_schedule", MethodSpec>;
|
|
56
58
|
export declare const user_only_backup_calls: ReadonlySet<string>;
|
package/dist/app/server.js
CHANGED
|
@@ -225,7 +225,8 @@ export async function build_server(app, options) {
|
|
|
225
225
|
// A phone that keeps no data calls from a page of its own with a token, the sign-in included; the event stream
|
|
226
226
|
// writes its own head, so it carries the open origin itself.
|
|
227
227
|
if (app.phone === "portal") {
|
|
228
|
-
|
|
228
|
+
// An app built on its REST routes takes PUT and DELETE from its portals too.
|
|
229
|
+
await register_open_cors(server, { allowed_headers: sync_peer_headers, methods: ["GET", "POST", "PUT", "DELETE"] });
|
|
229
230
|
server.addHook("onRequest", async (request, reply) => {
|
|
230
231
|
if (request.url.split("?")[0] === "/api/events")
|
|
231
232
|
reply.raw.setHeader("access-control-allow-origin", "*");
|
|
@@ -245,6 +246,7 @@ export async function build_server(app, options) {
|
|
|
245
246
|
open_prefixes: options.routes?.open_prefixes ?? [],
|
|
246
247
|
guarded_paths: options.routes?.guarded_paths ?? [],
|
|
247
248
|
oidc: options.oidc ?? null,
|
|
249
|
+
proxy: options.proxy ?? null,
|
|
248
250
|
...(options.session_seconds !== undefined ? { session_seconds: options.session_seconds } : {}),
|
|
249
251
|
...(options.remembered_seconds !== undefined ? { remembered_seconds: options.remembered_seconds } : {}),
|
|
250
252
|
...(options.second_factor_from ? { second_factor_from: options.second_factor_from } : {}),
|
|
@@ -371,8 +373,9 @@ export async function run_server(app, options) {
|
|
|
371
373
|
app_name: app.name,
|
|
372
374
|
...(app.roles ? { roles: app.roles } : {}),
|
|
373
375
|
...(app.token_prefix ? { token_prefix: app.token_prefix } : {}),
|
|
376
|
+
...(app.check_access ? { check_access: app.check_access } : {}),
|
|
374
377
|
...(config.token_days !== undefined ? { longest_token_days: config.token_days } : {}),
|
|
375
|
-
declared: load_accounts(`${prefix}_PASSWORD_HASH_FILE`, server_config.values.password_hash_file),
|
|
378
|
+
declared: load_accounts(`${prefix}_PASSWORD_HASH_FILE`, server_config.values.password_hash_file, config.admin_name?.toLowerCase()),
|
|
376
379
|
declared_tokens: declared_tokens.filter((token) => token.role !== null),
|
|
377
380
|
});
|
|
378
381
|
const oidc_settings = oidc_from_environment(prefix, options.env ?? process.env);
|
|
@@ -407,6 +410,7 @@ export async function run_server(app, options) {
|
|
|
407
410
|
...(config.session_seconds !== undefined ? { session_seconds: config.session_seconds } : {}),
|
|
408
411
|
...(config.remembered_seconds !== undefined ? { remembered_seconds: config.remembered_seconds } : {}),
|
|
409
412
|
...(config.second_factor_from ? { second_factor_from: config.second_factor_from } : {}),
|
|
413
|
+
...(config.proxy_user_header ? { proxy: { header: config.proxy_user_header.toLowerCase(), trusted: new Set(config.trusted_proxies ?? ["127.0.0.1", "::1"]) } } : {}),
|
|
410
414
|
theme: read_server_theme(config.theme),
|
|
411
415
|
plugins_folder: config.plugins_folder ? resolve(config.plugins_folder) : null,
|
|
412
416
|
files,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const no_server_text = "No server is chosen yet. Choose one under Settings \u2192 Connections.";
|
|
2
|
+
export declare function server_url(address: string, requested: string): string;
|
|
3
|
+
export declare function server_headers(headers: Iterable<[string, string]>, token: string): [string, string][];
|
|
4
|
+
export declare function server_fetch(server: () => {
|
|
5
|
+
address: string;
|
|
6
|
+
token: string;
|
|
7
|
+
} | null, send?: typeof fetch): typeof fetch;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// A page's request to its own server's routes, such as "/api/runs?limit=5", sent from a portal on another device: the
|
|
2
|
+
// path under the server's address chosen under Connections, with this device's token in place of the page's own
|
|
3
|
+
// credentials. The desktop's main process and the phone's portal both send it so.
|
|
4
|
+
// ── Requests ─────────────────────────────────────────────────────────────────
|
|
5
|
+
export const no_server_text = "No server is chosen yet. Choose one under Settings → Connections.";
|
|
6
|
+
// The server's URL for a request the page made to its own origin; only the API is reached.
|
|
7
|
+
export function server_url(address, requested) {
|
|
8
|
+
const url = new URL(requested, "http://page.invalid");
|
|
9
|
+
// A relative path resolves against the page's own address, such as a desktop page's file:// one, so the route is
|
|
10
|
+
// read from its /api/ on.
|
|
11
|
+
const at = url.pathname.indexOf("/api/");
|
|
12
|
+
if (at === -1)
|
|
13
|
+
throw new Error(`${url.pathname} is not one of the server's routes.`);
|
|
14
|
+
return `${address.replace(/\/+$/, "")}${url.pathname.slice(at)}${url.search}`;
|
|
15
|
+
}
|
|
16
|
+
// The page's headers with this device's token in place of any credentials or origin the page's own request carried.
|
|
17
|
+
export function server_headers(headers, token) {
|
|
18
|
+
const dropped = new Set(["authorization", "cookie", "origin", "referer", "host"]);
|
|
19
|
+
const kept = [...headers].filter(([name]) => !dropped.has(name.toLowerCase()));
|
|
20
|
+
return [...kept, ["authorization", `Bearer ${token}`]];
|
|
21
|
+
}
|
|
22
|
+
// A fetch of the page's that goes to the server the connection names, or refuses while none is chosen.
|
|
23
|
+
export function server_fetch(server, send = fetch) {
|
|
24
|
+
return async (input, init) => {
|
|
25
|
+
const chosen = server();
|
|
26
|
+
if (!chosen)
|
|
27
|
+
throw new Error(no_server_text);
|
|
28
|
+
const request = new Request(new URL(String(input instanceof Request ? input.url : input), "http://page.invalid"), init);
|
|
29
|
+
const path = new URL(request.url);
|
|
30
|
+
return send(server_url(chosen.address, `${path.pathname}${path.search}`), {
|
|
31
|
+
method: request.method,
|
|
32
|
+
headers: server_headers(request.headers.entries(), chosen.token),
|
|
33
|
+
body: request.method === "GET" || request.method === "HEAD" ? null : await request.arrayBuffer(),
|
|
34
|
+
...(init?.signal ? { signal: init.signal } : {}),
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
}
|
package/dist/app/web/hosts.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Where the page runs and how it reaches the data there: a browser posts every call to the server it was loaded from
|
|
2
2
|
// and follows its event stream; the desktop app's preload has put the main process's calls on the window, which go to
|
|
3
3
|
// this computer's daemon or to the server Settings → Connections chose.
|
|
4
|
+
import { create_page_fetch } from "../../electron/fetch.js";
|
|
4
5
|
import { LOCAL_CONNECTION_ID } from "../../connection_protocol.js";
|
|
5
6
|
import { LoginNeeded } from "../../web/states.js";
|
|
6
7
|
import { data_change_of, desktop_plugin_base, file_scheme_of } from "../protocol.js";
|
|
@@ -107,6 +108,8 @@ export function web_api(app) {
|
|
|
107
108
|
backups: bridges.backups.web_rpc((method, args) => post_call(backups_path, method, args)),
|
|
108
109
|
...(app.synced_files ? { file_src: async (name) => `api/files/${encodeURIComponent(name)}` } : {}),
|
|
109
110
|
plugin_base: "api/plugins/",
|
|
111
|
+
// The server's own routes, with the browser's sign-in.
|
|
112
|
+
fetch: (input, init) => fetch(input, init),
|
|
110
113
|
on_change: (listener) => {
|
|
111
114
|
listeners.add(listener);
|
|
112
115
|
return () => listeners.delete(listener);
|
|
@@ -154,6 +157,8 @@ export async function desktop_api(app) {
|
|
|
154
157
|
...(app.synced_files && app.id ? { file_src: async (name) => `${file_scheme_of(app.id)}://${name}` } : {}),
|
|
155
158
|
plugin_base: desktop_plugin_base,
|
|
156
159
|
read_plugin_file: (path) => desktop.read_plugin_file(path),
|
|
160
|
+
// The routes of the server the window works on, sent on by the main process with this device's token.
|
|
161
|
+
...(exposed[bridges.fetch.api_name] ? { fetch: create_page_fetch((request) => exposed[bridges.fetch.api_name].http_fetch(request)) } : {}),
|
|
157
162
|
on_change: (listener) => events.on("changed", listener),
|
|
158
163
|
on_event: (event, listener) => events.on("app_event", (sent) => sent.event === event && listener(sent.data)),
|
|
159
164
|
};
|
package/dist/app/web/portal.js
CHANGED
|
@@ -11,6 +11,7 @@ import { call_remote, follow_remote } from "../../web/remote_daemon.js";
|
|
|
11
11
|
import { route_of_link } from "../links.js";
|
|
12
12
|
import { data_change_of, notice_event, notice_is_on, notice_withdrawn_event } from "../protocol.js";
|
|
13
13
|
import { host_tables_of, read_host_abilities } from "./hosts.js";
|
|
14
|
+
import { server_fetch } from "../server_fetch.js";
|
|
14
15
|
// ── Switches ─────────────────────────────────────────────────────────────────
|
|
15
16
|
// The notice switches this phone keeps, each of the app's `notify_` desktop switches at its default where unset.
|
|
16
17
|
export function read_notice_switches(app, saved) {
|
|
@@ -100,6 +101,8 @@ export async function portal_api(app, options) {
|
|
|
100
101
|
return {
|
|
101
102
|
host: "android",
|
|
102
103
|
calls: bridges.calls.web_rpc(rpc("call")),
|
|
104
|
+
// The routes of the host this phone works on, with its token.
|
|
105
|
+
fetch: server_fetch(link.daemon, options.fetch ?? ((input, init) => fetch(input, init))),
|
|
103
106
|
device: null,
|
|
104
107
|
// The host's backups, which only an admin there reaches.
|
|
105
108
|
backups: abilities.admin ? bridges.backups.web_rpc(rpc("backups")) : null,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { IconGlyph } from "../../web/react/icons.tsx";
|
|
2
2
|
import type { AccountsClient, Me } from "../../web/accounts.ts";
|
|
3
|
+
import { type AccessEditor } from "../../web/react/accounts.tsx";
|
|
3
4
|
import { type SettingsCategory, type SettingsSection } from "../../web/react/layout.tsx";
|
|
4
5
|
import type { ShellSidePane, SidebarWidth } from "../../web/react/shell.tsx";
|
|
5
6
|
import type { LocalNotificationsLike } from "../../phone_notices.ts";
|
|
@@ -50,6 +51,8 @@ export interface AppPages {
|
|
|
50
51
|
remote_access_reach?: string;
|
|
51
52
|
exports_secrets?: boolean;
|
|
52
53
|
role_text?: Readonly<Record<string, string>>;
|
|
54
|
+
access_text?: (access: unknown) => string;
|
|
55
|
+
access_editor?: AccessEditor;
|
|
53
56
|
}
|
|
54
57
|
export interface SettingsViewer {
|
|
55
58
|
client: AccountsClient;
|
|
@@ -116,7 +116,12 @@ function storage_category(pages, is_admin) {
|
|
|
116
116
|
function users_category(pages, viewer) {
|
|
117
117
|
if (viewer === null)
|
|
118
118
|
return [];
|
|
119
|
-
const sections = users_settings({
|
|
119
|
+
const sections = users_settings({
|
|
120
|
+
client: viewer.client, me: viewer.me,
|
|
121
|
+
...(pages.role_text ? { role_text: pages.role_text } : {}),
|
|
122
|
+
...(pages.access_text ? { access_text: pages.access_text } : {}),
|
|
123
|
+
...(pages.access_editor ? { access_editor: pages.access_editor } : {}),
|
|
124
|
+
});
|
|
120
125
|
return sections.length > 0 ? [{ heading: "Users", sections }] : [];
|
|
121
126
|
}
|
|
122
127
|
export function SettingsView({ pages, is_admin = true, viewer = null }) {
|
|
@@ -147,7 +147,16 @@ export class AccountStore {
|
|
|
147
147
|
this.app_name = options.app_name;
|
|
148
148
|
this.roles = options.roles ?? default_roles;
|
|
149
149
|
this.declared = options.declared ?? new Map();
|
|
150
|
-
|
|
150
|
+
// An app's own check may throw a plain Error, such as one defined beside its page; it is refused as an account error.
|
|
151
|
+
const check = options.check_access ?? only_everything;
|
|
152
|
+
this.check_access = (value) => {
|
|
153
|
+
try {
|
|
154
|
+
return check(value);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
throw error instanceof AccountError ? error : new AccountError(error instanceof Error ? error.message : String(error));
|
|
158
|
+
}
|
|
159
|
+
};
|
|
151
160
|
this.token_prefix = options.token_prefix ?? "xt";
|
|
152
161
|
this.shortest_password = options.shortest_password ?? 12;
|
|
153
162
|
this.declared_token_list = options.declared_tokens ?? [];
|
|
@@ -33,6 +33,6 @@ export declare class PasswordHashSlots {
|
|
|
33
33
|
private in_slot;
|
|
34
34
|
}
|
|
35
35
|
export declare const shared_password_hashes: PasswordHashSlots;
|
|
36
|
-
export declare function parse_accounts(text: string, source?: string): Accounts;
|
|
37
|
-
export declare function load_accounts(variable: string, file_path?: string | undefined): Accounts;
|
|
36
|
+
export declare function parse_accounts(text: string, source?: string, bare_name?: string): Accounts;
|
|
37
|
+
export declare function load_accounts(variable: string, file_path?: string | undefined, bare_name?: string): Accounts;
|
|
38
38
|
export declare function set_account(text: string, name: string, stored: string): string;
|
package/dist/server/passwords.js
CHANGED
|
@@ -95,14 +95,14 @@ export class PasswordHashSlots {
|
|
|
95
95
|
export const shared_password_hashes = new PasswordHashSlots();
|
|
96
96
|
// ── Accounts file ────────────────────────────────────────────────────────────
|
|
97
97
|
// Reads `name:hash` lines, skipping blank lines and `#` comments.
|
|
98
|
-
export function parse_accounts(text, source = "the accounts file") {
|
|
98
|
+
export function parse_accounts(text, source = "the accounts file", bare_name = default_account_name) {
|
|
99
99
|
const accounts = new Map();
|
|
100
100
|
for (const [index, raw_line] of text.split("\n").entries()) {
|
|
101
101
|
const line = raw_line.trim();
|
|
102
102
|
if (line === "" || line.startsWith("#"))
|
|
103
103
|
continue;
|
|
104
104
|
const separator = line.indexOf(":");
|
|
105
|
-
const name = separator === -1 ?
|
|
105
|
+
const name = separator === -1 ? bare_name : line.slice(0, separator);
|
|
106
106
|
const stored = separator === -1 ? line : line.slice(separator + 1);
|
|
107
107
|
const where = `${source} line ${index + 1}`;
|
|
108
108
|
if (!account_name_pattern.test(name))
|
|
@@ -116,10 +116,11 @@ export function parse_accounts(text, source = "the accounts file") {
|
|
|
116
116
|
return accounts;
|
|
117
117
|
}
|
|
118
118
|
// The accounts in the file the environment variable names, such as X_NIX_PASSWORD_HASH_FILE.
|
|
119
|
-
|
|
119
|
+
// A line holding a hash alone is the admin named bare_name, "admin" unless set.
|
|
120
|
+
export function load_accounts(variable, file_path = process.env[variable], bare_name = default_account_name) {
|
|
120
121
|
if (!file_path)
|
|
121
122
|
throw new Error(`${variable} is not set; create one with \`npm run hash-password -- <name> <file>\`.`);
|
|
122
|
-
const accounts = parse_accounts(readFileSync(file_path, "utf8"), file_path);
|
|
123
|
+
const accounts = parse_accounts(readFileSync(file_path, "utf8"), file_path, bare_name);
|
|
123
124
|
if (accounts.size === 0)
|
|
124
125
|
throw new Error(`${file_path} holds no users; add one with \`npm run hash-password -- <name> ${file_path}\`.`);
|
|
125
126
|
return accounts;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.127.0",
|
|
4
4
|
"description": "Code the X-apps share: agents and their long runs, passwords and sign-in, the Fastify server pieces, durations, and the web page's helpers and widgets",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": {
|