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
package/src/accounts.js
ADDED
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
// Mail accounts added and removed from the side panel, as `sideora setup`
|
|
2
|
+
// does from a terminal: an IMAP mailbox (Gmail, iCloud, Fastmail… with an app
|
|
3
|
+
// password) or an Exchange/OWA one. Calendar-only accounts (Google, CalDAV)
|
|
4
|
+
// are added with the CLI; a Google calendar's sign-in, and connecting a Gmail
|
|
5
|
+
// mailbox to its Google calendar and contacts, are src/google-signin.js's.
|
|
6
|
+
//
|
|
7
|
+
// What the panel may do here is narrow on purpose. It must never be able to
|
|
8
|
+
// name a secret and have it sent to a server of its choosing:
|
|
9
|
+
// - add an account under a new name, after a sign-in with it worked, with a
|
|
10
|
+
// password typed for it, or with a 1Password item linked to it. The panel
|
|
11
|
+
// names the item by IDs only; the daemon reads the item's websites itself
|
|
12
|
+
// and refuses unless every server the password would go to (IMAP and SMTP,
|
|
13
|
+
// or Exchange) is the site of one of them (src/site-match.js). The panel's
|
|
14
|
+
// word for what the item is for is never taken. The items it is shown
|
|
15
|
+
// (onePasswordItems) are only those whose websites match the account's
|
|
16
|
+
// server or address: titles, vaults, websites and IDs, never a secret;
|
|
17
|
+
// - remove one; say which is the default;
|
|
18
|
+
// - give one a name to show (spaces and all: "Work mail"); its key, the
|
|
19
|
+
// name the CLI, rules, message ids and the Keychain know it by, stays;
|
|
20
|
+
// - change the address, servers, sign-in or password of one whose password
|
|
21
|
+
// is in the Keychain or comes from the 1Password app: only with its
|
|
22
|
+
// password typed again, or an item linked again (checked as above), and a
|
|
23
|
+
// sign-in with all of it that worked (the password kept for it is never
|
|
24
|
+
// sent anywhere new). A password read from a service account or the
|
|
25
|
+
// environment stays the CLI's. A password goes to the Keychain (a 1Password
|
|
26
|
+
// one as its cache) and is never read back. Connections
|
|
27
|
+
// are encrypted from the start (IMAP 993, SMTP 465) or required to become so
|
|
28
|
+
// (SMTP 587 with STARTTLS), and an Exchange address that redirects elsewhere
|
|
29
|
+
// is refused before any sign-in is sent to it.
|
|
30
|
+
|
|
31
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
32
|
+
import { fileURLToPath } from 'node:url';
|
|
33
|
+
import * as systemd from './host/systemd.js';
|
|
34
|
+
import { microsoftReady } from './microsoft-app.js';
|
|
35
|
+
import { loadConfig, updateConfig, normalizeConfig, keychainSet, keychainGet, keychainHas, keychainDelete, KEYCHAIN_SERVICE, hasMail, calendarProvider, getAccount, loadState, saveState, credentialsOf, isOnePassword } from './config.js';
|
|
36
|
+
import { onePasswordStatus, noteCredState, credIdentity } from './credentials.js';
|
|
37
|
+
import { getItem, getItemRevealed, listItems, formatRef, parseRef, OP_ID, opInstalled } from './onepassword.js';
|
|
38
|
+
import { hostOf, sameSite } from './site-match.js';
|
|
39
|
+
import { labelOf } from './labels.js';
|
|
40
|
+
import { googleAccessOf, canConnectGoogle, forgetSignIn } from './google-signin.js';
|
|
41
|
+
|
|
42
|
+
/** An account was added, set up otherwise or removed: what the Files page keeps of accounts that are not as they were is let go (src/files-index.js). */
|
|
43
|
+
const filesKeptChanged = () => { import('./files-index.js').then((m) => m.filesChanged(), () => {}); };
|
|
44
|
+
/** An account changed or removed: the connections this process kept to it (signed in as it was) are let go at once. */
|
|
45
|
+
const connectionsChanged = (name) => { import('./core.js').then((m) => m.closeConnections(name), () => {}); };
|
|
46
|
+
|
|
47
|
+
const NAME = /^[A-Za-z0-9][\w.-]{0,31}$/;
|
|
48
|
+
const HOST = /^(?=.{1,253}$)[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$/;
|
|
49
|
+
const EMAIL = /^[^\s@<>"]{1,64}@[^\s@<>"]{1,253}$/;
|
|
50
|
+
const TEST_MS = 30_000;
|
|
51
|
+
const PLIST_LABEL = 'com.sideora.daemon';
|
|
52
|
+
const PROBE = fileURLToPath(new URL('./account-probe.js', import.meta.url));
|
|
53
|
+
|
|
54
|
+
const text = (v, max = 200) => (typeof v === 'string' ? v.trim().slice(0, max) : '');
|
|
55
|
+
const MAX_LABEL = 40;
|
|
56
|
+
export { labelOf }; // (src/labels.js: a name to show)
|
|
57
|
+
/** A server named by a name, not an address: not this machine, not the network it is on (no 127.0.0.1, no 192.168.…). */
|
|
58
|
+
const isNamedHost = (h) => HOST.test(h) && !h.split('.').every((l) => /^(0x[0-9a-f]+|\d+)$/i.test(l)) && !/(^|\.)(localhost|local|internal|lan|home\.arpa)$/i.test(h);
|
|
59
|
+
|
|
60
|
+
const KINDS = 'an account here is an IMAP mailbox, a JMAP one or an Exchange one';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A JMAP server's session address, as typed: a host alone (mail.example.com) is its usual place,
|
|
64
|
+
* https://host/.well-known/jmap; a full https address is kept as it is (Fastmail's is /jmap/session).
|
|
65
|
+
*/
|
|
66
|
+
export function jmapSessionOf(typed) {
|
|
67
|
+
let v = text(typed, 300);
|
|
68
|
+
if (!v) throw new Error('the JMAP server is needed, like mail.example.com or https://api.fastmail.com/jmap/session');
|
|
69
|
+
if (!/^[a-z]+:\/\//i.test(v)) v = `https://${v}`;
|
|
70
|
+
let u;
|
|
71
|
+
try { u = new URL(v); } catch { throw new Error('the JMAP server is an address, like https://mail.example.com/.well-known/jmap'); }
|
|
72
|
+
if (u.protocol !== 'https:' || !isNamedHost(u.hostname) || u.username || u.password || u.hash) throw new Error('the JMAP server is an https address of a host, like https://mail.example.com/.well-known/jmap');
|
|
73
|
+
if (u.pathname === '/' && !u.search) u.pathname = '/.well-known/jmap';
|
|
74
|
+
return u.href;
|
|
75
|
+
}
|
|
76
|
+
const jmapHost = (a) => { try { return new URL(a.jmap?.session || '').hostname; } catch { return ''; } };
|
|
77
|
+
|
|
78
|
+
/** What the panel shows of each account: never a password, nor anything read from 1Password. */
|
|
79
|
+
export function accountsDoc(config = loadConfig()) {
|
|
80
|
+
let defaultAccount = null;
|
|
81
|
+
try { defaultAccount = getAccount(config).name; } catch { /* several and none set */ }
|
|
82
|
+
const state = loadState();
|
|
83
|
+
return {
|
|
84
|
+
accounts: Object.values(config.accounts).map((a) => ({
|
|
85
|
+
name: a.name,
|
|
86
|
+
...(labelOf(a.label) ? { label: labelOf(a.label).slice(0, MAX_LABEL) } : {}),
|
|
87
|
+
provider: a.provider,
|
|
88
|
+
email: a.email || (String(a.username || '').includes('@') ? a.username : '') || undefined,
|
|
89
|
+
server: a.provider === 'imap' ? a.imap?.host || '' : a.provider === 'jmap' ? jmapHost(a) : a.provider === 'ews' ? a.host || '' : a.provider === 'caldav' ? a.server || a.service || '' : '',
|
|
90
|
+
mail: hasMail(a),
|
|
91
|
+
calendar: calendarProvider(a) || null,
|
|
92
|
+
// a Google calendar: what its sign-in allows (never a token); a Gmail mailbox without a calendar: that it can be connected
|
|
93
|
+
...(calendarProvider(a) === 'google' ? { google: googleAccessOf(a.name) } : {}),
|
|
94
|
+
...(canConnectGoogle(a) ? { googleConnect: true } : {}),
|
|
95
|
+
// where its password comes from; for 1Password, which item (by its names) and how its last read went
|
|
96
|
+
...signInOf(a, state),
|
|
97
|
+
// what its form starts from (no password)
|
|
98
|
+
...(a.provider === 'imap' ? { spec: { provider: 'imap', email: a.email || '', username: a.username || '', imapHost: a.imap?.host || '', imapPort: Number(a.imap?.port ?? 993), smtpHost: a.smtp?.host || '', smtpPort: Number(a.smtp?.port ?? 587) } } : {}),
|
|
99
|
+
...(a.provider === 'jmap' ? { spec: { provider: 'jmap', email: a.email || '', username: a.username || '', jmapUrl: a.jmap?.session || '' } } : {}),
|
|
100
|
+
...(a.provider === 'ews' ? { spec: { provider: 'ews', email: a.email || '', username: a.username || '', host: a.host || '', domain: a.domain || '' } } : {})
|
|
101
|
+
})),
|
|
102
|
+
defaultAccount,
|
|
103
|
+
// (the 1Password CLI on this computer: not there, a 1Password item is not offered for a new password)
|
|
104
|
+
onePassword: opInstalled() ? 'here' : 'absent',
|
|
105
|
+
// (Sideora's Microsoft app configured: Microsoft 365 and Outlook.com can be added)
|
|
106
|
+
microsoft: microsoftReady() ? 'ready' : 'absent'
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function signInOf(a, state) {
|
|
111
|
+
if (a.provider === 'google') return { signIn: 'google' };
|
|
112
|
+
const src = credentialsOf(a);
|
|
113
|
+
if (isOnePassword(src)) return { signIn: '1password', onePassword: onePasswordStatus(a, state) };
|
|
114
|
+
if (src.source === 'env') return { signIn: 'env' };
|
|
115
|
+
if (src.source === 'microsoft') return { signIn: 'microsoft' };
|
|
116
|
+
return { signIn: keychainHas(a.name) ? 'keychain' : 'none' };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The account a panel form describes, checked: only the fields of its kind,
|
|
121
|
+
* each of the shape it must have. Throws with what is wrong.
|
|
122
|
+
*/
|
|
123
|
+
function accountOf(spec = {}) {
|
|
124
|
+
const provider = ['ews', 'imap', 'jmap'].includes(spec.provider) ? spec.provider : null;
|
|
125
|
+
if (!provider) throw new Error(KINDS);
|
|
126
|
+
if (spec.onePasswordItem || spec.onePasswordVault) throw new Error('an account with its password in 1Password is added with the command line (sideora setup)');
|
|
127
|
+
const email = text(spec.email, 320).toLowerCase();
|
|
128
|
+
if (email && !EMAIL.test(email)) throw new Error(`"${email}" is not an email address`);
|
|
129
|
+
const username = text(spec.username, 320);
|
|
130
|
+
const out = { provider, ...(email ? { email } : {}), ...(username ? { username } : {}) };
|
|
131
|
+
if (provider === 'jmap') {
|
|
132
|
+
if (!email) throw new Error('a JMAP account needs its email address');
|
|
133
|
+
out.jmap = { session: jmapSessionOf(spec.jmapUrl) };
|
|
134
|
+
} else if (provider === 'imap') {
|
|
135
|
+
if (!email) throw new Error('an IMAP account needs its email address');
|
|
136
|
+
const imapHost = text(spec.imapHost, 253).toLowerCase();
|
|
137
|
+
if (!isNamedHost(imapHost)) throw new Error('the IMAP server is a host name, like imap.example.com');
|
|
138
|
+
// encrypted from the first byte: a plain port could hand the password over before STARTTLS, or without it
|
|
139
|
+
if (Number(spec.imapPort ?? 993) !== 993) throw new Error('the IMAP port is 993 here (encrypted from the start); another is set with the command line');
|
|
140
|
+
out.imap = { host: imapHost, port: 993, secure: true };
|
|
141
|
+
const smtpHost = text(spec.smtpHost, 253).toLowerCase();
|
|
142
|
+
if (smtpHost) {
|
|
143
|
+
if (!isNamedHost(smtpHost)) throw new Error('the SMTP server is a host name, like smtp.example.com');
|
|
144
|
+
const smtpPort = Number(spec.smtpPort ?? 587);
|
|
145
|
+
if (smtpPort !== 465 && smtpPort !== 587) throw new Error('the SMTP port is 465 (encrypted) or 587 (encrypted by STARTTLS) here');
|
|
146
|
+
out.smtp = smtpPort === 465 ? { host: smtpHost, port: 465, secure: true } : { host: smtpHost, port: 587, secure: false, requireTLS: true };
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
let host = text(spec.host, 300);
|
|
150
|
+
if (host && !/^https:\/\//i.test(host)) host = `https://${host.replace(/^http:\/\//i, '')}`;
|
|
151
|
+
let url;
|
|
152
|
+
try { url = new URL(host); } catch { throw new Error('the Exchange server is an address, like https://mail.example.com'); }
|
|
153
|
+
if (url.protocol !== 'https:' || !isNamedHost(url.hostname) || url.username || url.password) throw new Error('the Exchange server is an https address of a host, like https://mail.example.com');
|
|
154
|
+
out.host = `https://${url.host}`;
|
|
155
|
+
if (!username) throw new Error('an Exchange account needs its sign-in name');
|
|
156
|
+
const domain = text(spec.domain, 100);
|
|
157
|
+
if (domain) out.domain = domain;
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The sign-in a typed password makes, as the clients take it (DOMAIN\name split). */
|
|
163
|
+
function credentialsFor(acct, password) {
|
|
164
|
+
if (typeof password !== 'string' || !password) throw new Error('the password is needed');
|
|
165
|
+
let creds = { username: acct.username || acct.email || '', password, domain: acct.domain || '' };
|
|
166
|
+
if (String(creds.username).includes('\\')) { const [domain, user] = creds.username.split('\\'); creds = { ...creds, domain, username: user }; }
|
|
167
|
+
return creds;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Exchange: the address is asked once without any sign-in. One that sends
|
|
172
|
+
* elsewhere (another host, or plain http) is refused: the NTLM sign-in that
|
|
173
|
+
* follows would go there.
|
|
174
|
+
*/
|
|
175
|
+
async function sameServer(acct) {
|
|
176
|
+
if (acct.provider !== 'ews' && acct.provider !== 'jmap') return;
|
|
177
|
+
const url = acct.provider === 'jmap' ? acct.jmap.session : `${acct.host}/EWS/Exchange.asmx`;
|
|
178
|
+
let res;
|
|
179
|
+
try { res = await fetch(url, { method: 'GET', redirect: 'manual', signal: AbortSignal.timeout(15_000) }); } catch { throw new Error('the server could not be reached at that address'); }
|
|
180
|
+
const to = res.headers.get('location');
|
|
181
|
+
if (to) {
|
|
182
|
+
let next;
|
|
183
|
+
try { next = new URL(to, url); } catch { throw new Error('the server sends the sign-in elsewhere: it is not added here'); }
|
|
184
|
+
if (next.protocol !== 'https:' || next.host !== new URL(url).host) throw new Error(`the server sends the sign-in elsewhere (${next.protocol}//${next.host}): it is not added here`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** An address of this machine or of a private network (what a server named by a name must not resolve to). */
|
|
189
|
+
function isPrivate(ip) {
|
|
190
|
+
if (ip.includes(':')) return /^(::1?$|f[cd]|fe[89ab]|::ffff:(127|10|192\.168|169\.254|172\.(1[6-9]|2\d|3[01]))\.)/i.test(ip);
|
|
191
|
+
const [a, b] = ip.split('.').map(Number);
|
|
192
|
+
return a === 127 || a === 10 || a === 0 || (a === 169 && b === 254) || (a === 192 && b === 168) || (a === 172 && b >= 16 && b <= 31) || (a === 100 && b >= 64 && b <= 127);
|
|
193
|
+
}
|
|
194
|
+
/** The servers of an account resolve to the internet, not to this machine or its network. */
|
|
195
|
+
async function publicServers(acct) {
|
|
196
|
+
return publicHosts(acct.provider === 'imap' ? [acct.imap.host, acct.smtp?.host].filter(Boolean) : acct.provider === 'jmap' ? [jmapHost(acct)] : [new URL(acct.host).hostname]);
|
|
197
|
+
}
|
|
198
|
+
/** These hosts resolve to the internet, not to this machine or its network. */
|
|
199
|
+
async function publicHosts(hosts) {
|
|
200
|
+
const { lookup } = await import('node:dns/promises');
|
|
201
|
+
for (const host of hosts) {
|
|
202
|
+
let found;
|
|
203
|
+
try { found = await lookup(host, { all: true }); } catch { throw new Error(`there is no server by the name ${host}`); }
|
|
204
|
+
if (found.some((f) => isPrivate(f.address))) throw new Error(`${host} is on this machine or its network: it is not added here`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** The sign-in, in a process of its own that is ended after TEST_MS whatever the server does. */
|
|
209
|
+
function probe(acct, creds) {
|
|
210
|
+
return new Promise((resolve) => {
|
|
211
|
+
const child = spawn(process.execPath, [PROBE], { stdio: ['pipe', 'pipe', 'ignore'] });
|
|
212
|
+
let out = '';
|
|
213
|
+
const timer = setTimeout(() => { child.kill('SIGKILL'); resolve({ ok: false, error: `the server did not answer in ${TEST_MS / 1000} seconds` }); }, TEST_MS);
|
|
214
|
+
child.stdout.setEncoding('utf8');
|
|
215
|
+
child.stdout.on('data', (d) => { out += d; });
|
|
216
|
+
child.on('close', () => {
|
|
217
|
+
clearTimeout(timer);
|
|
218
|
+
try { resolve(JSON.parse(out)); } catch { resolve({ ok: false, error: 'the sign-in could not be tried' }); }
|
|
219
|
+
});
|
|
220
|
+
child.on('error', () => { clearTimeout(timer); resolve({ ok: false, error: 'the sign-in could not be tried' }); });
|
|
221
|
+
child.stdin.end(JSON.stringify({ acct, creds }));
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Try an account before it is added: sign in, read the newest mail of the
|
|
227
|
+
* inbox. { ok: true, email, newest } or { ok: false, error }. Nothing is kept.
|
|
228
|
+
*/
|
|
229
|
+
export async function testAccount({ spec, password, onePassword } = {}) {
|
|
230
|
+
try {
|
|
231
|
+
const acct = normalizeConfig({ accounts: { probe: accountOf(spec) } }).accounts.probe;
|
|
232
|
+
if (oneSecret(password, onePassword) === 'item') {
|
|
233
|
+
const link = await linkedItem(onePassword, acct);
|
|
234
|
+
return await signInWith(acct, () => readLinked(link, acct));
|
|
235
|
+
}
|
|
236
|
+
return await signInWith(acct, password);
|
|
237
|
+
} catch (e) {
|
|
238
|
+
return { ok: false, error: String(e?.message || e).split('\n')[0].slice(0, 300) };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* A sign-in with an account as it is (normalized), and a password: the same
|
|
244
|
+
* checks as for a new one. `password` may be a function that reads it: it is
|
|
245
|
+
* then read only once the servers passed their checks.
|
|
246
|
+
*/
|
|
247
|
+
async function signInWith(acct, password) {
|
|
248
|
+
try {
|
|
249
|
+
if (typeof password !== 'function') credentialsFor(acct, password); // no password: said before anything is asked of the network
|
|
250
|
+
secureTransport(acct); // whatever made the account (this form, the command line): the panel's rules
|
|
251
|
+
await publicServers(acct);
|
|
252
|
+
await sameServer(acct);
|
|
253
|
+
const creds = credentialsFor(acct, typeof password === 'function' ? await password() : password);
|
|
254
|
+
return await probe(acct, creds);
|
|
255
|
+
} catch (e) {
|
|
256
|
+
return { ok: false, error: String(e?.message || e).split('\n')[0].slice(0, 300) };
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* What a sign-in from the panel keeps to, however the account was made (its
|
|
262
|
+
* form, or the command line, by hand): Exchange over https at a named host;
|
|
263
|
+
* IMAP on 993, encrypted from the start; SMTP on 465 (encrypted) or 587 with
|
|
264
|
+
* STARTTLS required; certificates checked. Throws with what is not so.
|
|
265
|
+
*/
|
|
266
|
+
function secureTransport(acct) {
|
|
267
|
+
const cli = 'it is changed with the command line (sideora setup)';
|
|
268
|
+
if (acct.provider === 'ews') {
|
|
269
|
+
let u;
|
|
270
|
+
try { u = new URL(String(acct.host || '')); } catch { throw new Error(`its Exchange server is not an https address of a host: ${cli}`); }
|
|
271
|
+
if (u.protocol !== 'https:' || !isNamedHost(u.hostname) || u.username || u.password) throw new Error(`its Exchange server is not an https address of a host: ${cli}`);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (acct.provider === 'jmap') {
|
|
275
|
+
let u;
|
|
276
|
+
try { u = new URL(String(acct.jmap?.session || '')); } catch { throw new Error(`its JMAP server is not an https address of a host: ${cli}`); }
|
|
277
|
+
if (u.protocol !== 'https:' || !isNamedHost(u.hostname) || u.username || u.password) throw new Error(`its JMAP server is not an https address of a host: ${cli}`);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (acct.provider !== 'imap') throw new Error(KINDS);
|
|
281
|
+
const imap = acct.imap || {};
|
|
282
|
+
if (!isNamedHost(String(imap.host || '')) || Number(imap.port) !== 993 || imap.secure !== true || imap.rejectUnauthorized === false) {
|
|
283
|
+
throw new Error(`its IMAP server is not on port 993, encrypted from the start, with its certificate checked: ${cli}`);
|
|
284
|
+
}
|
|
285
|
+
const smtp = acct.smtp || {};
|
|
286
|
+
if (!smtp.host) return;
|
|
287
|
+
const port = Number(smtp.port);
|
|
288
|
+
const tls = (port === 465 && smtp.secure === true) || (port === 587 && smtp.secure !== true && smtp.requireTLS === true);
|
|
289
|
+
if (!isNamedHost(String(smtp.host)) || !tls || smtp.rejectUnauthorized === false) {
|
|
290
|
+
throw new Error(`its SMTP server is not on 465, or on 587 with STARTTLS required, with its certificate checked: ${cli}`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// --- 1Password items linked from the panel ------------------------------------
|
|
295
|
+
|
|
296
|
+
/** The servers an account's password is sent to: IMAP and SMTP, or Exchange's. */
|
|
297
|
+
function passwordHosts(acct) {
|
|
298
|
+
return acct.provider === 'imap' ? [acct.imap?.host, acct.smtp?.host].filter(Boolean).map(hostOf) : acct.provider === 'jmap' ? [jmapHost(acct)] : [hostOf(acct.host)];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const shownTitle = (t) => labelOf(t).slice(0, 100) || 'an item without a title';
|
|
302
|
+
|
|
303
|
+
/** Why 1Password could not answer, as the panel shows it. */
|
|
304
|
+
const opReason = (e) => ({ NOT_INSTALLED: 'not-installed', LOCKED: 'locked', UNAVAILABLE: 'unavailable', TIMEOUT: 'locked' }[e?.code] || 'error');
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The 1Password Login and Password items whose websites cover every server
|
|
308
|
+
* an account's password would go to (its server, and its SMTP server): what
|
|
309
|
+
* the panel may offer to link. Titles, vaults, the matching websites and IDs
|
|
310
|
+
* only. Items that match the address alone are not shown, only counted
|
|
311
|
+
* (`addressOnly`): the panel learns nothing more of the vault.
|
|
312
|
+
* { items, addressOnly?, reason?, message? }: no items, and why (none match;
|
|
313
|
+
* 1Password is locked, off or not installed).
|
|
314
|
+
*/
|
|
315
|
+
export async function onePasswordItems({ server, email, smtp } = {}) {
|
|
316
|
+
// only servers the form could save (a named host; Exchange's as https, no user in it), resolving to the internet
|
|
317
|
+
const asked = [server, smtp].map((h) => text(h, 300)).filter(Boolean);
|
|
318
|
+
const hosts = asked.map((t) => (/@/.test(t) || (/^[a-z][a-z0-9+.-]*:\/\//i.test(t) && !/^https:\/\//i.test(t)) ? '' : hostOf(t)));
|
|
319
|
+
if (hosts.some((h) => !h || !isNamedHost(h))) return { items: [], reason: 'none', message: 'the server is a host name, like imap.example.com' };
|
|
320
|
+
const address = text(email, 320).toLowerCase();
|
|
321
|
+
const domain = EMAIL.test(address) ? hostOf(address.split('@').pop()) : '';
|
|
322
|
+
if (!hosts.length) return { items: [], reason: 'none', message: 'type the server first' };
|
|
323
|
+
try { await publicHosts(hosts); } catch (e) { return { items: [], reason: 'error', message: String(e?.message || e) }; }
|
|
324
|
+
let all;
|
|
325
|
+
try { all = await listItems({ categories: ['Login', 'Password'] }); } catch (e) {
|
|
326
|
+
return { items: [], reason: opReason(e), message: String(e?.message || e).split('\n')[0].slice(0, 300) };
|
|
327
|
+
}
|
|
328
|
+
const items = [];
|
|
329
|
+
let addressOnly = 0;
|
|
330
|
+
for (const it of all) {
|
|
331
|
+
if (!OP_ID.test(it.id) || !OP_ID.test(it.vault.id)) continue;
|
|
332
|
+
const sites = [...new Set(it.urls.map(hostOf).filter(Boolean))];
|
|
333
|
+
if (hosts.every((h) => sites.some((s) => sameSite(s, h)))) {
|
|
334
|
+
// only the websites that matched: the item's others are not the panel's to see
|
|
335
|
+
items.push({ itemId: it.id, vaultId: it.vault.id, title: shownTitle(it.title), vault: labelOf(it.vault.name).slice(0, 100), websites: sites.filter((s) => hosts.some((h) => sameSite(s, h))).slice(0, 5), linkable: true });
|
|
336
|
+
} else if (domain && sites.some((s) => sameSite(s, domain))) {
|
|
337
|
+
addressOnly++;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
items.sort((a, b) => a.title.localeCompare(b.title));
|
|
341
|
+
const more = addressOnly ? { addressOnly } : {};
|
|
342
|
+
return items.length ? { items, ...more } : { items, ...more, reason: 'none', message: `no Login item of 1Password has ${hosts.join(' and ')} among its websites` };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* The checks an item passes to be linked: the IDs asked for, a Login or
|
|
347
|
+
* Password item, and websites that cover every server the password goes to.
|
|
348
|
+
*/
|
|
349
|
+
function checkItem(item, vaultId, itemId, acct) {
|
|
350
|
+
if (item.id !== itemId || item.vault.id !== vaultId) throw new Error('1Password answered with another item: nothing was linked');
|
|
351
|
+
if (!['LOGIN', 'PASSWORD'].includes(item.category)) throw new Error('only a Login or Password item of 1Password is linked here');
|
|
352
|
+
const sites = item.urls.map(hostOf).filter(Boolean);
|
|
353
|
+
const off = passwordHosts(acct).filter((h) => !sites.some((s) => sameSite(s, h)));
|
|
354
|
+
if (off.length) throw new Error(`the 1Password item "${shownTitle(item.title)}" is not for ${off.join(' and ')}: add this server to the item's websites in 1Password (or link it from the command line: sideora setup)`);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* The item the panel picked ({ vaultId, itemId }), checked here, before any
|
|
359
|
+
* secret is read: looked up by its IDs (not revealed) and checked as
|
|
360
|
+
* checkItem says. → { vaultId, itemId, ref, label }
|
|
361
|
+
*/
|
|
362
|
+
async function linkedItem(link, acct) {
|
|
363
|
+
const vaultId = typeof link?.vaultId === 'string' ? link.vaultId : '';
|
|
364
|
+
const itemId = typeof link?.itemId === 'string' ? link.itemId : '';
|
|
365
|
+
if (!OP_ID.test(vaultId) || !OP_ID.test(itemId)) throw new Error('that is not a 1Password item');
|
|
366
|
+
let item;
|
|
367
|
+
try { item = await getItem(itemId, { vault: vaultId }); } catch (e) { throw new Error(`the 1Password item could not be read: ${String(e?.message || e).split('\n')[0]}`); }
|
|
368
|
+
checkItem(item, vaultId, itemId, acct);
|
|
369
|
+
return { vaultId, itemId, ref: formatRef({ vault: vaultId, item: itemId, field: 'password' }), label: { item: labelOf(item.title).slice(0, 100), vault: labelOf(item.vault.name).slice(0, 100) } };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* The password of a linked item, from one snapshot of it (revealed once):
|
|
374
|
+
* that snapshot passes checkItem again, so the websites checked and the
|
|
375
|
+
* password sent are the same item's at the same moment.
|
|
376
|
+
*/
|
|
377
|
+
async function readLinked(linked, acct) {
|
|
378
|
+
let snap;
|
|
379
|
+
try { snap = await getItemRevealed(linked.itemId, linked.vaultId); } catch (e) { throw new Error(`the password could not be read from 1Password: ${String(e?.message || e).split('\n')[0]}`); }
|
|
380
|
+
checkItem(snap.item, linked.vaultId, linked.itemId, acct);
|
|
381
|
+
if (!snap.password) throw new Error('the 1Password item holds no password');
|
|
382
|
+
// a username reference kept (into this item): the sign-in is tried as whom it names, from the same snapshot
|
|
383
|
+
if (linked.usernameField) { const u = snap.value(linked.usernameField); if (u) acct.username = u; }
|
|
384
|
+
linked.label = { item: labelOf(snap.item.title).slice(0, 100), vault: labelOf(snap.item.vault.name).slice(0, 100) };
|
|
385
|
+
return snap.password;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** A typed password, or a 1Password item, never both: the item when one is given. */
|
|
389
|
+
function oneSecret(password, onePassword) {
|
|
390
|
+
if (onePassword != null && password) throw new Error('a typed password or a 1Password item, not both');
|
|
391
|
+
return onePassword != null ? 'item' : 'typed';
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** Ask the daemon to start again: it reads the accounts when it starts. */
|
|
395
|
+
export function restartDaemon() {
|
|
396
|
+
try {
|
|
397
|
+
// (Linux: a systemd user unit, src/host/systemd.js)
|
|
398
|
+
if (systemd.isLinux) { systemd.restart('daemon', { noBlock: true }); return true; } // (asked from the hub too: the daemon itself)
|
|
399
|
+
execFileSync('launchctl', ['kickstart', '-k', `gui/${process.getuid()}/${PLIST_LABEL}`], { stdio: 'ignore', timeout: 10_000 });
|
|
400
|
+
return true;
|
|
401
|
+
} catch {
|
|
402
|
+
return false; // not installed as a launchd job (run by hand): it is told nothing
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Forget what was noted about an account's sign-in (the username and when its
|
|
408
|
+
* password was read): a new one starts clean. `mailbox`: also that its inbox
|
|
409
|
+
* was read once (another mailbox under that name is read once again, not
|
|
410
|
+
* taken as a flood of new mail that sets every rule off).
|
|
411
|
+
*/
|
|
412
|
+
function forgetCredState(name, mailbox = true) {
|
|
413
|
+
try {
|
|
414
|
+
const state = loadState();
|
|
415
|
+
let changed = false;
|
|
416
|
+
if (state.creds && Object.hasOwn(state.creds, name)) { delete state.creds[name]; changed = true; }
|
|
417
|
+
if (mailbox && state.baselinedAccounts && Object.hasOwn(state.baselinedAccounts, name)) { delete state.baselinedAccounts[name]; changed = true; }
|
|
418
|
+
// the account "default" of an old single-account setup kept these at the top
|
|
419
|
+
if (name === 'default') for (const k of ['credCachedAt', 'credUsername', 'credDomain']) if (k in state) { delete state[k]; changed = true; }
|
|
420
|
+
if (changed) saveState(state);
|
|
421
|
+
} catch { /* no state yet */ }
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Store a password, saying so plainly when it fails: the command's own error holds its arguments, the password among them. */
|
|
425
|
+
function storePassword(name, password) {
|
|
426
|
+
try { keychainSet(name, password); } catch { throw new Error('the password could not be stored in the Keychain (is access to it allowed?): nothing was saved'); }
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** The Keychain entry of an account: gone, or an error (a missing one is not an error). */
|
|
430
|
+
function deletePassword(name) {
|
|
431
|
+
try {
|
|
432
|
+
keychainDelete(KEYCHAIN_SERVICE, name);
|
|
433
|
+
} catch (e) {
|
|
434
|
+
throw new Error(`its password could not be taken out of the Keychain (${e?.status ?? e?.message ?? 'error'}): nothing was changed`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Add an account under a new name, once a sign-in with it worked: with a
|
|
440
|
+
* typed `password` (kept in the Keychain), or a 1Password item linked to it
|
|
441
|
+
* (`onePassword`: { vaultId, itemId }, checked as linkedItem says; its
|
|
442
|
+
* reference by IDs is kept, its password cached in the Keychain).
|
|
443
|
+
* { ...accountsDoc, restarted }
|
|
444
|
+
*/
|
|
445
|
+
export async function addAccount({ name, label, spec, password, onePassword } = {}) {
|
|
446
|
+
const key = typeof name === 'string' ? name.trim() : ''; // checked whole: a name too long is refused, not cut
|
|
447
|
+
if (!NAME.test(key)) throw new Error('an account name is letters, digits, . _ - (32 at most), starting with a letter or a digit');
|
|
448
|
+
const shown = checkedLabel(label);
|
|
449
|
+
const acct = accountOf(spec);
|
|
450
|
+
if (Object.hasOwn(loadConfig().accounts, key) || key in Object.prototype) throw new Error(`there is an account named "${key}" already`);
|
|
451
|
+
nameFree(loadConfig(), key, shown);
|
|
452
|
+
let link = null;
|
|
453
|
+
let secret = password;
|
|
454
|
+
if (oneSecret(password, onePassword) === 'item') {
|
|
455
|
+
link = await linkedItem(onePassword, normalizeConfig({ accounts: { probe: acct } }).accounts.probe);
|
|
456
|
+
secret = null;
|
|
457
|
+
}
|
|
458
|
+
const tried = link
|
|
459
|
+
? await signInWith(normalizeConfig({ accounts: { probe: acct } }).accounts.probe, async () => (secret = await readLinked(link, normalizeConfig({ accounts: { probe: acct } }).accounts.probe)))
|
|
460
|
+
: await testAccount({ spec, password });
|
|
461
|
+
if (!tried.ok) throw new Error(`the account did not answer: ${tried.error}`);
|
|
462
|
+
// From here on, nothing waits: the name is checked again, the password stored and the account written in one go,
|
|
463
|
+
// so a second add of the same name meanwhile can neither keep its password nor overwrite the first one's.
|
|
464
|
+
updateConfig((raw) => {
|
|
465
|
+
raw.accounts = raw.accounts && typeof raw.accounts === 'object' ? raw.accounts : {};
|
|
466
|
+
if (Object.hasOwn(raw.accounts, key)) throw new Error(`there is an account named "${key}" already`);
|
|
467
|
+
nameFree(raw, key, shown);
|
|
468
|
+
deletePassword(key); // a password left from an account of that name before is not this one's
|
|
469
|
+
deletePassword(`${key}/calendar`);
|
|
470
|
+
forgetSignIn(key); // nor its Google sign-in, nor one under way for the name before
|
|
471
|
+
deletePassword(`google:${key}`);
|
|
472
|
+
storePassword(key, secret);
|
|
473
|
+
raw.accounts[key] = {
|
|
474
|
+
...acct,
|
|
475
|
+
...(!acct.email && tried.email ? { email: tried.email } : {}),
|
|
476
|
+
...(shown && shown !== key ? { label: shown } : {}),
|
|
477
|
+
...(link ? { credentials: { source: '1password', password: link.ref, label: link.label } } : {})
|
|
478
|
+
};
|
|
479
|
+
// two accounts or more need one that is used when none is named: a mailbox
|
|
480
|
+
if (!raw.defaultAccount && Object.keys(raw.accounts).length > 1) raw.defaultAccount = Object.keys(raw.accounts).find((n) => hasMail({ provider: raw.accounts[n]?.provider || 'ews' })) ?? key;
|
|
481
|
+
});
|
|
482
|
+
forgetCredState(key);
|
|
483
|
+
if (link) noteRead(key, raw1(key));
|
|
484
|
+
filesKeptChanged();
|
|
485
|
+
return { ...accountsDoc(), restarted: restartDaemon() };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** A 1Password password just read and cached: noted as read now, under the username it signs in with. */
|
|
489
|
+
function noteRead(key, acct, signedInAs = null) {
|
|
490
|
+
try {
|
|
491
|
+
// as whom the sign-in was tried (a kept username reference's value), else the account's own
|
|
492
|
+
const creds = credentialsFor(signedInAs ? { ...acct, username: signedInAs } : acct, 'x');
|
|
493
|
+
noteCredState(key, { cachedAt: Date.now(), username: creds.username, domain: creds.domain, id: credIdentity(acct), failedAt: undefined, failure: undefined });
|
|
494
|
+
} catch { /* the next read notes it */ }
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* A username reference set with the command line, on an account linked to an
|
|
498
|
+
* item from the panel: kept only when it points into that item (the sign-in is
|
|
499
|
+
* then tried as whom it names, from the item's snapshot); else dropped, so the
|
|
500
|
+
* username used later is the one the sign-in was tried with.
|
|
501
|
+
* → { usernameRef, usernameField } or {}
|
|
502
|
+
*/
|
|
503
|
+
export function usernameKept(user, link) {
|
|
504
|
+
let r = null;
|
|
505
|
+
try { r = user ? parseRef(user) : null; } catch { r = null; }
|
|
506
|
+
return r && r.item === link.itemId && r.vault === link.vaultId ? { usernameRef: user, usernameField: r.field } : {};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** What of an account's credentials the panel does not set: kept when it links an item or a password is typed. */
|
|
510
|
+
function keptCredentials(a) {
|
|
511
|
+
const c = a?.credentials && typeof a.credentials === 'object' && !Array.isArray(a.credentials) ? a.credentials : {};
|
|
512
|
+
const { source: _s, password: _p, label: _l, item: _i, vault: _v, env: _e, ...rest } = c;
|
|
513
|
+
return rest;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** An account as config.json has it now, normalized. */
|
|
517
|
+
const raw1 = (key) => loadConfig().accounts[key];
|
|
518
|
+
|
|
519
|
+
function checkedLabel(label) {
|
|
520
|
+
const shown = labelOf(label);
|
|
521
|
+
if (shown.length > MAX_LABEL) throw new Error(`a name is at most ${MAX_LABEL} characters`);
|
|
522
|
+
return shown;
|
|
523
|
+
}
|
|
524
|
+
/** Two accounts are never shown by the same name (a mail could go from the wrong one): another's key or name is refused. */
|
|
525
|
+
function nameFree(raw, key, shown) {
|
|
526
|
+
if (!shown) return;
|
|
527
|
+
const low = shown.toLowerCase();
|
|
528
|
+
for (const [other, a] of Object.entries(raw.accounts || {})) {
|
|
529
|
+
if (other === key) continue;
|
|
530
|
+
if (other.toLowerCase() === low || labelOf(a?.label).toLowerCase() === low) throw new Error(`another account is called "${shown}"`);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// what decides where a sign-in goes and as whom: replaced whole by an edit
|
|
535
|
+
const SIGN_IN_KEYS = ['provider', 'email', 'username', 'domain', 'host', 'imap', 'smtp', 'jmap'];
|
|
536
|
+
|
|
537
|
+
/** An account's sign-in as it stands (where it goes, as whom, with what): an edit started before a change elsewhere does not land on it. */
|
|
538
|
+
const signInNow = (name, accounts) => {
|
|
539
|
+
const a = accounts[name];
|
|
540
|
+
return a ? JSON.stringify([...SIGN_IN_KEYS, 'onePassword', 'credentials', 'port'].map((k) => a[k] ?? null)) : null;
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Change an account: `label`, the name it is shown by ('' for none: its key
|
|
545
|
+
* then), and for one whose password is in the Keychain or the 1Password app,
|
|
546
|
+
* `spec` (its address, servers, sign-in) and its password: typed
|
|
547
|
+
* (`password`, kept in the Keychain) or a 1Password item linked
|
|
548
|
+
* (`onePassword`: { vaultId, itemId }, checked as linkedItem says). A change
|
|
549
|
+
* of either needs one of them and a sign-in with all of it that worked; then
|
|
550
|
+
* the password is stored (put back as it was if the config cannot be
|
|
551
|
+
* written) and the daemon starts again. A new password alone leaves the rest
|
|
552
|
+
* as it is. { ...accountsDoc, restarted }
|
|
553
|
+
*/
|
|
554
|
+
export async function editAccount({ name, label, spec, password, onePassword, was: opened } = {}) {
|
|
555
|
+
const key = text(name, 64);
|
|
556
|
+
const before = loadConfig().accounts;
|
|
557
|
+
if (!Object.hasOwn(before, key)) throw new Error(`there is no account named "${key}"`);
|
|
558
|
+
const shown = label === undefined ? undefined : checkedLabel(label);
|
|
559
|
+
if (shown !== undefined) nameFree(loadConfig(), key, shown);
|
|
560
|
+
// what the form started from ({ label, spec }): changed since (another window, the CLI), nothing is saved over it
|
|
561
|
+
const asOpened = () => {
|
|
562
|
+
if (!opened || typeof opened !== 'object') return;
|
|
563
|
+
const row = accountsDoc().accounts.find((r) => r.name === key);
|
|
564
|
+
if (!row || (opened.label !== undefined && (row.label ?? '') !== String(opened.label)) || (opened.spec !== undefined && JSON.stringify(row.spec ?? null) !== JSON.stringify(opened.spec ?? null))) {
|
|
565
|
+
throw new Error('it was changed elsewhere since its form was opened: nothing was saved');
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
asOpened();
|
|
569
|
+
const was = signInNow(key, before);
|
|
570
|
+
let acct = null; // the new sign-in, when it changes
|
|
571
|
+
let link = null; // the 1Password item linked, when one is
|
|
572
|
+
let secret = password;
|
|
573
|
+
let harden = false; // its SMTP on 587 made to require STARTTLS
|
|
574
|
+
let target = null; // the account as its sign-in is tried
|
|
575
|
+
const signIn = spec !== undefined || !!password || onePassword != null;
|
|
576
|
+
if (signIn) {
|
|
577
|
+
const now = before[key];
|
|
578
|
+
if (!['imap', 'ews', 'jmap'].includes(now.provider)) throw new Error('a calendar of its own is changed with the command line (sideora setup)');
|
|
579
|
+
const src = credentialsOf(now);
|
|
580
|
+
if (src.source === '1password-service') throw new Error('its password comes from a 1Password service account: it is changed with the command line (sideora setup)');
|
|
581
|
+
if (src.source === 'env') throw new Error('its password comes from the environment: it is changed with the command line (sideora setup)');
|
|
582
|
+
if (src.source === 'microsoft') throw new Error('it signs in with Microsoft: add it again (Microsoft 365 / Outlook.com) to sign in again');
|
|
583
|
+
if (!password && onePassword == null) throw new Error(isOnePassword(src) ? 'its 1Password item is linked again to change how it signs in (or a password typed)' : 'the password is needed to change how it signs in');
|
|
584
|
+
const kind = oneSecret(password, onePassword);
|
|
585
|
+
if (spec !== undefined) {
|
|
586
|
+
acct = accountOf(spec);
|
|
587
|
+
if (acct.provider !== now.provider) throw new Error('an account stays of its kind: remove it and add the other');
|
|
588
|
+
}
|
|
589
|
+
// tried as it will be: the new servers and sign-in, or (a new password or item alone) the account as it is,
|
|
590
|
+
// under the same rules as a new one (signInWith); its SMTP on 587 is made to require STARTTLS, as a new one's is
|
|
591
|
+
target = acct ? normalizeConfig({ accounts: { probe: acct } }).accounts.probe : structuredClone(now);
|
|
592
|
+
if (!acct && target.provider === 'imap' && target.smtp?.host && Number(target.smtp.port) === 587 && target.smtp.secure !== true && target.smtp.requireTLS !== true) {
|
|
593
|
+
target.smtp.requireTLS = true;
|
|
594
|
+
harden = true;
|
|
595
|
+
}
|
|
596
|
+
secureTransport(target); // refused before 1Password is asked anything
|
|
597
|
+
if (kind === 'item') {
|
|
598
|
+
link = await linkedItem(onePassword, target);
|
|
599
|
+
Object.assign(link, usernameKept(now.credentials?.username, link));
|
|
600
|
+
}
|
|
601
|
+
const tried = await signInWith(target, link ? async () => (secret = await readLinked(link, target)) : password);
|
|
602
|
+
if (!tried.ok) throw new Error(`the account did not answer: ${tried.error}`);
|
|
603
|
+
if (acct && !acct.email && tried.email) acct.email = tried.email;
|
|
604
|
+
}
|
|
605
|
+
let stored = false;
|
|
606
|
+
let old = null;
|
|
607
|
+
try {
|
|
608
|
+
updateConfig((raw) => {
|
|
609
|
+
const a = raw.accounts && Object.hasOwn(raw.accounts, key) ? raw.accounts[key] : null;
|
|
610
|
+
if (!a || typeof a !== 'object') throw new Error(`there is no account named "${key}"`);
|
|
611
|
+
if (signIn && signInNow(key, normalizeConfig(structuredClone(raw)).accounts) !== was) throw new Error('it was changed elsewhere meanwhile (removed, added again, set up): nothing was saved');
|
|
612
|
+
if (opened?.label !== undefined && labelOf(a.label) !== String(opened.label)) throw new Error('it was changed elsewhere since its form was opened: nothing was saved');
|
|
613
|
+
nameFree(raw, key, shown);
|
|
614
|
+
if (shown !== undefined) { if (shown && shown !== key) a.label = shown; else delete a.label; }
|
|
615
|
+
if (signIn) {
|
|
616
|
+
old = keychainGet(key);
|
|
617
|
+
stored = true;
|
|
618
|
+
storePassword(key, secret);
|
|
619
|
+
// where the password comes from now: the item linked, or (typed) the Keychain; what the
|
|
620
|
+
// panel does not set (a username reference set with the command line) is kept
|
|
621
|
+
const kept = keptCredentials(a);
|
|
622
|
+
delete a.onePassword;
|
|
623
|
+
if (link) {
|
|
624
|
+
delete kept.username;
|
|
625
|
+
a.credentials = { ...kept, ...(link.usernameRef ? { username: link.usernameRef } : {}), source: '1password', password: link.ref, label: link.label };
|
|
626
|
+
}
|
|
627
|
+
else if (Object.keys(kept).length) a.credentials = { ...kept, source: 'keychain' };
|
|
628
|
+
else delete a.credentials;
|
|
629
|
+
if (harden && a.smtp && typeof a.smtp === 'object') a.smtp.requireTLS = true;
|
|
630
|
+
}
|
|
631
|
+
if (acct) {
|
|
632
|
+
// (what the form does not hold is kept: a JMAP account's folders named for roles its server lacks)
|
|
633
|
+
const folders = a.jmap?.folders;
|
|
634
|
+
for (const k of SIGN_IN_KEYS) delete a[k];
|
|
635
|
+
Object.assign(a, acct);
|
|
636
|
+
if (folders && a.jmap) a.jmap.folders = folders;
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
} catch (e) {
|
|
640
|
+
// the password was stored but the config not written: the one before goes back
|
|
641
|
+
if (stored) { try { if (old !== null) keychainSet(key, old); else deletePassword(key); } catch { /* the error below says nothing was saved; the Keychain says otherwise */ } }
|
|
642
|
+
throw e;
|
|
643
|
+
}
|
|
644
|
+
if (!signIn) return accountsDoc(); // a name to show: the daemon is not concerned
|
|
645
|
+
forgetCredState(key, !!acct); // a new password alone: the same mailbox, read as before
|
|
646
|
+
if (link) noteRead(key, raw1(key), link.usernameRef ? target.username : null);
|
|
647
|
+
filesKeptChanged();
|
|
648
|
+
connectionsChanged(key);
|
|
649
|
+
return { ...accountsDoc(), restarted: restartDaemon() };
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/** Remove an account: its password from the Keychain first, then from the config. Its mail stays on its server. */
|
|
653
|
+
export function removeAccount({ name } = {}) {
|
|
654
|
+
const key = text(name, 64);
|
|
655
|
+
updateConfig((raw) => {
|
|
656
|
+
if (!raw.accounts || !Object.hasOwn(raw.accounts, key)) throw new Error(`there is no account named "${key}"`);
|
|
657
|
+
const mailboxes = Object.keys(raw.accounts).filter((n) => hasMail({ provider: raw.accounts[n]?.provider || 'ews' }));
|
|
658
|
+
if (mailboxes.length === 1 && mailboxes[0] === key) throw new Error('the last mailbox cannot be removed here: Sideora needs one');
|
|
659
|
+
forgetSignIn(key); // a Google sign-in under way for it ends, and keeps nothing it stores late
|
|
660
|
+
// its calendar's password first, as best it can (a failure there leaves the account as it was); then its own
|
|
661
|
+
try { deletePassword(`${key}/calendar`); } catch { /* left: an add of this name drops it before anything */ }
|
|
662
|
+
try { deletePassword(`google:${key}`); } catch { /* the same */ }
|
|
663
|
+
try { deletePassword(`microsoft:${key}`); } catch { /* the same: a Microsoft sign-in's tokens */ }
|
|
664
|
+
deletePassword(key); // a name added again must not find this one's password
|
|
665
|
+
delete raw.accounts[key];
|
|
666
|
+
if (raw.defaultAccount === key) {
|
|
667
|
+
// another mailbox, rather than a calendar of its own, is what mail and agents use when none is named
|
|
668
|
+
const names = Object.keys(raw.accounts);
|
|
669
|
+
raw.defaultAccount = names.find((n) => hasMail({ provider: raw.accounts[n]?.provider || 'ews' })) ?? names[0];
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
forgetCredState(key);
|
|
673
|
+
filesKeptChanged();
|
|
674
|
+
connectionsChanged(key);
|
|
675
|
+
return { ...accountsDoc(), restarted: restartDaemon() };
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/** The account used when none is named (new mail, the CLI, agents): a mailbox. */
|
|
679
|
+
export function setDefaultAccount({ name } = {}) {
|
|
680
|
+
const key = text(name, 64);
|
|
681
|
+
updateConfig((raw) => {
|
|
682
|
+
if (!raw.accounts || !Object.hasOwn(raw.accounts, key)) throw new Error(`there is no account named "${key}"`);
|
|
683
|
+
if (!hasMail({ provider: raw.accounts[key]?.provider || 'ews' })) throw new Error(`"${key}" holds no mail: the default is a mailbox`);
|
|
684
|
+
raw.defaultAccount = key;
|
|
685
|
+
});
|
|
686
|
+
return accountsDoc();
|
|
687
|
+
}
|