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.
Files changed (221) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +57 -6
  3. package/assets/sounds/freesound_community-mail-40275.mp3 +0 -0
  4. package/assets/sounds/mixkit-clear-announce-tones-2861.wav +0 -0
  5. package/assets/sounds/mixkit-happy-bells-notification-937.wav +0 -0
  6. package/assets/sounds/mixkit-musical-alert-notification-2309.wav +0 -0
  7. package/assets/sounds/nice-chime.mp3 +0 -0
  8. package/bin/sideora.js +2430 -12
  9. package/extension/dist/assets/background.ts-NW8YqAlO.js +1 -0
  10. package/extension/dist/assets/fillCode-Ci8IkBvY.js +1 -0
  11. package/extension/dist/assets/index.html-DrrPha1M.js +2 -0
  12. package/extension/dist/assets/modulepreload-polyfill-DcjICKwh.js +1 -0
  13. package/extension/dist/assets/shell-82MNMljp.css +1 -0
  14. package/extension/dist/assets/shell-QYBN1bx1.js +1 -0
  15. package/extension/dist/icons/icon-128.png +0 -0
  16. package/extension/dist/icons/icon-16.png +0 -0
  17. package/extension/dist/icons/icon-32.png +0 -0
  18. package/extension/dist/icons/icon-48.png +0 -0
  19. package/extension/dist/manifest.json +40 -0
  20. package/extension/dist/service-worker-loader.js +1 -0
  21. package/extension/dist/src/newtab/index.html +14 -0
  22. package/extension/dist/src/panel/index.html +15 -0
  23. package/extension/dist-web/assets/abnfDiagram-WEYOIA3C-CoXOc872.js +1 -0
  24. package/extension/dist-web/assets/apple-touch-icon-DSgRQASq.png +0 -0
  25. package/extension/dist-web/assets/architecture-WOLXFQ4H-BkJ4AerO.js +1 -0
  26. package/extension/dist-web/assets/architectureDiagram-ZHVYKT53-aPkSDlxA.js +36 -0
  27. package/extension/dist-web/assets/blockDiagram-RJAE7QA3-CH1G4NBZ.js +140 -0
  28. package/extension/dist-web/assets/c4Diagram-XDPPPE54-Baum1zDu.js +38 -0
  29. package/extension/dist-web/assets/channel-PsQg-mnK.js +1 -0
  30. package/extension/dist-web/assets/chunk-2E7WHVMD-CKvLrWRa.js +88 -0
  31. package/extension/dist-web/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
  32. package/extension/dist-web/assets/chunk-4EMNKR2T-6fjnroCZ.js +1 -0
  33. package/extension/dist-web/assets/chunk-55XCU76K-Ygs3sNpi.js +1 -0
  34. package/extension/dist-web/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
  35. package/extension/dist-web/assets/chunk-7S6XSTFO-iX5xYzfP.js +2 -0
  36. package/extension/dist-web/assets/chunk-AQDTSKZB-D9zwhFI8.js +72 -0
  37. package/extension/dist-web/assets/chunk-DSNLSHJZ-siGS-4qu.js +129 -0
  38. package/extension/dist-web/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
  39. package/extension/dist-web/assets/chunk-HAGFOTED-_3dtnWXn.js +62 -0
  40. package/extension/dist-web/assets/chunk-HNE4Q36P-BPzRHi72.js +1 -0
  41. package/extension/dist-web/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
  42. package/extension/dist-web/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
  43. package/extension/dist-web/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
  44. package/extension/dist-web/assets/chunk-NQOKRVIF-AXbmOBjs.js +213 -0
  45. package/extension/dist-web/assets/chunk-UHW7PFBP-B3xrZ8sD.js +1 -0
  46. package/extension/dist-web/assets/chunk-VQ5W62QI-D_k7Pa6b.js +1 -0
  47. package/extension/dist-web/assets/chunk-WQQL4CDK-BBIrWwoZ.js +10 -0
  48. package/extension/dist-web/assets/chunk-XXDRQBXY-i7MKP1NY.js +1 -0
  49. package/extension/dist-web/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
  50. package/extension/dist-web/assets/chunk-YZO67HFG-Z7qp5QAI.js +1 -0
  51. package/extension/dist-web/assets/chunk-ZAUX2LTX-CSt3c0Rb.js +1 -0
  52. package/extension/dist-web/assets/chunk-ZCHMQSOR-g8XRABGK.js +2 -0
  53. package/extension/dist-web/assets/classDiagram-v2-4MYWR3UA-BL706wzc.js +217 -0
  54. package/extension/dist-web/assets/cose-bilkent-JH36ORCC-C6J2IoHg.js +1 -0
  55. package/extension/dist-web/assets/cynefin-EF2NZ3EQ-DLhQOiAy.js +1 -0
  56. package/extension/dist-web/assets/cynefinDiagram-WF24UDRP-Cv9Lx6_l.js +62 -0
  57. package/extension/dist-web/assets/cytoscape.esm-nTrd5EVq.js +321 -0
  58. package/extension/dist-web/assets/d3-scale-CElPUUud.js +1 -0
  59. package/extension/dist-web/assets/d3-selection-COWvaQNI.js +1 -0
  60. package/extension/dist-web/assets/dagre-NK4GIZVX-ngM-w2EC.js +4 -0
  61. package/extension/dist-web/assets/diagram-CBK3MWTC-kp8sayNG.js +24 -0
  62. package/extension/dist-web/assets/diagram-I5XIIV2D-BZ_Dgs6K.js +24 -0
  63. package/extension/dist-web/assets/diagram-ODIL2UCW-DWhUyArG.js +30 -0
  64. package/extension/dist-web/assets/diagram-PFBEJUXE-CBAdEK7l.js +3 -0
  65. package/extension/dist-web/assets/diagram-RJA3I4VV-kOdIr_3K.js +41 -0
  66. package/extension/dist-web/assets/diagram-TYUNFSNR-BrWGa0x5.js +200 -0
  67. package/extension/dist-web/assets/dist-DdXDFcej.js +1 -0
  68. package/extension/dist-web/assets/docx-preview-D1zbEiLy.js +20 -0
  69. package/extension/dist-web/assets/earth-Ce3xs8Cb.webp +0 -0
  70. package/extension/dist-web/assets/ebnfDiagram-V4P3TTPT-0r3weWmj.js +1 -0
  71. package/extension/dist-web/assets/elk-QONTBCTQ-D3oElRZ3.js +27 -0
  72. package/extension/dist-web/assets/erDiagram-KYQIGQAA-B9oU7_j_.js +99 -0
  73. package/extension/dist-web/assets/eventmodeling-K75KTNOO-CMKgc1nP.js +1 -0
  74. package/extension/dist-web/assets/favicon-D_X6tUVf.svg +5 -0
  75. package/extension/dist-web/assets/flowDiagram-IUGLLWBU-yRwks0Mw.js +1 -0
  76. package/extension/dist-web/assets/ganttDiagram-R7GPNTJY-cJqD1qwQ.js +292 -0
  77. package/extension/dist-web/assets/gitGraph-VSP46ZUC-CuBQqkbr.js +1 -0
  78. package/extension/dist-web/assets/gitGraphDiagram-PDYL7XHY-D7bHnQXC.js +106 -0
  79. package/extension/dist-web/assets/graphlib-DS17s2tU.js +1 -0
  80. package/extension/dist-web/assets/home-CV7-8Trs.js +1 -0
  81. package/extension/dist-web/assets/index-CQvw7Wy_.js +7 -0
  82. package/extension/dist-web/assets/index-CVKnZ21i.css +1 -0
  83. package/extension/dist-web/assets/info-OHQRW6UA-B8kyzV4O.js +1 -0
  84. package/extension/dist-web/assets/infoDiagram-MVDRKAX5-CEm6lezX.js +2 -0
  85. package/extension/dist-web/assets/ishikawaDiagram-HPEHV2GE-Ce3uv2wr.js +70 -0
  86. package/extension/dist-web/assets/journeyDiagram-BOSHW7O7-QgdahHCP.js +139 -0
  87. package/extension/dist-web/assets/jszip.min-3XXygzvy.js +2 -0
  88. package/extension/dist-web/assets/kanban-definition-FNSA4HC5-DPuTtWgR.js +89 -0
  89. package/extension/dist-web/assets/katex-MEuSR36T.js +257 -0
  90. package/extension/dist-web/assets/mermaid-parser.core-h7U7YGn_.js +7 -0
  91. package/extension/dist-web/assets/mermaid.core-DkGhophj.js +44 -0
  92. package/extension/dist-web/assets/mindmap-definition-6OY57UG3-D7VQxbda.js +98 -0
  93. package/extension/dist-web/assets/mount-CBzzf6KQ.js +195 -0
  94. package/extension/dist-web/assets/mount-DtvYnQXt.css +1 -0
  95. package/extension/dist-web/assets/officeparser.browser.slim-kEjhTrQ1.js +1989 -0
  96. package/extension/dist-web/assets/packet-JDAUHWVQ-oafHgw6X.js +1 -0
  97. package/extension/dist-web/assets/pegDiagram-ECIUJNBX-C3zo1naj.js +1 -0
  98. package/extension/dist-web/assets/peoria-B5TBlKDG.jpg +0 -0
  99. package/extension/dist-web/assets/peoria-thumb-CN8upAdZ.jpg +0 -0
  100. package/extension/dist-web/assets/pie-XZMESJXO-DXzeBQqF.js +1 -0
  101. package/extension/dist-web/assets/pieDiagram-GNMPY74T-S5NzPthl.js +39 -0
  102. package/extension/dist-web/assets/quadrantDiagram-D57K4MC3-C9glHQmk.js +7 -0
  103. package/extension/dist-web/assets/radar-ABXABTNO-CWTNqtbu.js +1 -0
  104. package/extension/dist-web/assets/railroad-I3PHUGI6-BTpD5_Bu.js +1 -0
  105. package/extension/dist-web/assets/railroad-abnf-LNEFI6M7-bWyxoXQG.js +1 -0
  106. package/extension/dist-web/assets/railroad-ebnf-DWYD2UWJ-ri5733DQ.js +1 -0
  107. package/extension/dist-web/assets/railroad-peg-JR7BVNR7-CmGfTubP.js +1 -0
  108. package/extension/dist-web/assets/railroadDiagram-Y72NRBL7-DTfe6Dh1.js +1 -0
  109. package/extension/dist-web/assets/requirementDiagram-EQYSW5RA-DQDzcS3L.js +84 -0
  110. package/extension/dist-web/assets/rolldown-runtime-COiEfhsA.js +1 -0
  111. package/extension/dist-web/assets/rough.esm-Dy-Kn_BL.js +1 -0
  112. package/extension/dist-web/assets/sankeyDiagram-SY24JHO5-DyMKWxdB.js +40 -0
  113. package/extension/dist-web/assets/sequenceDiagram-BLEHNHQU-dIkB1mCn.js +169 -0
  114. package/extension/dist-web/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
  115. package/extension/dist-web/assets/stateDiagram-v2-A6IESKWZ-Bj_vxEVF.js +291 -0
  116. package/extension/dist-web/assets/swimlanes-B3ENRKFQ-BSkWEwHR.js +1 -0
  117. package/extension/dist-web/assets/swimlanesDiagram-AYQRV4AK-BTzSDlSM.js +8 -0
  118. package/extension/dist-web/assets/timeline-definition-JXYKGLGB-TwfWS0pw.js +120 -0
  119. package/extension/dist-web/assets/treeView-D4JQ5SDB-BzA_8YYb.js +1 -0
  120. package/extension/dist-web/assets/treemap-SAJKECNS-BI9d6--2.js +1 -0
  121. package/extension/dist-web/assets/usecaseDiagram-WPOLBNNX-BOVbAUPk.js +328 -0
  122. package/extension/dist-web/assets/vennDiagram-Q3Y3IDCW-X2kYAWaj.js +34 -0
  123. package/extension/dist-web/assets/wardley-7MLQ67FV-CItxqJ8G.js +1 -0
  124. package/extension/dist-web/assets/wardleyDiagram-IVAIDKWA-BnUXvKD_.js +78 -0
  125. package/extension/dist-web/assets/xychartDiagram-4URNHDU4-ysvBXvyE.js +7 -0
  126. package/extension/dist-web/icon-192.png +0 -0
  127. package/extension/dist-web/icon-512.png +0 -0
  128. package/extension/dist-web/index.html +25 -0
  129. package/extension/dist-web/manifest.webmanifest +14 -0
  130. package/package.json +64 -12
  131. package/plugin/.claude-plugin/icon.png +0 -0
  132. package/plugin/.claude-plugin/plugin.json +6 -0
  133. package/plugin/.mcp.json +8 -0
  134. package/plugin/README.md +72 -0
  135. package/plugin/mcp-launcher.mjs +48 -0
  136. package/src/account-probe.js +45 -0
  137. package/src/accounts.js +687 -0
  138. package/src/ai.js +269 -0
  139. package/src/assistant-runs.js +32 -0
  140. package/src/attached-mail.js +76 -0
  141. package/src/auth-gate.js +70 -0
  142. package/src/backup-cli.js +158 -0
  143. package/src/backup.js +329 -0
  144. package/src/bimi.js +233 -0
  145. package/src/bridge.js +101 -0
  146. package/src/calendar-alerts.js +56 -0
  147. package/src/chats.js +27 -0
  148. package/src/compose-editor.js +58 -0
  149. package/src/config.js +420 -0
  150. package/src/connections.d.ts +24 -0
  151. package/src/connections.js +37 -0
  152. package/src/core/agent.js +92 -0
  153. package/src/core/assistant.d.ts +13 -0
  154. package/src/core/assistant.js +790 -0
  155. package/src/core/chats.js +323 -0
  156. package/src/core/markdown.d.ts +8 -0
  157. package/src/core/markdown.js +239 -0
  158. package/src/core/notes.js +445 -0
  159. package/src/core/otp.js +152 -0
  160. package/src/core/rules.js +196 -0
  161. package/src/core/runs.d.ts +9 -0
  162. package/src/core/runs.js +211 -0
  163. package/src/core/thread.js +526 -0
  164. package/src/core/write.js +247 -0
  165. package/src/core-db.js +131 -0
  166. package/src/core.js +174 -0
  167. package/src/credentials.js +320 -0
  168. package/src/daemon.js +564 -0
  169. package/src/events.js +213 -0
  170. package/src/extension-id.d.ts +2 -0
  171. package/src/extension-id.js +5 -0
  172. package/src/file-lock.js +27 -0
  173. package/src/files-index.js +154 -0
  174. package/src/google-contacts.js +315 -0
  175. package/src/google-signin.js +306 -0
  176. package/src/host/assistant-html.js +44 -0
  177. package/src/host/chats-files.js +137 -0
  178. package/src/host/file-prep.js +122 -0
  179. package/src/host/notes-files.js +158 -0
  180. package/src/host/office-text.js +224 -0
  181. package/src/host/office-worker.js +8 -0
  182. package/src/host/rules-file.js +20 -0
  183. package/src/host/secrets-file.js +91 -0
  184. package/src/host/systemd.js +90 -0
  185. package/src/hub.js +245 -0
  186. package/src/identity.js +316 -0
  187. package/src/labels.js +5 -0
  188. package/src/mail-port.d.ts +79 -0
  189. package/src/mail-port.js +148 -0
  190. package/src/mcp-server.js +747 -0
  191. package/src/microsoft-app.js +20 -0
  192. package/src/microsoft-signin.js +194 -0
  193. package/src/migrate.js +327 -0
  194. package/src/notes-html.js +450 -0
  195. package/src/notes.js +19 -0
  196. package/src/notify.js +105 -0
  197. package/src/onepassword.js +223 -0
  198. package/src/op-registry.d.ts +14 -0
  199. package/src/op-registry.js +150 -0
  200. package/src/ops.js +1251 -0
  201. package/src/otp.js +6 -0
  202. package/src/panel-ops.js +273 -0
  203. package/src/pictures.js +735 -0
  204. package/src/postinstall.js +25 -0
  205. package/src/relay-passkey.js +122 -0
  206. package/src/relay-request.js +155 -0
  207. package/src/relay-seal.js +56 -0
  208. package/src/relay-settings.js +139 -0
  209. package/src/relay.js +876 -0
  210. package/src/render.js +82 -0
  211. package/src/rules-store.js +8 -0
  212. package/src/safe-fetch.js +215 -0
  213. package/src/sender-auth.js +270 -0
  214. package/src/site-match.js +77 -0
  215. package/src/tui-agenda.js +266 -0
  216. package/src/tui.js +670 -0
  217. package/src/vmc.js +376 -0
  218. package/src/wake.js +96 -0
  219. package/src/web-access.js +185 -0
  220. package/src/web-server.js +241 -0
  221. package/src/web-service.js +152 -0
