sideora 0.0.1-alpha.0 → 0.1.0-alpha.2
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/LICENSE.md +9 -0
- package/README.md +57 -6
- package/assets/sounds/freesound_community-mail-40275.mp3 +0 -0
- package/assets/sounds/mixkit-clear-announce-tones-2861.wav +0 -0
- package/assets/sounds/mixkit-happy-bells-notification-937.wav +0 -0
- package/assets/sounds/mixkit-musical-alert-notification-2309.wav +0 -0
- package/assets/sounds/nice-chime.mp3 +0 -0
- package/bin/sideora.js +2430 -12
- package/extension/dist/assets/background.ts-NW8YqAlO.js +1 -0
- package/extension/dist/assets/fillCode-Ci8IkBvY.js +1 -0
- package/extension/dist/assets/index.html-DrrPha1M.js +2 -0
- package/extension/dist/assets/modulepreload-polyfill-DcjICKwh.js +1 -0
- package/extension/dist/assets/shell-82MNMljp.css +1 -0
- package/extension/dist/assets/shell-QYBN1bx1.js +1 -0
- package/extension/dist/icons/icon-128.png +0 -0
- package/extension/dist/icons/icon-16.png +0 -0
- package/extension/dist/icons/icon-32.png +0 -0
- package/extension/dist/icons/icon-48.png +0 -0
- package/extension/dist/manifest.json +40 -0
- package/extension/dist/service-worker-loader.js +1 -0
- package/extension/dist/src/newtab/index.html +14 -0
- package/extension/dist/src/panel/index.html +15 -0
- package/extension/dist-web/assets/abnfDiagram-WEYOIA3C-CoXOc872.js +1 -0
- package/extension/dist-web/assets/apple-touch-icon-DSgRQASq.png +0 -0
- package/extension/dist-web/assets/architecture-WOLXFQ4H-BkJ4AerO.js +1 -0
- package/extension/dist-web/assets/architectureDiagram-ZHVYKT53-aPkSDlxA.js +36 -0
- package/extension/dist-web/assets/blockDiagram-RJAE7QA3-CH1G4NBZ.js +140 -0
- package/extension/dist-web/assets/c4Diagram-XDPPPE54-Baum1zDu.js +38 -0
- package/extension/dist-web/assets/channel-PsQg-mnK.js +1 -0
- package/extension/dist-web/assets/chunk-2E7WHVMD-CKvLrWRa.js +88 -0
- package/extension/dist-web/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/extension/dist-web/assets/chunk-4EMNKR2T-6fjnroCZ.js +1 -0
- package/extension/dist-web/assets/chunk-55XCU76K-Ygs3sNpi.js +1 -0
- package/extension/dist-web/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- package/extension/dist-web/assets/chunk-7S6XSTFO-iX5xYzfP.js +2 -0
- package/extension/dist-web/assets/chunk-AQDTSKZB-D9zwhFI8.js +72 -0
- package/extension/dist-web/assets/chunk-DSNLSHJZ-siGS-4qu.js +129 -0
- package/extension/dist-web/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
- package/extension/dist-web/assets/chunk-HAGFOTED-_3dtnWXn.js +62 -0
- package/extension/dist-web/assets/chunk-HNE4Q36P-BPzRHi72.js +1 -0
- package/extension/dist-web/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
- package/extension/dist-web/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/extension/dist-web/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
- package/extension/dist-web/assets/chunk-NQOKRVIF-AXbmOBjs.js +213 -0
- package/extension/dist-web/assets/chunk-UHW7PFBP-B3xrZ8sD.js +1 -0
- package/extension/dist-web/assets/chunk-VQ5W62QI-D_k7Pa6b.js +1 -0
- package/extension/dist-web/assets/chunk-WQQL4CDK-BBIrWwoZ.js +10 -0
- package/extension/dist-web/assets/chunk-XXDRQBXY-i7MKP1NY.js +1 -0
- package/extension/dist-web/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/extension/dist-web/assets/chunk-YZO67HFG-Z7qp5QAI.js +1 -0
- package/extension/dist-web/assets/chunk-ZAUX2LTX-CSt3c0Rb.js +1 -0
- package/extension/dist-web/assets/chunk-ZCHMQSOR-g8XRABGK.js +2 -0
- package/extension/dist-web/assets/classDiagram-v2-4MYWR3UA-BL706wzc.js +217 -0
- package/extension/dist-web/assets/cose-bilkent-JH36ORCC-C6J2IoHg.js +1 -0
- package/extension/dist-web/assets/cynefin-EF2NZ3EQ-DLhQOiAy.js +1 -0
- package/extension/dist-web/assets/cynefinDiagram-WF24UDRP-Cv9Lx6_l.js +62 -0
- package/extension/dist-web/assets/cytoscape.esm-nTrd5EVq.js +321 -0
- package/extension/dist-web/assets/d3-scale-CElPUUud.js +1 -0
- package/extension/dist-web/assets/d3-selection-COWvaQNI.js +1 -0
- package/extension/dist-web/assets/dagre-NK4GIZVX-ngM-w2EC.js +4 -0
- package/extension/dist-web/assets/diagram-CBK3MWTC-kp8sayNG.js +24 -0
- package/extension/dist-web/assets/diagram-I5XIIV2D-BZ_Dgs6K.js +24 -0
- package/extension/dist-web/assets/diagram-ODIL2UCW-DWhUyArG.js +30 -0
- package/extension/dist-web/assets/diagram-PFBEJUXE-CBAdEK7l.js +3 -0
- package/extension/dist-web/assets/diagram-RJA3I4VV-kOdIr_3K.js +41 -0
- package/extension/dist-web/assets/diagram-TYUNFSNR-BrWGa0x5.js +200 -0
- package/extension/dist-web/assets/dist-DdXDFcej.js +1 -0
- package/extension/dist-web/assets/docx-preview-D1zbEiLy.js +20 -0
- package/extension/dist-web/assets/earth-Ce3xs8Cb.webp +0 -0
- package/extension/dist-web/assets/ebnfDiagram-V4P3TTPT-0r3weWmj.js +1 -0
- package/extension/dist-web/assets/elk-QONTBCTQ-D3oElRZ3.js +27 -0
- package/extension/dist-web/assets/erDiagram-KYQIGQAA-B9oU7_j_.js +99 -0
- package/extension/dist-web/assets/eventmodeling-K75KTNOO-CMKgc1nP.js +1 -0
- package/extension/dist-web/assets/favicon-D_X6tUVf.svg +5 -0
- package/extension/dist-web/assets/flowDiagram-IUGLLWBU-yRwks0Mw.js +1 -0
- package/extension/dist-web/assets/ganttDiagram-R7GPNTJY-cJqD1qwQ.js +292 -0
- package/extension/dist-web/assets/gitGraph-VSP46ZUC-CuBQqkbr.js +1 -0
- package/extension/dist-web/assets/gitGraphDiagram-PDYL7XHY-D7bHnQXC.js +106 -0
- package/extension/dist-web/assets/graphlib-DS17s2tU.js +1 -0
- package/extension/dist-web/assets/home-CV7-8Trs.js +1 -0
- package/extension/dist-web/assets/index-CQvw7Wy_.js +7 -0
- package/extension/dist-web/assets/index-CVKnZ21i.css +1 -0
- package/extension/dist-web/assets/info-OHQRW6UA-B8kyzV4O.js +1 -0
- package/extension/dist-web/assets/infoDiagram-MVDRKAX5-CEm6lezX.js +2 -0
- package/extension/dist-web/assets/ishikawaDiagram-HPEHV2GE-Ce3uv2wr.js +70 -0
- package/extension/dist-web/assets/journeyDiagram-BOSHW7O7-QgdahHCP.js +139 -0
- package/extension/dist-web/assets/jszip.min-3XXygzvy.js +2 -0
- package/extension/dist-web/assets/kanban-definition-FNSA4HC5-DPuTtWgR.js +89 -0
- package/extension/dist-web/assets/katex-MEuSR36T.js +257 -0
- package/extension/dist-web/assets/mermaid-parser.core-h7U7YGn_.js +7 -0
- package/extension/dist-web/assets/mermaid.core-DkGhophj.js +44 -0
- package/extension/dist-web/assets/mindmap-definition-6OY57UG3-D7VQxbda.js +98 -0
- package/extension/dist-web/assets/mount-CBzzf6KQ.js +195 -0
- package/extension/dist-web/assets/mount-DtvYnQXt.css +1 -0
- package/extension/dist-web/assets/officeparser.browser.slim-kEjhTrQ1.js +1989 -0
- package/extension/dist-web/assets/packet-JDAUHWVQ-oafHgw6X.js +1 -0
- package/extension/dist-web/assets/pegDiagram-ECIUJNBX-C3zo1naj.js +1 -0
- package/extension/dist-web/assets/peoria-B5TBlKDG.jpg +0 -0
- package/extension/dist-web/assets/peoria-thumb-CN8upAdZ.jpg +0 -0
- package/extension/dist-web/assets/pie-XZMESJXO-DXzeBQqF.js +1 -0
- package/extension/dist-web/assets/pieDiagram-GNMPY74T-S5NzPthl.js +39 -0
- package/extension/dist-web/assets/quadrantDiagram-D57K4MC3-C9glHQmk.js +7 -0
- package/extension/dist-web/assets/radar-ABXABTNO-CWTNqtbu.js +1 -0
- package/extension/dist-web/assets/railroad-I3PHUGI6-BTpD5_Bu.js +1 -0
- package/extension/dist-web/assets/railroad-abnf-LNEFI6M7-bWyxoXQG.js +1 -0
- package/extension/dist-web/assets/railroad-ebnf-DWYD2UWJ-ri5733DQ.js +1 -0
- package/extension/dist-web/assets/railroad-peg-JR7BVNR7-CmGfTubP.js +1 -0
- package/extension/dist-web/assets/railroadDiagram-Y72NRBL7-DTfe6Dh1.js +1 -0
- package/extension/dist-web/assets/requirementDiagram-EQYSW5RA-DQDzcS3L.js +84 -0
- package/extension/dist-web/assets/rolldown-runtime-COiEfhsA.js +1 -0
- package/extension/dist-web/assets/rough.esm-Dy-Kn_BL.js +1 -0
- package/extension/dist-web/assets/sankeyDiagram-SY24JHO5-DyMKWxdB.js +40 -0
- package/extension/dist-web/assets/sequenceDiagram-BLEHNHQU-dIkB1mCn.js +169 -0
- package/extension/dist-web/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
- package/extension/dist-web/assets/stateDiagram-v2-A6IESKWZ-Bj_vxEVF.js +291 -0
- package/extension/dist-web/assets/swimlanes-B3ENRKFQ-BSkWEwHR.js +1 -0
- package/extension/dist-web/assets/swimlanesDiagram-AYQRV4AK-BTzSDlSM.js +8 -0
- package/extension/dist-web/assets/timeline-definition-JXYKGLGB-TwfWS0pw.js +120 -0
- package/extension/dist-web/assets/treeView-D4JQ5SDB-BzA_8YYb.js +1 -0
- package/extension/dist-web/assets/treemap-SAJKECNS-BI9d6--2.js +1 -0
- package/extension/dist-web/assets/usecaseDiagram-WPOLBNNX-BOVbAUPk.js +328 -0
- package/extension/dist-web/assets/vennDiagram-Q3Y3IDCW-X2kYAWaj.js +34 -0
- package/extension/dist-web/assets/wardley-7MLQ67FV-CItxqJ8G.js +1 -0
- package/extension/dist-web/assets/wardleyDiagram-IVAIDKWA-BnUXvKD_.js +78 -0
- package/extension/dist-web/assets/xychartDiagram-4URNHDU4-ysvBXvyE.js +7 -0
- package/extension/dist-web/icon-192.png +0 -0
- package/extension/dist-web/icon-512.png +0 -0
- package/extension/dist-web/index.html +25 -0
- package/extension/dist-web/manifest.webmanifest +14 -0
- package/package.json +64 -12
- package/plugin/.claude-plugin/icon.png +0 -0
- package/plugin/.claude-plugin/plugin.json +6 -0
- package/plugin/.mcp.json +8 -0
- package/plugin/README.md +72 -0
- package/plugin/mcp-launcher.mjs +48 -0
- package/src/account-probe.js +45 -0
- package/src/accounts.js +687 -0
- package/src/ai.js +269 -0
- package/src/assistant-runs.js +32 -0
- package/src/attached-mail.js +76 -0
- package/src/auth-gate.js +70 -0
- package/src/backup-cli.js +158 -0
- package/src/backup.js +329 -0
- package/src/bimi.js +233 -0
- package/src/bridge.js +101 -0
- package/src/calendar-alerts.js +56 -0
- package/src/chats.js +27 -0
- package/src/compose-editor.js +58 -0
- package/src/config.js +420 -0
- package/src/connections.d.ts +24 -0
- package/src/connections.js +37 -0
- package/src/core/agent.js +92 -0
- package/src/core/assistant.d.ts +13 -0
- package/src/core/assistant.js +790 -0
- package/src/core/chats.js +323 -0
- package/src/core/markdown.d.ts +8 -0
- package/src/core/markdown.js +239 -0
- package/src/core/notes.js +445 -0
- package/src/core/otp.js +152 -0
- package/src/core/rules.js +196 -0
- package/src/core/runs.d.ts +9 -0
- package/src/core/runs.js +211 -0
- package/src/core/thread.js +526 -0
- package/src/core/write.js +247 -0
- package/src/core-db.js +131 -0
- package/src/core.js +174 -0
- package/src/credentials.js +320 -0
- package/src/daemon.js +564 -0
- package/src/events.js +213 -0
- package/src/extension-id.d.ts +2 -0
- package/src/extension-id.js +5 -0
- package/src/file-lock.js +27 -0
- package/src/files-index.js +154 -0
- package/src/google-contacts.js +315 -0
- package/src/google-signin.js +306 -0
- package/src/host/assistant-html.js +44 -0
- package/src/host/chats-files.js +137 -0
- package/src/host/file-prep.js +122 -0
- package/src/host/notes-files.js +158 -0
- package/src/host/office-text.js +224 -0
- package/src/host/office-worker.js +8 -0
- package/src/host/rules-file.js +20 -0
- package/src/host/secrets-file.js +91 -0
- package/src/host/systemd.js +90 -0
- package/src/hub.js +245 -0
- package/src/identity.js +316 -0
- package/src/labels.js +5 -0
- package/src/mail-port.d.ts +79 -0
- package/src/mail-port.js +148 -0
- package/src/mcp-server.js +747 -0
- package/src/microsoft-app.js +20 -0
- package/src/microsoft-signin.js +194 -0
- package/src/migrate.js +327 -0
- package/src/notes-html.js +450 -0
- package/src/notes.js +19 -0
- package/src/notify.js +105 -0
- package/src/onepassword.js +223 -0
- package/src/op-registry.d.ts +14 -0
- package/src/op-registry.js +150 -0
- package/src/ops.js +1251 -0
- package/src/otp.js +6 -0
- package/src/panel-ops.js +273 -0
- package/src/pictures.js +735 -0
- package/src/postinstall.js +25 -0
- package/src/relay-passkey.js +122 -0
- package/src/relay-request.js +155 -0
- package/src/relay-seal.js +56 -0
- package/src/relay-settings.js +139 -0
- package/src/relay.js +876 -0
- package/src/render.js +82 -0
- package/src/rules-store.js +8 -0
- package/src/safe-fetch.js +215 -0
- package/src/sender-auth.js +270 -0
- package/src/site-match.js +77 -0
- package/src/tui-agenda.js +266 -0
- package/src/tui.js +670 -0
- package/src/vmc.js +376 -0
- package/src/wake.js +96 -0
- package/src/web-access.js +185 -0
- package/src/web-server.js +241 -0
- package/src/web-service.js +152 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// After `npm install` / an update: when an install from before the rename is on this computer (~/.mailwatcher, the
|
|
2
|
+
// com.mailwatcher.* LaunchAgents or mailwatcher-* systemd units, which run a bin/mailwatcher.js an update takes
|
|
3
|
+
// away), says to run `sideora migrate`. Looks only; changes nothing; never fails the install.
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const home = os.homedir();
|
|
10
|
+
const units = path.join(process.env.XDG_CONFIG_HOME || path.join(home, '.config'), 'systemd', 'user');
|
|
11
|
+
const there = [
|
|
12
|
+
path.join(home, '.mailwatcher'),
|
|
13
|
+
path.join(home, 'Library/LaunchAgents/com.mailwatcher.daemon.plist'),
|
|
14
|
+
path.join(home, 'Library/LaunchAgents/com.mailwatcher.web.plist'),
|
|
15
|
+
path.join(units, 'mailwatcher-daemon.service'),
|
|
16
|
+
path.join(units, 'mailwatcher-web.service')
|
|
17
|
+
].some((p) => { try { fs.lstatSync(p); return true; } catch { return false; } });
|
|
18
|
+
if (there) {
|
|
19
|
+
const say = '\nSideora: an install from before the rename (MailWatcher) is on this computer, and its services may run a program this update took away. Run `sideora migrate` to move it over.\n\n';
|
|
20
|
+
process.stderr.write(say);
|
|
21
|
+
// (npm keeps an install script's words to itself: the terminal is told too, when there is one)
|
|
22
|
+
if (!process.stderr.isTTY) { try { fs.writeFileSync('/dev/tty', say); } catch { /* no terminal */ } }
|
|
23
|
+
}
|
|
24
|
+
} catch { /* never in the way of the install */ }
|
|
25
|
+
process.exit(0);
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// A paired device's passkey (WebAuthn), as this Mac checks it: src/relay.js keeps one per device, and asks for it
|
|
2
|
+
// to open the app after it was left unused and before what reaches other people or changes this Mac.
|
|
3
|
+
//
|
|
4
|
+
// The passkey is made by the device's own browser, for the relay's host (the page's address: the browser signs
|
|
5
|
+
// for no other). Its public half comes here sealed, as everything a paired device says does, and is kept with
|
|
6
|
+
// that device. What is checked afterwards is the standard's: that the signature is this key's, over this Mac's
|
|
7
|
+
// own challenge (made here, good once, for a few minutes), for this host and from this origin, with the person
|
|
8
|
+
// verified by the device (Face ID, Touch ID, a screen lock: the "user verified" flag), not merely present.
|
|
9
|
+
//
|
|
10
|
+
// No attestation is asked for or looked at: which make of authenticator holds the key is not this Mac's question,
|
|
11
|
+
// and the key arrives by a channel only the paired device can write to. So the page hands the key as the browser
|
|
12
|
+
// gives it (SubjectPublicKeyInfo) beside the authenticator's data, and nothing here reads CBOR.
|
|
13
|
+
|
|
14
|
+
import crypto from 'node:crypto';
|
|
15
|
+
|
|
16
|
+
const b64u = (bytes) => Buffer.from(bytes).toString('base64url');
|
|
17
|
+
const bytesOf = (text, max) => {
|
|
18
|
+
if (typeof text !== 'string' || !text || text.length > max || !/^[A-Za-z0-9_-]+$/.test(text)) throw new Error('not a passkey answer');
|
|
19
|
+
return Buffer.from(text, 'base64url');
|
|
20
|
+
};
|
|
21
|
+
const sha256 = (bytes) => crypto.createHash('sha256').update(bytes).digest();
|
|
22
|
+
|
|
23
|
+
/** The algorithms a passkey may sign with (COSE numbers): ES256, RS256, EdDSA. */
|
|
24
|
+
export const ALGS = [-7, -257, -8];
|
|
25
|
+
const UP = 0x01;
|
|
26
|
+
const UV = 0x04;
|
|
27
|
+
const BE = 0x08; // the passkey may be backed up (a synced one)
|
|
28
|
+
const BS = 0x10; // and is
|
|
29
|
+
const AT = 0x40;
|
|
30
|
+
const ED = 0x80; // extension data follows
|
|
31
|
+
|
|
32
|
+
export const newChallenge = () => b64u(crypto.randomBytes(32));
|
|
33
|
+
|
|
34
|
+
/** The relay's address as a passkey knows it: the host it is made for, the origin its browser says it signed at. */
|
|
35
|
+
export function partyOf(url) {
|
|
36
|
+
const u = new URL(url);
|
|
37
|
+
return { rpId: u.hostname, origin: u.origin };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What the browser says it signed: of this kind, over this challenge, at this origin, and not from inside another site's frame. */
|
|
41
|
+
function clientData(text, { type, challenge, origin }) {
|
|
42
|
+
const raw = bytesOf(text, 4096);
|
|
43
|
+
let said;
|
|
44
|
+
try { said = JSON.parse(raw.toString('utf8')); } catch { throw new Error('not a passkey answer'); }
|
|
45
|
+
if (said?.type !== type) throw new Error('not the kind of passkey answer that was asked for');
|
|
46
|
+
const theirs = Buffer.from(typeof said.challenge === 'string' ? said.challenge : '');
|
|
47
|
+
const ours = Buffer.from(challenge);
|
|
48
|
+
if (theirs.length !== ours.length || !crypto.timingSafeEqual(theirs, ours)) throw new Error('an answer to another asking');
|
|
49
|
+
if (said.origin !== origin) throw new Error('signed at another address than the relay\'s');
|
|
50
|
+
if (said.crossOrigin === true) throw new Error('signed from inside another site\'s page');
|
|
51
|
+
return raw;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The authenticator's own account: for this host, with the person verified. Returns { flags, count, rest }. */
|
|
55
|
+
function authenticatorData(text, rpId) {
|
|
56
|
+
const data = bytesOf(text, 4096);
|
|
57
|
+
if (data.length < 37) throw new Error('not a passkey answer');
|
|
58
|
+
if (!crypto.timingSafeEqual(data.subarray(0, 32), sha256(rpId))) throw new Error('a passkey of another site');
|
|
59
|
+
const flags = data[32];
|
|
60
|
+
if (!(flags & UP) || !(flags & UV)) throw new Error('the device did not verify who was holding it');
|
|
61
|
+
if ((flags & BS) && !(flags & BE)) throw new Error('not a passkey answer'); // (backed up, of a kind that cannot be: no authenticator says so)
|
|
62
|
+
return { data, flags, count: data.readUInt32BE(33), rest: data.subarray(37) };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function publicKey(spki, alg) {
|
|
66
|
+
let key;
|
|
67
|
+
try { key = crypto.createPublicKey({ key: Buffer.isBuffer(spki) ? spki : bytesOf(spki, 2048), format: 'der', type: 'spki' }); } catch { throw new Error('not a key a passkey signs with'); }
|
|
68
|
+
const kind = key.asymmetricKeyType;
|
|
69
|
+
const details = key.asymmetricKeyDetails ?? {};
|
|
70
|
+
const fits = alg === -7 ? kind === 'ec' && details.namedCurve === 'prime256v1'
|
|
71
|
+
: alg === -257 ? kind === 'rsa' && details.modulusLength >= 2048
|
|
72
|
+
: alg === -8 ? kind === 'ed25519' : false;
|
|
73
|
+
if (!fits) throw new Error('not a key a passkey signs with');
|
|
74
|
+
return key;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A passkey just made: `made` is what the page hands over ({ id, key, alg, authData, clientData }, base64url but
|
|
79
|
+
* `alg`), `expected` what this Mac asked ({ challenge, origin, rpId }). Returns what is kept of it: { id, key, alg,
|
|
80
|
+
* count }. Throws with the reason.
|
|
81
|
+
*/
|
|
82
|
+
export function verifyRegistration(made, expected) {
|
|
83
|
+
const alg = made?.alg;
|
|
84
|
+
if (!ALGS.includes(alg)) throw new Error('not a key a passkey signs with');
|
|
85
|
+
clientData(made.clientData, { type: 'webauthn.create', challenge: expected.challenge, origin: expected.origin });
|
|
86
|
+
const { flags, count, rest } = authenticatorData(made.authData, expected.rpId);
|
|
87
|
+
// the credential this data is about is the one handed over (its id follows the authenticator's own 16 bytes and a length)
|
|
88
|
+
if (!(flags & AT) || rest.length < 18) throw new Error('not a passkey answer');
|
|
89
|
+
const length = rest.readUInt16BE(16);
|
|
90
|
+
const id = bytesOf(made.id, 1400);
|
|
91
|
+
// (after the id comes the key, as CBOR: at least a few bytes of it are there)
|
|
92
|
+
if (length < 1 || length > 1023 || rest.length < 18 + length + 8 || length !== id.length || !crypto.timingSafeEqual(rest.subarray(18, 18 + length), id)) throw new Error('not a passkey answer');
|
|
93
|
+
const key = bytesOf(made.key, 2048);
|
|
94
|
+
publicKey(key, alg);
|
|
95
|
+
return { id: b64u(id), key: b64u(key), alg, count };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A passkey used: `given` is { id, authData, clientData, signature } (base64url), `expected` { challenge, origin,
|
|
100
|
+
* rpId, passkey } with the passkey as kept. Returns { count } (to keep: an authenticator that counts its uses must
|
|
101
|
+
* count upwards; one that copied the key would not know where the other stands). Throws with the reason.
|
|
102
|
+
*/
|
|
103
|
+
export function verifyAssertion(given, expected) {
|
|
104
|
+
const kept = expected.passkey;
|
|
105
|
+
if (!kept || typeof given?.id !== 'string' || given.id !== kept.id) throw new Error('another passkey than this device\'s');
|
|
106
|
+
const client = clientData(given.clientData, { type: 'webauthn.get', challenge: expected.challenge, origin: expected.origin });
|
|
107
|
+
const { data, flags, count, rest } = authenticatorData(given.authData, expected.rpId);
|
|
108
|
+
// a use carries no credential of its own, and nothing after the count but extensions it says it has
|
|
109
|
+
if ((flags & AT) || (rest.length > 0 && !(flags & ED)) || ((flags & ED) && rest.length === 0)) throw new Error('not a passkey answer');
|
|
110
|
+
const signature = bytesOf(given.signature, 2048);
|
|
111
|
+
const key = publicKey(kept.key, kept.alg);
|
|
112
|
+
const signed = Buffer.concat([data, sha256(client)]);
|
|
113
|
+
let good = false;
|
|
114
|
+
try {
|
|
115
|
+
good = kept.alg === -7 ? crypto.verify('sha256', signed, { key, dsaEncoding: 'der' }, signature)
|
|
116
|
+
: kept.alg === -257 ? crypto.verify('sha256', signed, key, signature)
|
|
117
|
+
: crypto.verify(null, signed, key, signature);
|
|
118
|
+
} catch { good = false; }
|
|
119
|
+
if (!good) throw new Error('the passkey\'s signature does not hold');
|
|
120
|
+
if ((count || kept.count) && count <= (kept.count || 0)) throw new Error('this passkey was used elsewhere since (its count went backwards)');
|
|
121
|
+
return { count };
|
|
122
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Asking a relay to let this computer in, without an invitation (the relay's src/requests.ts): with a person's name
|
|
2
|
+
// and email, confirmed by a code sent there; the relay's owner then accepts or declines. The keys are made here when
|
|
3
|
+
// asking (the relay gets their public halves); with them a secret only this computer holds, by which it asks how the
|
|
4
|
+
// request stands. Accepted, the request becomes the membership, as a join does.
|
|
5
|
+
//
|
|
6
|
+
// Kept while it waits: relay-request.json (where, which, the phrase, how it stands; nothing secret), and in the
|
|
7
|
+
// Keychain (sideora-relay / request) the keys and the secret.
|
|
8
|
+
|
|
9
|
+
import crypto from 'node:crypto';
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { HOME_DIR, keychainReadStrict, keychainWrite, keychainDelete } from './config.js';
|
|
14
|
+
import * as seals from './relay-seal.js';
|
|
15
|
+
import { RELAY_PATH, keychainStore, readState, relayUrl, writeState } from './relay.js';
|
|
16
|
+
|
|
17
|
+
export const REQUEST_PATH = path.join(HOME_DIR, 'relay-request.json');
|
|
18
|
+
export const requestStore = {
|
|
19
|
+
read: () => keychainReadStrict('sideora-relay', 'request'),
|
|
20
|
+
write: (secret) => keychainWrite('sideora-relay', 'request', secret),
|
|
21
|
+
remove: () => { try { keychainDelete('sideora-relay', 'request'); } catch { /* none */ } }
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** The request waiting, or null: { url, id, phrase, name, email, label, state: unconfirmed | pending | declined, asked }. */
|
|
25
|
+
export function readRequest(file = REQUEST_PATH) {
|
|
26
|
+
try {
|
|
27
|
+
const r = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
28
|
+
return r && typeof r.url === 'string' && /^[0-9a-f]{16}$/.test(r.id) ? r : null;
|
|
29
|
+
} catch { return null; }
|
|
30
|
+
}
|
|
31
|
+
function writeRequest(r, file = REQUEST_PATH) {
|
|
32
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
33
|
+
const tmp = `${file}.${process.pid}.${crypto.randomBytes(4).toString('hex')}.tmp`;
|
|
34
|
+
fs.writeFileSync(tmp, JSON.stringify(r, null, 2) + '\n', { mode: 0o600 });
|
|
35
|
+
fs.renameSync(tmp, file);
|
|
36
|
+
}
|
|
37
|
+
function forget({ file, store }) {
|
|
38
|
+
try { fs.unlinkSync(file); } catch { /* gone */ }
|
|
39
|
+
store.remove();
|
|
40
|
+
}
|
|
41
|
+
/** The relay no longer knows the request (cancelled, or past keeping): nothing of it is kept here either. */
|
|
42
|
+
class Gone extends Error {}
|
|
43
|
+
const answer = async (res) => {
|
|
44
|
+
if (res.status === 404) throw new Gone('this request is no longer known to the relay (it ran out, or was cancelled): ask again');
|
|
45
|
+
const said = await res.json().catch(() => null);
|
|
46
|
+
if (!res.ok) throw new Error(said?.error || `the relay answered ${res.status}`);
|
|
47
|
+
return said;
|
|
48
|
+
};
|
|
49
|
+
const post = (fetchFn, url, body) => fetchFn(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), redirect: 'error', signal: AbortSignal.timeout(15_000) });
|
|
50
|
+
|
|
51
|
+
/** Ask: the keys made here, a code sent to the email. */
|
|
52
|
+
export async function relayRequest({ url, name, email, note = '', label = os.hostname().replace(/\.local$/, ''), file = REQUEST_PATH, relayFile = RELAY_PATH, store = requestStore, fetchFn = fetch } = {}) {
|
|
53
|
+
if (readState(relayFile)) throw new Error('this computer is already joined to a relay (leave it first)');
|
|
54
|
+
if (readRequest(file)) throw new Error('this computer asked already: confirm the code, or cancel that request first');
|
|
55
|
+
const origin = relayUrl(url);
|
|
56
|
+
const signing = await seals.newSignPair();
|
|
57
|
+
const box = await seals.newBoxPair();
|
|
58
|
+
const secret = crypto.randomBytes(32).toString('base64url');
|
|
59
|
+
const said = await answer(await post(fetchFn, `${origin}/requests`, { name, email, note, label: String(label).slice(0, 80), key: signing.publicRaw, box: box.publicRaw, secret }));
|
|
60
|
+
if (!/^[0-9a-f]{16}$/.test(said?.id ?? '')) throw new Error('the relay did not take the request');
|
|
61
|
+
store.write(JSON.stringify({ id: said.id, secret, sign: signing.pkcs8, box: box.pkcs8 }));
|
|
62
|
+
const r = { url: origin, id: said.id, phrase: String(said.phrase ?? ''), name: String(name), email: String(email).toLowerCase(), label: String(label).slice(0, 80), box: box.publicRaw, state: 'unconfirmed', asked: Date.now() };
|
|
63
|
+
writeRequest(r, file);
|
|
64
|
+
return r;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function held(file, store) {
|
|
68
|
+
const r = readRequest(file);
|
|
69
|
+
if (!r) throw new Error('this computer has not asked to join a relay');
|
|
70
|
+
let s = null;
|
|
71
|
+
try { s = JSON.parse(store.read() || 'null'); } catch { /* below */ }
|
|
72
|
+
// (the secret is the request's own: one kept for another request, by another process meanwhile, is not used)
|
|
73
|
+
if (!s?.secret || !s?.sign || !s?.box || (s.id && s.id !== r.id)) throw new Error('what this request was made with is not in the Keychain any more: cancel it and ask again');
|
|
74
|
+
return { r, s };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** What the relay said, or that the request is gone (then forgotten here, and said). */
|
|
78
|
+
async function asking(r, { file, store }, fn) {
|
|
79
|
+
try { return await fn(); } catch (e) {
|
|
80
|
+
if (e instanceof Gone && readRequest(file)?.id === r.id) forget({ file, store });
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function relayRequestConfirm({ code, file = REQUEST_PATH, store = requestStore, fetchFn = fetch } = {}) {
|
|
86
|
+
const { r, s } = held(file, store);
|
|
87
|
+
const res = await post(fetchFn, `${r.url}/requests/${r.id}/confirm`, { secret: s.secret, code: String(code ?? '') });
|
|
88
|
+
// (confirmed already: its answer was lost on the way; what the relay says now is where it stands)
|
|
89
|
+
const said = res.status === 409 ? await res.json().catch(() => ({})) : await asking(r, { file, store }, () => answer(res));
|
|
90
|
+
const state = ['pending', 'accepted', 'declined'].includes(said?.state) ? (said.state === 'accepted' ? 'pending' : said.state) : r.state;
|
|
91
|
+
if (readRequest(file)?.id === r.id) writeRequest({ ...r, state }, file);
|
|
92
|
+
if (said?.state === 'accepted') await relayRequestCheck({ file, store, fetchFn });
|
|
93
|
+
return readRequest(file) ?? { ...r, state: 'accepted' };
|
|
94
|
+
}
|
|
95
|
+
export async function relayRequestResend({ file = REQUEST_PATH, store = requestStore, fetchFn = fetch } = {}) {
|
|
96
|
+
const { r, s } = held(file, store);
|
|
97
|
+
return asking(r, { file, store }, async () => answer(await post(fetchFn, `${r.url}/requests/${r.id}/resend`, { secret: s.secret })));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* How it stands, asked of the relay: accepted, and it becomes this computer's membership (its keys moved to where a
|
|
102
|
+
* joined computer keeps them, the request forgotten); declined, kept as such until it is cleared.
|
|
103
|
+
*/
|
|
104
|
+
// (one asking at a time in this process: Settings and the web app may both ask; two processes finishing the same
|
|
105
|
+
// accepted request write the same membership)
|
|
106
|
+
let checking = null;
|
|
107
|
+
export function relayRequestCheck(options = {}) {
|
|
108
|
+
checking ??= check(options).finally(() => { checking = null; });
|
|
109
|
+
return checking;
|
|
110
|
+
}
|
|
111
|
+
async function check({ file = REQUEST_PATH, relayFile = RELAY_PATH, store = requestStore, membership = keychainStore, fetchFn = fetch } = {}) {
|
|
112
|
+
{
|
|
113
|
+
const r = readRequest(file);
|
|
114
|
+
if (!r || r.state === 'declined') return r;
|
|
115
|
+
const { s } = held(file, store);
|
|
116
|
+
const res = await post(fetchFn, `${r.url}/requests/${r.id}/status`, { secret: s.secret });
|
|
117
|
+
// (asked for this request: another one made meanwhile, or this one cancelled, is not touched by its answer)
|
|
118
|
+
if (readRequest(file)?.id !== r.id) return readRequest(file);
|
|
119
|
+
if (res.status === 404) { forget({ file, store }); return { ...r, state: 'gone' }; } // (cancelled, or past keeping)
|
|
120
|
+
const said = await answer(res);
|
|
121
|
+
if (said.state === 'accepted' && /^[0-9a-f]{16}$/.test(said.instance ?? '') && typeof said.token === 'string') {
|
|
122
|
+
if (readState(relayFile)) throw new Error('this computer joined another relay meanwhile');
|
|
123
|
+
membership.write(JSON.stringify({ token: said.token, sign: s.sign, box: s.box }));
|
|
124
|
+
writeState({ url: r.url, instance: said.instance, label: r.label, box: r.box, devices: [] }, relayFile);
|
|
125
|
+
forget({ file, store });
|
|
126
|
+
return { ...r, state: 'accepted', instance: said.instance };
|
|
127
|
+
}
|
|
128
|
+
const state = ['unconfirmed', 'pending', 'declined'].includes(said.state) ? said.state : r.state;
|
|
129
|
+
if (state !== r.state) writeRequest({ ...r, state }, file);
|
|
130
|
+
return { ...r, state };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Cancelled at the relay, then nothing of it kept here. A declined one is only cleared. The relay not reached: nothing
|
|
136
|
+
* is forgotten (the request would wait there with nobody to take it). Accepted meanwhile: the membership is taken.
|
|
137
|
+
*/
|
|
138
|
+
export async function relayRequestCancel({ file = REQUEST_PATH, relayFile = RELAY_PATH, store = requestStore, membership = keychainStore, fetchFn = fetch } = {}) {
|
|
139
|
+
const r = readRequest(file);
|
|
140
|
+
if (!r) return false;
|
|
141
|
+
if (r.state !== 'declined') {
|
|
142
|
+
let s = null;
|
|
143
|
+
try { s = JSON.parse(store.read() || 'null'); } catch { /* none */ }
|
|
144
|
+
if (s?.secret) {
|
|
145
|
+
const res = await post(fetchFn, `${r.url}/requests/${r.id}/cancel`, { secret: s.secret }).catch(() => null);
|
|
146
|
+
if (!res) throw new Error('the relay could not be reached: the request was not cancelled. Try again');
|
|
147
|
+
if (res.status === 409) {
|
|
148
|
+
const said = await res.json().catch(() => ({}));
|
|
149
|
+
if (said?.state === 'accepted') { await relayRequestCheck({ file, relayFile, store, membership, fetchFn }); throw new Error('the owner accepted this computer just before: it is joined now (Leave the relay to undo it)'); }
|
|
150
|
+
} else if (!res.ok && res.status !== 404) throw new Error(`the relay answered ${res.status}: the request was not cancelled. Try again`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (readRequest(file)?.id === r.id) forget({ file, store });
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// The seal between the Mac and a paired browser reached through the relay (src/relay.js): everything asked and
|
|
2
|
+
// answered crosses as ciphertext the relay cannot open. Its twin in the page is
|
|
3
|
+
// extension/src/platform/relaySeal.ts (the same bytes: test/relay.test.js makes the two talk to each other).
|
|
4
|
+
//
|
|
5
|
+
// keys X25519 on both sides; the shared secret through HKDF-SHA256 (salt: the instance's id, info below)
|
|
6
|
+
// to one AES-256-GCM key per paired browser
|
|
7
|
+
// sealed base64url( iv(12) || ciphertext || tag(16) ), with what it is for as additional data
|
|
8
|
+
|
|
9
|
+
import { webcrypto } from 'node:crypto';
|
|
10
|
+
|
|
11
|
+
const { subtle } = webcrypto;
|
|
12
|
+
export const INFO = 'mailwatcher relay v1'; // (the name from before the rename, kept: both ends seal with it, and pages paired already must still open what is sealed)
|
|
13
|
+
const enc = new TextEncoder();
|
|
14
|
+
const dec = new TextDecoder();
|
|
15
|
+
|
|
16
|
+
export const b64u = (b) => Buffer.from(b instanceof ArrayBuffer ? new Uint8Array(b) : b).toString('base64url');
|
|
17
|
+
export const unb64u = (s) => new Uint8Array(Buffer.from(String(s), 'base64url'));
|
|
18
|
+
|
|
19
|
+
/** A new X25519 pair, to be kept: { privateKey (CryptoKey), publicRaw, pkcs8 (base64, the private half as it is stored) }. */
|
|
20
|
+
export async function newBoxPair() {
|
|
21
|
+
const k = await subtle.generateKey({ name: 'X25519' }, true, ['deriveBits']);
|
|
22
|
+
return { privateKey: k.privateKey, publicRaw: b64u(await subtle.exportKey('raw', k.publicKey)), pkcs8: Buffer.from(await subtle.exportKey('pkcs8', k.privateKey)).toString('base64') };
|
|
23
|
+
}
|
|
24
|
+
export const boxPrivate = (pkcs8) => subtle.importKey('pkcs8', Buffer.from(pkcs8, 'base64'), { name: 'X25519' }, false, ['deriveBits']);
|
|
25
|
+
|
|
26
|
+
/** A new Ed25519 pair, for the credentials of paired browsers: { privateKey, publicRaw, pkcs8 }. */
|
|
27
|
+
export async function newSignPair() {
|
|
28
|
+
const k = await subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']);
|
|
29
|
+
return { privateKey: k.privateKey, publicRaw: b64u(await subtle.exportKey('raw', k.publicKey)), pkcs8: Buffer.from(await subtle.exportKey('pkcs8', k.privateKey)).toString('base64') };
|
|
30
|
+
}
|
|
31
|
+
export const signPrivate = (pkcs8) => subtle.importKey('pkcs8', Buffer.from(pkcs8, 'base64'), { name: 'Ed25519' }, false, ['sign']);
|
|
32
|
+
export const sign = async (privateKey, text) => b64u(await subtle.sign({ name: 'Ed25519' }, privateKey, enc.encode(text)));
|
|
33
|
+
|
|
34
|
+
/** The key this pair of X25519 keys shares, for one instance. */
|
|
35
|
+
export async function sharedKey(mine, theirs, instance) {
|
|
36
|
+
const pub = await subtle.importKey('raw', unb64u(theirs), { name: 'X25519' }, false, []);
|
|
37
|
+
const secret = await subtle.deriveBits({ name: 'X25519', public: pub }, mine, 256);
|
|
38
|
+
const hk = await subtle.importKey('raw', secret, 'HKDF', false, ['deriveKey']);
|
|
39
|
+
return subtle.deriveKey({ name: 'HKDF', hash: 'SHA-256', salt: enc.encode(instance), info: enc.encode(INFO) }, hk, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function seal(key, text, aad) {
|
|
43
|
+
const iv = webcrypto.getRandomValues(new Uint8Array(12));
|
|
44
|
+
const ct = new Uint8Array(await subtle.encrypt({ name: 'AES-GCM', iv, additionalData: enc.encode(aad) }, key, enc.encode(text)));
|
|
45
|
+
return b64u(Buffer.concat([iv, ct]));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Throws when it is not what was sealed, for that purpose, under that key. */
|
|
49
|
+
export async function open(key, sealed, aad) {
|
|
50
|
+
const raw = unb64u(sealed);
|
|
51
|
+
if (raw.length < 28) throw new Error('not sealed');
|
|
52
|
+
return dec.decode(await subtle.decrypt({ name: 'AES-GCM', iv: raw.subarray(0, 12), additionalData: enc.encode(aad) }, key, raw.subarray(12)));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A short name for a public key, carried by the pairing link. */
|
|
56
|
+
export const fingerprint = async (publicRaw) => b64u(await subtle.digest('SHA-256', unb64u(publicRaw))).slice(0, 22);
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// The relay from Settings, on the computer Sideora runs on (its web app or the extension; never through the relay
|
|
2
|
+
// itself: src/op-registry.js): where this computer stands with its relay, joining one with the invitation its owner
|
|
3
|
+
// gave, a pairing link shown as a code to scan, the paired devices, moving to another address of the relay, leaving.
|
|
4
|
+
//
|
|
5
|
+
// The connection is the web app's (`sideora web`): it follows relay.json, and notes here whether it is connected.
|
|
6
|
+
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { createRequire } from 'node:module';
|
|
10
|
+
import { HOME_DIR } from './config.js';
|
|
11
|
+
import * as relay from './relay.js';
|
|
12
|
+
import * as access from './web-access.js';
|
|
13
|
+
import { readRequest, REQUEST_PATH } from './relay-request.js';
|
|
14
|
+
|
|
15
|
+
export const STATUS_PATH = path.join(HOME_DIR, 'relay-status.json');
|
|
16
|
+
|
|
17
|
+
/** Said by the web app as its connection changes: connected or not, to which address, by which process. */
|
|
18
|
+
export function noteStatus(state, { url, why = '', file = STATUS_PATH, pid = process.pid, now = Date.now() } = {}) {
|
|
19
|
+
try {
|
|
20
|
+
// two web apps at once (a terminal's beside the service's): one that holds no connection does not say over one
|
|
21
|
+
// that does, and one that quits says "off" only for itself
|
|
22
|
+
let was = null;
|
|
23
|
+
try { was = JSON.parse(fs.readFileSync(file, 'utf8')); } catch { /* none */ }
|
|
24
|
+
if (was && Number.isInteger(was.pid) && was.pid !== pid && alive(was.pid) && state !== 'connected') return;
|
|
25
|
+
const tmp = `${file}.${pid}.tmp`;
|
|
26
|
+
fs.writeFileSync(tmp, JSON.stringify({ state, url, why, pid, at: now }) + '\n', { mode: 0o600 });
|
|
27
|
+
fs.renameSync(tmp, file);
|
|
28
|
+
} catch { /* said again at the next change */ }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch (e) { return e.code === 'EPERM'; } };
|
|
32
|
+
|
|
33
|
+
/** connected | connecting | refused | off (no web app holds the connection, or it holds another). */
|
|
34
|
+
function connection(state, file) {
|
|
35
|
+
let s = null;
|
|
36
|
+
try { s = JSON.parse(fs.readFileSync(file, 'utf8')); } catch { /* none */ }
|
|
37
|
+
if (!s || !Number.isInteger(s.pid) || !alive(s.pid)) return { state: 'off' };
|
|
38
|
+
// (a web app is running, and has not yet taken up a membership made a moment ago: it does within seconds)
|
|
39
|
+
if (s.url !== state.url) return { state: 'connecting' };
|
|
40
|
+
return { state: ['connected', 'connecting', 'refused'].includes(s.state) ? s.state : 'connecting', since: s.at, ...(s.why ? { why: String(s.why) } : {}) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Where this computer stands: not joined, or joined at an address, connected or not, with its devices. */
|
|
44
|
+
export function relaySettings({ file = relay.RELAY_PATH, status = STATUS_PATH, requestFile = REQUEST_PATH } = {}) {
|
|
45
|
+
const state = relay.readState(file);
|
|
46
|
+
if (!state) {
|
|
47
|
+
// (asked to join and waiting: what the page shows of it, nothing secret)
|
|
48
|
+
const r = readRequest(requestFile);
|
|
49
|
+
return { joined: false, ...(r ? { request: { url: r.url, phrase: r.phrase, name: r.name, email: r.email, label: r.label, state: r.state } } : {}) };
|
|
50
|
+
}
|
|
51
|
+
return { joined: true, url: state.url, instance: state.instance, label: state.label, connection: connection(state, status), devices: relay.relayDevices({ file }) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What was pasted: the command the relay's console copies (`sideora relay join <url> <invitation>`), or the
|
|
56
|
+
* address and the invitation alone. Nothing else is read from it.
|
|
57
|
+
*/
|
|
58
|
+
export function readInvitation(text) {
|
|
59
|
+
const words = String(text ?? '').trim().split(/\s+/).filter(Boolean);
|
|
60
|
+
// (mailwatcher, mw, rmail: the command's names before the rename, as an older console copied it)
|
|
61
|
+
while (words.length && /^(sideora|mailwatcher|mw|rmail|relay|join)$/i.test(words[0])) words.shift();
|
|
62
|
+
if (words.length !== 2) throw new Error('paste the invitation as it was given: `sideora relay join <address> <invitation>`, or the address and the invitation');
|
|
63
|
+
return { url: relay.relayUrl(words[0]), code: words[1] };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function joinFromSettings({ invitation, url: at, code: given, fetchFn, keyStore, file } = {}) {
|
|
67
|
+
// (the address and the invitation apart, or pasted together as the console copies them)
|
|
68
|
+
// (the code field may hold the whole command as the console copies it: then it says the address itself)
|
|
69
|
+
const { url, code } = given ? (/\s/.test(String(given).trim()) ? readInvitation(given) : { url: relay.relayUrl(at), code: String(given).trim() }) : readInvitation(invitation);
|
|
70
|
+
await relay.relayJoin({ url, code, ...(fetchFn ? { fetchFn } : {}), ...(keyStore ? { keyStore } : {}), ...(file ? { file } : {}) });
|
|
71
|
+
return relaySettings({ ...(file ? { file } : {}) });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A pairing link, and the same as a code to scan (an SVG, drawn here: the page shows it as it is). */
|
|
75
|
+
export async function pairFromSettings({ limited = false, file } = {}) {
|
|
76
|
+
const { link, ref } = await relay.relayPairing({ admin: !limited, ...(file ? { file } : {}) });
|
|
77
|
+
return { link, ref, svg: qrSvg(link), limited: !!limited, minutes: 10 };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** What became of a code the page showed: still waiting, used (and by which device), or gone (past its time, withdrawn). */
|
|
81
|
+
export function pairStatus({ ref, file } = {}) {
|
|
82
|
+
if (access.codeWaiting(ref)) return { state: 'waiting' };
|
|
83
|
+
const d = relay.readState(file)?.devices.find((x) => x.via === ref);
|
|
84
|
+
const device = d && relay.relayDevices(file ? { file } : {}).find((x) => x.id === d.id);
|
|
85
|
+
return device ? { state: 'paired', device } : { state: 'gone' };
|
|
86
|
+
}
|
|
87
|
+
/** A code withdrawn: the card that showed it closed, or another is made in its place. */
|
|
88
|
+
export function cancelPairing({ ref } = {}) {
|
|
89
|
+
return { withdrawn: access.dropCode(ref) };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** A QR code as SVG: dark squares on a light ground, a quiet zone around, sized by the page. Null without the library. */
|
|
93
|
+
export function qrSvg(text) {
|
|
94
|
+
let QRCode, Level;
|
|
95
|
+
try {
|
|
96
|
+
const require = createRequire(import.meta.url);
|
|
97
|
+
QRCode = require('qrcode-terminal/vendor/QRCode');
|
|
98
|
+
Level = require('qrcode-terminal/vendor/QRCode/QRErrorCorrectLevel');
|
|
99
|
+
} catch { return null; }
|
|
100
|
+
const qr = new QRCode(-1, Level.M);
|
|
101
|
+
qr.addData(String(text));
|
|
102
|
+
qr.make();
|
|
103
|
+
const n = qr.getModuleCount();
|
|
104
|
+
const q = 4; // the quiet zone, in modules
|
|
105
|
+
let d = '';
|
|
106
|
+
for (let r = 0; r < n; r++) for (let c = 0; c < n; c++) if (qr.isDark(r, c)) d += `M${c + q} ${r + q}h1v1h-1z`;
|
|
107
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${n + 2 * q} ${n + 2 * q}" shape-rendering="crispEdges"><rect width="100%" height="100%" fill="#fff"/><path d="${d}" fill="#000"/></svg>`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function forgetFromSettings({ id, file } = {}) {
|
|
111
|
+
if (typeof id !== 'string' || !id) throw new Error('which device');
|
|
112
|
+
relay.relayForget(id, file ? { file } : {});
|
|
113
|
+
return relaySettings({ ...(file ? { file } : {}) });
|
|
114
|
+
}
|
|
115
|
+
export function adminFromSettings({ id, on, file } = {}) {
|
|
116
|
+
if (typeof id !== 'string' || !id || id === 'all' && on) throw new Error('which device');
|
|
117
|
+
relay.relayAdmin(id, on !== false, file ? { file } : {});
|
|
118
|
+
return relaySettings({ ...(file ? { file } : {}) });
|
|
119
|
+
}
|
|
120
|
+
export async function moveFromSettings({ url, file, fetchFn } = {}) {
|
|
121
|
+
await relay.relayMove({ url, ...(file ? { file } : {}), ...(fetchFn ? { fetchFn } : {}) });
|
|
122
|
+
return relaySettings({ ...(file ? { file } : {}) });
|
|
123
|
+
}
|
|
124
|
+
export async function leaveFromSettings({ force = false, file, keyStore, fetchFn } = {}) {
|
|
125
|
+
await relay.relayLeave({ force: force === true, ...(file ? { file } : {}), ...(keyStore ? { keyStore } : {}), ...(fetchFn ? { fetchFn } : {}) });
|
|
126
|
+
return relaySettings({ ...(file ? { file } : {}) });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// --- asking to join, without an invitation (src/relay-request.js)
|
|
130
|
+
const req = () => import('./relay-request.js');
|
|
131
|
+
const after = (file) => relaySettings({ ...(file ? { file } : {}) });
|
|
132
|
+
export async function requestFromSettings({ url, name, email, note } = {}) {
|
|
133
|
+
await (await req()).relayRequest({ url, name: String(name ?? '').trim(), email: String(email ?? '').trim(), note: String(note ?? '').trim() });
|
|
134
|
+
return after();
|
|
135
|
+
}
|
|
136
|
+
export async function confirmFromSettings({ code } = {}) { await (await req()).relayRequestConfirm({ code }); return after(); }
|
|
137
|
+
export async function resendFromSettings() { return (await req()).relayRequestResend(); }
|
|
138
|
+
export async function checkFromSettings() { await (await req()).relayRequestCheck(); return after(); }
|
|
139
|
+
export async function cancelRequestFromSettings() { await (await req()).relayRequestCancel(); return after(); }
|