emailengine-app 2.78.0 → 2.79.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/.ncurc.js +23 -13
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +29 -0
  4. package/config/e2e.toml +2 -1
  5. package/config/test.toml +2 -1
  6. package/data/google-crawlers.json +1 -1
  7. package/lib/account.js +70 -35
  8. package/lib/api-reference/format.js +91 -4
  9. package/lib/api-reference/model.js +144 -19
  10. package/lib/api-reference/schema-tree.js +282 -28
  11. package/lib/api-routes/account-routes.js +80 -34
  12. package/lib/api-routes/blocklist-routes.js +21 -47
  13. package/lib/api-routes/chat-routes.js +10 -12
  14. package/lib/api-routes/delivery-test-routes.js +6 -26
  15. package/lib/api-routes/export-routes.js +2 -0
  16. package/lib/api-routes/gateway-routes.js +52 -11
  17. package/lib/api-routes/license-routes.js +2 -0
  18. package/lib/api-routes/mailbox-routes.js +29 -26
  19. package/lib/api-routes/message-routes.js +27 -21
  20. package/lib/api-routes/oauth2-app-routes.js +11 -29
  21. package/lib/api-routes/operation-impact.js +80 -0
  22. package/lib/api-routes/outbox-routes.js +15 -5
  23. package/lib/api-routes/permission-map.js +397 -0
  24. package/lib/api-routes/route-helpers.js +223 -9
  25. package/lib/api-routes/route-metadata.js +28 -0
  26. package/lib/api-routes/settings-routes.js +30 -25
  27. package/lib/api-routes/stats-routes.js +2 -2
  28. package/lib/api-routes/submit-routes.js +53 -31
  29. package/lib/api-routes/template-routes.js +3 -0
  30. package/lib/api-routes/token-routes.js +319 -33
  31. package/lib/api-routes/webhook-route-routes.js +14 -4
  32. package/lib/auth-token.js +80 -8
  33. package/lib/consts.js +10 -0
  34. package/lib/db.js +20 -2
  35. package/lib/delivery-error.js +30 -1
  36. package/lib/email-client/base-client.js +27 -41
  37. package/lib/email-client/gmail-client.js +19 -10
  38. package/lib/email-client/imap/mailbox.js +23 -24
  39. package/lib/email-client/imap/subconnection.js +10 -7
  40. package/lib/email-client/imap/sync-operations.js +46 -46
  41. package/lib/email-client/imap-client.js +36 -26
  42. package/lib/email-client/message-builder.js +3 -3
  43. package/lib/email-client/notification-handler.js +1 -1
  44. package/lib/email-client/outlook/graph-api.js +10 -4
  45. package/lib/email-client/outlook-client.js +53 -54
  46. package/lib/email-client/smtp-pool-manager.js +1 -1
  47. package/lib/enum-descriptions.js +92 -2
  48. package/lib/export.js +1 -1
  49. package/lib/field-usage.js +133 -0
  50. package/lib/gateway.js +1 -1
  51. package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +3 -3
  52. package/lib/imapproxy/imap-core/lib/commands/id.js +2 -13
  53. package/lib/imapproxy/imap-core/lib/commands/starttls.js +1 -1
  54. package/lib/imapproxy/imap-core/lib/imap-command.js +21 -5
  55. package/lib/imapproxy/imap-core/lib/imap-connection.js +15 -5
  56. package/lib/imapproxy/imap-server.js +27 -7
  57. package/lib/license-beacon.js +4 -9
  58. package/lib/lists.js +49 -1
  59. package/lib/llm-pre-process.js +5 -5
  60. package/lib/logger.js +5 -1
  61. package/lib/metrics-collector.js +2 -2
  62. package/lib/oauth/gmail.js +9 -12
  63. package/lib/oauth/mail-ru.js +5 -7
  64. package/lib/oauth/outlook.js +9 -9
  65. package/lib/oauth/pubsub/google.js +25 -10
  66. package/lib/oauth/scope-checker.js +1 -1
  67. package/lib/oauth2-apps.js +223 -294
  68. package/lib/openapi/build-document.js +15 -7
  69. package/lib/openapi/joi-schema.js +20 -0
  70. package/lib/outbox.js +85 -17
  71. package/lib/pre-process.js +8 -8
  72. package/lib/queue-stats.js +49 -0
  73. package/lib/read-env-value.js +1 -1
  74. package/lib/reconnection-manager.js +2 -2
  75. package/lib/rewrite-text-nodes.js +4 -3
  76. package/lib/routes-ui.js +4 -0
  77. package/lib/schemas.js +216 -160
  78. package/lib/settings.js +84 -5
  79. package/lib/sub-script.js +2 -1
  80. package/lib/swagger-options.js +2 -1
  81. package/lib/templates.js +4 -4
  82. package/lib/threads.js +2 -2
  83. package/lib/token-audit-log.js +208 -0
  84. package/lib/token-permission-view.js +219 -0
  85. package/lib/token-permissions.js +172 -0
  86. package/lib/tokens.js +99 -15
  87. package/lib/tools.js +142 -37
  88. package/lib/ui-routes/account-routes.js +31 -11
  89. package/lib/ui-routes/admin-config-routes.js +18 -10
  90. package/lib/ui-routes/admin-entities-routes.js +66 -22
  91. package/lib/ui-routes/auth-routes.js +30 -24
  92. package/lib/ui-routes/export-routes.js +10 -4
  93. package/lib/ui-routes/oauth-config-routes.js +5 -5
  94. package/lib/ui-routes/reference-routes.js +3 -3
  95. package/lib/ui-routes/route-helpers.js +51 -2
  96. package/lib/ui-routes/settings-page.js +1 -1
  97. package/lib/ui-routes/smtp-test-routes.js +1 -1
  98. package/lib/ui-routes/suppression-list-routes.js +294 -0
  99. package/lib/ui-routes/unsubscribe-routes.js +19 -29
  100. package/lib/utils/network.js +2 -2
  101. package/lib/web-safe-html.js +1 -1
  102. package/lib/webhook-routing.js +80 -0
  103. package/lib/webhooks.js +20 -14
  104. package/package.json +11 -10
  105. package/sbom.json +1 -1
  106. package/server.js +80 -41
  107. package/static/css/flyonui.css +1 -1
  108. package/static/css/src/app.tailwind.css +136 -9
  109. package/static/js/reference.js +456 -10
  110. package/static/js/ui.js +43 -0
  111. package/static/licenses.html +51 -41
  112. package/translations/messages.pot +59 -59
  113. package/views/accounts/account.hbs +69 -7
  114. package/views/accounts/browse.hbs +18 -35
  115. package/views/config/logging.hbs +6 -0
  116. package/views/config/oauth/app.hbs +0 -2
  117. package/views/config/security.hbs +13 -0
  118. package/views/partials/oauth_form.hbs +0 -15
  119. package/views/partials/reference/disclosure.hbs +8 -2
  120. package/views/partials/reference/operation.hbs +47 -13
  121. package/views/partials/reference/params.hbs +17 -1
  122. package/views/partials/reference/schema-node.hbs +110 -52
  123. package/views/partials/reference/schema-tools.hbs +25 -0
  124. package/views/partials/reference/try-it.hbs +84 -11
  125. package/views/partials/side_menu.hbs +1 -0
  126. package/views/partials/ui/checkbox.hbs +2 -2
  127. package/views/partials/ui/tabs.hbs +7 -1
  128. package/views/reference/token.hbs +4 -0
  129. package/views/suppression-lists/index.hbs +85 -0
  130. package/views/suppression-lists/list.hbs +87 -0
  131. package/views/templates/index.hbs +8 -2
  132. package/views/tokens/index.hbs +31 -4
  133. package/views/tokens/new.hbs +324 -2
  134. package/workers/api.js +146 -57
  135. package/workers/documents.js +46 -26
  136. package/workers/export.js +20 -8
  137. package/workers/imap-proxy.js +2 -2
  138. package/workers/imap.js +12 -6
  139. package/workers/smtp.js +13 -7
  140. package/workers/submit.js +40 -30
  141. package/workers/webhooks.js +59 -25
  142. package/lib/feature-flags.js +0 -48