@@ -0,0 +1,320 @@
1
+ // Where each account's password comes from: its "credentials" in config.json.
2
+ //
3
+ // { "source": "keychain" } the Keychain entry is the password (the default)
4
+ // { "source": "1password", the 1Password app's CLI integration
5
+ // "password": "op://<vaultId>/<itemId>/password",
6
+ // "username": "op://<vaultId>/<itemId>/username", (optional)
7
+ // "label": { "item": "Mail Work", "vault": "Private" } } (what to show; not used to read)
8
+ // { "source": "1password-service", … } the same, through a 1Password service
9
+ // account (its token: `sideora op-token set`)
10
+ // { "source": "env", "env": "MAIL_PASSWORD" } the process's environment (servers)
11
+ //
12
+ // A 1Password password is cached in the Keychain (service sideora,
13
+ // account = the account's name) for passwordMaxAgeDays, read again after an
14
+ // auth failure (forceRefresh), and the cached one is used when 1Password
15
+ // cannot be reached. When it was last read, and whether the last read failed,
16
+ // is noted in state.json (creds.<account>).
17
+ //
18
+ // An account of the older shape ("onePassword": { "item", "vault" }) is read
19
+ // by the item's name, then rewritten once to references by IDs, which survive
20
+ // a rename: its Keychain entry is left as it is.
21
+ //
22
+ // The username: the `username` reference when there is one, else the
23
+ // account's own username or email; "DOMAIN\user" is split for NTLM.
24
+
25
+ import {
26
+ DEFAULT_ACCOUNT, CONFIG_PATH, KEYCHAIN_SERVICE, getAccount, loadState, saveState, keychainGet, keychainSet,
27
+ keychainRead, keychainWrite, keychainDelete, updateConfig, credentialsOf, isOnePassword, CREDENTIAL_SOURCES
28
+ } from './config.js';
29
+ import crypto from 'node:crypto';
30
+ import { opRead, getItem, itemRefs, parseRef, hasField, OpError, OP_FIX, OP_TRANSIENT } from './onepassword.js';
31
+
32
+ // the 1Password service account token: one Keychain entry of its own
33
+ export const OP_TOKEN_SERVICE = 'sideora-1password';
34
+ export const OP_TOKEN_ACCOUNT = 'service-account-token';
35
+
36
+ export const serviceTokenStored = () => keychainRead(OP_TOKEN_SERVICE, OP_TOKEN_ACCOUNT) !== null;
37
+ /** Store the token (a service account's starts "ops_"). It is never printed or logged. */
38
+ export function setServiceToken(token) {
39
+ const t = String(token || '').trim();
40
+ if (!t) throw new Error('no token was given');
41
+ if (/\s/.test(t)) throw new Error('a service account token is one word (it starts with "ops_")');
42
+ keychainWrite(OP_TOKEN_SERVICE, OP_TOKEN_ACCOUNT, t);
43
+ }
44
+ export const clearServiceToken = () => keychainDelete(OP_TOKEN_SERVICE, OP_TOKEN_ACCOUNT);
45
+
46
+ /** The token for a 1password-service read; throws (NO_TOKEN) when none is stored. */
47
+ export function serviceToken() {
48
+ const t = keychainRead(OP_TOKEN_SERVICE, OP_TOKEN_ACCOUNT);
49
+ if (!t) throw new OpError('NO_TOKEN', `no 1Password service account token is stored — ${OP_FIX.NO_TOKEN}`);
50
+ return t.trim();
51
+ }
52
+
53
+ /** The reasons that mean the access was taken away: a cached password is dropped, not used. */
54
+ export const OP_PERMANENT = ['NOT_FOUND', 'TOKEN', 'NO_TOKEN', 'BAD_REF'];
55
+
56
+ export const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
57
+
58
+ /** Why a 1Password read failed, in a few words, by OpError code. */
59
+ export const OP_WHY = {
60
+ LOCKED: '1Password is locked, or the request was not allowed',
61
+ UNAVAILABLE: '1Password could not be reached: the app is not running, or Integrate with 1Password CLI is off',
62
+ NOT_FOUND: 'the item, its vault or its field is gone',
63
+ NO_TOKEN: 'no service account token is stored',
64
+ TOKEN: 'the service account token was refused',
65
+ NOT_INSTALLED: 'the 1Password CLI is not installed',
66
+ TIMEOUT: '1Password did not answer',
67
+ BAD_REF: 'its secret reference is not one'
68
+ };
69
+
70
+ // --- what is noted of each account's reads (state.json creds.<account>) ---------
71
+
72
+ /** The legacy top-level credCachedAt/credUsername/credDomain belong to "default". */
73
+ export function credState(state, account) {
74
+ if (state.creds && Object.hasOwn(state.creds, account)) return state.creds[account] || {};
75
+ if (account === DEFAULT_ACCOUNT && state.credCachedAt) {
76
+ return { cachedAt: state.credCachedAt, username: state.credUsername, domain: state.credDomain };
77
+ }
78
+ return {};
79
+ }
80
+
81
+ /** Change what is noted of an account's reads: `patch` merged in (a key set to undefined is taken out). */
82
+ export function noteCredState(account, patch) {
83
+ const state = loadState();
84
+ const now = { ...credState(state, account), ...patch };
85
+ for (const k of Object.keys(now)) if (now[k] === undefined) delete now[k];
86
+ const { credCachedAt, credUsername, credDomain, ...rest } = state;
87
+ saveState({ ...rest, creds: { ...(state.creds || {}), [account]: now } });
88
+ }
89
+
90
+ /** "CHW\user" carries its own domain: split it, or NTLM gets the domain twice and Exchange answers 401. */
91
+ export function splitDomain(creds) {
92
+ const u = String(creds.username || '');
93
+ if (u.includes('\\')) [creds.domain, creds.username] = u.split('\\');
94
+ return creds;
95
+ }
96
+
97
+ // --- reading --------------------------------------------------------------------
98
+
99
+ /**
100
+ * { username, domain, password } for an account (a name, or the account
101
+ * itself, as mailcore passes it: "<name>/calendar" for an IMAP mailbox's own
102
+ * calendar). forceRefresh: a 1Password source is read again, not the cache.
103
+ */
104
+ export async function getCredentials(config, account, forceRefresh = false) {
105
+ if (typeof account === 'boolean') { forceRefresh = account; account = undefined; } // legacy signature
106
+ const acct = typeof account === 'object' && account ? account : getAccount(config, account);
107
+ const name = acct.name || DEFAULT_ACCOUNT;
108
+ const src = credentialsOf(acct);
109
+ const own = () => splitDomain({ username: acct.username || acct.email || '', domain: acct.domain || '' });
110
+
111
+ switch (src.source) {
112
+ case 'keychain': {
113
+ const cached = keychainGet(name);
114
+ if (cached) return { ...own(), password: cached };
115
+ throw new Error(
116
+ `No password for account "${name}" in the Keychain.\nStore one (sideora setup ${name.split('/')[0]}), take it from 1Password (credentials.source "1password" in ${CONFIG_PATH}), or by hand:\n` +
117
+ ` security add-generic-password -U -s ${KEYCHAIN_SERVICE} -a ${name} -w (a bare -w asks for it: never on the command line)`
118
+ );
119
+ }
120
+ case 'env': {
121
+ if (!ENV_NAME.test(String(src.env || ''))) throw new Error(`account "${name}": credentials.env names the environment variable that holds the password`);
122
+ const v = process.env[src.env];
123
+ if (!v) throw new Error(`account "${name}": the environment variable ${src.env} is not set (it holds the password)`);
124
+ return { ...own(), password: v };
125
+ }
126
+ case '1password':
127
+ case '1password-service':
128
+ return fromOnePassword(config, acct, name, src, forceRefresh);
129
+ case 'microsoft':
130
+ // signed in with Microsoft: no password, a token asked for at each connection (src/microsoft-signin.js)
131
+ return (await import('./microsoft-signin.js')).microsoftCredentials({ ...acct, name }, { force: forceRefresh });
132
+ default:
133
+ throw new Error(`account "${name}": credentials.source "${src.source}" is not one of ${CREDENTIAL_SOURCES.join(', ')}`);
134
+ }
135
+ }
136
+
137
+ /**
138
+ * What a cached password is for: its source and references, and where it
139
+ * signs in, as whom. A cache noted under another identity (the reference or
140
+ * the server changed since) is not this account's: it is neither reused nor
141
+ * fallen back on. A cache noted before identities were (no `id`) is taken as
142
+ * this account's until its next read notes one.
143
+ */
144
+ export function credIdentity(acct, src = credentialsOf(acct)) {
145
+ const where = acct.provider === 'imap' ? [acct.imap?.host, acct.imap?.port, acct.smtp?.host, acct.smtp?.port]
146
+ : acct.provider === 'caldav' ? [acct.server || acct.service] : acct.provider === 'jmap' ? [acct.jmap?.session] : [acct.host];
147
+ const what = [src.source, src.password || '', src.username || '', src.item || '', src.vault || '', acct.provider || '', ...where.map((v) => String(v ?? '')), acct.username || acct.email || ''];
148
+ return crypto.createHash('sha256').update(JSON.stringify(what)).digest('hex').slice(0, 32);
149
+ }
150
+
151
+ /** Forget what was noted of an account's reads and drop its cached password: its credentials changed. */
152
+ export function forgetCachedPassword(name) {
153
+ try {
154
+ const state = loadState();
155
+ if (state.creds && Object.hasOwn(state.creds, name)) { delete state.creds[name]; saveState(state); }
156
+ } catch { /* no state yet */ }
157
+ try { keychainDelete(KEYCHAIN_SERVICE, name); } catch { /* said by the next read */ }
158
+ }
159
+
160
+ async function fromOnePassword(config, acct, name, src, forceRefresh) {
161
+ const meta = credState(loadState(), name);
162
+ const identity = credIdentity(acct, src);
163
+ // a cache is this account's when it was noted for these credentials (the same identity); one noted
164
+ // before identities were (cachedAt, no id) is taken as this one's, and bound to it when first used;
165
+ // a Keychain entry with no note at all, or one invalidated (the item gone, the token refused), is not
166
+ const ours = !meta.invalid && (meta.id ? meta.id === identity : !!meta.cachedAt);
167
+ const bind = () => { if (!meta.id) { try { noteCredState(name, { id: identity }); } catch { /* bound at the next read */ } } };
168
+ const maxAgeMs = (config?.passwordMaxAgeDays || 30) * 86400e3;
169
+ const cacheFresh = ours && meta.cachedAt && Date.now() - meta.cachedAt < maxAgeMs;
170
+ const fromCache = (password) => splitDomain({
171
+ username: meta.username || acct.username || acct.email || '',
172
+ domain: meta.domain ?? acct.domain ?? '',
173
+ password
174
+ });
175
+
176
+ if (!forceRefresh && cacheFresh) {
177
+ const cached = keychainGet(name);
178
+ if (cached) { bind(); return fromCache(cached); }
179
+ }
180
+
181
+ let read;
182
+ try {
183
+ read = await readOnePassword(acct, name, src);
184
+ } catch (e) {
185
+ const code = e?.code || 'ERROR';
186
+ if (OP_PERMANENT.includes(code)) {
187
+ // the access was taken away (item gone, token refused or missing, no reference): the cached
188
+ // password is not this account's any more, for the fresh path and the fallback alike, until a read works
189
+ try { noteCredState(name, { failedAt: Date.now(), failure: code, invalid: true, cachedAt: undefined, id: undefined }); } catch { /* below */ }
190
+ try { keychainDelete(KEYCHAIN_SERVICE, name); } catch { console.error(`[sideora] the password cached for "${name}" could not be removed from the Keychain`); }
191
+ throw e;
192
+ }
193
+ try { noteCredState(name, { failedAt: Date.now(), failure: code }); } catch { /* the read's own error is what matters */ }
194
+ // 1Password out of reach for now (locked, integration off, launchd context): the cached password stands in.
195
+ // Never for an item gone or a token refused (the access was taken away), never for another
196
+ // account's cache, and never right after a sign-in was refused (forceRefresh: that password would
197
+ // be sent again, and an Exchange account locks after a few)
198
+ const cached = !forceRefresh && ours && OP_TRANSIENT.includes(code) ? keychainGet(name) : null;
199
+ if (cached) {
200
+ console.error(`[sideora] 1Password could not be read for "${name}" (${code}) — continuing with the cached Keychain password`);
201
+ bind();
202
+ return fromCache(cached);
203
+ }
204
+ throw e;
205
+ }
206
+ const creds = splitDomain({ username: read.username || acct.username || acct.email || '', domain: acct.domain || '', password: read.password });
207
+ // the read worked: a cache that cannot be written is said, and the read stands
208
+ try {
209
+ keychainSet(name, creds.password);
210
+ noteCredState(name, { cachedAt: Date.now(), username: creds.username, domain: creds.domain, id: read.identity || identity, failedAt: undefined, failure: undefined, invalid: undefined });
211
+ } catch {
212
+ console.error(`[sideora] the password of "${name}" was read from 1Password but could not be cached in the Keychain`);
213
+ }
214
+ return creds;
215
+ }
216
+
217
+ /** The password (and username, when a reference names one) of a 1Password source. */
218
+ async function readOnePassword(acct, name, src) {
219
+ const token = src.source === '1password-service' ? serviceToken() : null;
220
+ if (src.password) {
221
+ const password = await opRead(src.password, { token });
222
+ if (!password) throw new OpError('NOT_FOUND', `1Password: the password of account "${name}" is empty — ${OP_FIX.NOT_FOUND}`);
223
+ const username = src.username ? await opRead(src.username, { token }) : '';
224
+ return { password, username };
225
+ }
226
+ if (src.item) {
227
+ // the older shape: the item by its name, then references by IDs from it, once
228
+ const item = await getItem(src.item, { vault: src.vault || null, token });
229
+ const refs = itemRefs(item, { username: !acct.username }); // an account's own username was always the one used
230
+ const password = await opRead(refs.password, { token });
231
+ if (!password) throw new OpError('NOT_FOUND', `1Password item "${item.title}" has no password — ${OP_FIX.NOT_FOUND}`);
232
+ const username = refs.username ? await opRead(refs.username, { token }) : '';
233
+ const now = { source: src.source, ...refs, label: { item: item.title, vault: item.vault.name } };
234
+ // the cache is noted for the references it is read by from now on (or still by the item's name, when not rewritten)
235
+ const identity = migrate(name, src, now) ? credIdentity(acct, now) : credIdentity(acct, src);
236
+ return { password, username, identity };
237
+ }
238
+ throw new Error(`account "${name}": a ${src.source} source needs "password": a secret reference (op://<vault>/<item>/password)`);
239
+ }
240
+
241
+ /**
242
+ * Rewrite an account of the older shape to its references, once: only while
243
+ * it still names that item (a change made meanwhile wins). Its Keychain entry
244
+ * is not touched. A failure is said, not thrown: the password was read.
245
+ */
246
+ function migrate(name, was, credentials) {
247
+ let done = false;
248
+ try {
249
+ updateConfig((raw) => {
250
+ const [base, part, ...more] = name.split('/');
251
+ const acct = raw.accounts && Object.hasOwn(raw.accounts, base) ? raw.accounts[base] : null;
252
+ const target = more.length ? null : part === undefined ? acct : part === 'calendar' ? acct?.calendar : null;
253
+ if (!target || typeof target !== 'object') return false;
254
+ const now = credentialsOf(target);
255
+ if (now.source !== was.source || now.password || now.item !== was.item || (now.vault || null) !== (was.vault || null)) return false;
256
+ delete target.onePassword;
257
+ target.credentials = credentials;
258
+ done = true;
259
+ });
260
+ } catch {
261
+ done = false;
262
+ console.error(`[sideora] account "${name}": its 1Password item could not be rewritten as a reference (the settings could not be written); it is read by name until then`);
263
+ }
264
+ return done;
265
+ }
266
+
267
+ /**
268
+ * What the panel and `status` show of a 1Password account: the item and vault
269
+ * (names), whether it is a service account's, when it was last read and
270
+ * whether the last read failed. No secret, no reference. null for another source.
271
+ */
272
+ export function onePasswordStatus(acct, state = loadState()) {
273
+ const src = credentialsOf(acct);
274
+ if (!isOnePassword(src)) return null;
275
+ const meta = credState(state, acct.name || DEFAULT_ACCOUNT);
276
+ const label = src.label && typeof src.label === 'object' ? src.label : {};
277
+ const out = {
278
+ item: String(label.item || src.item || '').slice(0, 100),
279
+ vault: String(label.vault || src.vault || '').slice(0, 100),
280
+ ...(src.source === '1password-service' ? { service: true } : {}),
281
+ ...(meta.cachedAt ? { readAt: new Date(meta.cachedAt).toISOString() } : {})
282
+ };
283
+ if (meta.failedAt && (!meta.cachedAt || meta.failedAt > meta.cachedAt)) {
284
+ out.failed = true;
285
+ out.failedAt = new Date(meta.failedAt).toISOString();
286
+ out.why = OP_WHY[meta.failure] || 'it could not be read';
287
+ }
288
+ return out;
289
+ }
290
+
291
+ /**
292
+ * Whether a 1Password account's references still name an item and its fields
293
+ * (looked up, never revealed: no secret is read). { ok: true, item, vault,
294
+ * legacy? } or { ok: false, code, why, fix }.
295
+ */
296
+ export async function checkOnePassword(acct) {
297
+ const src = credentialsOf(acct);
298
+ try {
299
+ const token = src.source === '1password-service' ? serviceToken() : null;
300
+ if (src.password) {
301
+ const refs = [src.password, ...(src.username ? [src.username] : [])].map(parseRef);
302
+ let item = null;
303
+ for (const r of refs) {
304
+ const it = await getItem(r.item, { vault: r.vault, token });
305
+ if (!hasField(it, r.field)) throw new OpError('NOT_FOUND', `the item "${it.title}" has no field "${r.field}"`);
306
+ item ??= it;
307
+ }
308
+ return { ok: true, item: item.title, vault: item.vault.name };
309
+ }
310
+ if (src.item) {
311
+ const item = await getItem(src.item, { vault: src.vault || null, token });
312
+ itemRefs(item);
313
+ return { ok: true, item: item.title, vault: item.vault.name, legacy: true };
314
+ }
315
+ return { ok: false, code: 'BAD_REF', why: 'no secret reference is set ("password": "op://…")', fix: `sideora setup ${String(acct.name || '').split('/')[0]}` };
316
+ } catch (e) {
317
+ const code = e?.code || 'ERROR';
318
+ return { ok: false, code, why: OP_WHY[code] || 'it could not be read', fix: OP_FIX[code] || OP_FIX.ERROR };
319
+ }
320
+ }