@@ -3,6 +3,22 @@
3
3
  {{!-- the account level renders only for account-scoped tokens (ui/breadcrumb skips empty levels) --}}
4
4
  {{> ui/breadcrumb parentHref="/admin/tokens" parentLabel="Access Tokens" midHref=(when account accountTokensLink) midLabel=account.name current="New" class="mt-6 mb-6"}}
5
5
 
6
+ {{!-- A token is never invalidated once minted, so one handed to a caller that reached this page
7
+ without authenticating would keep working - POST /admin/tokens/new refuses for that reason. The
8
+ form used to render anyway, so the only way to discover it was to fill it in and lose the work to
9
+ a 403. Same condition as the gate, so the two cannot disagree. --}}
10
+ {{#unless canProvision}}
11
+ {{#> ui/stat-card variant="error" title="Set an admin password first" icon="icon-[tabler--shield-lock]" class="mb-6"}}
12
+
13
+ <p class="mb-4">Access tokens do not expire on their own, so EmailEngine only issues one to a request
14
+ that carries an admin session. This instance has no admin password set, so there is no session to
15
+ carry - set one and the form appears here.</p>
16
+ <div>
17
+ {{> ui/btn href="/admin/account/password" variant="primary" icon="icon-[tabler--arrow-right]" label="Enable authentication"}}
18
+ </div>
19
+
20
+ {{/ui/stat-card}}
21
+ {{else}}
6
22
  <form method="post" id="token-form" action="/admin/tokens/new">
7
23
  <input type="hidden" name="crumb" value="{{crumb}}" />
8
24
  <input type="hidden" id="account" name="account" value="{{values.account}}" />
@@ -32,6 +48,99 @@
32
48
  <div class="text-base-content/60 code-link">["api"]</div>
33
49
  </li>
34
50
 
51
+ {{!-- Nested inside the API row, not stacked in a card of its own. As two sibling
52
+ cards the relationship had to be asserted in prose: a reader saw "Allowed
53
+ scopes" and "Permissions" and had no reason to think the second was a
54
+ subset of one row of the first. Here the indent says it. --}}
55
+ <li id="permissionRow" class="p-4 hidden">
56
+ <div class="border-s-2 border-base-content/10 ps-4 space-y-4">
57
+ <div>
58
+ <h6 class="mb-1 font-bold">Limit what the API token can do</h6>
59
+ <p class="text-base-content/60 text-sm mb-3">
60
+ By default this token can use the whole API. Restrict it to hand out a
61
+ credential for a single job: an agent that reads mail but cannot delete
62
+ it, or a service that can only send.
63
+ </p>
64
+
65
+ <div id="permissionToggle">
66
+ {{> ui/checkbox id="permissionsEnabled" label="Restrict what this token can do" checked=values.permissionsEnabled error=errors.permissions noName=true}}
67
+ </div>
68
+ </div>
69
+
70
+ <div id="permissionSection" class="space-y-5{{#unless values.permissionsEnabled}} hidden{{/unless}}">
71
+
72
+ <div>
73
+ <h6 class="mb-1 font-bold">Pick a preset</h6>
74
+ <p class="text-base-content/60 text-sm mb-2">
75
+ These cover the usual shapes. Adjust the details below if you need something else.
76
+ </p>
77
+ <div class="flex flex-wrap gap-2">
78
+ {{#each permissionPresets}}
79
+ <button type="button" class="btn btn-sm btn-soft permission-preset" data-actions="{{actionList}}"
80
+ data-groups="{{groupList}}" title="{{title}}">{{label}}</button>
81
+ {{/each}}
82
+ </div>
83
+ </div>
84
+
85
+ {{!-- The whole point of the two axes is what they produce together, and a reader should
86
+ not have to multiply four boxes by thirteen to find out. Saying what the token
87
+ CANNOT do matters as much: that is the reassurance someone wants when handing out
88
+ a restricted credential. --}}
89
+ <div id="permissionOutcome" class="rounded-box border border-base-content/10 bg-base-200/40 p-4 text-sm space-y-1"></div>
90
+
91
+ {{!-- Filled in by the script below whenever a scope or an axis changes. Holding a
92
+ scope the permissions make unusable is legitimate, so this warns rather than
93
+ refuses - but it should never be a surprise. What each surface requires is
94
+ handed over as data, derived from the tables that enforce it, so this cannot
95
+ describe rules that have since changed. --}}
96
+ <div id="permissionScopeWarning" class="alert alert-soft alert-warning text-sm hidden"
97
+ data-surfaces="{{permissionSurfacesJson}}"></div>
98
+
99
+ <div>
100
+ <h6 class="mb-2 font-bold">What it can do</h6>
101
+ <ul class="divide-y divide-base-content/10 rounded-box border border-base-content/10">
102
+ {{#each permissionActions}}
103
+ <li class="flex items-center gap-2 p-4">
104
+ {{> ui/checkbox id=inputId label=label checked=checked noName=true dataValue=value class="permission-axis permission-action"}}
105
+ <div class="text-base-content/60 code-link ms-auto self-start">["{{value}}"]</div>
106
+ </li>
107
+ {{/each}}
108
+ </ul>
109
+ </div>
110
+
111
+ <div>
112
+ <h6 class="mb-2 font-bold">What it can reach</h6>
113
+ <div class="space-y-3">
114
+ {{#each permissionGroupClusters}}
115
+ <div>
116
+ <div class="text-base-content/60 text-xs uppercase tracking-wide mb-1">{{label}}</div>
117
+ <ul class="divide-y divide-base-content/10 rounded-box border border-base-content/10">
118
+ {{#each groups}}
119
+ <li class="flex items-center gap-2 p-4">
120
+ {{> ui/checkbox id=inputId label=label checked=checked hint=description noName=true dataValue=value class="permission-axis permission-group"}}
121
+ <div class="text-base-content/60 code-link ms-auto self-start">["{{value}}"]</div>
122
+ </li>
123
+ {{/each}}
124
+ </ul>
125
+ </div>
126
+ {{/each}}
127
+ </div>
128
+
129
+ {{!-- Rendered as a visibly refused option rather than omitted: leaving it out
130
+ entirely reads as an oversight, and the reason is the point. --}}
131
+ <div class="mt-3 alert alert-soft alert-warning text-sm">
132
+ <span class="icon-[tabler--lock] size-5 shrink-0"></span>
133
+ <span>
134
+ Some endpoints are never available to a restricted token, whatever you select here:
135
+ settings, OAuth2 applications, licensing, access tokens and stored account
136
+ credentials. A token that could reach those could grant itself more.
137
+ </span>
138
+ </div>
139
+ </div>
140
+ </div>
141
+ </div>
142
+ </li>
143
+
35
144
  <li class="flex items-center gap-2 p-4">
36
145
  <input type="checkbox" class="checkbox checkbox-sm scope-entry scope-list" id="scopesSMTP"
37
146
  name="scopesSMTP" data-scope="smtp" {{#if values.scopesSMTP}}checked{{/if}} />
@@ -63,7 +172,9 @@
63
172
  </div>
64
173
  </form>
65
174
 
66
- {{#> ui/modal id="showToken" title="Access tokens" static=true}}
175
+ {{!-- Wider than the default: the token is 64 characters and this is the only time it is shown, so
176
+ it has to be readable in full rather than scrolled through a 32-character window. --}}
177
+ {{#> ui/modal id="showToken" title="Access tokens" static=true dialogClass="max-w-2xl"}}
67
178
  <div class="modal-body">
68
179
  <div id="showTokenLoader" class="py-2 text-center">
69
180
  <span class="loading loading-spinner loading-sm align-middle"></span> Generating token&hellip;
@@ -71,7 +182,11 @@
71
182
  <div id="showTokenContent" class="hidden">
72
183
 
73
184
  <div class="mb-2">Access token:</div>
74
- {{> ui/copy-field id="showTokenValue" ariaLabel="Access token" join=true inputClass="font-mono bg-base-200 border-0 autoselect"}}
185
+ {{!-- w-full because ui/copy-field's join wrapper is inline-flex, so the `grow` on the input
186
+ has nothing to grow inside and the field kept its intrinsic width. text-sm so all 64
187
+ characters fit: this is the only time the value is ever shown, and a token truncated at
188
+ 32 characters cannot be checked against what was copied. --}}
189
+ {{> ui/copy-field id="showTokenValue" ariaLabel="Access token" join=true class="w-full" inputClass="font-mono text-sm bg-base-200 border-0 autoselect"}}
75
190
 
76
191
  </div>
77
192
  <div id="showTokenError" class="hidden alert alert-soft alert-error whitespace-pre-line"></div>
@@ -86,12 +201,193 @@
86
201
 
87
202
  let showTokenValueElm = document.getElementById('showTokenValue');
88
203
 
204
+ // The permission axes only mean anything while the restriction is on, so the whole block is
205
+ // hidden until then rather than shown greyed out - an axis you can read but not use invites
206
+ // the reading that it applies.
207
+ let permissionsEnabledElm = document.getElementById('permissionsEnabled');
208
+ let permissionSectionElm = document.getElementById('permissionSection');
209
+ let permissionRowElm = document.getElementById('permissionRow');
210
+
211
+ // Permissions are resolved per endpoint on the REST API. Without API access there is no
212
+ // setting here that helps: the remaining scopes are checked once against everything their
213
+ // surface can do, so any restriction either changes nothing or leaves the token unable to do
214
+ // the one thing it was issued for. Scopes are fixed once the token exists, so the row is
215
+ // absent rather than disabled - and its absence needs no caption, because a section that
216
+ // only appears under API is self-evidently about API.
217
+ //
218
+ // "All scopes" only disables the individual boxes, it does not tick them, so the API row can
219
+ // be greyed while this is open. That reads correctly: API access is granted by "All", and
220
+ // this is how to narrow it.
221
+ let hasApiScope = () =>
222
+ Array.from(document.querySelectorAll('.scope-entry')).some(
223
+ elm => elm.checked && !elm.disabled && (elm.dataset.scope === '*' || elm.dataset.scope === 'api')
224
+ );
225
+
226
+ let syncPermissionSection = () => {
227
+ let apiScope = hasApiScope();
228
+
229
+ permissionRowElm.classList.toggle('hidden', !apiScope);
230
+ permissionSectionElm.classList.toggle('hidden', !apiScope || !permissionsEnabledElm.checked);
231
+ };
232
+
233
+ permissionsEnabledElm.addEventListener('change', () => {
234
+ // Enabling with nothing ticked would post two empty allowlists, which grant nothing. Start
235
+ // from everything selected so the first state is "no narrowing yet" and each untick is a
236
+ // deliberate subtraction.
237
+ if (permissionsEnabledElm.checked && !document.querySelector('.permission-axis:checked')) {
238
+ for (let elm of document.querySelectorAll('.permission-axis')) {
239
+ elm.checked = true;
240
+ }
241
+ }
242
+ syncPermissionSection();
243
+ });
244
+
245
+ syncPermissionSection();
246
+
247
+ let permissionWarningElm = document.getElementById('permissionScopeWarning');
248
+
249
+ // Derived server-side from SCOPE_OPERATIONS and the route map rather than restated here, so a
250
+ // change to what a surface requires cannot leave this warning describing the old rules.
251
+ let surfaceRequirements = {};
252
+ for (let entry of JSON.parse(permissionWarningElm.dataset.surfaces || '[]')) {
253
+ surfaceRequirements[entry.scope] = {
254
+ label: entry.label,
255
+ actions: entry.actionList.split(',').filter(value => value),
256
+ groups: entry.groupList.split(',').filter(value => value)
257
+ };
258
+ }
259
+ let permissionOutcomeElm = document.getElementById('permissionOutcome');
260
+
261
+ let tickedLabels = selector =>
262
+ Array.from(document.querySelectorAll(selector))
263
+ .filter(elm => elm.checked)
264
+ .map(elm => document.querySelector('label[for="' + elm.id + '"]').textContent.trim());
265
+
266
+ let untickedLabels = selector =>
267
+ Array.from(document.querySelectorAll(selector))
268
+ .filter(elm => !elm.checked)
269
+ .map(elm => document.querySelector('label[for="' + elm.id + '"]').textContent.trim());
270
+
271
+ let asList = values => (values.length > 1 ? values.slice(0, -1).join(', ') + ' and ' + values[values.length - 1] : values[0] || '');
272
+
273
+ // Says what the two axes produce together. Without it a reader has to cross four boxes with
274
+ // thirteen to work out what they just built - and the half that reassures, what the token
275
+ // CANNOT do, is not visible anywhere at all.
276
+ let syncPermissionOutcome = () => {
277
+ let actions = tickedLabels('.permission-action');
278
+ let groups = tickedLabels('.permission-group');
279
+ let deniedActions = untickedLabels('.permission-action');
280
+
281
+ permissionOutcomeElm.replaceChildren();
282
+
283
+ if (!actions.length || !groups.length) {
284
+ let line = document.createElement('div');
285
+ line.className = 'text-warning';
286
+ line.textContent = 'Nothing is selected yet, so this token would not be able to do anything.';
287
+ permissionOutcomeElm.append(line);
288
+ return;
289
+ }
290
+
291
+ let can = document.createElement('div');
292
+ let canLabel = document.createElement('strong');
293
+ canLabel.textContent = 'This token can ';
294
+ can.append(canLabel, document.createTextNode(asList(actions).toLowerCase() + ' in ' + asList(groups) + '.'));
295
+ permissionOutcomeElm.append(can);
296
+
297
+ if (deniedActions.length) {
298
+ let cannot = document.createElement('div');
299
+ cannot.className = 'text-base-content/60';
300
+ cannot.textContent = 'It cannot ' + asList(deniedActions).toLowerCase() + ', anywhere.';
301
+ permissionOutcomeElm.append(cannot);
302
+ }
303
+
304
+ let rest = document.createElement('div');
305
+ rest.className = 'text-base-content/60';
306
+ rest.textContent = 'It cannot reach anything else, or read credentials and settings.';
307
+ permissionOutcomeElm.append(rest);
308
+ };
309
+
310
+ let syncPermissionWarning = () => {
311
+ if (!permissionsEnabledElm.checked || !hasApiScope()) {
312
+ permissionWarningElm.classList.add('hidden');
313
+ return;
314
+ }
315
+
316
+ let selectedScopes = Array.from(document.querySelectorAll('.scope-entry'))
317
+ .filter(elm => elm.checked && !elm.disabled)
318
+ .map(elm => elm.dataset.scope);
319
+
320
+ // "All scopes" carries every surface with it
321
+ if (selectedScopes.includes('*')) {
322
+ selectedScopes = Object.keys(surfaceRequirements);
323
+ }
324
+
325
+ let ticked = selector => Array.from(document.querySelectorAll(selector)).filter(elm => elm.checked).map(elm => elm.dataset.value);
326
+ let actions = ticked('.permission-action');
327
+ let groups = ticked('.permission-group');
328
+
329
+ let blocked = selectedScopes
330
+ .filter(scope => surfaceRequirements[scope])
331
+ .filter(scope => {
332
+ let need = surfaceRequirements[scope];
333
+ return !need.actions.every(value => actions.includes(value)) || !need.groups.every(value => groups.includes(value));
334
+ })
335
+ .map(scope => surfaceRequirements[scope].label);
336
+
337
+ if (!blocked.length) {
338
+ permissionWarningElm.classList.add('hidden');
339
+ return;
340
+ }
341
+
342
+ permissionWarningElm.textContent =
343
+ 'These permissions stop the token doing something its scopes allow: it will not be able to ' +
344
+ blocked.join(', or ') +
345
+ '. Untick that scope, or allow what it needs.';
346
+ permissionWarningElm.classList.remove('hidden');
347
+ };
348
+
349
+ for (let elm of document.querySelectorAll('.permission-axis, .scope-entry')) {
350
+ elm.addEventListener('change', () => {
351
+ syncPermissionSection();
352
+ syncPermissionWarning();
353
+ syncPermissionOutcome();
354
+ });
355
+ }
356
+ permissionsEnabledElm.addEventListener('change', () => {
357
+ syncPermissionWarning();
358
+ syncPermissionOutcome();
359
+ });
360
+ syncPermissionWarning();
361
+ syncPermissionOutcome();
362
+
363
+ for (let button of document.querySelectorAll('.permission-preset')) {
364
+ button.addEventListener('click', () => {
365
+ let actions = (button.dataset.actions || '').split(',').filter(value => value);
366
+ let groups = (button.dataset.groups || '').split(',').filter(value => value);
367
+
368
+ for (let elm of document.querySelectorAll('.permission-action')) {
369
+ elm.checked = actions.includes(elm.dataset.value);
370
+ }
371
+ for (let elm of document.querySelectorAll('.permission-group')) {
372
+ elm.checked = groups.includes(elm.dataset.value);
373
+ }
374
+
375
+ syncPermissionWarning();
376
+ syncPermissionOutcome();
377
+ });
378
+ }
379
+
89
380
  document.getElementById('token-form').addEventListener('submit', e => {
90
381
  e.preventDefault()
91
382
 
92
383
  let description = document.getElementById('description').value.trim();
93
384
  document.getElementById('description').value = description
94
385
 
386
+ let collectAxis = selector =>
387
+ Array.from(document.querySelectorAll(selector))
388
+ .filter(elm => elm.checked)
389
+ .map(elm => elm.dataset.value);
390
+
95
391
  let scopes = [];
96
392
  let scopeElms = document.querySelectorAll('.scope-entry');
97
393
  for (let elm of scopeElms) {
@@ -123,9 +419,34 @@
123
419
  document.getElementById('showTokenError').classList.add('hidden');
124
420
  uiModal.open('#showToken');
125
421
 
422
+ // Sent only when the restriction is on. Omitting the field is what "not narrowed" means on
423
+ // the record, so an unchecked box must not post an empty object - that is a different
424
+ // answer, and the API refuses it.
425
+ let permissions = permissionsEnabledElm.checked && hasApiScope()
426
+ ? {
427
+ actions: collectAxis('.permission-action'),
428
+ groups: collectAxis('.permission-group')
429
+ }
430
+ : null;
431
+
432
+ // Said here rather than left to the schema. tokenPermissionsSchema refuses an empty axis
433
+ // too, but a joi rejection reaches this page as a bare "HTTP error! status: 400", which
434
+ // tells the reader nothing about the box they left unticked.
435
+ if (permissions && (!permissions.actions.length || !permissions.groups.length)) {
436
+ let missing = !permissions.actions.length ? 'action' : 'section';
437
+ document.getElementById('showTokenLoader').classList.add('hidden');
438
+ document.getElementById('showTokenContent').classList.add('hidden');
439
+ document.getElementById('showTokenError').classList.remove('hidden');
440
+ document.getElementById('showTokenError').textContent =
441
+ `Select at least one ${missing}. A restriction that allows nothing would create a token that cannot make any request.`;
442
+ uiModal.open('#showToken');
443
+ return;
444
+ }
445
+
126
446
  uiPostJson('/admin/tokens/new', {
127
447
  description,
128
448
  scopes,
449
+ permissions,
129
450
  account: document.getElementById('account').value || ''
130
451
  }).then(data => {
131
452
 
@@ -149,3 +470,4 @@
149
470
  });
150
471
  })
151
472
  </script>
473
+ {{/unless}}